-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsqrt.c
67 lines (62 loc) · 1.35 KB
/
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
63
64
65
66
67
#include <stdio.h>
#include <math.h>
#define precision 0.00001
typedef int bool;
typedef enum
{
false,
true,
FLASE,
TRUE
} BOOL;
/**
* 求一个数的平方根;
*/
double sqrtN(double x);
/**
* 检查两个数是否为近似相等;
*/
bool approximateEqual(double x, double y);
/**
* output:
* sqrtN(2) = 1.41422
* sqrt(2) = 1.41421
* sqrtN(3) = 1.73205
* sqrt(3) = 1.73205
* sqrtN(4) = 2.00000
* sqrt(4) = 2.00000
*/
int main()
{
double i;
for (i = 1; i < 50; i++)
{
printf("\nsqrtN(%.f) = %.5f", i, sqrtN(i));
printf("\n sqrt(%.f) = %.5f", i, sqrt(i));
}
return 0;
}
double sqrtN(double x)
{
// 牛顿连续逼近法,用一个自然数作为猜测数
// 然后两者相除得到一个余数
// 猜测数加上余数的平均值作为新的猜测数
// 依此类推,直到猜测数非常接近平方根
if (x == 0)
return 0;
double g;
g = x;
while (!approximateEqual(x, g * g))
{
g = (g + x / g) / 2;
}
return g;
}
bool approximateEqual(double x, double y)
{
// abs(x - y) / min(x, y) < precision;
// 两个数相减的绝对值除以小的那个数,小于设定的精度即近似相等
int min;
min = x > y ? y : x;
return fabs(x - y) / (min) < precision;
}