Documentation for sub is-approx assembled from the following pages:

Module: Test §

From Test

(Test) sub is-approx §

Defined as:

multi sub is-approx(Numeric $gotNumeric $expected$desc = '')
multi sub is-approx(Numeric $gotNumeric $expectedNumeric $abs-tol,
                    $desc = '')
multi sub is-approx(Numeric $gotNumeric $expected$desc = '',
                    Numeric :$rel-tol is required)
multi sub is-approx(Numeric $gotNumeric $expected$desc = '',
                    Numeric :$abs-tol is required)
multi sub is-approx(Numeric $gotNumeric $expected$desc = '',
                    Numeric :$rel-tol is required,
                    Numeric :$abs-tol is required)

Marks a test as passed if the $got and $expected numerical values are approximately equal to each other. The subroutine can be called in numerous ways that let you test using relative tolerance ($rel-tol) or absolute tolerance ($abs-tol) of different values.

If no tolerance is set, the function will base the tolerance on the absolute value of $expected: if it's smaller than 1e-6, use absolute tolerance of 1e-5; if it's larger, use relative tolerance of 1e-6.

my Numeric ($value$expected$abs-tol$rel-tol= ...
 
is-approx $value$expected;
is-approx $value$expected'test description';
 
is-approx $value$expected$abs-tol;
is-approx $value$expected$abs-tol'test description';
 
is-approx $value$expected:$rel-tol;
is-approx $value$expected:$rel-tol'test description';
 
is-approx $value$expected:$abs-tol;
is-approx $value$expected:$abs-tol'test description';
 
is-approx $value$expected:$abs-tol:$rel-tol;
is-approx $value$expected:$abs-tol:$rel-tol'test description';

Absolute tolerance §

When an absolute tolerance is set, it's used as the actual maximum value by which the first and the second parameters can differ. For example:

is-approx 342# success 
is-approx 362# fail 
 
is-approx 3003022# success 
is-approx 3004002# fail 
is-approx 3006002# fail 

Regardless of values given, the difference between them cannot be more than 2.

Relative tolerance §

When a relative tolerance is set, the test checks the relative difference between values. Given the same tolerance, the larger the numbers given, the larger the value they can differ by can be.

For example:

is-approx 1010.5:rel-tol<0.1># success 
is-approx 1011.5:rel-tol<0.1># fail 
 
is-approx 100105:rel-tol<0.1># success 
is-approx 100115:rel-tol<0.1># fail 

Both versions use 0.1 for relative tolerance, yet the first can differ by about 1 while the second can differ by about 10. The function used to calculate the difference is:

              |value - expected|
⁣rel-diff = ────────────────────────
           max(|value|, |expected|)

and the test will fail if rel-diff is higher than $rel-tol.

Both absolute and relative tolerance specified §

is-approx $value$expected:rel-tol<.5>:abs-tol<10>;

When both absolute and relative tolerances are specified, each will be tested independently, and the is-approx test will succeed only if both pass.