Rich C
Rich C

Reputation: 3244

Why does 0.1 * 10.0 == 1

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.

Upvotes: 0

Views: 191

Answers (1)

Simon Byrne
Simon Byrne

Reputation: 7874

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).

Upvotes: 9

Related Questions