JDK-4704007 : (c = a - b ) or (a = c + b) maths equation false with double/float
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 1.4.0
  • Priority: P3
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_nt
  • CPU: x86
  • Submitted: 2002-06-18
  • Updated: 2002-06-18
  • Resolved: 2002-06-18
Related Reports
Relates :  
Relates :  
Relates :  
Description

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) 
======================================================================

Comments
EVALUATION While the behavior described is initially surprising, it is the correct, expected behavior for floating-point arithmetic. A number of issues come into play: 1. Numeric values exactly representable as decimal numbers, like 12.1, are not exactly representable as binary floating-point numbers. 2. Individiual floating-point operations (addition, multiplication, etc.) are not always exact. 3. Java's floating-point -> string conversion prints out enough digits to recover the exact floating-point values; other languages don't print out as many digits by default. For a fuller discussion of floating-point in Java see my talk "What Everybody Using the JavaTM Programming Language Should Know About Floating-Point Arithmetic:" Older version: http://java.sun.com/people/darcy/JavaOne/2001/1789darcy.pdf Newer version: Talk TS-1079 at JavaOne 2002; http://servlet.java.sun.com/javaone/sf2002/home/index.jsp (JDC account required to download pdf file.) Also see bugs 4430070, 4497156, 4421899 for similar issues. ###@###.### 2002-06-18
18-06-2002