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

ArithmeticException thrown by BigDecimal divide​

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Windows 10. Java 14.

      A DESCRIPTION OF THE PROBLEM :
      The problem is that BigDecimal throws an ArithmeticException when asked to compute 40 divided by 60. The exception thrown is as follows: "Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result."

      This is clearly wrong. The answer to 40 divided by 60 is 0.64. Which is a terminating decimal.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Enter the following program:

      public class BigDecimalBug() {
      public static void main(String[] args) {
      BigDecimal a = new BigDecimal(40);
      BigDecimal b = new BigDecimal(60);
      System.out.println(a.divide(b));
      }
      }


      ACTUAL -
      Exception in thread "main" java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result."

      ---------- BEGIN SOURCE ----------
      public class BigDecimalBug() {
      public static void main(String[] args) {
      BigDecimal a = new BigDecimal(40);
      BigDecimal b = new BigDecimal(60);
      System.out.println(a.divide(b));
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :

      FREQUENCY : often


            tongwan Andrew Wang
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: