先找出接近m的浮点数,然后通过下面的不等式中的等于条件得到其平方根。
#include <iostream>
#include <math.h>
using namespace std;
float sqroot(float m)
{
float i=0;
float x1,x2;
while ((i*i)<=m)
{
i+=0.1;
}
x1=i;
for (int j=0;j<10;j++)
{
x2=m;
x2/=x1;
x2+=x1;
x2/=2;
x1=x2;
cout<<x2<<"\t"<<j<<endl;
}
return x2;
}
int main()
{
float m=9.12341212;
cout<<sqroot(m)<<endl;
cout<<sqrt(m)<<endl;
system("pause");
return 1;
}