Module

Data.Number.Approximate

Package
purescript-numbers
Repository
purescript/purescript-numbers

This module defines functions for comparing numbers.

#Fraction Source

newtype Fraction

A newtype for (small) numbers, typically in the range [0:1]. It is used as an argument for eqRelative.

Constructors

#eqRelative Source

eqRelative :: Fraction -> Number -> Number -> Boolean

Compare two Numbers and return true if they are equal up to the given relative error (Fraction parameter).

This comparison is scale-invariant, i.e. if eqRelative frac x y, then eqRelative frac (s * x) (s * y) for a given scale factor s > 0.0 (unless one of x, y is exactly 0.0).

Note that the relation that eqRelative frac induces on Number is not an equivalence relation. It is reflexive and symmetric, but not transitive.

Example:

> (eqRelative (Fraction 0.01)) 133.7 133.0
true

> (eqRelative (Fraction 0.001)) 133.7 133.0
false

> (eqRelative (Fraction 0.01)) (0.1 + 0.2) 0.3
true

#eqApproximate Source

eqApproximate :: Number -> Number -> Boolean

Test if two numbers are approximately equal, up to a relative difference of one part in a million:

eqApproximate = eqRelative (Fraction 1.0e-6)

Example

> 0.1 + 0.2 == 0.3
false

> 0.1 + 0.2 ≅ 0.3
true

#(~=) Source

Operator alias for Data.Number.Approximate.eqApproximate (non-associative / precedence 4)

#(≅) Source

Operator alias for Data.Number.Approximate.eqApproximate (non-associative / precedence 4)

#neqApproximate Source

neqApproximate :: Number -> Number -> Boolean

The complement of eqApproximate.

#(≇) Source

Operator alias for Data.Number.Approximate.neqApproximate (non-associative / precedence 4)

#Tolerance Source

newtype Tolerance

A newtype for (small) numbers. It is used as an argument for eqAbsolute.

Constructors

#eqAbsolute Source

eqAbsolute :: Tolerance -> Number -> Number -> Boolean

Compare two Numbers and return true if they are equal up to the given (absolute) tolerance value. Note that this type of comparison is not scale-invariant. The relation induced by (eqAbsolute (Tolerance eps)) is symmetric and reflexive, but not transitive.

Example:

> (eqAbsolute (Tolerance 1.0)) 133.7 133.0
true

> (eqAbsolute (Tolerance 0.1)) 133.7 133.0
false