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

BigDecimal(String) must allow a larger range for the exponent

    XMLWordPrintable

Details

    • CSR
    • Status: Closed
    • P4
    • Resolution: Approved
    • 19
    • core-libs
    • None
    • behavioral
    • low
    • Invocations of BigDecimal(String) which currently throw on some arguments might return a correct result if this CSR is approved.
    • Java API
    • SE

    Description

      Summary

      The spec for BigDecimal(String) requires the exponent to be in the int range.

      This causes the issue described in JDK-8233760, where a string produced by BigDecimal.toString() cannot be parsed by the constructor.

      Problem

      The spec of BigDecimal(String) limits the values of the exponent part to be in the int range. This is unnecessarily restrictive.

      For example, BigDecimal.valueOf(10, Integer.MIN_VALUE).toString() produces "1.0E+2147483649", but new BigDecimal("1.0E+2147483649") throws. This is unacceptable, as every string returned by toString() and passed to the constructor must produce the same BigDecimal.

      The cause of the constructor throwing is the requirement for the exponent part to be in the int range.

      Solution

      Drop the requirement for the exponent to lie in the int range from the spec of the constructor.

      Specification

      --- a/src/java.base/share/classes/java/math/BigDecimal.java
      +++ b/src/java.base/share/classes/java/math/BigDecimal.java
      @@ -819,9 +798,7 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> {
            *
            * <p>The exponent consists of the character {@code 'e'}
            * (<code>'&#92;u0065'</code>) or {@code 'E'} (<code>'&#92;u0045'</code>)
      -     * followed by one or more decimal digits.  The value of the
      -     * exponent must lie between -{@link Integer#MAX_VALUE} ({@link
      -     * Integer#MIN_VALUE}+1) and {@link Integer#MAX_VALUE}, inclusive.
      +     * followed by one or more decimal digits.
            *
            * <p>More formally, the strings this constructor accepts are
            * described by the following grammar:

      Attachments

        Issue Links

          Activity

            People

              rgiulietti Raffaello Giulietti
              webbuggrp Webbug Group
              Brian Burkhalter, Roger Riggs
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: