This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double subtraction

 
Tony Stoyle
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know what on earth is going on here? This code is inside a JSP page but I guess that is academic -
double f1 = 569.56;
double f2 = 316.505;
double f3 = f1 - f2;
The result should be 253.055 but is coming out as 253.05499999999995 .............
Any advice appreciated before I go mad! Thanks.
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There have been several threads on the same topics. See for example:

thread 1
thread 2
W.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This past conversations in the Cattle Drive mentions links that describe well what is going on.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blame the IEEE.
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try using java.math.BigDecimal using a String as a constructor. For some reason it keeps the correct precision.

output:
The result is 253.055
Not exactly what I call efficient though!
Jamie
 
Tony Stoyle
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks for your replies..
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic