# [Haskell-cafe] Why does [1.0, 3 ..4] contain 5?

Bence Kodaj bence.kodaj at gmail.com
Wed Oct 19 12:13:04 UTC 2016

```Hi all,

Does anybody happen to know why [1.0, 3 ..4 ] is [1.0, 3.0, 5.0] ?

I do realize I'm not supposed to use enumerated lists with doubles, so this
is just a question out of pure curiosity. I ran into this example
accidentally, and I find it counter-intuitive - I would naively expect that
[x, y .. z] does not contain elements greater than z (assuming x < y < z).

The root cause of why [1.0, 3 .. 4] contains 5.0 is that in the Enum
instances for Double and Float, enumFromThenTo is defined like this:

numericEnumFromThenTo
(numericEnumFromThen
where
2                                 predicate
| otherwise

and with the concrete values in the example, the predicate becomes (<=5.0).

My question is this: why can't we simply use (<= e3) as the predicate? Why
is the upper limit  (e3) increased by half of the length of the e1 .. e2
interval (mid)? Can someone give an example where using (<=e3) as predicate

I'm guessing that the answer has something to do with the quirks of
floating-point arithmetic (rounding etc.), of which I'm not an expert at
all :)

Regards,
Bence
-------------- next part --------------
An HTML attachment was scrubbed...