12 Replies Latest reply on Jan 2, 2007 10:29 AM by 807599

Subtraction with Doubles

I have this code below:
``double y = 200.000 - 191.544;``
and the output is:
y = 8.455999999999989

Why does this happen, I was expecting "8.456"?
• 1. Re: Subtraction with Doubles
http://docs.sun.com/source/806-3568/ncg_goldberg.html
• 2. Re: Subtraction with Doubles
Does that apply to doubles?

e.g.
``````double a;
double b;
double y = a - b;``````
• 3. Re: Subtraction with Doubles
Does that apply to doubles?
Why do you think I posted the reference?
• 4. Re: Subtraction with Doubles
I can't see how that applies with a quick read. The sum doesn't need to round?
• 5. Re: Subtraction with Doubles
observe that the variable double has 64 size bits
• 6. Re: Subtraction with Doubles
64 bits yes, but only 11 bits for the exponent and 53 for the mantissa. This translates into 15-17 digit precision for doubles:

http://www.particle.kth.se/~lindsey/JavaCourse/Book/Part1/Tech/Chapter02/floatingPt.html

%
• 7. Re: Subtraction with Doubles
It seems that you only want it to show three places. A very simple answer, check into java.text.DecimalFormat for decimal formatting.
• 8. Re: Subtraction with Doubles
Thanks for all your help, I understand whats happening a bit now, not fully though (e.g. why a - can reproduce this effect and the + doesn't). I found this effect very strange as I've never heard of it before. I'll try to implement a suitable work around now as the input data into my program will be decimals ranging from 2-6 decimal places.

Thanks.
• 9. Re: Subtraction with Doubles
Thanks for all your help, I understand whats
happening a bit now, not fully though (e.g. why a -
can reproduce this effect and the + doesn't).
No, that's incorrect. Another wrong conclusion.
I found
this effect very strange as I've never heard of it
before.
So you've never read anything about how floating point numbers are represented in computers?

%
• 10. Re: Subtraction with Doubles
Thanks for all your help, I understand whats
happening a bit now, not fully though (e.g. why a -
can reproduce this effect and the + doesn't). I found
this effect very strange as I've never heard of it
before.
Sometimes the error is smaller than the representation precision, or smaller than the default display precision, such that the rounding ends up being a value that looks "exact."

I'm too lazy to find a real example, but, hypothetically, if something ends up as 1.00025, when the correct answer is 1, it could end up rounding to 1.000, which happens to be the right answer anway. Two wrongs made a right, sort of.

I'll try to implement a suitable work around
now as the input data into my program will be
decimals ranging from 2-6 decimal places.
If you want fixed decimal precision, use BigDecimal. If you need decimal precision of 6 decimal digits, you can probably get away with using double, which has decimal precision of something like 13-15 decimal digits, I think. Any error will get rounded away, BUT, errors can accumulate such that you're sixth (for example) decimal digit will be off.

I asume at least one of these has been posted already, but here are my standard references for this topic:

SOME THINGS YOU SHOULD KNOW ABOUT FLOATING-POINT ARITHMETIC
What Every Computer Scientist Should Know About Floating-Point Arithmetic

Another good (slightly simpler) FP explanation:
http://mindprod.com/jgloss/floatingpoint.html
• 11. Re: Subtraction with Doubles
Thanks, especially for that last link. It explained it very well and helped me a lot.

I think the use of the
``BigDecimal``
class will help me make exact calculations for my program.

If anyone in the future reads this thread, this gives a good example on how to perform exact calculations with floating-point numbers:
http://java.sun.com/developer/JDCTechTips/2001/tt0807.html