3 ms·
> The real mystery is why most modern languages continue to use floats and integers as the default numeric types rather than some sort of BigDecimal representat
by jcranmer 3y ago
> The real mystery is why most modern languages continue to use floats and integers as the default numeric types rather than some sort of BigDecimal representation, given that precision and programmer intuitiveness trumps raw performance in the vast majority of cases.
BigDecimal actually doesn't fix anything. The underlying problem is that decimal representations are rarely finite--in base 10, 1 / 3 has to get rounded to 0.333, so the same problem comes up as 1 / 3 * 3 = 0.999 ≠ 1. If you want to fix that, you have to move to rational representations, but then you get flummoxed by things like sqrt(2) or atan(1/10) or pi.
No, the thing is that 0.1 + 0.2 is 0.3 in double arithmetic. The result differs from the closest representation to 0.3 only a tiny bit--like measuring the distance between LA and NY and being off by a few atoms. The problem is that == is checking for equality of all bits rather than a tolerance, and that string printing is using rules that it prints the shortest base-10 representation that rounds to that extra floating-point value, both of which exaggerate the importance of the last few bits. So the real mystery is why no one's designed a language that makes floating-point comparisons default to relative tolerance of 10^-10 or so.
- diydsp 3y ago> real mystery is why no one's designed a language that makes floating-point comparisons default to relative tolerance of 10^-10 or so. Bc the very next step is a footgun: "the function said these two values are equal but they're not."
- taeric 3y agoI think it is fair to wonder why no language made it so that you can only compare floats with a defined tolerance?