-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_sqrt.c
executable file
·62 lines (59 loc) · 1.74 KB
/
ft_sqrt.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sqrt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/16 18:38:49 by akharrou #+# #+# */
/* Updated: 2019/03/18 23:24:49 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** ft_sqrt -- square root function.
**
** SYNOPSIS
** #include "math_42.h"
**
** double
** ft_sqrt(double x);
**
** PARAMETERS
**
** double x A number of type double.
**
** DESCRIPTION
** The ft_sqrt() function computes the non-negative square root of x.
**
** RETURN VALUES
** Returns square root of x.
**
** If 'x' is smaller than 0, returns 0, if 'x' is 0 or 1, 'x' is
** returned.
*/
double ft_sqrt(double x)
{
long double res;
long double root;
double precision;
if (x < 2)
return ((x < 0) ? 0.0 : x);
res = 0;
root = 2;
while ((res = root * root) <= x)
{
if (res == x)
return (root);
root++;
}
--root;
precision = 0.000001;
while ((res = root * root) <= x)
{
if (res == x)
return (root);
root += precision;
}
return (root - precision);
}