3 ms·
Floating-point numbers are tricky[1]. One point which often bites people which isn't highlighted in this article, is that due to having an explicit sign bit 0
by magicalhippo 4y ago
Floating-point numbers are tricky[1].
One point which often bites people which isn't highlighted in this article, is that due to having an explicit sign bit 0 and -0 have different encodings, but in most cases you'll want them to compare equal. So just comparing the bits won't do.
IIRC even the .Net CLR had this issue way back in the 1.1 days.
For some problems, it can often make sense to switch to arbitrary precision numbers, at least for some parts of the calculations.
[1]: https://floating-point-gui.de/ https://floating-point-gui.de/
- maweki 4y agoYou mean back in the 1.099999904632568359375 days?
- arunc 4y agoProbably back in 1.100000004632568359375 days. Wait, when did HN become reddit?!
- Agingcoder 4y agoI do enjoy the occasional joke on hn - for reasons I don't understand they seem to be frowned upon.
- vbezhenar 4y ago> One point which often bites people which isn't highlighted in this article, is that due to having an explicit sign bit 0 and -0 have different encodings, but in most cases you'll want them to compare equal. So just comparing the bits won't do. How does JS do it? (0 / -1) === (0 / 1) is true.
- TAForObvReasons 4y agoconst n = 0/-1, p = 0/1; n === p; // true 1/n === 1/p; // false 1/n === -Infinity; // true 1/p === Infinity; // true Infinity === -Infinity; // false
- IshKebab 4y agoAnd because of that `x + 0` is not always equal to `x`.