问题描述
我在C语言编程初学者,我得到这个任务来写,计算自然数的平方根,而无需使用程序文件math.h
或除了其他库函数 stdio.h中
。
I am a beginner in C programming language and I was given this task to write a program that calculates the square root of a natural number without using math.h
or any other library function except stdio.h
.
请参阅我们在全国各地设有 INT
工作,所以对于非平方数,我们发现的n方根的地板上。现在,我有此计划,但它仅适用于完全平方,但对于非正方形这是行不通的。我在这里引用它,我希望有一些方法来修改它。
See we work with int
throughout, so for non-square numbers we find the floor of square root of n. Now I have made this program, but it only works for perfect squares, but for non-squares it doesn't work. I am quoting it here, I hope there is some way to modify it.
#include <stdio.h>
main()
{
int a;
scanf("%d", &a);
int s = 1, l = a, mid = 0;
while(s < l)
{
mid=(s + l)/2;
if((mid * mid) == a)
{
break;
}
else if ((mid * mid) < a)
{
s = mid + 1;
}
else
{
l = mid;
}
}
printf("%d", mid);
}
非常感谢您的帮助,并请纠正我,只要你能。
Thanks a lot for all your help, and please correct me whenever you can.
编辑:对不起,给您带来不便
Edited : Sorry for the inconvenience.
推荐答案
您似乎使用二分法。这将工作,并且是一个稳定的近似的方法,但是很慢的。
You appear to be using the bisection method. This will work, and is a stable approximation method, but is very slow.
有一个更好的办法是在牛顿迭代方法。有些情况下它可以变得不稳定(如:n阶多项式根求解,多个根是非常接近对方,例如: Y =(X-1),(X-1.1)(X-1.1 )
,但它能够完成任务,通过更快,我的意思是,这将需要较少的迭代在比较你的方法显著数字所需数量到达。
A better approach would be the Newton-Raphson method. There are instances where it can become unstable (ie: nth order polynomial root solving where multiple roots are very close to each other, eg: y=(x-1)(x-1.1)(x-1.1)
, but it gets the job done. By faster, I mean that it will require fewer iterations to arrive at a desired number of significant figures compared to your approach.
编辑:既然你必须使用分而治之的办法,我已经把两种算法在code
Since you must use the divide-and-conquer approach, I have put both algorithms in the code.
code清单
/* Preprocessor inclusions */
#include <stdio.h>
/* Preprocessor Macros */
#define TOL (0.001) /* Error tolerance bound */
#define FABS(a) ((a)>0.0?(a):-(a))
/* Function Prototypes */
double sq_root(double x);
int divideAndConquerAndTruncateForSquareRoot(int n);
/* Function Definitions */
double sq_root(double x) {
double rt = 1, ort = 0;
while(FABS(ort-rt) > TOL) {
ort = rt;
rt = ((x/rt) + rt) / 2;
}
return rt;
}
int divideAndConquerAndTruncateForSquareRoot(int n) {
int low = 0;
int high = n+1;
int mid;
while ((high-low) > 1) {
mid = (low+high) / 2;
if ((mid*mid) <= n) {
low = mid;
} else {
high = mid;
}
}
return low;
}
int main(void) {
int i;
printf("Calculating floating point square roots with Newton-Raphson method with a tolerance of %lf\n", TOL);
for(i = 2; i<10; i++) {
printf("square root of %d is %f\n",i, sq_root(i));
}
printf("Calculating truncated integer square roots with divide and conquer approach\n");
for(i = 2; i<10; i++) {
printf("square root of %d is %d\n",i, divideAndConquerAndTruncateForSquareRoot(i));
}
return 0;
}
示例输出
Calculating floating point square roots with Newton-Raphson method with a tolerance of 0.001000
square root of 2 is 1.414214
square root of 3 is 1.732051
square root of 4 is 2.000000
square root of 5 is 2.236068
square root of 6 is 2.449490
square root of 7 is 2.645751
square root of 8 is 2.828427
square root of 9 is 3.000000
Calculating truncated integer square roots with divide and conquer approach
square root of 2 is 1
square root of 3 is 1
square root of 4 is 2
square root of 5 is 2
square root of 6 is 2
square root of 7 is 2
square root of 8 is 2
square root of 9 is 3
此外,这里的定义为一个根求解算法误差范围和公差的重要的演示。注意误差范围有多大改变:
Also, here's a demonstration of the important of defining error bounds and tolerances for a root-solving algorithm. Notice how much the error bounds change:
code清单
/* Preprocessor inclusions */
#include <stdio.h>
/* Preprocessor Macros */
#define FABS(a) ((a)>0.0?(a):-(a))
double TOL = 1.0;
/* Function Prototypes */
double sq_root(double x);
/* Function Definitions */
double sq_root(double x) {
double rt = 1, ort = 0;
while(FABS(ort-rt) > TOL) {
ort = rt;
rt = ((x/rt) + rt) / 2;
diff = ort-rt;
}
return rt;
}
int main(void) {
int i;
int j;
TOL = 1.0;
for (i=0; i<5; i++, TOL /= 10) {
printf("Calculating floating point square roots with Newton-Raphson method with a tolerance of %lf\n", TOL);
for(j = 2; j<10; j++) {
printf("square root of %d is %f\n", j, sq_root(j));
}
}
return 0;
}
示例输出
Calculating floating point square roots with Newton-Raphson method with a tolerance of 1.000000
square root of 2 is 1.000000
square root of 3 is 1.000000
square root of 4 is 1.000000
square root of 5 is 1.000000
square root of 6 is 1.000000
square root of 7 is 1.000000
square root of 8 is 1.000000
square root of 9 is 1.000000
Calculating floating point square roots with Newton-Raphson method with a tolerance of 0.100000
square root of 2 is 1.416667
square root of 3 is 1.732143
square root of 4 is 2.000610
square root of 5 is 2.238095
square root of 6 is 2.449494
square root of 7 is 2.645767
square root of 8 is 2.828469
square root of 9 is 3.000092
Calculating floating point square roots with Newton-Raphson method with a tolerance of 0.010000
square root of 2 is 1.414216
square root of 3 is 1.732051
square root of 4 is 2.000000
square root of 5 is 2.236069
square root of 6 is 2.449494
square root of 7 is 2.645767
square root of 8 is 2.828427
square root of 9 is 3.000000
Calculating floating point square roots with Newton-Raphson method with a tolerance of 0.001000
square root of 2 is 1.414214
square root of 3 is 1.732051
square root of 4 is 2.000000
square root of 5 is 2.236068
square root of 6 is 2.449490
square root of 7 is 2.645751
square root of 8 is 2.828427
square root of 9 is 3.000000
Calculating floating point square roots with Newton-Raphson method with a tolerance of 0.000100
square root of 2 is 1.414214
square root of 3 is 1.732051
square root of 4 is 2.000000
square root of 5 is 2.236068
square root of 6 is 2.449490
square root of 7 is 2.645751
square root of 8 is 2.828427
square root of 9 is 3.000000
Calculating floating point square roots with Newton-Raphson method with a tolerance of 0.000010
square root of 2 is 1.414214
square root of 3 is 1.732051
square root of 4 is 2.000000
square root of 5 is 2.236068
square root of 6 is 2.449490
square root of 7 is 2.645751
square root of 8 is 2.828427
square root of 9 is 3.000000
Calculating floating point square roots with Newton-Raphson method with a tolerance of 0.000001
square root of 2 is 1.414214
square root of 3 is 1.732051
square root of 4 is 2.000000
square root of 5 is 2.236068
square root of 6 is 2.449490
square root of 7 is 2.645751
square root of 8 is 2.828427
square root of 9 is 3.000000
Calculating floating point square roots with Newton-Raphson method with a tolerance of 0.000000
square root of 2 is 1.414214
square root of 3 is 1.732051
square root of 4 is 2.000000
square root of 5 is 2.236068
square root of 6 is 2.449490
square root of 7 is 2.645751
square root of 8 is 2.828427
square root of 9 is 3.000000
最后,这里的最后一个例子中,用于比较每一个方法使用,以产生相同的结果的迭代次数。
Finally, here's one last example for comparing the number of iterations each approach uses to generate the same results.
code清单
/*******************************************************************************
* Preprocessor Inclusions and Definitions
******************************************************************************/
#include <stdio.h>
#define FABS(a) ((a)>0.0?(a):-(a))
typedef double (*fcnPtr)(double, double, int*);
/*******************************************************************************
* Function Prototypes
******************************************************************************/
double sqrt_bisection(double x, double tolerance, int* iterations);
double sqrt_newton_raphson(double x, double tolerance, int* iterations);
/*******************************************************************************
* Function Definitions
******************************************************************************/
/******************************************************************************/
double sqrt_newton_raphson(double x, double tolerance, int* iterations) {
if (!iterations || tolerance < 0.0) {
printf("Invalid input\n");
return 0.0;
}
*iterations = 0;
double rt = 1, ort = 0;
while(FABS(ort-rt) > tolerance) {
(*iterations)++;
ort = rt;
rt = ((x/rt) + rt) / 2;
}
return rt;
}
/******************************************************************************/
double sqrt_bisection(double x, double tolerance, int* iterations) {
if (!iterations || tolerance < 0.0) {
printf("Invalid input\n");
return 0.0;
}
*iterations = 0;
double low = 0;
double high = x+1;
double mid;
while (FABS(high-low) > tolerance) {
(*iterations)++;
mid = (low+high) / 2;
if ((mid*mid) <= x) {
low = mid;
} else {
high = mid;
}
}
return low;
}
/******************************************************************************/
int main(void) {
int i, j, k;
fcnPtr fn = NULL;
double tolerance;
int iterations;
fcnPtr functions[2] = { sqrt_bisection, sqrt_newton_raphson };
char* functionNames[2] = { "Bisection Method", "Newton Raphson Method" };
for (i=0; i<2; i++) {
fn = functions[i];
for(j = 2; j<10; j++) {
tolerance = 0.1;
for (k=0; k<10; k++) {
printf("square root of %d is %lf (%d iterations for tolerance of %e) using %s\n", j,
fn((double)j, tolerance, &iterations), iterations, tolerance, functionNames[i]);
tolerance /= 10;
}
}
}
return 0;
}
示例输出
square root of 2 is 1.406250 (5 iterations for tolerance of 1.000000e-01) using Bisection Method
square root of 2 is 1.412109 (9 iterations for tolerance of 1.000000e-02) using Bisection Method
square root of 2 is 1.413574 (12 iterations for tolerance of 1.000000e-03) using Bisection Method
square root of 2 is 1.414124 (15 iterations for tolerance of 1.000000e-04) using Bisection Method
square root of 2 is 1.414209 (19 iterations for tolerance of 1.000000e-05) using Bisection Method
square root of 2 is 1.414213 (22 iterations for tolerance of 1.000000e-06) using Bisection Method
square root of 2 is 1.414213 (25 iterations for tolerance of 1.000000e-07) using Bisection Method
square root of 2 is 1.414214 (29 iterations for tolerance of 1.000000e-08) using Bisection Method
square root of 2 is 1.414214 (32 iterations for tolerance of 1.000000e-09) using Bisection Method
square root of 2 is 1.414214 (35 iterations for tolerance of 1.000000e-10) using Bisection Method
square root of 3 is 1.687500 (6 iterations for tolerance of 1.000000e-01) using Bisection Method
square root of 3 is 1.726562 (9 iterations for tolerance of 1.000000e-02) using Bisection Method
square root of 3 is 1.731445 (12 iterations for tolerance of 1.000000e-03) using Bisection Method
square root of 3 is 1.731995 (16 iterations for tolerance of 1.000000e-04) using Bisection Method
square root of 3 is 1.732048 (19 iterations for tolerance of 1.000000e-05) using Bisection Method
square root of 3 is 1.732050 (22 iterations for tolerance of 1.000000e-06) using Bisection Method
square root of 3 is 1.732051 (26 iterations for tolerance of 1.000000e-07) using Bisection Method
square root of 3 is 1.732051 (29 iterations for tolerance of 1.000000e-08) using Bisection Method
square root of 3 is 1.732051 (32 iterations for tolerance of 1.000000e-09) using Bisection Method
square root of 3 is 1.732051 (36 iterations for tolerance of 1.000000e-10) using Bisection Method
square root of 4 is 1.953125 (6 iterations for tolerance of 1.000000e-01) using Bisection Method
square root of 4 is 1.992188 (9 iterations for tolerance of 1.000000e-02) using Bisection Method
square root of 4 is 1.999512 (13 iterations for tolerance of 1.000000e-03) using Bisection Method
square root of 4 is 1.999969 (16 iterations for tolerance of 1.000000e-04) using Bisection Method
square root of 4 is 1.999998 (19 iterations for tolerance of 1.000000e-05) using Bisection Method
square root of 4 is 2.000000 (23 iterations for tolerance of 1.000000e-06) using Bisection Method
square root of 4 is 2.000000 (26 iterations for tolerance of 1.000000e-07) using Bisection Method
square root of 4 is 2.000000 (29 iterations for tolerance of 1.000000e-08) using Bisection Method
square root of 4 is 2.000000 (33 iterations for tolerance of 1.000000e-09) using Bisection Method
square root of 4 is 2.000000 (36 iterations for tolerance of 1.000000e-10) using Bisection Method
square root of 5 is 2.156250 (6 iterations for tolerance of 1.000000e-01) using Bisection Method
square root of 5 is 2.232422 (10 iterations for tolerance of 1.000000e-02) using Bisection Method
square root of 5 is 2.235352 (13 iterations for tolerance of 1.000000e-03) using Bisection Method
square root of 5 is 2.235992 (16 iterations for tolerance of 1.000000e-04) using Bisection Method
square root of 5 is 2.236067 (20 iterations for tolerance of 1.000000e-05) using Bisection Method
square root of 5 is 2.236068 (23 iterations for tolerance of 1.000000e-06) using Bisection Method
square root of 5 is 2.236068 (26 iterations for tolerance of 1.000000e-07) using Bisection Method
square root of 5 is 2.236068 (30 iterations for tolerance of 1.000000e-08) using Bisection Method
square root of 5 is 2.236068 (33 iterations for tolerance of 1.000000e-09) using Bisection Method
square root of 5 is 2.236068 (36 iterations for tolerance of 1.000000e-10) using Bisection Method
square root of 6 is 2.406250 (7 iterations for tolerance of 1.000000e-01) using Bisection Method
square root of 6 is 2.447266 (10 iterations for tolerance of 1.000000e-02) using Bisection Method
square root of 6 is 2.448975 (13 iterations for tolerance of 1.000000e-03) using Bisection Method
square root of 6 is 2.449455 (17 iterations for tolerance of 1.000000e-04) using Bisection Method
square root of 6 is 2.449489 (20 iterations for tolerance of 1.000000e-05) using Bisection Method
square root of 6 is 2.449489 (23 iterations for tolerance of 1.000000e-06) using Bisection Method
square root of 6 is 2.449490 (27 iterations for tolerance of 1.000000e-07) using Bisection Method
square root of 6 is 2.449490 (30 iterations for tolerance of 1.000000e-08) using Bisection Method
square root of 6 is 2.449490 (33 iterations for tolerance of 1.000000e-09) using Bisection Method
square root of 6 is 2.449490 (37 iterations for tolerance of 1.000000e-10) using Bisection Method
square root of 7 is 2.625000 (7 iterations for tolerance of 1.000000e-01) using Bisection Method
square root of 7 is 2.640625 (10 iterations for tolerance of 1.000000e-02) using Bisection Method
square root of 7 is 2.645508 (13 iterations for tolerance of 1.000000e-03) using Bisection Method
square root of 7 is 2.645691 (17 iterations for tolerance of 1.000000e-04) using Bisection Method
square root of 7 is 2.645744 (20 iterations for tolerance of 1.000000e-05) using Bisection Method
square root of 7 is 2.645751 (23 iterations for tolerance of 1.000000e-06) using Bisection Method
square root of 7 is 2.645751 (27 iterations for tolerance of 1.000000e-07) using Bisection Method
square root of 7 is 2.645751 (30 iterations for tolerance of 1.000000e-08) using Bisection Method
square root of 7 is 2.645751 (33 iterations for tolerance of 1.000000e-09) using Bisection Method
square root of 7 is 2.645751 (37 iterations for tolerance of 1.000000e-10) using Bisection Method
square root of 8 is 2.812500 (7 iterations for tolerance of 1.000000e-01) using Bisection Method
square root of 8 is 2.821289 (10 iterations for tolerance of 1.000000e-02) using Bisection Method
square root of 8 is 2.827881 (14 iterations for tolerance of 1.000000e-03) using Bisection Method
square root of 8 is 2.828362 (17 iterations for tolerance of 1.000000e-04) using Bisection Method
square root of 8 is 2.828422 (20 iterations for tolerance of 1.000000e-05) using Bisection Method
square root of 8 is 2.828427 (24 iterations for tolerance of 1.000000e-06) using Bisection Method
square root of 8 is 2.828427 (27 iterations for tolerance of 1.000000e-07) using Bisection Method
square root of 8 is 2.828427 (30 iterations for tolerance of 1.000000e-08) using Bisection Method
square root of 8 is 2.828427 (34 iterations for tolerance of 1.000000e-09) using Bisection Method
square root of 8 is 2.828427 (37 iterations for tolerance of 1.000000e-10) using Bisection Method
square root of 9 is 2.968750 (7 iterations for tolerance of 1.000000e-01) using Bisection Method
square root of 9 is 2.998047 (10 iterations for tolerance of 1.000000e-02) using Bisection Method
square root of 9 is 2.999878 (14 iterations for tolerance of 1.000000e-03) using Bisection Method
square root of 9 is 2.999954 (17 iterations for tolerance of 1.000000e-04) using Bisection Method
square root of 9 is 2.999992 (20 iterations for tolerance of 1.000000e-05) using Bisection Method
square root of 9 is 3.000000 (24 iterations for tolerance of 1.000000e-06) using Bisection Method
square root of 9 is 3.000000 (27 iterations for tolerance of 1.000000e-07) using Bisection Method
square root of 9 is 3.000000 (30 iterations for tolerance of 1.000000e-08) using Bisection Method
square root of 9 is 3.000000 (34 iterations for tolerance of 1.000000e-09) using Bisection Method
square root of 9 is 3.000000 (37 iterations for tolerance of 1.000000e-10) using Bisection Method
square root of 2 is 1.416667 (2 iterations for tolerance of 1.000000e-01) using Newton Raphson Method
square root of 2 is 1.414216 (3 iterations for tolerance of 1.000000e-02) using Newton Raphson Method
square root of 2 is 1.414214 (4 iterations for tolerance of 1.000000e-03) using Newton Raphson Method
square root of 2 is 1.414214 (4 iterations for tolerance of 1.000000e-04) using Newton Raphson Method
square root of 2 is 1.414214 (4 iterations for tolerance of 1.000000e-05) using Newton Raphson Method
square root of 2 is 1.414214 (5 iterations for tolerance of 1.000000e-06) using Newton Raphson Method
square root of 2 is 1.414214 (5 iterations for tolerance of 1.000000e-07) using Newton Raphson Method
square root of 2 is 1.414214 (5 iterations for tolerance of 1.000000e-08) using Newton Raphson Method
square root of 2 is 1.414214 (5 iterations for tolerance of 1.000000e-09) using Newton Raphson Method
square root of 2 is 1.414214 (5 iterations for tolerance of 1.000000e-10) using Newton Raphson Method
square root of 3 is 1.732143 (3 iterations for tolerance of 1.000000e-01) using Newton Raphson Method
square root of 3 is 1.732051 (4 iterations for tolerance of 1.000000e-02) using Newton Raphson Method
square root of 3 is 1.732051 (4 iterations for tolerance of 1.000000e-03) using Newton Raphson Method
square root of 3 is 1.732051 (4 iterations for tolerance of 1.000000e-04) using Newton Raphson Method
square root of 3 is 1.732051 (5 iterations for tolerance of 1.000000e-05) using Newton Raphson Method
square root of 3 is 1.732051 (5 iterations for tolerance of 1.000000e-06) using Newton Raphson Method
square root of 3 is 1.732051 (5 iterations for tolerance of 1.000000e-07) using Newton Raphson Method
square root of 3 is 1.732051 (5 iterations for tolerance of 1.000000e-08) using Newton Raphson Method
square root of 3 is 1.732051 (6 iterations for tolerance of 1.000000e-09) using Newton Raphson Method
square root of 3 is 1.732051 (6 iterations for tolerance of 1.000000e-10) using Newton Raphson Method
square root of 4 is 2.000610 (3 iterations for tolerance of 1.000000e-01) using Newton Raphson Method
square root of 4 is 2.000000 (4 iterations for tolerance of 1.000000e-02) using Newton Raphson Method
square root of 4 is 2.000000 (4 iterations for tolerance of 1.000000e-03) using Newton Raphson Method
square root of 4 is 2.000000 (5 iterations for tolerance of 1.000000e-04) using Newton Raphson Method
square root of 4 is 2.000000 (5 iterations for tolerance of 1.000000e-05) using Newton Raphson Method
square root of 4 is 2.000000 (5 iterations for tolerance of 1.000000e-06) using Newton Raphson Method
square root of 4 is 2.000000 (5 iterations for tolerance of 1.000000e-07) using Newton Raphson Method
square root of 4 is 2.000000 (6 iterations for tolerance of 1.000000e-08) using Newton Raphson Method
square root of 4 is 2.000000 (6 iterations for tolerance of 1.000000e-09) using Newton Raphson Method
square root of 4 is 2.000000 (6 iterations for tolerance of 1.000000e-10) using Newton Raphson Method
square root of 5 is 2.238095 (3 iterations for tolerance of 1.000000e-01) using Newton Raphson Method
square root of 5 is 2.236069 (4 iterations for tolerance of 1.000000e-02) using Newton Raphson Method
square root of 5 is 2.236068 (5 iterations for tolerance of 1.000000e-03) using Newton Raphson Method
square root of 5 is 2.236068 (5 iterations for tolerance of 1.000000e-04) using Newton Raphson Method
square root of 5 is 2.236068 (5 iterations for tolerance of 1.000000e-05) using Newton Raphson Method
square root of 5 is 2.236068 (5 iterations for tolerance of 1.000000e-06) using Newton Raphson Method
square root of 5 is 2.236068 (6 iterations for tolerance of 1.000000e-07) using Newton Raphson Method
square root of 5 is 2.236068 (6 iterations for tolerance of 1.000000e-08) using Newton Raphson Method
square root of 5 is 2.236068 (6 iterations for tolerance of 1.000000e-09) using Newton Raphson Method
square root of 5 is 2.236068 (6 iterations for tolerance of 1.000000e-10) using Newton Raphson Method
square root of 6 is 2.449494 (4 iterations for tolerance of 1.000000e-01) using Newton Raphson Method
square root of 6 is 2.449494 (4 iterations for tolerance of 1.000000e-02) using Newton Raphson Method
square root of 6 is 2.449490 (5 iterations for tolerance of 1.000000e-03) using Newton Raphson Method
square root of 6 is 2.449490 (5 iterations for tolerance of 1.000000e-04) using Newton Raphson Method
square root of 6 is 2.449490 (5 iterations for tolerance of 1.000000e-05) using Newton Raphson Method
square root of 6 is 2.449490 (6 iterations for tolerance of 1.000000e-06) using Newton Raphson Method
square root of 6 is 2.449490 (6 iterations for tolerance of 1.000000e-07) using Newton Raphson Method
square root of 6 is 2.449490 (6 iterations for tolerance of 1.000000e-08) using Newton Raphson Method
square root of 6 is 2.449490 (6 iterations for tolerance of 1.000000e-09) using Newton Raphson Method
square root of 6 is 2.449490 (6 iterations for tolerance of 1.000000e-10) using Newton Raphson Method
square root of 7 is 2.645767 (4 iterations for tolerance of 1.000000e-01) using Newton Raphson Method
square root of 7 is 2.645767 (4 iterations for tolerance of 1.000000e-02) using Newton Raphson Method
square root of 7 is 2.645751 (5 iterations for tolerance of 1.000000e-03) using Newton Raphson Method
square root of 7 is 2.645751 (5 iterations for tolerance of 1.000000e-04) using Newton Raphson Method
square root of 7 is 2.645751 (6 iterations for tolerance of 1.000000e-05) using Newton Raphson Method
square root of 7 is 2.645751 (6 iterations for tolerance of 1.000000e-06) using Newton Raphson Method
square root of 7 is 2.645751 (6 iterations for tolerance of 1.000000e-07) using Newton Raphson Method
square root of 7 is 2.645751 (6 iterations for tolerance of 1.000000e-08) using Newton Raphson Method
square root of 7 is 2.645751 (6 iterations for tolerance of 1.000000e-09) using Newton Raphson Method
square root of 7 is 2.645751 (6 iterations for tolerance of 1.000000e-10) using Newton Raphson Method
square root of 8 is 2.828469 (4 iterations for tolerance of 1.000000e-01) using Newton Raphson Method
square root of 8 is 2.828427 (5 iterations for tolerance of 1.000000e-02) using Newton Raphson Method
square root of 8 is 2.828427 (5 iterations for tolerance of 1.000000e-03) using Newton Raphson Method
square root of 8 is 2.828427 (5 iterations for tolerance of 1.000000e-04) using Newton Raphson Method
square root of 8 is 2.828427 (6 iterations for tolerance of 1.000000e-05) using Newton Raphson Method
square root of 8 is 2.828427 (6 iterations for tolerance of 1.000000e-06) using Newton Raphson Method
square root of 8 is 2.828427 (6 iterations for tolerance of 1.000000e-07) using Newton Raphson Method
square root of 8 is 2.828427 (6 iterations for tolerance of 1.000000e-08) using Newton Raphson Method
square root of 8 is 2.828427 (6 iterations for tolerance of 1.000000e-09) using Newton Raphson Method
square root of 8 is 2.828427 (7 iterations for tolerance of 1.000000e-10) using Newton Raphson Method
square root of 9 is 3.000092 (4 iterations for tolerance of 1.000000e-01) using Newton Raphson Method
square root of 9 is 3.000000 (5 iterations for tolerance of 1.000000e-02) using Newton Raphson Method
square root of 9 is 3.000000 (5 iterations for tolerance of 1.000000e-03) using Newton Raphson Method
square root of 9 is 3.000000 (5 iterations for tolerance of 1.000000e-04) using Newton Raphson Method
square root of 9 is 3.000000 (6 iterations for tolerance of 1.000000e-05) using Newton Raphson Method
square root of 9 is 3.000000 (6 iterations for tolerance of 1.000000e-06) using Newton Raphson Method
square root of 9 is 3.000000 (6 iterations for tolerance of 1.000000e-07) using Newton Raphson Method
square root of 9 is 3.000000 (6 iterations for tolerance of 1.000000e-08) using Newton Raphson Method
square root of 9 is 3.000000 (7 iterations for tolerance of 1.000000e-09) using Newton Raphson Method
square root of 9 is 3.000000 (7 iterations for tolerance of 1.000000e-10) using Newton Raphson Method
参考
- 的 1.1.7举例:平方根由牛顿法的,访问的2014年8月19日,的
- 的牛顿法的,访问的2014年8月19日,
- 1.1.7 Example: Square Roots by Newton's Method, Accessed 2014-08-19,
<http://mitpress.mit.edu/sicp/chapter1/node9.html>
- Square root in C using Newton-Raphson method, Accessed 2014-08-19,
<http://stackoverflow.com/questions/14038456/square-root-in-c-using-newton-raphson-method>
- Newton's Method, Accessed 2014-08-19,
<http://en.wikipedia.org/wiki/Newton%27s_method>
这篇关于一个更好的方案来评估的平方根的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!