#include
#include
#include
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define eps 1e-6
float mysqrt(float n)
{
if(n<0)
return n;
float ans;
float low,up;
low=0,up=max(n,1);
ans=(low+up)/2;
while(ans*ans-n>eps || ans*ans-n<-eps)
{
if(ans*ans>n)
{
up=ans;
}
else
{
low=ans;
}
ans=(low+up)/2;
}
return ans;
}
int main()
{
float a=9.0;
printf("%f\n",mysqrt(a));
return 0;
}
阅读(1073) | 评论(0) | 转发(0) |