[GHC] #15081: Finite list becomes infinite after maping fractional function for high numbers
GHC
ghc-devs at haskell.org
Wed Apr 25 01:37:25 UTC 2018
#15081: Finite list becomes infinite after maping fractional function for high
numbers
-------------------------------------+-------------------------------------
Reporter: Onsed | Owner: (none)
Type: bug | Status: new
Priority: normal | Milestone: 8.6.1
Component: Compiler | Version: 8.2.2
Resolution: | Keywords:
Operating System: Linux | Architecture: x86_64
Type of failure: Incorrect result | (amd64)
at runtime | Test Case:
Blocked By: | Blocking:
Related Tickets: | Differential Rev(s):
Wiki Page: |
-------------------------------------+-------------------------------------
Comment (by sighingnow):
Look at the enumerate method for fractional types:
{{{#!hs
numericEnumFrom :: (Fractional a) => a -> [a]
numericEnumFrom n = n `seq` (n : numericEnumFrom (n + 1))
numericEnumFromTo :: (Ord a, Fractional a) => a -> a -> [a]
numericEnumFromTo n m = takeWhile (<= m + 1/2) (numericEnumFrom n)
}}}
A possible improvement should be recording the accumulate increment and
add it to the base number, rather than `+1` every time.
For `Double` (64-bit floating point), the number `9007199254740991 + 1/2`
has binary representation:
{{{
0x4340000000000000
}}}
However, the number
* `9007199254740990` is: `0x433ffffffffffffe`
* `9007199254740990 + 1` is `0x433fffffffffffff`
* `(9007199254740990 + 1) + 1` is `0x4340000000000000`
* `((9007199254740990 + 1) + 1) + 1` is `0x4340000000000000`
Note that `(9007199254740990 + 1) + 1` has the same binary representation
with `((9007199254740990 + 1) + 1) + 1`, so the condition `(<= m + 1/2)`
will always hold. Then we get the infinite list.
If we change the upper bound, for example,
{{{
map (/2) [9007199254740989..9007199254740990]
}}}
It works fine and prints `[4.5035996273704945e15,4.503599627370495e15]`.
--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/15081#comment:2>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler
More information about the ghc-tickets
mailing list