Skip to content

Commit 8333644

Browse files
committed
Added comments on mathematical semantic
1 parent f964611 commit 8333644

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/java.base/share/classes/java/math/BigDecimal.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5255,8 +5255,9 @@ private static BigInteger fiveToTwoToThe(int n) {
52555255
private static BigDecimal createAndStripZerosToMatchScale(BigInteger intVal, int scale, long preferredScale) {
52565256
// avoid overflow of scale - preferredScale
52575257
preferredScale = Math.clamp(preferredScale, Integer.MIN_VALUE - 1L, Integer.MAX_VALUE);
5258-
// a multiple of 10^n must be a multiple of 2^n
52595258
int powsOf2 = intVal.getLowestSetBit();
5259+
// remainingZeros >= max{n : (intVal % 10^n) == 0 && scale - n >= preferredScale}
5260+
// a multiple of 10^n must be a multiple of 2^n
52605261
long remainingZeros = Math.min(scale - preferredScale, powsOf2);
52615262
if (remainingZeros <= 0L)
52625263
return valueOf(intVal, scale, 0);
@@ -5266,7 +5267,7 @@ private static BigDecimal createAndStripZerosToMatchScale(BigInteger intVal, int
52665267
intVal = intVal.negate(); // speed up computation of shiftRight() and bitLength()
52675268

52685269
intVal = intVal.shiftRight(powsOf2); // remove powers of 2
5269-
// maxPowsOf5 == ceil(log5(intVal)) roughly
5270+
// maxPowsOf5 >= log5(intVal)
52705271
long maxPowsOf5 = (long) Math.ceil(intVal.bitLength() * LOG_5_OF_2);
52715272
remainingZeros = Math.min(remainingZeros, maxPowsOf5);
52725273

0 commit comments

Comments
 (0)