Number::Tolerant(3pm)
π Number::Tolerant β the Number::Tolerant(3pm) bestie era π
okay so picture this: it's the User Contributed Perl Documentation and we're talking about Number::Tolerant β tolerance ranges for inexact numbers. Yes bestie, ranges. For numbers that are likeβ¦ approximately that. Slay. π
β¨ VERSION β¨
version 1.710 β the one and only, no cap. π
π 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";
likeβ¦ the die literally cannot happen?? iconic. the range ate and left no crumbs. π
π¦ 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. π«Ά
The author uses this module to simplify the comparison of measurement results to specified tolerances. And honestly? relatable. π―
reject $product unless $measurement == $specification;
one line. no drama. the vibe check passes. β¨
πΈ PERL VERSION πΈ
This library should run on perls released even a long time ago. It should work on any version of perl released in the last five years.
Although it may work on older versions of perl, no guarantee is made that the minimum required version will not be increased. The version may be increased for any reason, and there is no promise that patches will be accepted to lower the minimum required perl. π
fr fr, do not come at this module asking for a lower minimum perl. that door is closed. πͺ
π§ METHODS π§
π« Instantiation π«
new
tolerance
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 = Number::Tolerant->new( $x => $method => $y);
my $range = tolerance( $x => $method => $y);
The meaning of $x and $y are dependent 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
more_than | x to Inf, not x
less_than | x to -Inf, not x
to | x to y
infinite | -Inf to Inf
offset | (x + y1) to (x + y2)
(yes, bestie, the list says "five" and then shows nine. the original said it, so we keep it. we do not edit the lore. π)
For "or_less" and "or_more", $y is ignored if passed. For "infinite", neither $x nor $y is used; "infinite" should be the sole argument. The first two arguments can be reversed for "more_than" and "less_than", to be more English-like. π£οΈ
Offset tolerances are slightly unusual. Here is an example:
my $offset_tolerance = tolerance(10 => offset => (-3, 5));
# stringifies to: 10 (-3 +5)
An offset is very much like a "plus_or_minus" tolerance, but its center value is not necessarily the midpoint between its extremes. This is significant for comparisons and numifications of the tolerance. Given the following two tolerances:
my $pm_dice = tolerance(10.5 => plus_or_minus => 7.5);
my $os_dice = tolerance(11 => offset => (-8, 7));
The first will sort as numerically less than the second. π―
If the given arguments can't be formed into a tolerance, an exception will be raised. not a vibe, but it's the rules.
from_string
A new tolerance can be instantiated from the stringification of an old tolerance. For example:
my $range = Number::Tolerant->from_string("10 to 12");
die "Everything's OK!" if 11 == $range; # program dies of joy
This will not yet parse stringified unions, but that will be implemented in the future. (The author just doesn't need it yet. π )
If a string can't be parsed, an exception is raised.
stringify_as
my $string = $tolerance->stringify_as($type);
This method does nothing! π Someday, it will stringify the given tolerance as a different type, if possible. "10 +/- 1" will "stringify_as('plus_or_minus_pct')" to "10 +/- 10%" for example.
it's giving "coming soon to a CPAN near you". π
numify
my $n = $tolerance->numify;
This returns the numeric form of a tolerance. If a tolerance has both a minimum and a maximum, and they are the same, then that is the numification. Otherwise, numify returns undef. π
π Overloading π
Tolerances overload a few operations, mostly comparisons.
- boolean β Tolerances are always true. every single one. periodt. β¨
- numify β Most tolerances numify to
undef; see"numify". - stringify β A tolerance stringifies to a short description of itself, generally something like
"m < x < n":infiniteβ"any number"toβ"m <= x <= n"or_moreβ"m <= x"or_lessβ"x <= n"more_thanβ"m < x"less_thanβ"x < n"offsetβ"x (-y1 +y2)"constantβ"x"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.)
- smart match β Same as equality. π«Ά
- 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. obviously. π
- 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. π
- tolerance union β A tolerance
"|"a tolerance or number is the union of the two. Unions allow multiple tolerances, whether they intersect or not, to be treated as one. See Number::Tolerant::Union for more information.
π₯ EXTENDING π₯
This feature is slighly experimental, but it's here. π«‘
New tolerance types may be written as subclasses of Number::Tolerant::Type, providing the interface described in its documentation. They can then be enabled or disabled with the following methods:
π " enable_plugin " π
Number::Tolerant->enable_plugin($class_name);
This method enables the named class, so that attempts to create new tolerances will check against this class. Classes are checked against "validate_plugin" before being enabled. An exception is thrown if the class does not appear to provide the Number::Tolerant::Type interface.
π " disable_plugin " π
Number::Tolerant->disable_plugin($class_name);
This method will disable the named class, so that future attempts to create new tolerances will not check against this class. π«
π " validate_plugin " π
Number::Tolerant->validate_plugin($class_name);
This method checks (naively) that the given class provides the interface defined in Number::Tolerant::Type. If it does not, an exception is thrown.
π TODO π
- Extend
"from_string"to cover unions. - Extend
"from_string"to includeNumber::Range-type specifications. - Allow translation into forms not originally used:
my $range = tolerance(9 => to => 17);
my $range_pm = $range->convert_to('plus_minus');
$range->stringify_as('plus_minus_pct');
- Create a factory so that you can simultaneously work with two sets of plugins.
This one is very near completion. There will now be two classes that should be used: Number::Tolerant::Factory, which produces tolerances, and Number::Tolerant::Tolerance, which is a tolerance. Both will inherit from N::T, for supporting old code, and N::T will dispatch construction methods to a default factory.
π SEE ALSO π
The module Number::Range provides another way to deal with ranges of numbers. The major differences are: N::R is set-like, not range-like; N::R does not overload any operators. Number::Tolerant will not (like N::R) attempt to parse a textual range specification like "1..2,5,7..10" unless specifically instructed to. (The valid formats for strings passed to "from_string" does not match Number::Range exactly. See TODO.)
The "Number::Range" code:
$range = Number::Range->new("10..15","20..25");
Is equivalent to the "Number::Tolerant" code:
$range = Number::Tolerant::Union->new(10..15,20..25);
...while the following code expresses an actual range:
$range = tolerance(10 => to => 15) | tolerance(20 => to => 25);
see the difference?? that's the whole point bestie. iykyk. π
π«Ά THANKS π«Ά
Thanks to Yuval Kogman and #perl-qa for helping find the bizarre bug that drove the minimum required perl up to 5.8 π
Thanks to Tom Freedman, who reminded the author that this code was fun to work on, and also provided the initial implementation for the offset type. π
β AUTHOR β
Ricardo Signes <cpan@semiotic.systems>
π CONTRIBUTORS π
- Alexandre Mestiashvili
<alex@biotec.tu-dresden.de> - Karen Etheridge
<ether@cpan.org> - Michael Carman
<mjcarman@cpan.org> - Ricardo SIGNES
<rjbs@codesimply.com> - Ricardo Signes
<rjbs@semiotic.systems> - Smylers
<Smylers@stripey.com>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2004 by Ricardo Signes.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. π«Ά
and that's the whole page bestie!! β¨ this lil writeup lives in Number::Tolerant(3pm) from perl v5.36.0, dated 2023-01-08. go forth and tolerance your numbers, you absolute icon. π
ππ¦
π¦ where this page comes from
- debian bookworm: libnumber-tolerant-perl 1.710-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libnumber-tolerant-perl 1.710-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libnumber-tolerant-perl 1.710-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libnumber-tolerant-perl 1.710-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libnumber-tolerant-perl 1.710-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms