NAME
Number::Tolerant -- tolerance ranges for inexact numbers
SYNOPSIS
use Number::Tolerant;
my $range = tolerance(10 => to => 12);
my $random = 10 + rand(2);
die "I shouldn't die" unless $random == $range;
print "This line will always print.\n";
DESCRIPTION
Number::Tolerant creates a number-like object whose value refers to a range of possible values, each equally acceptable. It overloads comparison operations to reflect this.
I use this module to simplify the comparison of measurement results to specified tolerances.
reject $product unless $measurement == $specification;
METHODS
Instantiation
There is a new
method on the Number::Tolerant class, but it also exports a simple function, tolerance
, which will return an object of the Number::Tolerant class. Both use the same syntax:
my $range = tolerance( $x => $method => $y);
The meaning of $x
and $y
are dependant on the value of $method
, which describes the nature of the tolerance. Tolerances can be defined in five ways, at present:
method range
-------------------+---------------
plus_or_minus | x ± y
plus_or_minus_pct | x ± (y% of x)
or_more | x to Inf
or_less | x to -Inf
to | x to y
infinite | -Inf to Inf
For or_less
and or_more
, $y
is ignored if passed. For infinite
, $x
and $y
are both ignored, if passed.
Overloading
Tolerances overload a few operations, mostly comparisons.
- numification
-
Tolerances with finite ranges numify to their center values. Tolerances with infinite ranges numify to their fixed end.
- stringification
-
A tolerance stringifies to a short description of itself.
infinite - "any number" to - "x to y" or_more - "x or more" or_less - "x or less" plus_or_minus - "x +/- y" plus_or_minus_pct - "x +/- y%"
- equality
-
A number is equal to a tolerance if it is neither less than nor greater than it. (See below).
- comparison
-
A number is greater than a tolerance if it is greater than its maximum value.
A number is less than a tolerance if it is less than its minimum value.
No number is greater than an "or_more" tolerance or less than an "or_less" tolerance.
"...or equal to" comparisons include the min/max values in the permissible range, as common sense suggests.
- tolerance intersection
-
A tolerance
&
a tolerance or number is the intersection of the two ranges. Intersections allow you to quickly narrow down a set of tolerances to the most stringent intersection of values.tolerance(5 => to => 6) & tolerance(5.5 => to => 6.5); # this yields: tolerance(5.5 => to => 6)
If the given values have no intersection,
()
is returned.An intersection with a normal number will yield that number, if it is within the tolerance.
TODO
Overload | (bitwise or) to create multiple range options (unions).
Allow translation into forms not originally used:
$range = tolerance(9 => to => 17);
$range->convert_to('plus_minus');
AUTHOR
Ricardo SIGNES, <rjbs@cpan.org>
COPYRIGHT
(C) 2004, Ricardo SIGNES. Number::Tolerant is available under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
- Around line 52:
Non-ASCII character seen before =encoding in '±'. Assuming CP1252