Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/java.base/share/classes/java/math/BigInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2768,17 +2768,17 @@ public BigInteger[] sqrtAndRemainder() {
* @throws ArithmeticException if {@code n} is even and {@code this} is negative.
* @see #sqrt()
* @since 26
* @apiNote Note that calling {@code nthRoot(2)} is equivalent to calling {@code sqrt()}.
* @apiNote Note that calling {@code rootn(2)} is equivalent to calling {@code sqrt()}.
*/
public BigInteger nthRoot(int n) {
public BigInteger rootn(int n) {
if (n == 1)
return this;

if (n == 2)
return sqrt();

checkRootDegree(n);
return new MutableBigInteger(this.mag).nthRootRem(n)[0].toBigInteger(signum);
return new MutableBigInteger(this.mag).rootnRem(n)[0].toBigInteger(signum);
}

/**
Expand All @@ -2793,20 +2793,20 @@ public BigInteger nthRoot(int n) {
* @throws ArithmeticException if {@code n} is even and {@code this} is negative.
* @see #sqrt()
* @see #sqrtAndRemainder()
* @see #nthRoot(int)
* @see #rootn(int)
* @since 26
* @apiNote Note that calling {@code nthRootAndRemainder(2)} is equivalent to calling
* @apiNote Note that calling {@code rootnAndRemainder(2)} is equivalent to calling
* {@code sqrtAndRemainder()}.
*/
public BigInteger[] nthRootAndRemainder(int n) {
public BigInteger[] rootnAndRemainder(int n) {
if (n == 1)
return new BigInteger[] { this, ZERO };

if (n == 2)
return sqrtAndRemainder();

checkRootDegree(n);
MutableBigInteger[] rootRem = new MutableBigInteger(this.mag).nthRootRem(n);
MutableBigInteger[] rootRem = new MutableBigInteger(this.mag).rootnRem(n);
return new BigInteger[] {
rootRem[0].toBigInteger(signum),
rootRem[1].toBigInteger(signum)
Expand Down
16 changes: 8 additions & 8 deletions src/java.base/share/classes/java/math/MutableBigInteger.java
Original file line number Diff line number Diff line change
Expand Up @@ -1906,7 +1906,7 @@ private boolean unsignedLongCompare(long one, long two) {
* @param n the root degree
* @return the integer {@code n}th root of {@code this} and the remainder
*/
MutableBigInteger[] nthRootRem(int n) {
MutableBigInteger[] rootnRem(int n) {
// Special cases.
if (this.isZero() || this.isOne())
return new MutableBigInteger[] { this, new MutableBigInteger() };
Expand All @@ -1923,7 +1923,7 @@ MutableBigInteger[] nthRootRem(int n) {
if (bitLength <= Long.SIZE) {
// Initial estimate is the root of the unsigned long value.
final long x = this.toLong();
long sLong = (long) nthRootApprox(Math.nextUp(x >= 0 ? x : x + 0x1p64), n) + 1L;
long sLong = (long) rootnApprox(Math.nextUp(x >= 0 ? x : x + 0x1p64), n) + 1L;
/* The integer-valued recurrence formula in the algorithm of Brent&Zimmermann
* simply discards the fraction part of the real-valued Newton recurrence
* on the function f discussed in the referenced work.
Expand Down Expand Up @@ -1996,7 +1996,7 @@ MutableBigInteger[] nthRootRem(int n) {
// Use the root of the shifted value as an estimate.
// rad ≤ 2^ME, so Math.nextUp(rad) < Double.MAX_VALUE
rad = Math.nextUp(rad);
approx = nthRootApprox(rad, n);
approx = rootnApprox(rad, n);
} else { // fp arithmetic gives too few correct bits
// Set the root shift to the root's bit length minus 1
// The initial estimate will be 2^rootLen == 2 << (rootLen - 1)
Expand Down Expand Up @@ -2050,7 +2050,7 @@ MutableBigInteger[] nthRootRem(int n) {
MutableBigInteger x = new MutableBigInteger(this);
x.rightShift(rootSh * n);

newtonRecurrenceNthRoot(x, s, n, s.toBigInteger().pow(n - 1));
newtonRecurrenceRootn(x, s, n, s.toBigInteger().pow(n - 1));
s.add(ONE); // round up to ensure s is an upper bound of the root
}

Expand All @@ -2060,7 +2060,7 @@ MutableBigInteger[] nthRootRem(int n) {
}

// Do the 1st iteration outside the loop to ensure an overestimate
newtonRecurrenceNthRoot(this, s, n, s.toBigInteger().pow(n - 1));
newtonRecurrenceRootn(this, s, n, s.toBigInteger().pow(n - 1));
// Refine the estimate.
do {
BigInteger sBig = s.toBigInteger();
Expand All @@ -2069,18 +2069,18 @@ MutableBigInteger[] nthRootRem(int n) {
if (rem.subtract(this) <= 0)
return new MutableBigInteger[] { s, rem };

newtonRecurrenceNthRoot(this, s, n, sToN1);
newtonRecurrenceRootn(this, s, n, sToN1);
} while (true);
}

private static double nthRootApprox(double x, int n) {
private static double rootnApprox(double x, int n) {
return Math.nextUp(n == 3 ? Math.cbrt(x) : Math.pow(x, Math.nextUp(1.0 / n)));
}

/**
* Computes {@code ((n-1)*s + x/sToN1)/n} and places the result in {@code s}.
*/
private static void newtonRecurrenceNthRoot(
private static void newtonRecurrenceRootn(
MutableBigInteger x, MutableBigInteger s, int n, BigInteger sToN1) {
MutableBigInteger dividend = new MutableBigInteger();
s.mul(n - 1, dividend);
Expand Down