很奇怪,关于Newton-Raphson迭代法的编程~
写了一段程序关于Newton-Raphson迭代法求平方根的程序代码:
#include <stdio.h> float absoluteValue (float x) { if ( x < 0 ) x = -x; return (x); } float squareRoot (float x, float epsilon) { int guess = 1.0; while (absoluteValue (guess * guess - x) >= epsilon) guess = (x / guess + guess ) / 2.0; return guess; } int main (void) { float result; result = squareRoot (155.0, 0.0001); printf ("%f\n", result); result = squareRoot (155.0, 0.0005); printf ("%f\n", result); return 0; }
很奇怪的是如果flaot x等于一个诸如100,144一类整数平方根就能出来结果,但是等于诸如15,133之类的数字,也可以运行,但是出不来结果,程序也一直不结束,光标就在下面闪啊闪……