给出平方根寻找算法的数学证明

时间:2017-05-12 10:47:22

标签: c++ algorithm proof square-root

任何人都可以解释以下代码的数学正确性,以便在C ++中找到正数的平方根?

#include <iostream>
#include <cmath>

using namespace std;

int main(){
    float a; cin>>a;
    cout<<"Answer by Math library\n"<<sqrt(a)<<"\n";

    //the following is the code for finding the square root of a positive double

    double g=a;

    while(abs(g*g-a)>1e-8){
        g= (g+(a/g))/2;
    }

    cout<<"Answer by this algorithm\n"<<g<<"\n";

    return 0;
}

0 个答案:

没有答案