double MySqrt(unsigned int n)
{
double low=0;
double high=n;
if (n<=4)
{
low=n/2;
}
else high=n/2;
double sqrtValue=(low+high)/2;
while((sqrtValue*sqrtValue>n?sqrtValue*sqrtValue-n:n-sqrtValue*sqrtValue)>1e-3)
{
if (sqrtValue*sqrtValue<n)
{
low=sqrtValue;
}
else
{
high=sqrtValue;
}
sqrtValue=(low+high)/2;
}
return sqrtValue;
}
二分查找计算平方根
最新推荐文章于 2022-12-08 16:13:41 发布