aspose file tools*
The moose likes Beginning Java and the fly likes floating point anomaly? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "floating point anomaly?" Watch "floating point anomaly?" New topic
Author

floating point anomaly?

Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
Will this return true or false??
System.out.println(1.0 - 2.0/3.0 == 1.0/3.0);

mathematically its true but if you try working it out you'll get false.Can anyone give the reason behind it??
regs Rajesh
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
Go back to the classics: What every computer scientist should know about floating-point arithmetic


Ping & DNS - updated with new look and Ping home screen widget
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
mathematics != floating point arithmetic


There is no emoticon for what I am feeling!
Rajesh Chandra
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
Thanks to Ulf & Jeff .The link that was posted is too volumous.Do you know of any place wherin I can get the basics explained in a simple way.Anyway is there a way by which we can ascertain that the aritmetic will be different as in the above case?
Regs
Rajesh
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

In floating point arithmetic, most decimals are rounded - simply because it is impossible to define it in binary. Where arithmetic has problems with 1/3, floating point arithmetic has problems with many other numbers as well, like 1/10. In binary, this leads to a repetition similar to 1/3 or 1/9.

Therefore, in any programming language, floating point comparison should be one by checking if the difference is small enough:
Where EPSILON is a very small value - the smaller EPSILON is, the smaller the fault in comparison.
Ideally EPSILON would of course be 0.0, meaning f1 == f2, but as you've just read - you can't rely on that comparison.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Eric McIntyre
Greenhorn

Joined: Mar 03, 2005
Posts: 26
This is one of the more obscure things for a beginner to grasp and it seems like the Question is Asked Frequently....
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
It sure is a FAQ. I've added an entry to the Java Beginners FAQ.
Eric McIntyre
Greenhorn

Joined: Mar 03, 2005
Posts: 26
You know, I didn't mean to seem lazy. I just now realized the FAQ section is a Wiki.
Ashu Sindhu
Greenhorn

Joined: Nov 04, 2005
Posts: 5
Try the following links:-

http://www.codeproject.com/dotnet/ExtremeFloatingPoint1.asp
http://www.cs.berkeley.edu/~wkahan/ieee754status/why-ieee.pdf

This should help.

Thanks,
Ashu Sindhu
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: floating point anomaly?
 
Similar Threads
Best book for the SCBCD ?
Is EJB 3.0 support EJB 1.0 ?
Doubt
'if' with float &double
EJB 3.0 and Hibernate