=head1 NAME Math::Decimal::FastPP - Fast pure-Perl decimal math =head1 SYNOPSIS use Math::Decimal::FastPP qw(dadd dmul drhtz drhfz); $a = dadd($a, "1.23"); # $a += 1.23 $c = dmul($a, $b); # $c = $a * $b $a = drhtz($a); # Round half toward zero =head1 DESCRIPTION Math::Decimal::FastPP provides a few common decimal arithmetic and rounding functions written in pure Perl. The functions are of course slower than Perl's built-in binary floating-point math, but they're faster than L and other commonly used decimal math modules. This module is currently less complete than Perl's built-in math and other decimal math modules. So far it only includes addition, multiplication, and two rounding functions. Despite the similar name and purpose, this module is not compatible with L. =head1 PHILOSOPHY This module is designed both to run fast and to be used fast. The functions are written to be as short and fast as possible, at a small cost in readability. For help reading the bodies of these functions, see L below. The names and parameters of this module's functions are kept minimal to allow them to be typed quickly and to take little space in calling code. After all, a function to add two numbers shouldn't take much more typing than C<+>. The names of the rounding functions may look strange, but they are simply initialisms (or acronyms, if you're creative enough) of "decimal round toward zero" and "decimal round (away) from zero". =head1 THEORY The binary operation functions (C and C) operate on two numbers, C and C. And obviously the unary operation functions (C and C) operate on one number, C. The binary operation functions produce a resulting number, C. The input numbers C and C are broken into their integer (C) and fractional (C) parts, for example C<$ai> and C<$af>. All three numbers C, C, and C are comprised of a significand C and (negated) exponent C and can be expressed as C. The significand is all of the significant digits (although the digits are preserved exactly as given, so leading zeroes are considered "significant") in the form of an integer, with no radix point. It is formed simply by concatenating the integer and fractional parts, e.g. C<$as = $ai . $af>. The exponent is simply the number of digits in the fractional part, e.g. C<$ae = length($af)>. Multiplication is done simply be multiplying the integer significands and adding the exponents. The resulting significand and exponent is converted back into a string with integer and fractional parts. Addition is a little more complicated. The exponents of the two input numbers must match; if they don't, zeroes are appended to the significand of the number with the smaller exponent to make the exponents match. The significands are then added. The output number is converted from the resulting significand and the common exponent of the input numbers. =head1 FUNCTIONS =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); # The substr() code is 400% faster than this regular expression code. #$cs =~ s/(.{$ce})$/.$1/; #return $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); # The substr() code is 400% faster than this regular expression code. #$cs =~ s/(.{$ce})$/.$1/; #return $cs; return substr($cs, 0, length($cs) - $ce) . "." . substr($cs, length($cs) - $ce); } =head2 drhtz() $a = drhtz($a, $p); Rounds C<$a> with precision C<$p>. Halves are rounded toward zero. For example: drhtz("23.5", 0); # Returns "23." drhtz("2.35", 1); # Returns "2.3" drhtz("-23.5", 0); # Returns "-23." drhtz("-2.35", 1); # Returns "-2.3" 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/^(.*)\.(.{$_[1]})(.*)$/ or return; my $as = $ai . $af; if ($as >= 0) { ++$as if $ad > "5" . "0" x (length($ad) - 1); } else { --$as if $ad > "5" . "0" x (length($ad) - 1); } $as = sprintf("%0$_[1]i", $as); # The substr() code is 400% faster than this regular expression code. #$as =~ s/(.{$_[1]})$/.$1/; #return $cs; return substr($as, 0, length($as) - $_[1]) . "." . substr($as, length($as) - $_[1]); } =head2 drhfz() $a = drhfz($a, $p); Rounds C<$a> with precision C<$p>. Halves are rounded away from zero. For example: drhfz("23.5", 0); # Returns "24." drhtz("2.35", 1); # Returns "2.4" drhfz("-23.5", 0); # Returns "-24." drhtz("-2.35", 1); # Returns "-2.4" 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/^(.*)\.(.{$_[1]})(.*)$/ or return; my $as = $ai . $af; if ($as >= 0) { ++$as if $ad >= "5" . "0" x (length($ad) - 1); } else { --$as if $ad >= "5" . "0" x (length($ad) - 1); } $as = sprintf("%0$_[1]i", $as); # The substr() code is 400% faster than this regular expression code. #$as =~ s/(.{$_[1]})$/.$1/; #return $as; return substr($as, 0, length($as) - $_[1]) . "." . substr($as, length($as) - $_[1]); } 1; __END__ =head1 CAVEATS These arithmetic functions preserve all significant fractional digits, including trailing zeroes. They also don't always add a leading zero before the radix point for numbers with absolute values less than one. So the output numbers can look "ugly", like ".123000". This is only an issue if the numbers (which are returned as strings) are concatenated into other strings or printed without formatting. If this is an issue in your code, use the outputs as numbers (e.g. C<$c + 0>) or print with formatting (with C). =head1 AUTHOR Patrick McDermott L =head1 SEE ALSO L, L =head1 COPYRIGHT Copyright (C) 2017 Patrick McDermott =head1 LICENSE This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 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