sub is-approx
1 | module Test |
1.1 | (Test) sub is-approx |
1.1.1 | Absolute tolerance |
1.1.2 | Relative tolerance |
1.1.3 | Both absolute and relative tolerance specified |
Documentation for sub is-approx
assembled from the following types:
module Test
From Test
(Test) sub is-approx
Defined as:
multi sub is-approx(Numeric , Numeric , = '')
multi sub is-approx(Numeric , Numeric , Numeric ,= '')
multi sub is-approx(Numeric , Numeric , = '',Numeric : is required)
multi sub is-approx(Numeric , Numeric , = '',Numeric : is required)
multi sub is-approx(Numeric , Numeric , = '',Numeric : is required,Numeric : is required)
Marks a test as passed if the $value
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 (, , , ) = ...is-approx , ;is-approx , , 'test description';is-approx , , ;is-approx , , , 'test description';is-approx , , :;is-approx , , :, 'test description';is-approx , , :;is-approx , , :, 'test description';is-approx , , :, :;is-approx , , :, :, 'test description';
Absolute tolerance
When an absolute tolerance is set, it's used as the actual maximum value by which the $value
and $expected
can differ. For example:
is-approx 3, 4, 2; # successis-approx 3, 6, 2; # failis-approx 300, 302, 2; # successis-approx 300, 400, 2; # failis-approx 300, 600, 2; # 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 10, 10.5, :rel-tol<0.1>; # successis-approx 10, 11.5, :rel-tol<0.1>; # failis-approx 100, 105, :rel-tol<0.1>; # successis-approx 100, 115, :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 , , :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.