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

SYNOPSIS

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

DESCRIPTION

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

USAGE

63       use Test::Number::Delta;
64
65       With no arguments, epsilon defaults to 1e-6. (An arbitrary choice on
66       the author's part.)
67
68       use Test::Number::Delta within => 1e-9;
69
70       To specify a different default value for epsilon, provide a "within"
71       parameter when importing the module.  The value must be non-zero.
72
73       use Test::Number::Delta relative => 1e-3;
74
75       As an alternative to using a fixed value for epsilon, provide a "rela‐
76       tive" 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 val‐
86       ues.  A relative value of 1e-6 means that the arguments must differ by
87       less than 1 millionth of the larger value.  The relative value must be
88       non-zero.
89
90       Combining with a test plan
91
92        use Test::Number::Delta 'no_plan';
93
94        # or
95
96        use Test::Number::Delta within => 1e-9, tests => 1;
97
98       If a test plan has not already been specified, the optional parameter
99       for Test::Number::Delta may be followed with a test plan (see
100       Test::More for details).  If a parameter for Test::Number::Delta is
101       given, it must come first.
102

FUNCTIONS

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

SEE ALSO

165       Test::More, Test::Harness, Test::Builder
166

BUGS

168       Please report any bugs or feature using the CPAN Request Tracker.  Bugs
169       can be submitted by email to "bug-Test-Number-Delta@rt.cpan.org" or
170       through the web interface at <http://rt.cpan.org/Dist/Dis
171       play.html?Queue=Test-Number-Delta>
172
173       When submitting a bug or request, please include a test-file or a patch
174       to an existing test-file that illustrates the bug or desired feature.
175

AUTHOR

177       David A Golden (DAGOLDEN)
178
179       dagolden@cpan.org
180
181       <http://dagolden.com/>
182
184       Copyright (c) 2005, 2006 by David A. Golden
185
186       This program is free software; you can redistribute it and/or modify it
187       under the same terms as Perl itself.
188
189       The full text of the license can be found in the LICENSE file included
190       with this module.
191

DISCLAIMER OF WARRANTY

193       BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
194       FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT
195       WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER
196       PARTIES PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND,
197       EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
198       WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
199       ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
200       YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
201       NECESSARY SERVICING, REPAIR, OR CORRECTION.
202
203       IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
204       WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
205       REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE LIABLE
206       TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL, OR CON‐
207       SEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE SOFT‐
208       WARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED
209       INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF
210       THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF SUCH HOLDER
211       OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
212
213
214
215perl v5.8.8                       2007-06-25            Test::Number::Delta(3)
Impressum