二分法拓展——求根号2近似值
作者:互联网
#include<stdio.h>
#include<math.h>
const double eps = 1e-5;
double f(double x)
{
return x * x;
}
double calSqrt()
{
double left = 1, right = 2, mid;
while(right - left > eps)
{
mid = (left + right) / 2;
if(f(mid) > 2) right = mid;
else left = mid;
}
return mid;
}
int main()
{
printf("%.4f", calSqrt());
return 0;
}
标签:calSqrt,right,return,double,mid,二分法,近似值,求根,left 来源: https://blog.csdn.net/tian__si/article/details/113704352