math-functions-0.3.4.4: Collection of tools for numeric computations

Copyright | (c) 2011 Bryan O'Sullivan
License | BSD3
Maintainer | bos@serpentine.com
Stability | experimental
Portability | portable
Safe Haskell | None
Language | Haskell2010

Numeric.MathFunctions.Comparison

Contents

- Relative erros
- Ulps-based comparison

Description

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

Synopsis

- relativeError :: Double -> Double -> Double
- eqRelErr :: Double -> Double -> Double -> Bool
- addUlps :: Int -> Double -> Double
- ulpDistance :: Double -> Double -> Word64
- ulpDelta :: Double -> Double -> Int64
- within :: Int -> Double -> Double -> Bool

# 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

eqRelErr Source #

Arguments

:: Double | eps relative error should be in [0,1) range
-> Double | a
-> Double | b
-> Bool |

Check that relative error between two numbers `a` and `b`. If `relativeError` returns NaN it returns `False`.

# Ulps-based comparison

addUlps :: Int -> Double -> Double Source #

Add N ULPs (units of least precision) to `Double` number.

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

within Source #

Arguments

:: Int | Number of ULPs of accuracy desired.
-> Double |
-> Double |
-> Bool |

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`.