Skip to content

Instantly share code, notes, and snippets.

@jfager
Created April 10, 2011 18:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jfager/912588 to your computer and use it in GitHub Desktop.
Save jfager/912588 to your computer and use it in GitHub Desktop.
Full precision summation based on http://code.activestate.com/recipes/393090/
-- Full precision summation based on http://code.activestate.com/recipes/393090/
msum :: [Double] -> Double
msum [] = 0
msum (x:xs) = sum $ foldl' (\ps x' -> reverse (partials x' ps)) [x] xs
partials :: Double -> [Double] -> [Double]
partials x = foldl' (\acc p -> case hilo x p of (hi, 0) -> hi:acc
(hi, lo) -> hi:lo:acc) []
hilo :: Double -> Double -> (Double, Double)
hilo x y | abs x < abs y = hilo y x
| otherwise = (hi, lo) where hi = x + y
lo = y - (hi - x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment