<div dir="ltr">Hi all,<div><br></div><div>Does anybody happen to know why [1.0, 3 ..4 ] is [1.0, 3.0, 5.0] ?</div><div><br></div><div>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). </div><div><br></div><div>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:</div><div><pre style="color:rgb(0,0,0)"><a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#numericEnumFromThenTo" class="gmail-" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier" style="color:rgb(7,54,66)">numericEnumFromThenTo</span></a> <a name="local-1627413496"></a><a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413496" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier" style="color:rgb(7,54,66)">e1</span></a> <a name="local-1627413497"></a><a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413497" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier" style="color:rgb(7,54,66)">e2</span></a> <a name="local-1627413498"></a><a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413498" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier" style="color:rgb(7,54,66)">e3</span></a>
<a name="line-229"></a> <span class="gmail-hs-glyph" style="color:rgb(220,50,47)">=</span> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.List.html#takeWhile" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">takeWhile</span></a> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413500" class="gmail-" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">predicate</span></a> <span class="gmail-hs-special" style="color:rgb(220,50,47)">(</span><a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#numericEnumFromThen" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">numericEnumFromThen</span></a> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413496" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">e1</span></a> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413497" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">e2</span></a><span class="gmail-hs-special" style="color:rgb(220,50,47)">)</span>
<a name="line-230"></a> <span class="gmail-hs-keyword" style="color:rgb(175,0,95)">where</span>
<a name="line-231"></a> <a name="local-1627413499"></a><a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413499" class="gmail-" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier" style="color:rgb(7,54,66)">mid</span></a> <span class="gmail-hs-glyph" style="color:rgb(220,50,47)">=</span> <span class="gmail-hs-special" style="color:rgb(220,50,47)">(</span><a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413497" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">e2</span></a> <span class="gmail-hs-glyph" style="color:rgb(220,50,47)">-</span> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413496" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">e1</span></a><span class="gmail-hs-special" style="color:rgb(220,50,47)">)</span> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#%2F" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-operator gmail-hs-var" style="color:rgb(211,54,130)">/</span></a> <span class="gmail-hs-number" style="color:rgb(38,139,210)">2</span>
<a name="line-232"></a> <a name="local-1627413500"></a><a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413500" class="gmail-" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier" style="color:rgb(7,54,66)">predicate</span></a> <span class="gmail-hs-glyph" style="color:rgb(220,50,47)">|</span> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413497" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">e2</span></a> <span class="gmail-hs-operator gmail-hs-var" style="color:rgb(211,54,130)">>=</span> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413496" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">e1</span></a> <span class="gmail-hs-glyph" style="color:rgb(220,50,47)">=</span> <span class="gmail-hs-special" style="color:rgb(220,50,47)">(</span><span class="gmail-hs-operator gmail-hs-var" style="color:rgb(211,54,130)"><=</span> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413498" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">e3</span></a> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Num.html#%2B" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-operator gmail-hs-var" style="color:rgb(211,54,130)">+</span></a> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413499" class="gmail-" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">mid</span></a><span class="gmail-hs-special" style="color:rgb(220,50,47)">)</span>
<a name="line-233"></a> <span class="gmail-hs-glyph" style="color:rgb(220,50,47)">|</span> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Base.html#otherwise" class="gmail-" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">otherwise</span></a> <span class="gmail-hs-glyph" style="color:rgb(220,50,47)">=</span> <span class="gmail-hs-special" style="color:rgb(220,50,47)">(</span><span class="gmail-hs-operator gmail-hs-var" style="color:rgb(211,54,130)">>=</span> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413498" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">e3</span></a> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Num.html#%2B" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-operator gmail-hs-var" style="color:rgb(211,54,130)">+</span></a> <a href="http://hackage.haskell.org/package/base-4.9.0.0/docs/src/GHC.Real.html#local-1627413499" class="gmail-" style="text-decoration:none;border-bottom:1px solid rgb(238,232,213)"><span class="gmail-hs-identifier gmail-hs-var" style="color:rgb(7,54,66)">mid</span></a><span class="gmail-hs-special" style="color:rgb(220,50,47)">)</span></pre></div><div>and with the concrete values in the example, the predicate becomes (<=5.0). <br></div><div><br></div><div>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 would give a bad result? </div><div><br></div><div>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 :)</div><div><br></div><div>Regards,</div><div>Bence</div><div><br></div></div>