Zubair Bashir
Zubair Bashir

Reputation: 11

N-th Root Upto 101 Significant Places

I'm required to determine the n-th(n is a positive 32-bit integer) root of a given floating point number x of arbitrary precision upto 101 significant places. My approach using Newton's method however gives me results upto 53 decimal places only. Any help as such would be appreciated.

#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;
}

Upvotes: 1

Views: 60

Answers (0)

Related Questions