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

Math.pow function does not give correct result for fractional base and exponent

XMLWordPrintable

    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      Windows and Java 8

      A DESCRIPTION OF THE PROBLEM :
      The value for 0.2 raised to power 3 is expected to be 0.008 however java function Math.pow results in 0.008000000000000002

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      public class JavaMathPower {
          public static void main(String args []){
              double base = 0.2;
              double exponent = 3;
              double calVal = Math.pow(base,exponent);
              System.out.println(calVal);
          }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      0.008
      ACTUAL -
      0.008000000000000002

      ---------- BEGIN SOURCE ----------
      public class JavaMathPower {
          public static void main(String args []){
              double base = 0.2;
              double exponent = 3;
              double calVal = Math.pow(base,exponent);
              System.out.println(calVal);
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use BigDecimal instead of double

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

              Created:
              Updated:
              Resolved: