Skip to content

Enum Float/Double does not match Haskell Report

Section 6.3.4 of the report says that the Enum instance for Float and Double should define the enumFromThen* functions such that enumFromThen e1 e2 is the list [e1, e1 + i, e1 + 2i, ...] where i = e2 - e1. The actual implementation in base is that of successive additions (which is approximately the same thing for some types).

Successive additions causes issues with floating point rounding. Changing the definition to something like

enumFromThen_ e1 e2 = let i = e2 - e1 in map (\n -> e1 + n * i) [0..]

significantly improves the accuracy of floating point ranges, as well as **matching what the report says**.

Prelude> last $ take 101 $ [0,0.1..] :: Float
10.000028

Prelude> last $ take 101 $ enumFromThen_ 0 0.1 :: Float
10.0
Trac metadata
Trac field Value
Version 8.0.1
Type Bug
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component libraries/base
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information