Copyright | (c) 2011 Bryan O'Sullivan |
---|---|

License | BSD3 |

Maintainer | bos@serpentine.com |

Stability | experimental |

Portability | portable |

Safe Haskell | None |

Language | Haskell2010 |

Functions for approximate comparison of floating point numbers.

Approximate floating point comparison, based on Bruce Dawson's "Comparing floating point numbers": http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

# Relative erros

relativeError :: Double -> Double -> Double Source #

Calculate relative error of two numbers:

\[ \frac{|a - b|}{\max(|a|,|b|)} \]

It lies in [0,1) interval for numbers with same sign and (1,2] for numbers with different sign. If both arguments are zero or negative zero function returns 0. If at least one argument is transfinite it returns NaN

Check that relative error between two numbers `a`

and `b`

. If
`relativeError`

returns NaN it returns `False`

.

# Ulps-based comparison

ulpDistance :: Double -> Double -> Word64 Source #

Measure distance between two `Double`

s in ULPs (units of least
precision). Note that it's different from `abs (ulpDelta a b)`

since it returns correct result even when `ulpDelta`

overflows.

ulpDelta :: Double -> Double -> Int64 Source #

Measure signed distance between two `Double`

s in ULPs (units of least
precision). Note that unlike `ulpDistance`

it can overflow.

>>> ulpDelta 1 (1 + m_epsilon) 1

Compare two `Double`

values for approximate equality, using
Dawson's method.

The required accuracy is specified in ULPs (units of least
precision). If the two numbers differ by the given number of ULPs
or less, this function returns `True`

.