publicstaticdoublenthroot(int n, double A) { doubler= nthroot(n, A, .001); return BigDecimal.valueOf(r).setScale(3, RoundingMode.HALF_DOWN).doubleValue(); }
publicstaticdoublenthroot(int n, double A, double p) { if(A < 0) { System.err.println("A < 0");// we handle only real positive numbers return -1; } elseif(A == 0) { return0; } doublex_prev= A; doublex= A / n; // starting "guessed" value... while(Math.abs(x - x_prev) > p) { x_prev = x; x = ((n - 1.0) * x + A / Math.pow(x, n - 1.0)) / n; } return x; }