Copyright | (c) Edward Kmett 2013-2015 |
---|---|
License | BSD3 |
Maintainer | Edward Kmett <ekmett@gmail.com> |
Stability | experimental |
Portability | non-portable |
Safe Haskell | Trustworthy |
Language | Haskell98 |
Documentation
Log
-domain Float
and Double
values.
Instances
sum :: (RealFloat a, Foldable f) => f (Log a) -> Log a Source #
Efficiently and accurately compute the sum of a set of log-domain numbers
While folding with (+)
accomplishes the same end, it requires an
additional n-2
logarithms to sum n
terms. In addition,
here we introduce fewer opportunities for round-off error.
While for small quantities the naive sum accumulates error,
>>>
let xs = Prelude.replicate 40000 (Exp 1e-4) :: [Log Float]
>>>
Prelude.sum xs ~= 4.00e4
True
This sum gives a more accurate result,
>>>
Numeric.Log.sum xs ~= 4.00e4
True
NB: This does require two passes over the data.