6 ms·
Haskell is relatively WTF free as a language, behaving pretty much like the lambda calculus, but the standard libraries have their share of WTFs, such as the En
by tromp 1y ago
Haskell is relatively WTF free as a language, behaving pretty much like the lambda calculus, but the standard libraries have their share of WTFs, such as the Enum instance for Double:
ghci> [0, 0.3 .. 2.0]
[0.0,0.3,0.6,0.8999999999999999,1.2,1.5,1.7999999999999998,2.1]
I'd rather not see 2.1 in a range up to 2.0
- jose_zap 1y agoThat’s floating point, though. Not Double. Floating point has the same gotchas in all languages because it is a defined standard.
- tromp 1y agoThat list is of type (Fractional a, Enum a) => [a], but ghci defaults to Doubles, as you can see from ghci> [0, 0.3 .. 2.0] :: [Float] [0.0,0.3,0.6,0.90000004,1.2,1.5,1.8000001,2.1000001] ghci> [0, 0.3 .. 2.0] :: [Double] [0.0,0.3,0.6,0.8999999999999999,1.2,1.5,1.7999999999999998,2.1] Exceeding the upper bound by 0.1 is not due to a general floating gotcha, but a specific choice in the definition of typeclass method enumFromThenTo for both Floats and Doubles.