summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2017-01-17 16:30:54 (EST)
committer P. J. McDermott <pj@pehjota.net>2017-01-17 16:30:54 (EST)
commita9862a0e4127d4ed7c0c6fdc494b123b6f1a127c (patch)
treeda2496d59c0ad784b20fa3529d1c1131f5e7bd4b /lib
parent2cb54e56e503fd648fb257a2d9452c784e437eb3 (diff)
downloadMath-Decimal-FastPP-a9862a0e4127d4ed7c0c6fdc494b123b6f1a127c.zip
Math-Decimal-FastPP-a9862a0e4127d4ed7c0c6fdc494b123b6f1a127c.tar.gz
Math-Decimal-FastPP-a9862a0e4127d4ed7c0c6fdc494b123b6f1a127c.tar.bz2
Math::Decimal::FastPP: Move POD to after __END__
Also remove a useless "=cut".
Diffstat (limited to 'lib')
-rw-r--r--lib/Math/Decimal/FastPP.pm182
1 files changed, 85 insertions, 97 deletions
diff --git a/lib/Math/Decimal/FastPP.pm b/lib/Math/Decimal/FastPP.pm
index 3fe00c7..c1a04df 100644
--- a/lib/Math/Decimal/FastPP.pm
+++ b/lib/Math/Decimal/FastPP.pm
@@ -1,3 +1,88 @@
+package Math::Decimal::FastPP;
+
+use strict;
+use warnings;
+
+use Exporter qw(import);
+our @EXPORT_OK = qw(dadd dmul drhtz drhfz);
+
+our $VERSION = '0.001';
+
+sub dadd
+{
+ my ($ai, $af) = split(/[.]/, $_[0]);
+ my ($bi, $bf) = split(/[.]/, $_[1]);
+ $af ||= '';
+ $bf ||= '';
+ my $ae = length($af);
+ my $be = length($bf);
+ my $ce;
+ if ($ae == $be) {
+ $ce = $ae;
+ } elsif ($ae < $be) {
+ $af .= '0' x ($be - $ae);
+ $ce = $be;
+ } else {
+ $bf .= '0' x ($ae - $be);
+ $ce = $ae;
+ }
+ my $as = $ai . $af;
+ my $bs = $bi . $bf;
+ my $cs = $as + $bs;
+ $cs = sprintf("%0${ce}i", $cs);
+ return substr($cs, 0, length($cs) - $ce) . '.' .
+ substr($cs, length($cs) - $ce);
+}
+
+sub dmul
+{
+ my ($ai, $af) = split(/[.]/, $_[0]);
+ my ($bi, $bf) = split(/[.]/, $_[1]);
+ $af ||= '';
+ $bf ||= '';
+ my $as = $ai . $af;
+ my $ae = length($af);
+ my $bs = $bi . $bf;
+ my $be = length($bf);
+ my $cs = $as * $bs;
+ my $ce = $ae + $be;
+ $cs = sprintf("%0${ce}i", $cs);
+ return substr($cs, 0, length($cs) - $ce) . '.' .
+ substr($cs, length($cs) - $ce);
+}
+
+sub drhtz
+{
+ my ($ai, $af, $ad) = $_[0] =~ m/^(-?\d*)[.](\d{$_[1]})(\d*)$/ or return;
+ my $as = $ai . $af;
+ if ($as >= 0) {
+ if ($ad > '5' . '0' x (length($ad) - 1)) { ++$as; }
+ } else {
+ if ($ad > '5' . '0' x (length($ad) - 1)) { --$as; }
+ }
+ $as = sprintf("%0$_[1]i", $as);
+ return substr($as, 0, length($as) - $_[1]) . '.' .
+ substr($as, length($as) - $_[1]);
+}
+
+sub drhfz
+{
+ my ($ai, $af, $ad) = $_[0] =~ m/^(-?\d*)[.](\d{$_[1]})(\d*)$/ or return;
+ my $as = $ai . $af;
+ if ($as >= 0) {
+ if ($ad >= '5' . '0' x (length($ad) - 1)) { ++$as; }
+ } else {
+ if ($ad >= '5' . '0' x (length($ad) - 1)) { --$as; }
+ }
+ $as = sprintf("%0$_[1]i", $as);
+ return substr($as, 0, length($as) - $_[1]) . '.' .
+ substr($as, length($as) - $_[1]);
+}
+
+1;
+
+__END__
+
=head1 NAME
Math::Decimal::FastPP - Fast pure-Perl decimal math
@@ -72,77 +157,18 @@ the common exponent of the input numbers.
=head1 SUBROUTINES/METHODS
-=cut
-
-package Math::Decimal::FastPP;
-
-use strict;
-use warnings;
-
-use Exporter qw(import);
-our @EXPORT_OK = qw(dadd dmul drhtz drhfz);
-
-our $VERSION = '0.001';
-
=head2 dadd()
$c = dadd($a, $b);
Adds C<$a> and C<$b> and returns their sum.
-=cut
-
-sub dadd
-{
- my ($ai, $af) = split(/[.]/, $_[0]);
- my ($bi, $bf) = split(/[.]/, $_[1]);
- $af ||= '';
- $bf ||= '';
- my $ae = length($af);
- my $be = length($bf);
- my $ce;
- if ($ae == $be) {
- $ce = $ae;
- } elsif ($ae < $be) {
- $af .= '0' x ($be - $ae);
- $ce = $be;
- } else {
- $bf .= '0' x ($ae - $be);
- $ce = $ae;
- }
- my $as = $ai . $af;
- my $bs = $bi . $bf;
- my $cs = $as + $bs;
- $cs = sprintf("%0${ce}i", $cs);
- return substr($cs, 0, length($cs) - $ce) . '.' .
- substr($cs, length($cs) - $ce);
-}
-
=head2 dmul()
$c = dmul($a, $b);
Multiplies C<$a> and C<$b> and returns their product.
-=cut
-
-sub dmul
-{
- my ($ai, $af) = split(/[.]/, $_[0]);
- my ($bi, $bf) = split(/[.]/, $_[1]);
- $af ||= '';
- $bf ||= '';
- my $as = $ai . $af;
- my $ae = length($af);
- my $bs = $bi . $bf;
- my $be = length($bf);
- my $cs = $as * $bs;
- my $ce = $ae + $be;
- $cs = sprintf("%0${ce}i", $cs);
- return substr($cs, 0, length($cs) - $ce) . '.' .
- substr($cs, length($cs) - $ce);
-}
-
=head2 drhtz()
$a = drhtz($a, $p);
@@ -158,22 +184,6 @@ example:
C<$p> is a non-negative (i.e. zero or positive) integer representing the number
of significant digits right of the radix point.
-=cut
-
-sub drhtz
-{
- my ($ai, $af, $ad) = $_[0] =~ m/^(-?\d*)[.](\d{$_[1]})(\d*)$/ or return;
- my $as = $ai . $af;
- if ($as >= 0) {
- if ($ad > '5' . '0' x (length($ad) - 1)) { ++$as; }
- } else {
- if ($ad > '5' . '0' x (length($ad) - 1)) { --$as; }
- }
- $as = sprintf("%0$_[1]i", $as);
- return substr($as, 0, length($as) - $_[1]) . '.' .
- substr($as, length($as) - $_[1]);
-}
-
=head2 drhfz()
$a = drhfz($a, $p);
@@ -189,26 +199,6 @@ example:
C<$p> is a non-negative (i.e. zero or positive) integer representing the number
of significant digits right of the radix point.
-=cut
-
-sub drhfz
-{
- my ($ai, $af, $ad) = $_[0] =~ m/^(-?\d*)[.](\d{$_[1]})(\d*)$/ or return;
- my $as = $ai . $af;
- if ($as >= 0) {
- if ($ad >= '5' . '0' x (length($ad) - 1)) { ++$as; }
- } else {
- if ($ad >= '5' . '0' x (length($ad) - 1)) { --$as; }
- }
- $as = sprintf("%0$_[1]i", $as);
- return substr($as, 0, length($as) - $_[1]) . '.' .
- substr($as, length($as) - $_[1]);
-}
-
-1;
-
-__END__
-
=head1 DIAGNOSTICS
This module has no diagnostics.
@@ -261,5 +251,3 @@ GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see L<http://www.gnu.org/licenses/>.
-
-=cut