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

BigInteger/BigDecimal not immune to overflow, contrary to spec.

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P4
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 11
    • Component/s: core-libs
    • Labels:
      None
    • Subcomponent:
    • Resolved In Build:
      b13

      Description

      Spec says,

      """All of the details in the Spec concerning overflow are ignored, as BigIntegers are made as large as necessary to accommodate the results of an operation."""

      BUT a colleague reports:

      BigDecimal and BigInteger document some cases where they throw ArithmeticException (eg. division by 0), but there are other cases where they throw but which aren't documented.

      For example:

        BigInteger.TEN.pow(1000000000);

      and

          BigDecimal x = new BigDecimal("1e1000000000");
          x.add(new BigDecimal(1));

      both throw (after running for a long time). Here's an example stacktrace:

      Exception in thread "main" java.lang.ArithmeticException: BigInteger would overflow supported range
              at java.math.BigInteger.reportOverflow(BigInteger.java:1084)
              at java.math.BigInteger.checkRange(BigInteger.java:1079)
              at java.math.BigInteger.<init>(BigInteger.java:1055)
              at java.math.BigInteger.shiftLeft(BigInteger.java:3073)
              at java.math.BigInteger.multiplyToomCook3(BigInteger.java:1740)
              at java.math.BigInteger.multiply(BigInteger.java:1512)
              at java.math.BigInteger.pow(BigInteger.java:2302)
              at java.math.BigDecimal.bigTenToThe(BigDecimal.java:3543)
              at java.math.BigDecimal.bigMultiplyPowerTen(BigDecimal.java:4508)
              at java.math.BigDecimal.add(BigDecimal.java:4443)
              at java.math.BigDecimal.add(BigDecimal.java:1289)
              at Main.main(Main.java:9)

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                darcy Joe Darcy
                Reporter:
                martin Martin Buchholz
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: