Skip to content

Commit 8332890

Browse files
committed
Do round up with ulp in the final result
Do round up with ulp in the final result to ensure the result does not exceed the requested precision
1 parent 2a73a12 commit 8332890

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,13 +2178,12 @@ public BigDecimal sqrt(MathContext mc) {
21782178
// Check for even powers of 10. Numerically sqrt(10^2N) = 10^N
21792179
if (stripped.isPowerOfTen() && (strippedScale & 1) == 0) {
21802180
result = valueOf(1L, strippedScale >> 1);
2181-
if (result.scale < preferredScale) {
2182-
// Adjust to requested precision and preferred
2183-
// scale as appropriate.
2184-
int maxSCale = mc.precision == 0 ?
2185-
preferredScale : (int) Math.min(preferredScale, result.scale + (mc.precision - 1L));
2181+
// Adjust to requested precision and preferred
2182+
// scale as appropriate.
2183+
final int maxSCale = mc.precision == 0 ?
2184+
preferredScale : (int) Math.min(preferredScale, result.scale + (mc.precision - 1L));
2185+
if (result.scale < maxSCale)
21862186
result = result.setScale(maxScale);
2187-
}
21882187

21892188
return result;
21902189
}
@@ -2234,13 +2233,13 @@ public BigDecimal sqrt(MathContext mc) {
22342233

22352234
BigInteger sqrt;
22362235
long resultScale = normScale >> 1;
2236+
boolean increment = false;
22372237
if (mc.roundingMode == RoundingMode.DOWN || mc.roundingMode == RoundingMode.FLOOR) { // No need to round
22382238
sqrt = workingInt.sqrt();
22392239
} else { // Round sqrt with the specified settings
22402240
BigInteger[] sqrtRem = workingInt.sqrtAndRemainder();
22412241
sqrt = sqrtRem[0];
22422242

2243-
boolean increment = false;
22442243
if (halfWay) { // half-way rounding
22452244
// remove the one-tenth digit
22462245
BigInteger[] quotRem10 = sqrt.divideAndRemainder(BigInteger.TEN);
@@ -2263,11 +2262,11 @@ public BigDecimal sqrt(MathContext mc) {
22632262
if (sqrtRem[1].signum != 0 || working.compareTo(new BigDecimal(workingInt)) != 0)
22642263
increment = true;
22652264
}
2266-
if (increment)
2267-
sqrt = sqrt.add(1L);
22682265
}
22692266

22702267
result = new BigDecimal(sqrt, checkScale(sqrt, resultScale));
2268+
if (increment)
2269+
result = result.add(result.ulp());
22712270
}
22722271

22732272
// Test numerical properties at full precision before any

0 commit comments

Comments
 (0)