0

In MATLAB the following is true

0.1 * 10.0 == 1

But 0.1 is not represented exactly in floating point, so I expected it to not be true. Did I just get lucky and the error happened to be smaller than eps, so it got rounded to 1?

MATLAB implements IEEE 754, so I think it should apply to all languages. But this post makes me think it might be something specific to MATLAB.

Community
  • 1
  • 1
Rich C
  • 3,164
  • 6
  • 26
  • 37
  • 1
    Python does evaluate to true: >>> 0.99999999999999999 == 1 – ergonaut Oct 23 '15 at 17:52
  • You are probably lucky yes. This post may provide more insight: http://stackoverflow.com/questions/686439/why-is-24-0000-not-equal-to-24-0000-in-matlab – rayryeng Oct 23 '15 at 17:53
  • `0.1 + 0.2 + 0.3 - 0.6 = 1.110223024625157e-16` while `0.1 + 0.2 + 0.3 + 0.4 - 1.0 = 0.0` (R2012b) :-) – il_raffa Oct 23 '15 at 17:59
  • If I just got lucky, how can I prove it? – Rich C Oct 23 '15 at 18:05
  • Try increasing the total number of digits of precision for display: `format long g;`, then go and compute your stuff. On my machine, doing `0.1*10.0` does in fact give me 1, and this is probably because of what you're suspecting. However, doing something like what @il_raffa does you don't see unless you increase the total number of digits. – rayryeng Oct 23 '15 at 18:19

1 Answers1

9

Your specific example is true for any language which uses IEEE754 floating point arithmetic (well, 64-bit at least).

The literal 0.1 is exactly

0.1000000000000000055511151231257827021181583404541015625

10.0 is exactly 10

Their product is therefore

1.000000000000000055511151231257827021181583404541015625

The two closest floating point values are:

1.0
1.000000000000000222044604925031308084726333618164062

of which the first is closest, so the result is rounded to that.

(I'm not 100% sure what is going on in that example you link to: I suspect it has to do with C# using higher intermediate precision)

In general, however, this sort of thing isn't true. e.g. 0.51255*1e5 isn't 51255 (though MATLAB may lie when printing, try 0.51255*1e5-51255).

Community
  • 1
  • 1
Simon Byrne
  • 7,694
  • 1
  • 26
  • 50