1 #include <stdio.h> 2 #include <math.h> 3 4 double mysqrt(double n, double x0) 5 { 6 double x1; 7 double y; 8 9 x1 = (x0 + n/x0)/2.0; 10 11 if (fabs(x1-x0) >= 0.00001) 12 y = mysqrt(n, x1); 13 else 14 y = x1; 15 16 return y; 17 } 18 19 int main(void) 20 { 21 printf("sqrt(2) = %lf ", mysqrt(2.0, 1.0)); 22 23 return 0; 24 }
显示结果为
sqrt(2) = 1.414214