aspose file tools*
The moose likes Beginning Java and the fly likes Java does not seem to calculate doubles accurately Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java does not seem to calculate doubles accurately" Watch "Java does not seem to calculate doubles accurately" New topic
Author

Java does not seem to calculate doubles accurately

Jason Kirschner
Greenhorn

Joined: Jun 30, 2008
Posts: 7
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


SJCA 1 90%<br />SCJP 6 72%
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

Floating point numbers are approximations. This article explains the details.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Andre Brito
Ranch Hand

Joined: Dec 13, 2007
Posts: 95

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

Joined: Jun 30, 2008
Posts: 7
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jason Kirschner
Greenhorn

Joined: Jun 30, 2008
Posts: 7
okay thanks, truncating the extra decimal places. I must just remember computers cant always do maths.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

It seems that BigDecimal works -- at least in this case.



You may want to try out the BigDecimal class.

Henry
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

And for all "normal" float / double comparison you should check if the difference is small enough:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jason Kirschner
Greenhorn

Joined: Jun 30, 2008
Posts: 7
Thanks All,

I thought they would have covered this in the SCJP/SCJA. evidently not.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
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

Joined: Oct 13, 2005
Posts: 39409
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Java does not seem to calculate doubles accurately