• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java does not seem to calculate doubles accurately

 
Jason Kirschner
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
example code

public class Test {
public static void main (String [] args) {
double d = 2.04d;
double d2 = 78.7d;

d += d2;
System.out.println(d);
}
}

result 80.74000000000001

result differs by .00000000000001

Is this a bug in the version of Java i am running?

Thanks
Jason
 
Joe Ess
Bartender
Posts: 9313
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Floating point numbers are approximations. This article explains the details.
 
Andre Brito
Ranch Hand
Posts: 95
Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, that's not a bug in your JVM.
If you want just to print the number correctly, use printf("%.xf", d);, where x is the after the comma.
 
Jason Kirschner
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the issue i have is i am adding up the value of a field in a file and then trying to ensure the total agrees with the trailer record of the file. my == statement differs now by .0000000000000001 which means it returns false and i determine the trailer record does not agree with the contents of the file.

Any idea's what i should do to get accurate maths?
 
Henry Wong
author
Marshal
Pie
Posts: 21432
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any idea's what i should do to get accurate maths?


It's not a Java issue. It is an issue with floating point, and affects all processors and languages, as they are all based on IEEE. Anyway, as Andre stated, the best you can do is to round it to a decimal place that you are comfortable with.

Another option that you can try is the BigDecimal class.

Henry
 
Jason Kirschner
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay thanks, truncating the extra decimal places. I must just remember computers cant always do maths.
 
Henry Wong
author
Marshal
Pie
Posts: 21432
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems that BigDecimal works -- at least in this case.



You may want to try out the BigDecimal class.

Henry
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And for all "normal" float / double comparison you should check if the difference is small enough:
 
Jason Kirschner
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks All,

I thought they would have covered this in the SCJP/SCJA. evidently not.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49848
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jason Kirschner:
Thanks All,

I thought they would have covered this in the SCJP/SCJA. evidently not.
Possibly because SCJP/SCJA exams require the candidate to be able to predict the answers, and that sort of imprecision in floating-point arithmetic is common, but not predictable.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49848
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, there is a subtle inaccuracy in the thread title. I am getting all pedantic, but the calculations are all accurate.

They are slightly imprecise.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic