JDK-8233452 : java.math.BigDecimal.sqrt() with RoundingMode.FLOOR results in incorrect result
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.math
  • Affected Version: 11,14
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: generic
  • CPU: generic
  • Submitted: 2019-10-18
  • Updated: 2020-11-20
  • Resolved: 2020-01-15
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 14 JDK 15
11.0.9-oracleFixed 13.0.6Fixed 14 b32Fixed 15Fixed
Related Reports
Relates :  
Relates :  
Description
ADDITIONAL SYSTEM INFORMATION :
Also tested OpenJDK 11.0.2 with the same result.

A DESCRIPTION OF THE PROBLEM :
The method java.math.BigDecimal.sqrt(MathContext mc) does not respect the intention of RoundingMode.FLOOR.
Other RoundingMode values have not been tested, but may also return incorrectly rounded results.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Execute the simple source code snippet in a main method.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
1.99999999999
ACTUAL -
2.00000000000

---------- BEGIN SOURCE ----------
MathContext mc = new MathContext(12, RoundingMode.FLOOR);
System.out.println(new BigDecimal("3.999999999999999999999999999999").sqrt(mc));
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Using the sqrt() method of the bigmath library from https://github.com/eobermuhlner/big-math does deliver the correct result.

FREQUENCY : always



Comments
Fix request (13u) jdk13u is also affected. I would like to downport it, too. Applies clean.
20-11-2020

Fix request (11u) -- will label after testing completed. I would like to downport this for parity with 11.0.9-oracle. Applies clean.
02-06-2020

URL: https://hg.openjdk.java.net/jdk/jdk14/rev/d2f1fd498726 User: darcy Date: 2020-01-15 04:20:19 +0000
15-01-2020

Review thread: http://mail.openjdk.java.net/pipermail/core-libs-dev/2019-November/063292.html
06-11-2019

JDK 11.0.5 - Fail JDK 13 - Fail JDK 14-ea - Fail Output: 2.00000000000
04-11-2019