1Test::Number::Delta(3)User Contributed Perl DocumentationTest::Number::Delta(3)
2
3
4

NAME

6       Test::Number::Delta - Compare the difference between numbers against a
7       given tolerance
8

VERSION

10       version 1.06
11

SYNOPSIS

13         # Import test functions
14         use Test::Number::Delta;
15
16         # Equality test with default tolerance
17         delta_ok( 1e-5, 2e-5, 'values within 1e-6');
18
19         # Inequality test with default tolerance
20         delta_not_ok( 1e-5, 2e-5, 'values not within 1e-6');
21
22         # Provide specific tolerance
23         delta_within( 1e-3, 2e-3, 1e-4, 'values within 1e-4');
24         delta_not_within( 1e-3, 2e-3, 1e-4, 'values not within 1e-4');
25
26         # Compare arrays or matrices
27         @a = ( 3.14, 1.41 );
28         @b = ( 3.15, 1.41 );
29         delta_ok( \@a, \@b, 'compare @a and @b' );
30
31         # Set a different default tolerance
32         use Test::Number::Delta within => 1e-5;
33         delta_ok( 1.1e-5, 2e-5, 'values within 1e-5'); # ok
34
35         # Set a relative tolerance
36         use Test::Number::Delta relative => 1e-3;
37         delta_ok( 1.01, 1.0099, 'values within 1.01e-3');
38

DESCRIPTION

40       At some point or another, most programmers find they need to compare
41       floating-point numbers for equality.  The typical idiom is to test if
42       the absolute value of the difference of the numbers is within a desired
43       tolerance, usually called epsilon.  This module provides such a
44       function for use with Test::More.  Usage is similar to other test
45       functions described in Test::More.  Semantically, the "delta_within"
46       function replaces this kind of construct:
47
48        ok ( abs($p - $q) < $epsilon, '$p is equal to $q' ) or
49            diag "$p is not equal to $q to within $epsilon";
50
51       While there's nothing wrong with that construct, it's painful to type
52       it repeatedly in a test script.  This module does the same thing with a
53       single function call.  The "delta_ok" function is similar, but either
54       uses a global default value for epsilon or else calculates a 'relative'
55       epsilon on the fly so that epsilon is scaled automatically to the size
56       of the arguments to "delta_ok".  Both functions are exported
57       automatically.
58
59       Because checking floating-point equality is not always reliable, it is
60       not possible to check the 'equal to' boundary of 'less than or equal to
61       epsilon'.  Therefore, Test::Number::Delta only compares if the absolute
62       value of the difference is less than epsilon (for equality tests) or
63       greater than epsilon (for inequality tests).
64

USAGE

66   use Test::Number::Delta;
67       With no arguments, epsilon defaults to 1e-6. (An arbitrary choice on
68       the author's part.)
69
70   use Test::Number::Delta within => 1e-9;
71       To specify a different default value for epsilon, provide a "within"
72       parameter when importing the module.  The value must be non-zero.
73
74   use Test::Number::Delta relative => 1e-3;
75       As an alternative to using a fixed value for epsilon, provide a
76       "relative" parameter when importing the module.  This signals that
77       "delta_ok" should test equality with an epsilon that is scaled to the
78       size of the arguments.  Epsilon is calculated as the relative value
79       times the absolute value of the argument with the greatest magnitude.
80       Mathematically, for arguments 'x' and 'y':
81
82        epsilon = relative * max( abs(x), abs(y) )
83
84       For example, a relative value of "0.01" would mean that the arguments
85       are equal if they differ by less than 1% of the larger of the two
86       values.  A relative value of 1e-6 means that the arguments must differ
87       by less than 1 millionth of the larger value.  The relative value must
88       be non-zero.
89
90   Combining with a test plan
91        use Test::Number::Delta 'no_plan';
92
93        # or
94
95        use Test::Number::Delta within => 1e-9, tests => 1;
96
97       If a test plan has not already been specified, the optional parameter
98       for Test::Number::Delta may be followed with a test plan (see
99       Test::More for details).  If a parameter for Test::Number::Delta is
100       given, it must come first.
101

FUNCTIONS

103   delta_within
104        delta_within(  $p,  $q, $epsilon, '$p and $q are equal within $epsilon' );
105        delta_within( \@p, \@q, $epsilon, '@p and @q are equal within $epsilon' );
106
107       This function tests for equality within a given value of epsilon. The
108       test is true if the absolute value of the difference between $p and $q
109       is less than epsilon.  If the test is true, it prints an "OK" statement
110       for use in testing.  If the test is not true, this function prints a
111       failure report and diagnostic.  Epsilon must be non-zero.
112
113       The values to compare may be scalars or references to arrays.  If the
114       values are references to arrays, the comparison is done pairwise for
115       each index value of the array.  The pairwise comparison is recursive,
116       so matrices may be compared as well.
117
118       For example, this code sample compares two matrices:
119
120           my @a = (   [ 3.14, 6.28 ],
121                       [ 1.41, 2.84 ]   );
122
123           my @b = (   [ 3.14, 6.28 ],
124                       [ 1.42, 2.84 ]   );
125
126           delta_within( \@a, \@b, 1e-6, 'compare @a and @b' );
127
128       The sample prints the following:
129
130           not ok 1 - compare @a and @b
131           # At [1][0]: 1.4100000 and 1.4200000 are not equal to within 0.000001
132
133   delta_ok
134        delta_ok(  $p,  $q, '$p and $q are close enough to equal' );
135        delta_ok( \@p, \@q, '@p and @q are close enough to equal' );
136
137       This function tests for equality within a default epsilon value.  See
138       "USAGE" for details on changing the default.  Otherwise, this function
139       works the same as "delta_within".
140
141   delta_not_within
142        delta_not_within(  $p,  $q, '$p and $q are different' );
143        delta_not_within( \@p, \@q, $epsilon, '@p and @q are different' );
144
145       This test compares inequality in excess of a given value of epsilon.
146       The test is true if the absolute value of the difference between $p and
147       $q is greater than epsilon.  For array or matrix comparisons, the test
148       is true if any pair of values differs by more than epsilon.  Otherwise,
149       this function works the same as "delta_within".
150
151   delta_not_ok
152        delta_not_ok(  $p,  $q, '$p and $q are different' );
153        delta_not_ok( \@p, \@q, '@p and @q are different' );
154
155       This function tests for inequality in excess of a default epsilon
156       value.  See "USAGE" for details on changing the default.  Otherwise,
157       this function works the same as "delta_not_within".
158

SEE ALSO

160       ·   Number::Tolerant
161
162       ·   Test::Deep::NumberTolerant
163

SUPPORT

165   Bugs / Feature Requests
166       Please report any bugs or feature requests through the issue tracker at
167       <https://github.com/dagolden/Test-Number-Delta/issues>.  You will be
168       notified automatically of any progress on your issue.
169
170   Source Code
171       This is open source software.  The code repository is available for
172       public review and contribution under the terms of the license.
173
174       <https://github.com/dagolden/Test-Number-Delta>
175
176         git clone https://github.com/dagolden/Test-Number-Delta.git
177

AUTHOR

179       David Golden <dagolden@cpan.org>
180
182       This software is Copyright (c) 2014 by David Golden.
183
184       This is free software, licensed under:
185
186         The Apache License, Version 2.0, January 2004
187
188
189
190perl v5.30.0                      2019-07-26            Test::Number::Delta(3)
Impressum