4 ms·
If you write a decimal literal, the sensible default behavior is to preserve precision rather than throwing it out for speed optomization. Sure, there is a long
by dragonwriter 5y ago
If you write a decimal literal, the sensible default behavior is to preserve precision rather than throwing it out for speed optomization. Sure, there is a long history of programming languages mostly choosing the reverse default, but it's a bad choice for correctness.
- FastMonkey 5y agoI think "sensible" is highly context dependent.
- tadfisher 5y agoI prefer compiler warnings when a float/double literal is not representable. Not perfect for interpreted runtimes, though.
- Someone 5y agoIf you write literals such as 0xa.bp-3, 1/3 or 22/7, why would those be allowed to throw out precision for speed? (That hex literal is valid C++17; the fractions aren’t literal in the standards, but IMO are literals in human heads) ⇒ If you think the “sensible default behavior is to preserve precision“ I think you would have to call for (at least) rational as the default non-integer integral type. (If you think of √2 as a literal, it gets more complicated. In the end, you might have to use a representation that’s similar to that used in computer algebra systems) Edit: you could also require constants that do have an exact representation in your floating point format. That would be annoying, too, though, but maybe not too annoying if you had a way to specify “the number closest to this one”, say by requiring one to write ~2.1~ instead of 2.1
- lmm 5y ago> If you write literals such as 0xa.bp-3, 1/3 or 22/7, why would those be allowed to throw out precision for speed? (That hex literal is valid C++17; the fractions aren’t literal in the standards, but IMO are literals in human heads) IMO decimal rounding of those fractions, while not ideal, is a lot more understandable to a typical person. If 1/3 * 3 = 0.99999999999999999 that's annoying, but not crazy in the same way that the floating-point equivalent is.
- eternityforest 5y agoCorrectness is relative to expectations though. It might be worse for bug count to have some languages use decimals and others use floats. People might make even more mistakes.
- cozzyd 5y agoParticularly when Python is reliant on C extensions for any sort of performance...
- creata 5y agoThat works well in Haskell, which turns literals into rationals (pairs of big integers) and calls an overloaded function fromRational on them implicitly. But Python can't do this kind of inference to give you a nice syntax, so I think this "sensible default behavior" would be a huge pain for users. (Also, "correctness" seems like the wrong word: making rationals the default type would give you perfectly precise literals, but you'd immediately lose that once you perform almost any operation on them.)