This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • 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: 2984
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