-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSqrt.c
38 lines (36 loc) · 833 Bytes
/
getSqrt.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
#include <stdio.h>
#include <stdlib.h>
// calculate the sqrt of a
double getSqrt(double a) {
// if a is negative, return error
if(a < 0)
return -1;
// if a is 0 or 1, return a
if(a == 0 || a == 1)
return a;
double low = 0, high = a, precision = 0.000000001, v, mid;
if(a < 1)
high = 1;
while(high - low > precision) {
mid = (low + high) / 2;
v = mid * mid;
if(v == a)
return mid;
else if(v > a) {
high = mid;
} else {
low = mid;
}
}
return (high + low) / 2;
}
// some test cases
main()
{
printf("%f\n", getSqrt(1));
printf("%f\n", getSqrt(2));
printf("%f\n", getSqrt(3));
printf("%f\n", getSqrt(5));
printf("%f\n", getSqrt(10));
printf("%f\n", getSqrt(100));
}