Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions src/java.base/share/classes/java/math/BigDecimal.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
* <tr><th scope="row">Subtract</th><td>max(minuend.scale(), subtrahend.scale())</td>
* <tr><th scope="row">Multiply</th><td>multiplier.scale() + multiplicand.scale()</td>
* <tr><th scope="row">Divide</th><td>dividend.scale() - divisor.scale()</td>
* <tr><th scope="row">Square root</th><td>radicand.scale()/2</td>
* <tr><th scope="row">Square root</th><td>ceil(radicand.scale()/2.0)</td>
* </tbody>
* </table>
*
Expand Down Expand Up @@ -2113,7 +2113,7 @@ public BigDecimal[] divideAndRemainder(BigDecimal divisor, MathContext mc) {
* with rounding according to the context settings.
*
* <p>The preferred scale of the returned result is equal to
* {@code this.scale()/2}. The value of the returned result is
* {@code Math.ceilDiv(this.scale(), 2)}. The value of the returned result is
* always within one ulp of the exact decimal value for the
* precision in question. If the rounding mode is {@link
* RoundingMode#HALF_UP HALF_UP}, {@link RoundingMode#HALF_DOWN
Expand Down Expand Up @@ -2174,7 +2174,7 @@ public BigDecimal sqrt(MathContext mc) {

// The code below favors relative simplicity over checking
// for special cases that could run faster.
final int preferredScale = this.scale/2;
final int preferredScale = Math.ceilDiv(this.scale, 2);

BigDecimal result;
if (mc.roundingMode == RoundingMode.UNNECESSARY || mc.precision == 0) { // Exact result requested
Expand Down
7 changes: 4 additions & 3 deletions test/jdk/java/math/BigDecimal/SquareRootTests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -348,7 +348,7 @@ private static int lowPrecisionPerfectSquares() {

for (int scale = 0; scale <= 4; scale++) {
BigDecimal scaledSquare = square.setScale(scale, RoundingMode.UNNECESSARY);
int expectedScale = scale/2;
int expectedScale = Math.ceilDiv(scale, 2);
for (int precision = 0; precision <= 5; precision++) {
for (RoundingMode rm : RoundingMode.values()) {
MathContext mc = new MathContext(precision, rm);
Expand Down Expand Up @@ -582,7 +582,8 @@ public static BigDecimal sqrt(BigDecimal bd, MathContext mc) {
// The code below favors relative simplicity over checking
// for special cases that could run faster.

int preferredScale = bd.scale()/2;
int preferredScale = Math.ceilDiv(bd.scale(), 2);

BigDecimal zeroWithFinalPreferredScale =
BigDecimal.valueOf(0L, preferredScale);

Expand Down