double sqrt_bin(double n){
if(n < 1){
n = 1;
}
double left = 0;
double right = n;
double mid = n / 2;
while(abs(mid * mid - n) > 0.000001){
if(mid*mid < n){
left = mid;
}
else{
right = mid;
}
mid = (left + right) / 2;
}
return mid;
}
double sqrt_newton(double n){
if(n < 1){
n = 1;
}
double x0 = n;
double x1 = x0;
while(x1 * x1 - n > 0.0000001){
x1 = 0.5 * (x0 + n / x0);
x0 = x1;
}
return x0;
}
原文地址:https://www.cnblogs.com/theodoric008/p/9581646.html
时间: 2024-10-25 14:52:43