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

(c = a - b ) or (a = c + b) maths equation false with double/float

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • None
    • 1.4.0
    • core-libs
    • x86
    • windows_nt



      Name: gm110360 Date: 06/18/2002


      FULL PRODUCT VERSION :
      C:\>java -version
      java version "1.4.0"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-b92)
      Java HotSpot(TM) Client VM (build 1.4.0-b92, mixed mode)

      FULL OPERATING SYSTEM VERSION :
      Windows NT Version 4.0

      A DESCRIPTION OF THE PROBLEM :
      double a = 12.10d;
      double b = 10.11d;

      double c = a-b ;

      "The value of c is 1.9900000000000002" i think it should be
      1.99 (by basic maths)
      So the statement 1.99 == c gives false

      as c is having the value 1.9900000000000002
      the logical answer to the output c + b is
      12.1000000000000002

      but the actule answer is 12.1 ????
      which is equal to a

      The above thing Violating the basic maths...






      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Execute the code once

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      double a = 12.10d;
      double b = 10.11d;

      double c = a-b ;

      value of c should be 1.99

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class sh
      {
         public static void main(String w[])
         {
      try
      {
      double a = 12.10d;
      double b = 10.11d;

                  double c = a-b ; // 1.99
      //Point 1
      System.out.println(c == 1.99d); /* output false ,
      Logically it should be true */

      //Point 2
      System.out.println(" value of c -> "+c); /* out put is
      1.9900000000000002 so it is not 1.99 !!!!!! */

                      //Point 3
      System.out.println( (c + b)); // ouput 12.1 ??????
      //Point 4
      System.out.println( (c + b)== a); //true !!!
      }catch(Exception e)
      {
      System.out.println(e);
      }
        }
      }

      ---------- END SOURCE ----------
      (Review ID: 153759)
      ======================================================================

            darcy Joe Darcy
            gmanwanisunw Girish Manwani (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: