huangblue / Algorithm

计算机算法
0 stars 0 forks source link

Newton-Raphson Method to Compute the Square Root of x #2

Open huangblue opened 7 years ago

huangblue commented 7 years ago

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.

huangblue commented 7 years ago

//阅读版本 // Function to calculate the absolute value of a number

include

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; }

huangblue commented 7 years ago

//运行版本 // Function to calculate the absolute value of a number

include

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; }

huangblue commented 7 years ago

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.