File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes double addition error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double addition error" Watch "double addition error" New topic
Author

double addition error

Deli Dumrul
Greenhorn

Joined: Jul 06, 2004
Posts: 9
Hi,

Sorry if this problem was asked before

When i add 0.14+0.01 it gives 0.15000000000000002.

Looks like one way to get rid of this problem is to use BigDecimal.

Is there another way and how do some other compilers like visual c++ handle this that they don't give this error for double values?

Thanks.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2864
    
  11

Rounding errors are a fact of life when doing floating point arithmetic on a computer. The compiler has nothing to do with it, because it's caused by the inner workings of the CPU. What you can do is round the result (see Math.round) or use a number formatter when displaying it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: double addition error