3 ms·
> It is inherently an issue with binary floating point[1]. It is not an issue with decimal floating point. In decimal floating point, 0.1 + 0.2 = 0.3 exactly.
by jcranmer 7y ago
> It is inherently an issue with binary floating point[1]. It is not an issue with decimal floating point. In decimal floating point, 0.1 + 0.2 = 0.3 exactly.
Decimal floating point doesn't fix the problem, it just makes the simplest example work. If you did ⅓ + ⅓ + ⅓ in decimal floating point, you'd still get 0.99999 instead of 1.
- skissane 7y agoIt doesn't completely fix the problem, but it makes the problem smaller. The gap between naive expectations of how it should work, and how it actually works, exists for both binary and decimal floating point, but the gap is bigger in the case of binary than it is in the case of decimal. Also, the problem that ⅓ + ⅓ + ⅓ < 1 is not unique to floating point – the same issue occurs with fixed point arithmetic (unless your scale factor is a multiple of 3, which is not a common choice). In the general case, if one wants to avoid this problem, one needs to use rationals. I think the issue with ⅓ + ⅓ + ⅓ is easier to explain to beginners – it requires an understanding of rounding and that ⅓ cannot be represented exactly as a decimal, which in many countries the school curriculum pays more attention to than it does to other number bases, so many people have a better intuitive understanding of the former than the later.