N次根达101个重要位置

时间:2017-12-24 13:56:38

标签: c++ newtons-method nth-root

我需要确定给定f 漏洞点数的第n (n是正32位整数) root x 任意精度,最高 101个重要位置。我使用牛顿方法的方法只能给我带来小数53位的结果。 任何帮助都将受到赞赏

#include <bits/stdc++.h>
using namespace std;

double nthRoot(double A, int N)
{
    double xPre = rand() % 10;//INITIAL GUESS
    double eps = 1e-100;//SETTING PRECISION
    double delX = INT_MAX;//SETTING DIFFERENCE BETWEEN VALUES OF 'X'
    double xK;
    while (delX > eps)
        {
            xK = (double)((N - 1.0) * xPre +(double)A/pow(xPre, N-1)) 
                 /(double)N;//FORMULA FROM NEWTON'S METHOD
            delX = abs(xK - xPre);
            xPre = xK;
        }
    return xK;
}

int main()
{
    int N;
    double A;
    cin>>N>>A;
    double nthRootValue = nthRoot(A, N);
    cout.setf(ios::showpoint);
    cout.precision(100);
    cout <<"\n"<< "Nth root is " << nthRootValue << endl;
    return 0;
}

0 个答案:

没有答案