Open huangblue opened 7 years ago
//阅读版本 // Function to calculate the absolute value of a number
float absoluteValue (float x) { if ( x < 0 ) x = -x; return (x); } // Function to compute the square root of a number float squareRoot (float x) { const float epsilon = .00001; float guess = 1.0; while ( absoluteValue (guess * guess - x) >= epsilon ) guess = ( x / guess + guess ) / 2.0; return guess; } int main (void) { printf ("squareRoot (2.0) = %f\n", squareRoot (2.0)); printf ("squareRoot (144.0) = %f\n", squareRoot (144.0)); printf ("squareRoot (17.5) = %f\n", squareRoot (17.5)); return 0; }
//运行版本 // Function to calculate the absolute value of a number
float absoluteValue (float x) { if ( x < 0 ) x = -x; return (x); } // Function to compute the square root of a number float squareRoot (float x) { const float epsilon = .00001; float guess = 1.0; while ( absoluteValue (guess * guess - x) >= epsilon ) guess = ( x / guess + guess ) / 2.0; return guess; } int main (void) { printf ("squareRoot (2.0) = %f\n", squareRoot (2.0)); printf ("squareRoot (144.0) = %f\n", squareRoot (144.0)); printf ("squareRoot (17.5) = %f\n", squareRoot (17.5)); return 0; }
squareRoot (2.0) = 1.414216 squareRoot (144.0) = 12.000000 squareRoot (17.5) = 4.183300
Process returned 0 (0x0) execution time : 0.016 s Press any key to continue.
Newton-Raphson Method to Compute the Square Root of x
Step 1: Set the value of guess to 1. Step 2: If |guess2 - x| < ε, proceed to step 4. Step 3: Set the value of guess to (x / guess + guess) / 2 and return to step 2. Step 4: The guess is the approximation of the square root.