0

Possible Duplicate:
Floating point arithmetic not producing exact results in Java

I was recently working on a project when I came across a strange bug.

When 2 was subtracted from 65.12 the value was greater (not equal to) 63.12.

Here's the simplified code: System.out.println(65.12-2);

And the output in the console: 63.120000000000005

I'm not sure why this is the case and if anyone knows a simple fix/workaround that would be great!

Thanks.

Community
  • 1
  • 1
aly
  • 523
  • 2
  • 7
  • 1
    See [What Every Computer Scientist Should Know About Floating-Point Arithmetic](http://download.oracle.com/docs/cd/E19957-01/806-3568/ncg_goldberg.html). – Andrew Thompson Dec 28 '12 at 02:45
  • You're welcome. :) Happy learning. – Andrew Thompson Dec 28 '12 at 02:48
  • 3
    This is more subtle than the duplicate. You could have thought that subtrracting an integer would be exact, which is the case for 63.12 - 2 == 61.12. Unfortunately, if you cross a 2^n boundary, then one number will have one more fractional bit than the other. And here, you crossed 2^6 (64). – aka.nice Dec 28 '12 at 09:50
  • 2
    @aka.nice: It is even subtler than that. `65.12-2` is exact. However, when printing `65.12`, the double `65.12` (that is, the double that the source text “65.12” is converted to) is closer to 65.12 than any other double is. Printing it shows “65.12” because the Java specification says, essentially, “65.12” has enough digits to reconvert it to the double that was printed. `65.12-2` is the same distance from 63.12 that `65.12` is from 65.12. However, because it is in a finer interval, there is another number between `65.12-2` and 63.12. So Java has to print more digits to distinguish the value. – Eric Postpischil Dec 28 '12 at 16:03
  • I voted to reopen this because the unexpected result is not due to floating-point inaccuracy (the subtraction is exact in this case) but due to the Java specification regarding how floating-point values are displayed. – Eric Postpischil Dec 28 '12 at 16:06
  • @EricPostpischil yes I agree, the operation is exact but now we have one bit left to make a closer approximation of 6312/100 and it happens that there is a closer one by subtracting 1 ulp... It's only due to IEEE. Java is printing the minimum number of decimal digits such that the number can be reinterpreted unchanged, but i rather see that as a helper clue. – aka.nice Dec 28 '12 at 17:02

1 Answers1

-1

It has to do with the way floating-point values are handled by computers. The recommended text for fully understanding the topic is: What Every Computer Scientist Should Know About Floating-Point Arithmetic

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • This answer fails to explain the unexpected result, as noted in my comment on the question. The subtraction is exact; the result of subtracting `2` from the double represented by the numeral `65.12` in the source text is exactly 2 less than `65.12`. Although the explanation can, in theory, be partially derived from the linked article, it is not due to rounding during arithmetic as we see asked about so commonly. Instead it involves the Java specification regarding displaying floating-point values, which is not part of the linked article. – Eric Postpischil Dec 28 '12 at 16:10