log-domain-0.3.0.1: Log-domain arithmetic

Portabilitynon-portable
Stabilityexperimental
MaintainerEdward Kmett <ekmett@gmail.com>
Safe HaskellNone

Numeric.Log

Description

 

Synopsis

Documentation

newtype Log a Source

Log-domain Float and Double values.

Constructors

Log 

Fields

runLog :: a
 

Instances

class Floating a => Precise a whereSource

This provides log1p and expm1 for working more accurately with small numbers.

Methods

log1p :: a -> aSource

Computes log(1 + x)

This is far enough from 0 that the Taylor series is defined.

expm1 :: a -> aSource

The Taylor series for exp(x) is given by

 exp(x) = 1 + x + x^2/2! + ...

When x is small, the leading 1 consumes all of the available precision.

This computes:

 exp(x) - 1 = x + x^2/2! + ..

which can afford you a great deal of additional precision if you move things around algebraically to provide the 1 by other means.

sum :: (RealFloat a, Ord a, Precise a, Foldable f) => f (Log a) -> Log aSource

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 = replicate 40000 (Log 1e-4) :: [Log Float]
>>> Prelude.sum xs
40001.3

This sum gives a more accurate result,

>>> Numeric.Log.sum xs
40004.01

NB: This does require two passes over the data.