approxEqual

Computes whether two values are approximately equal, admitting a maximum relative difference, and a maximum absolute difference.

  1. bool approxEqual(T lhs, T rhs, T maxRelDiff, T maxAbsDiff)
    bool
    approxEqual
    (
    T
    )
    (
    const T lhs
    ,
    const T rhs
    ,
    const T maxRelDiff = T(0x1p-20f)
    ,
    const T maxAbsDiff = T(0x1p-20f)
    )
    if (
    isFloatingPoint!T
    )
  2. bool approxEqual(T lhs, T rhs, float maxRelDiff, float maxAbsDiff)
  3. bool approxEqual(T lhs, T rhs, double maxRelDiff, double maxAbsDiff)
  4. bool approxEqual(T lhs, T rhs, real maxRelDiff, real maxAbsDiff)

Parameters

lhs T

First item to compare.

rhs T

Second item to compare.

maxRelDiff T

Maximum allowable difference relative to rhs. Defaults to 0.5 ^^ 20.

maxAbsDiff T

Maximum absolute difference. Defaults to 0.5 ^^ 20.

Return Value

Type: bool

true if the two items are equal or approximately equal under either criterium.

Examples

assert(approxEqual(1.0, 1.0000001));
assert(approxEqual(1.0f, 1.0000001f));
assert(approxEqual(1.0L, 1.0000001L));

assert(approxEqual(10000000.0, 10000001));
assert(approxEqual(10000000f, 10000001f));
assert(!approxEqual(100000.0L, 100001L));

Complex types works as approxEqual(l.re, r.re) && approxEqual(l.im, r.im)

import mir.internal.utility: isComplexOf;
static struct UserComplex(T) { T re, im; }
alias _cdouble = UserComplex!double;

static assert(isComplexOf!(_cdouble, double));

assert(approxEqual(_cdouble(1.0, 1), _cdouble(1.0000001, 1), 1.0000001));
assert(!approxEqual(_cdouble(100000.0L, 0), _cdouble(100001L, 0)));

Meta