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

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

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • 11
    • None
    • core-libs
    • None
    • b13

      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)

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

              Created:
              Updated:
              Resolved: