mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8334755: Asymptotically faster implementation of square root algorithm
Reviewed-by: rgiulietti
This commit is contained in:
parent
34edc7358f
commit
367e0a6556
4 changed files with 390 additions and 85 deletions
|
@ -2723,7 +2723,7 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
throw new ArithmeticException("Negative BigInteger");
|
||||
}
|
||||
|
||||
return new MutableBigInteger(this.mag).sqrt().toBigInteger();
|
||||
return new MutableBigInteger(this.mag).sqrtRem(false)[0].toBigInteger();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -2742,10 +2742,12 @@ public class BigInteger extends Number implements Comparable<BigInteger> {
|
|||
* @since 9
|
||||
*/
|
||||
public BigInteger[] sqrtAndRemainder() {
|
||||
BigInteger s = sqrt();
|
||||
BigInteger r = this.subtract(s.square());
|
||||
assert r.compareTo(BigInteger.ZERO) >= 0;
|
||||
return new BigInteger[] {s, r};
|
||||
if (this.signum < 0) {
|
||||
throw new ArithmeticException("Negative BigInteger");
|
||||
}
|
||||
|
||||
MutableBigInteger[] sqrtRem = new MutableBigInteger(this.mag).sqrtRem(true);
|
||||
return new BigInteger[] { sqrtRem[0].toBigInteger(), sqrtRem[1].toBigInteger() };
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue