c++编程求 平方根 (不用sqrt函数)

2025-04-13 00:14:15
推荐回答(3个)
回答1:

X=根号a
X(n+2)=1/2(Xn+a/Xn)
double sqroot(double a)
{
double x0,x1;
x0=a/2;
x1=(x0+a/x0)/2;
do{
x0=x1;
x1=(x0+a/x0)/2;
}( (x1>x0 ? x1-x0 : x0-x1)<1e-8);
return x1;
}

回答2:

#include
using namespace std;
void main(void)
{
float x=0.0;
cin>>x;
float xhalf = 0.5l * x;
float num = x;
int i = *(int *)& x;
i = 0x5f375a86 - (i >> 1);
x = *(float * ) & i;
x = x * (1.5f - xhalf*x*x);
x = x * (1.5f - xhalf*x*x);
x = x * (1.5f - xhalf*x*x);
cout<}

回答3:

穷举就可以了