enumFromThenTo for Doubles
qdunkan at gmail.com
Wed Aug 10 22:05:50 UTC 2016
Way back when I started with haskell I noticed this, and switched to using
-- | Enumerate an inclusive range. Uses multiplication instead of
-- addition to avoid loss of precision.
-- Also it doesn't require an Enum instance.
range :: (Num a, Ord a) => a -> a -> a -> [a]
range start end step = go 0
| step >= 0 && val > end = 
| step < 0 && val < end = 
| otherwise = val : go (i+1)
where val = start + (i*step)
It's always seemed better in every way, except syntax convenience.
Wouldn't any approach with successive addition lose precision?
On Tue, Aug 9, 2016 at 8:22 PM, Andrew Farmer <xichekolas at gmail.com> wrote:
> Noticed this today:
> ghci> let xs = [0.0,0.1 .. 86400.0] in maximum xs
> enumFromThenTo is implemented by numericEnumFromThenTo:
> Which probably accumulates error in numericEnumFromThen with the (m+m-n):
> numericEnumFromThen n m = n `seq` m `seq` (n : numericEnumFromThen m
> Why not define numericEnumFromThen as:
> numericEnumFromThen n m = let d = m - n in d `seq` go d n
> where go delta x = x `seq` (x : go delta (x + delta))
> (or with BangPatterns)
> numericEnumFromThen n m = go (m - n) n
> where go !delta !x = x : go delta (x + delta)
> Seems like we'd save a lot of subtractions by using the worker function.
> ghc-devs mailing list
> ghc-devs at haskell.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the ghc-devs