• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double addition error

 
Deli Dumrul
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2989
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic