牛顿求平方根法(输入是double,输出也是double)

#include <iostream>
#include <string>
using namespace std;

//牛顿求平方根法
double CustomizedSqrt(double n){
    double r=1.0;
    while(abs(r*r-n)>1e-9)
        r=(r+n/r)/2;
    return r;
}

int main()
{
    int n = 200;
    double root = CustomizedSqrt(n);
    cout<<root<<endl;
    return 0;
}

 

 

 

 

 

 

 

EOF

你可能感兴趣的:(double)