Square root of a number

on Saturday, December 12, 2009


Write a code which finds out the square root of a number, w/o sqrt() obviously.










SOLUTION

#define TOLERABLE_LIMIT 0.0000001
double square_root( double n){
double a=n, p=a*a, b;
while(p-n < TOLERABLE_LIMIT){
b = (a+n/a)/2;
a = b;
p = a*a;
}
return a;
}

0 comments:

Post a Comment