Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

floating point anomaly?

 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43016
76
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mathematics != floating point arithmetic
 
Rajesh Chandra
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 21997
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is one of the more obscure things for a beginner to grasp and it seems like the Question is Asked Frequently....
 
Ulf Dittmer
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It sure is a FAQ. I've added an entry to the Java Beginners FAQ.
 
Eric McIntyre
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You know, I didn't mean to seem lazy. I just now realized the FAQ section is a Wiki.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic