HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulemath-functions-0.3.4.4Haskell2010

Numeric.MathFunctions.Comparison

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

  • 6 values

Relative erros

2 declarations
valuerelativeError :: Double -> Double -> Double
#

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

Ulps-based comparison

4 declarations
valueulpDistance :: Double -> Double -> Word64
#

Measure distance between two Doubles 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.

valueulpDelta :: Double -> Double -> Int64
#

Measure signed distance between two Doubles in ULPs (units of least precision). Note that unlike ulpDistance it can overflow.

>>> ulpDelta 1 (1 + m_epsilon)
1
valuewithin
  1. :: Int

    Number of ULPs of accuracy desired.

  2. -> Double
  3. -> Double
  4. -> 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.