File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes how to sum floats Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "how to sum floats" Watch "how to sum floats" New topic
Author

how to sum floats

Tapio Niemela
Ranch Hand

Joined: Jan 06, 2006
Posts: 77
I'm bit puzzled..I should compare one double value with a sum of other double values



I've read it is because of rounding inaccuracy. To my understanding this is because of how numbers are stored (as binary values)

However, is there any way to successfully compare 0.2+0.2+0.2 to 0.6? Or similar. Numbers can be anything, not just 0.2..

With currency I could convert 1.34$ to 134 pennies and use that, but in my case that's not possible, as I don't know the actual numbers, so I can't just multiply the "source" with 100..

Or do I have to accept inaccuracy and threat two counted decimals equal if their difference is low enough..(with the example 0.6000000000000001-0.6=000000000000001--> numbers can be considered "equal")?

thanks in advance
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18564
    
    8

Well, you should use BigDecimal if you want to use exact decimal numbers instead of binary approximations of them. But you should also read the documentation for the BigDecimal constructor you used. Not just the one-line summary, the full documentation. Really. It answers the question you are asking here.
Ireneusz Kordal
Ranch Hand

Joined: Jun 21, 2008
Posts: 423
Try this:
This is because literals 0.2 or 0.6 casted to double cannot be represented as 'exactly' the same numbers using floating point binary representation,
but if you use strings in call to BigDecimal constructor, numbers are not casted to double, but directly to internal BigDecimal representation.
This is described here:
http://download.oracle.com/javase/6/docs/api/java/math/BigDecimal.html#BigDecimal%28double%29


You will see this effect if you run this:
Result is:
0.200000000000000011102230246251565404236316680908203125
0.59999999999999997779553950749686919152736663818359375
0.2
0.6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to sum floats