Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double calculation

 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Why does

gives 0.5800000000000001 ? where does the .0000...001 come from ? Thanks.
[ October 17, 2005: Message edited by: Ronnie Ho ]
 
Grant Gainey
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The short answer is, computers store their data in binary, and "5.8" is not precisely representable.

The slightly-longer answer is that Java uses IEEE-754 to store/represent floating-point numbers, and the number you're seeing is the closest one can come to 5.8 in that format.

The best answer is that it's important to understand that a programming language's "float" and "double" are not the same as the real number line. Check out this link for a great explanation:

What Every Computer Scientist Should Know About Floating Point

Grant

[Edited to fix broken tags]
[ October 19, 2005: Message edited by: G Gainey ]
 
Ronnie Ho
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I'll just take the short answer. The link contains too much details.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I always jump in when I see two decimal points. If you're dealing with money, floating types will probably make you very unhappy. You've already started to see why. The PowerBall is at $340 million so I'm thinking about money again today.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15632
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So if you're dealing with amounts of money in your application, use java.math.BigDecimal instead of float or double.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic