0.1 + 0.2 equals what exactly? #softwareengineering #programming

preview_player
Показать описание
Floating point is fun! (Or not)
Рекомендации по теме
Комментарии
Автор

Love it. Important note for the general case: Use <= and abs.

tim-janus-rs
Автор

Begs the question: since the computer can't exactly represent 0.3 in binary, what exact binary representation did the computer use for that "0.3" which ended up different from the one coming from 0.1 + 0.2, so that the operation 0.1 + 0.2 - 0.3 ends up something different from exact 0.0? Did the representation of "0.3" have a higher precision?

Because even if 0.1 + 0.2 cannot exactly represent 0.3, 0.3 itself cannot be represented exactly, so both values, even if inexact with respect to "0.3", could plausibly exactly match each other in binary.

That is in fact typically the case if using fixed points instead of floating points. In fixed points you can't exactly represent 0.3 either, but 0.1 + 0.2 == 0.3 will typically yield true. So within a fixed point representation, 0.1 + 0.2 - 0.3 can yield exactly 0, regardless of 0.3 still being represented imprecisely.

In fact I just checked using C and long doubles, and it turns out 0.1L + 0.2L == 0.3L yields true on x86, so not even with fixed points. Inspected the resulting internal bits in the long doubles, and they are exactly the same to the last bit. On raspberry Pi/arm it does yield false, and that's because long double precision in arm is better than on x86 (128 bits in arm, vs. 80 on x86, ) and the very last bit in 0.1 + 0.2 ends up different from the last bit in the internal representation of 0.3 (which ought to be that one extra bit more precise.)

So the result of these equality tests using fractional numbers are very much language-, compiler-, implementation-, platform-, and environment-dependent.

raulsaavedra
Автор

Very cool fact that I know I will never use but was still fascinated by it

TimothyKidman-ztkk
Автор

Haha, floating point comparisons are evil. But actually a neat trick with the Epsilon.

Comradin
Автор

wait, what is this REPL thing for Rust?

jahongirrahmonov