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

javax.script.ScriptEngine eval gives wrong digits for decimal multiplication

    XMLWordPrintable

Details

    Description

      A DESCRIPTION OF THE PROBLEM :

      When ScripEngine is used for multiplying decimal numbers the answer is incorrect.
      E.g. when engine eval method with formula multiplies 7.0*0.01*20 it gives 1.4000000000001. The correct answer is 1.4000000000.


      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;



      public class Test {

      public static void main(String[] args) throws Exception {
      ScriptEngineManager factory = new ScriptEngineManager();
      ScriptEngine engine = factory.getEngineByName("JavaScript");

      System.out.println(engine.eval("7.0*0.01*20"));
      }
      }

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :

      When ScripEngine is used for multiplying decimal numbers the answer is incorrect.
      E.g. when engine eval method with formula multiplies 7.0*0.01*20 it gives 1.4000000000001. The correct answer is 1.4000000000.


      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;



      public class Test {

      public static void main(String[] args) throws Exception {
      ScriptEngineManager factory = new ScriptEngineManager();
      ScriptEngine engine = factory.getEngineByName("JavaScript");

      System.out.println(engine.eval("7.0*0.01*20"));
      }
      }

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      7.0*0.01*20 it gives 1.4000000000001
      ACTUAL -
      7.0*0.01*20 it gives 1.4000000000000

      ---------- BEGIN SOURCE ----------
      import javax.script.ScriptEngine;
      import javax.script.ScriptEngineManager;



      public class Test {

      public static void main(String[] args) throws Exception {
      ScriptEngineManager factory = new ScriptEngineManager();
      ScriptEngine engine = factory.getEngineByName("JavaScript");

      System.out.println(engine.eval("7.0*0.01*20"));
      }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: