Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8233452

java.math.BigDecimal.sqrt() with RoundingMode.FLOOR results in incorrect result

XMLWordPrintable

    • b32
    • generic
    • generic
    • Verified

        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


              darcy Joe Darcy
              webbuggrp Webbug Group
              Votes:
              0 Vote for this issue
              Watchers:
              8 Start watching this issue

                Created:
                Updated:
                Resolved: