Subject: | A patch to include a rounding mode of 'common' |
Implements common rounding as described in
http://en.wikipedia.org/wiki/Rounding. I couldn't determine an existing
technique for this sort of rounding.
Subject: | common_rounding.patch |
diff -Naur old/lib/Math/BigInt.pm new/lib/Math/BigInt.pm
--- old/lib/Math/BigInt.pm 2005-05-18 01:49:34.000000000 +1000
+++ new/lib/Math/BigInt.pm 2006-09-29 12:20:41.032497590 +1000
@@ -148,7 +148,7 @@
# These vars are public, but their direct usage is not recommended, use the
# accessor methods instead
-$round_mode = 'even'; # one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'
+$round_mode = 'even'; # one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'
$accuracy = undef;
$precision = undef;
$div_scale = 40;
@@ -201,7 +201,7 @@
if (defined $_[0])
{
my $m = shift;
- if ($m !~ /^(even|odd|\+inf|\-inf|zero|trunc)$/)
+ if ($m !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/)
{
require Carp; Carp::croak ("Unknown round mode '$m'");
}
@@ -882,7 +882,7 @@
return ($self->bnan()) if defined $a && defined $p; # error
$r = ${"$c\::round_mode"} unless defined $r;
- if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc)$/)
+ if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/)
{
require Carp; Carp::croak ("Unknown round mode '$r'");
}
@@ -939,7 +939,7 @@
return $self->bnan() if defined $a && defined $p;
$r = ${"$c\::round_mode"} unless defined $r;
- if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc)$/)
+ if ($r !~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/)
{
require Carp; Carp::croak ("Unknown round mode '$r'");
}
@@ -2778,7 +2778,7 @@
Math::BigInt->precision(); # get/set global P for all BigInt objects
Math::BigInt->accuracy(); # get/set global A for all BigInt objects
Math::BigInt->round_mode(); # get/set global round mode, one of
- # 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'
+ # 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'
Math::BigInt->config(); # return hash containing configuration
=head1 DESCRIPTION
@@ -3480,6 +3480,12 @@
E.g., when rounding to the first sigdig, 0.45 becomes 0.4, -0.55
becomes -0.5, but 0.4501 becomes 0.5.
+=item 'common'
+
+round up if the digit immediately to the right of the rounding place
+is 5, otherwise round down. E.g., 0.15 becomes 0.2 and 0.149 becomes
+0.1
+
=back
The handling of A & P in MBI/MBF (the old core code shipped with Perl
@@ -3672,7 +3678,7 @@
is for precision
* the two rounding functions take as the second parameter one of the
following rounding modes (R):
- 'even', 'odd', '+inf', '-inf', 'zero', 'trunc'
+ 'even', 'odd', '+inf', '-inf', 'zero', 'trunc', 'common'
* you can set/get the global R by using C<< Math::SomeClass->round_mode() >>
or by setting C<< $Math::SomeClass::round_mode >>
* after each operation, C<< $result->round() >> is called, and the result may