From a9862a0e4127d4ed7c0c6fdc494b123b6f1a127c Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Tue, 17 Jan 2017 16:30:54 -0500 Subject: Math::Decimal::FastPP: Move POD to after __END__ Also remove a useless "=cut". --- 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. - -=cut -- cgit v0.9.1