Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8341402 BigDecimal's square root optimization
  3. JDK-8364045

Release Note: Method `BigDecimal.sqrt()` Might Now Throw on Powers of 100 and Huge Precisions

XMLWordPrintable

    • Icon: Sub-task Sub-task
    • Resolution: Delivered
    • Icon: P4 P4
    • 25
    • 25
    • core-libs

      The method `BigDecimal.sqrt()` has been reimplemented to perform much better.

      However, in some very rare and quite artificial cases involving powers of 100 and huge precisions, the new implementation throws whereas the old one returns a result.

      For example
      ```
      BigDecimal.valueOf(100).sqrt(new MathContext(1_000_000_000, RoundingMode.UP))
      ```
      returns `10` in the old implementation.
      Note, however, that rather than returning `11`, it throws when evaluating
      ```
      BigDecimal.valueOf(121).sqrt(new MathContext(1_000_000_000, RoundingMode.UP))
      ```

      Indeed, the old implementation treats powers of 100 as special cases (see 1st example). Other exact squares are treated normally, so throw when the requested precision is too high (see 2nd example). This special behavior for powers of 100 is not recommended, as it is more confusing than helpful when compared to other exact squares.

      The new implementation is agnostic about powers of 100, and throws whenever internal intermediate results would exceed supported ranges. In particular, it uniformly throws on both the examples above.

            rgiulietti Raffaello Giulietti
            rgiulietti Raffaello Giulietti
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: