wood burning stoves
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 Elasticsearch in Action this week in the Big Data forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "double addition error" Watch "double addition error" New topic

double addition error

Deli Dumrul

Joined: Jul 06, 2004
Posts: 9

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?

Greg Charles

Joined: Oct 01, 2001
Posts: 2867

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://kickstarter.com/rocket-mass-heater
subject: double addition error