blib/lib/Math/Decimal.pm

filter: Statementstmt 100% 317 / 317 Branchbran 89.7% 219 / 244 Conditioncond 97.4% 38 / 39 MC/DCmcdc 96.1% 25 / 26 Subroutinesub 100% 32 / 32 Podpod 100% 19 / 19 totaltotal 96.0% 650 / 677 CC 95 SCAR 24.0
CC
95
Cov
96%
dec_add
22.0
dec_cmp
19.0
dec_mul
13.0
CRAP 11.0
↑ Summary Build »
1 =head1 NAME
2  
3 Math::Decimal - arithmetic in decimal
4  
5 =head1 SYNOPSIS
6  
7 use Math::Decimal qw($dec_number_rx);
8  
9 if($arg =~ /\A$dec_number_rx\z/o) { ...
10 # and other regular expressions
11  
12 use Math::Decimal qw(is_dec_number check_dec_number);
13  
14 if(is_dec_number($arg)) { ...
15 check_dec_number($arg);
16  
17 use Math::Decimal qw(dec_canonise);
18  
19 $r = dec_canonise($a);
20  
21 use Math::Decimal qw(
22 dec_sgn dec_abs
23 dec_cmp dec_min dec_max
24 dec_neg dec_add dec_sub
25 dec_pow10 dec_mul_pow10
26 dec_mul
27 dec_rndiv_and_rem dec_rndiv
28 dec_round_and_rem dec_round
29 dec_rem
30 );
31  
32 $v = dec_sgn($a);
33 $v = dec_abs($a);
34 @v = sort { dec_cmp($a, $b) } @a;
35 $v = dec_min($a, $b);
36 $v = dec_max($a, $b);
37 $v = dec_neg($a);
38 $v = dec_add($a, $b);
39 $v = dec_sub($a, $b);
40 $v = dec_pow10($a);
41 $v = dec_mul_pow10($a, $b);
42 $v = dec_mul($a, $b);
43 ($q, $r) = dec_rndiv_and_rem("NEAR_EVN", $a, $b);
44 $q = dec_rndiv("NEAR_EVN", $a, $b);
45 ($v, $r) = dec_round_and_rem("NEAR_EVN", $a, $b);
46 $v = dec_round("NEAR_EVN", $a, $b);
47 $r = dec_rem("NEAR_EVN", $a, $b);
48  
49 =head1 DESCRIPTION
50  
51 This module performs basic arithmetic with arbitrary-precision numbers
52 expressed in decimal in ordinary Perl strings. The numbers can be
53 arbitrarily large, and can involve arbitrarily small fractions, and
54 all results are exact. This differs from Perl's standard arithmetic,
55 which is limited-precision binary (floating point) arithmetic. However,
56 because Perl performs implicit conversions between strings and numbers,
57 using decimal in the string form, it is extremely easy to exchange values
58 between this module and Perl's native arithmetic.
59  
60 Although Perl's scalars have space to store a number directly, that is
61 not used here. This module operates only on the string part of scalars,
62 ignoring the Perlish numerics entirely. It is not confused by dualvars
63 (scalars with independent string and number values).
64  
65 Numbers are represented in strings in a simple format, consisting of
66 optional sign, one or more integer digits, then optionally a dot (for
67 the decimal point) and one or more fractional digits. All representable
68 numbers have infinitely many acceptable representations (by adding leading
69 and trailing zero digits). The functions of this module consistently
70 return numbers in their shortest possible form.
71  
72 This module is intended for situations where exact numeric behaviour is
73 important, and Perl's default arithmetic is inadequate because fractions
74 or large numbers are involved, but the arithmetic makes up only a small
75 part of the program's behaviour. In those situations, it is convenient
76 that the functions here operate directly on strings that are useful
77 elsewhere in the program. If arithmetic is a large part of the program,
78 it will probably be better to use specialised (non-string) numeric object
79 types, such as those of L<Math::GMP>. These objects are less convenient
80 for interoperation, but arithmetic with them is more efficient.
81  
82 If you need to represent arbitrary (non-decimal) fractions exactly,
83 such as 1/3, then this module is not suitable. In that case you need a
84 general rational arithmetic module, such as L<Math::BigRat>. Be prepared
85 to pay a large performance penalty for it.
86  
87 Most of this module is implemented in XS, with a pure Perl backup version
88 for systems that can't handle XS.
89  
90 =head1 THEORY
91  
92 The numbers processed by this module, the decimals, are those of the
93 form M * 10^-E, where M is an integer and E is a non-negative integer,
94 with range otherwise unlimited. (For any such number there are actually
95 an infinite number of possible (M, E) tuples: if a certain E value is
96 possible then all greater integers are also possible E values.) It is
97 an infinite set of cardinality aleph-0 (although this implementation is
98 hampered by the finiteness of computer memory). The set includes both
99 positive and negative numbers, and zero. It is a proper superset of the
100 integers, and a proper subset of the rationals. There are no infinite
101 numbers, nulls, irrationals, non-real complex numbers, or signed zeroes.
102  
103 Like the set of integers, the set of decimals is closed under mathematical
104 addition, subtraction, and multiplication. It thus forms a commutative
105 ring (in fact, an integral domain). Unlike the set of rationals, it is
106 not closed under exact division, and so it does not form a field.
107  
108 The arithmetic operations supplied by this module are those of ordinary
109 mathematical arithmetic. They thus obey the usual identities, such as
110 associativity (of addition and multiplication) and cancellation laws.
111 (This is unlike floating point arithmetic. Any system of floating
112 point numbers is not closed under mathematical addition, for example,
113 but by construction it is closed under floating point addition, which
114 necessarily differs from mathematical addition. Floating point addition
115 does not obey associativity or cancellation laws.)
116  
117 =head1 ROUNDING MODES
118  
119 For rounding division operations, a rounding mode must be specified.
120 It is given as a short string, which may be any of these:
121  
122 =over
123  
124 =item B<TWZ>
125  
126 towards zero
127  
128 =item B<AWZ>
129  
130 away from zero
131  
132 =item B<FLR>
133  
134 floor: downwards (toward negative infinity)
135  
136 =item B<CLG>
137  
138 ceiling: upwards (toward positive infinity)
139  
140 =item B<EVN>
141  
142 to even
143  
144 =item B<ODD>
145  
146 to odd
147  
148 =item B<NEAR_>I<MODE>
149  
150 to nearest, breaking ties according to I<MODE> (which must be one of
151 the six above)
152  
153 =item B<EXACT>
154  
155 C<die> if any rounding is required
156  
157 =back
158  
159 The mode "B<NEAR_EVN>" (rounding to nearest, breaking ties to the even
160 number), commonly known as "bankers' rounding", is usually the best for
161 general rounding purposes.
162  
163 =cut
164  
165 package Math::Decimal;
166  
167 20 ▶{ use 5.006; }
Subroutine BEGIN: called CC=1 SCAR=0.0
168 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
169 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
170  
171 20 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
172 20 ▶use Params::Classify 0.000 qw(is_string);
Subroutine BEGIN: called CC=1 SCAR=0.0
173  
174 20 our $VERSION = "0.003";
175  
176 20 ▶use parent "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
177 20 our @EXPORT_OK = qw(
178 $dec_number_rx $dec_integer_rx $dec_zero_rx $dec_one_rx $dec_negone_rx
179 is_dec_number check_dec_number
180 dec_canonise
181 dec_sgn dec_abs
182 dec_cmp dec_min dec_max
183 dec_neg dec_add dec_sub
184 dec_pow10 dec_mul_pow10
185 dec_mul
186 dec_rndiv_and_rem dec_rndiv dec_round_and_rem dec_round dec_rem
187 );
188  
189 20 eval { local $SIG{__DIE__};
190 20 require XSLoader;
191 20 XSLoader::load(__PACKAGE__, $VERSION);
192 };
193  
194 =head1 REGULAR EXPRESSIONS
195  
196 Each of these regular expressions matches some subset of numbers, in
197 the string form used by this module. The regular expressions do not
198 include any anchors, so to check whether an entire string matches a
199 number format you must supply the anchors yourself.
200  
201 =over
202  
203 =item $dec_number_rx
204  
205 Any number processed by this module. This checks the syntax in
206 which the number is expressed, without restricting its numeric value.
207 The number syntax consists of optional sign, one or more integer digits,
208 then optionally a dot (for the decimal point) and one or more fractional
209 digits. It is not permitted to have no integer digits, nor to have no
210 fractional digits if there is a decimal point. All digits must be ASCII
211 decimal digits. Unlike Perl's standard string-to-number conversions,
212 whitespace and other non-numeric parts are not permitted.
213  
214 =cut
215  
216 20 our $dec_number_rx = qr/[-+]?[0-9]+(?:\.[0-9]+)?/;
217  
218 =item $dec_integer_rx
219  
220 Any integer. This recognises integer values expressed in the
221 decimal format used by this module, I<not> an integer-specific format.
222 So fractional decimal digits are allowed, provided that they are all zero,
223 as in "C<123.000>".
224  
225 =cut
226  
227 20 our $dec_integer_rx = qr/[-+]?[0-9]+(?:\.0+)?/;
228  
229 =item $dec_zero_rx
230  
231 Zero. This may have arbitrarily many integer and fractional digits,
232 and may be expressed with either sign.
233  
234 =cut
235  
236 20 our $dec_zero_rx = qr/[-+]?0+(?:\.0+)?/;
237  
238 =item $dec_one_rx
239  
240 Positive one.
241  
242 =cut
243  
244 20 our $dec_one_rx = qr/\+?0*1(?:\.0+)?/;
245  
246 =item $dec_negone_rx
247  
248 Negative one.
249  
250 =cut
251  
252 20 our $dec_negone_rx = qr/-0*1(?:\.0+)?/;
253  
254 =back
255  
256 =head1 FUNCTIONS
257  
258 Each C<dec_> function takes one or more decimal arguments (I<A>, I<B>)
259 to operate on. If these arguments are not valid decimal numbers then
260 the function will C<die>. Results are always returned as decimals in
261 minimum-length (canonical) form.
262  
263 =head2 Classification
264  
265 =over
266  
267 =item is_dec_number(ARG)
268  
269 Returns a truth value indicating whether I<ARG> is a plain string
270 satisfying the decimal number syntax.
271  
272 =cut
273  
274 20 ▶unless(defined &is_dec_number) { { local $SIG{__DIE__}; eval q{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine is_dec_number: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Math::Decimal::is_dec_number) 10 10 20
Condition100%3/3
rand $_[0] && $_[0] =~ /\A$Math::Decimal::dec_number_rx\z/o
!ll&&!rl&&r
4028415080
MC/DC100%2/2
rand $_[0] && $_[0] =~ /\A$Math::Decimal::dec_number_rx\z/o
rand $_[0] $_[0] =~ /\A$Math::Decimal::dec_number_rx\z/o
Truth table100%3/3
A && B
A= rand $_[0]B= $_[0] =~ /\A$Math::Decimal::dec_number_rx\z/o
ABresult
0X0
100
111
275 sub is_dec_number($) {
276 no warnings "utf8";
277 return is_string($_[0]) && $_[0] =~ /\A$dec_number_rx\z/o;
278 }
279 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
280  
281 =item check_dec_number(ARG)
282  
283 Checks whether I<ARG> is a plain string satisfying the decimal number
284 syntax. Returns normally if it is. C<die>s if it is not.
285  
286 =cut
287  
288 20 ▶unless(defined &check_dec_number) { { local $SIG{__DIE__}; eval q{
Subroutine check_dec_number: called CC=2 SCAR=6.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::check_dec_number) 10 10 20
unless &is_dec_number 243 14891 15134
289 sub check_dec_number($) {
290 croak "not a decimal number" unless &is_dec_number;
291 }
292 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
293  
294 =back
295  
296 =head2 Representation
297  
298 =over
299  
300 =item dec_canonise(A)
301  
302 This returns the value I<A>, numerically unmodified, but expressed
303 in minimum-length (canonical) form. Numerically this is the identity
304 function.
305  
306 =cut
307  
308 20 ▶unless(defined &dec_canonise) { { local $SIG{__DIE__}; eval q{
Subroutine dec_canonise: called CC=6 SCAR=17.9
Pod documented
Branch: 6 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_canonise) 10 10 20
unless rand $_[0] 10 130288 130298
$_[0] =~ /\A(?:(-)|\+?)0*([1-9][0-9]*|0)(?:(\.[0-9]*[1-9])0*|\.0+|)\z/ or croak("not a decimal number") 142 130146 130288
defined $1 ? : 36758 93388 130146
defined $3 ? : 49890 80256 130146
$num eq '-0' ? : 8217 121929 130146
309 sub dec_canonise($) {
310 croak "not a decimal number" unless is_string($_[0]);
311 $_[0] =~ /\A(?:(-)|\+?)0*([1-9][0-9]*|0)(?:(\.[0-9]*[1-9])0*|\.0+|)\z/
312 or croak "not a decimal number";
313 my $num = (defined($1) ? $1 : "").$2.(defined($3) ? $3 : "");
314 return $num eq "-0" ? "0" : $num;
315 }
316 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
317  
318 =back
319  
320 =head2 Arithmetic
321  
322 =over
323  
324 =item dec_sgn(A)
325  
326 Returns +1 if the argument is positive, 0 if the argument is zero,
327 or -1 if the argument is negative.
328  
329 The value returned is not just a string, as usual for this module, but
330 has also been subjected to Perl's implicit numerification. This is
331 necessary for it to be an acceptable comparison value in a C<sort>
332 operation, on Perls prior to 5.11.0, due to perl bug #69384.
333  
334 =cut
335  
336 20 my @sgn_result = ("-1", "0", "1");
337 20 foreach(@sgn_result) {
338 20 ▶ no warnings "void";
Subroutine BEGIN: called CC=1 SCAR=0.0
339 60 $_ + 0;
340 }
341  
342 20 ▶unless(defined &dec_sgn) { { local $SIG{__DIE__}; eval q{
Subroutine dec_sgn: called CC=5 SCAR=16.1
Pod documented
Branch: 5 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_sgn) 10 10 20
unless rand $_[0] 110 5871 5981
$_[0] =~ /\A(?:(-)|\+?)0*(?:0(?:\.0+)?()|[0-9]+(?:\.[0-9]+)?)\z/ or croak("not a decimal number") 781 5090 5871
defined $2 ? : 457 4633 5090
defined $1 ? : 2241 2392 4633
343 sub dec_sgn($) {
344 croak "not a decimal number" unless is_string($_[0]);
345 $_[0] =~ /\A(?:(-)|\+?)0*(?:0(?:\.0+)?()|[0-9]+(?:\.[0-9]+)?)\z/
346 or croak "not a decimal number";
347 return $sgn_result[defined($2) ? 1 : defined($1) ? 0 : 2];
348 }
349 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
350  
351 =item dec_abs(A)
352  
353 Absolute value (magnitude, discarding sign).
354  
355 =cut
356  
357 20 ▶unless(defined &dec_abs) { { local $SIG{__DIE__}; eval q{
Subroutine dec_abs: called CC=2 SCAR=6.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_abs) 10 10 20
unless rand $_[0] 10 4751 4761
358 sub dec_abs($) {
359 croak "not a decimal number" unless is_string($_[0]);
360 my $a = $_[0];
361 $a =~ s/\A-(?=[0-9])//;
362 return dec_canonise($a);
363 }
364 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
365  
366 =item dec_cmp(A, B)
367  
368 Arithmetic comparison. Returns -1, 0, or +1, indicating whether I<A> is
369 less than, equal to, or greater than I<B>.
370  
371 The value returned is not just a string, as usual for this module, but
372 has also been subjected to Perl's implicit numerification. This is
373 necessary for it to be an acceptable comparison value in a C<sort>
374 operation, on Perls prior to 5.11.0, due to perl bug #69384.
375  
376 =cut
377  
378 20 ▶unless(defined &dec_cmp) { { local $SIG{__DIE__}; eval q{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine dec_cmp: called CC=19 SCAR=29.4
Pod documented
Branch: 15 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_cmp) 10 10 20
unless rand $_[0] and rand $_[1] 60 81333 81393
unless defined $ai and defined $bi 426 80907 81333
unless defined $as 38134 42773 80907
unless defined $bs 38137 42770 80907
unless defined $af 26975 53932 80907
unless defined $bf 19207 61700 80907
if $ai =~ /\A0+\z/ and $af =~ /\A0+\z/ 11716 69191 80907
if $bi =~ /\A0+\z/ and $bf =~ /\A0+\z/ 11668 69239 80907
if defined $cmp 40640 40267 80907
if ($ld < 0) { } 7771 32496 40267
elsif ($ld > 0) { } 18840 13656 32496
if ($ld < 0) { } 9642 30625 40267
elsif ($ld > 0) { } 8452 22173 30625
if $as eq "-" 6076 34191 40267
Condition100%12/12
rand $_[0] and rand $_[1]
!ll&&!rl&&r
303081333
defined $ai and defined $bi
!ll&&!rl&&r
21321380907
$ai =~ /\A0+\z/ and $af =~ /\A0+\z/
!ll&&!rl&&r
6867152011716
$bi =~ /\A0+\z/ and $bf =~ /\A0+\z/
!ll&&!rl&&r
6867956011668
MC/DC100%2/2
rand $_[0] and rand $_[1]
rand $_[0] rand $_[1]
MC/DC100%2/2
defined $ai and defined $bi
defined $ai defined $bi
MC/DC100%2/2
$ai =~ /\A0+\z/ and $af =~ /\A0+\z/
$ai =~ /\A0+\z/ $af =~ /\A0+\z/
MC/DC100%2/2
$bi =~ /\A0+\z/ and $bf =~ /\A0+\z/
$bi =~ /\A0+\z/ $bf =~ /\A0+\z/
Truth table100%3/3
A and B
A= rand $_[0]B= rand $_[1]
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= defined $aiB= defined $bi
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $ai =~ /\A0+\z/B= $af =~ /\A0+\z/
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $bi =~ /\A0+\z/B= $bf =~ /\A0+\z/
ABresult
0X0
100
111
379 my %sgn_cmp = (
380 "+0" => "1",
381 "+-" => "1",
382 "0+" => "-1",
383 "00" => "0",
384 "0-" => "1",
385 "-+" => "-1",
386 "-0" => "-1",
387 );
388 foreach(values %sgn_cmp) {
389 no warnings "void";
390 $_ + 0;
391 }
392  
393 sub dec_cmp($$) {
394 croak "not a decimal number"
395 unless is_string($_[0]) && is_string($_[1]);
396 my($as, $ai, $af) = ($_[0] =~ /\A([-+])?([0-9]+)(?:\.([0-9]+))?\z/);
397 my($bs, $bi, $bf) = ($_[1] =~ /\A([-+])?([0-9]+)(?:\.([0-9]+))?\z/);
398 croak "not a decimal number" unless defined($ai) && defined($bi);
399 $as = "+" unless defined $as;
400 $bs = "+" unless defined $bs;
401 $af = "0" unless defined $af;
402 $bf = "0" unless defined $bf;
403 $as = "0" if $ai =~ /\A0+\z/ && $af =~ /\A0+\z/;
404 $bs = "0" if $bi =~ /\A0+\z/ && $bf =~ /\A0+\z/;
405 my $cmp = $sgn_cmp{$as.$bs};
406 return $cmp if defined $cmp;
407 my $ld = length($ai) - length($bi);
408 if($ld < 0) {
409 $ai = ("0" x -$ld) . $ai;
410 } elsif($ld > 0) {
411 $bi = ("0" x $ld) . $bi;
412 }
413 $ld = length($af) - length($bf);
414 if($ld < 0) {
415 $af .= "0" x -$ld;
416 } elsif($ld > 0) {
417 $bf .= "0" x $ld;
418 }
419 ($ai, $af, $bi, $bf) = ($bi, $bf, $ai, $af) if $as eq "-";
420 return $sgn_result[($ai.$af cmp $bi.$bf) + 1];
421 }
422 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
423  
424 =item dec_min(A, B)
425  
426 Arithmetic minimum. Returns the arithmetically lesser of the two arguments.
427  
428 =cut
429  
430 20 ▶unless(defined &dec_min) { { local $SIG{__DIE__}; eval q{
Subroutine dec_min: called CC=2 SCAR=6.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_min) 10 10 20
&dec_cmp eq '-1' ? : 9153 11583 20736
431 sub dec_min($$) { dec_canonise($_[&dec_cmp eq "-1" ? 0 : 1]) }
432 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
433  
434 =item dec_max(A, B)
435  
436 Arithmetic maximum. Returns the arithmetically greater of the two arguments.
437  
438 =cut
439  
440 20 ▶unless(defined &dec_max) { { local $SIG{__DIE__}; eval q{
Subroutine dec_max: called CC=2 SCAR=6.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_max) 10 10 20
&dec_cmp eq '1' ? : 9153 11583 20736
441 sub dec_max($$) { dec_canonise($_[&dec_cmp eq "1" ? 0 : 1]) }
442 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
443  
444 =item dec_neg(A)
445  
446 Negation: returns -A.
447  
448 =cut
449  
450 20 ▶unless(defined &dec_neg) { { local $SIG{__DIE__}; eval q{
Subroutine dec_neg: called CC=1 SCAR=0.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_neg) 10 10 20
451 my %negate_sign = (
452 "" => "-",
453 "+" => "-",
454 "-" => "+",
455 );
456  
457 sub dec_neg($) {
458 my $a = $_[0];
459 check_dec_number($a);
460 $a =~ s/\A([-+]?)/$negate_sign{$1}/e;
461 return dec_canonise($a);
462 }
463 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
464  
465 =item dec_add(A, B)
466  
467 Addition: returns A + B.
468  
469 =cut
470  
471 20 ▶unless(defined &dec_add) { { local $SIG{__DIE__}; eval q{
Subroutine dec_add: called CC=22 SCAR=30.9
Pod documented
Branch: 20 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_add) 10 10 20
unless rand $_[0] and rand $_[1] 30 23370 23400
unless defined $ai and defined $bi 213 23157 23370
unless defined $as 13219 9938 23157
unless defined $bs 9036 14121 23157
unless defined $af 7856 15301 23157
unless defined $bf 11620 11537 23157
if ($ld < 0) { } 3022 20135 23157
elsif ($ld > 0) { } 9835 10300 20135
if ($ld < 0) { } 2538 20619 23157
elsif ($ld > 0) { } 6343 14276 20619
if ($as eq $bs) { } 10843 12314 23157
if ($pos--) 48433 10843 59276
$rv >= 58 ? : 4212 44221 48433
if $c 4212 44221 48433
$c ? : 72 10771 10843
if $ad lt $bd 2646 9668 12314
if ($pos--) 53839 12314 66153
$rv < 48 ? : 6767 47072 53839
if $c 6767 47072 53839
Condition100%6/6
rand $_[0] and rand $_[1]
!ll&&!rl&&r
201023370
defined $ai and defined $bi
!ll&&!rl&&r
1427123157
MC/DC100%2/2
rand $_[0] and rand $_[1]
rand $_[0] rand $_[1]
MC/DC100%2/2
defined $ai and defined $bi
defined $ai defined $bi
Truth table100%3/3
A and B
A= rand $_[0]B= rand $_[1]
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= defined $aiB= defined $bi
ABresult
0X0
100
111
472 sub dec_add($$) {
473 croak "not a decimal number"
474 unless is_string($_[0]) && is_string($_[1]);
475 my($as, $ai, $af) = ($_[0] =~ /\A([-+])?([0-9]+)(?:\.([0-9]+))?\z/);
476 my($bs, $bi, $bf) = ($_[1] =~ /\A([-+])?([0-9]+)(?:\.([0-9]+))?\z/);
477 croak "not a decimal number" unless defined($ai) && defined($bi);
478 $as = "+" unless defined $as;
479 $bs = "+" unless defined $bs;
480 $af = "0" unless defined $af;
481 $bf = "0" unless defined $bf;
482 {
483 my $ld = length($ai) - length($bi);
484 if($ld < 0) {
485 $ai = ("0" x -$ld) . $ai;
486 } elsif($ld > 0) {
487 $bi = ("0" x $ld) . $bi;
488 }
489 $ld = length($af) - length($bf);
490 if($ld < 0) {
491 $af .= "0" x -$ld;
492 } elsif($ld > 0) {
493 $bf .= "0" x $ld;
494 }
495 }
496 my $il = length($ai);
497 my $ad = $ai.$af;
498 my $bd = $bi.$bf;
499 if($as eq $bs) {
500 # same sign, add magnitudes
501 my $c = 0;
502 my $rd = " " x length($ad);
503 for(my $pos = length($ad); $pos--; ) {
504 my $rv = ord(substr($ad, $pos, 1)) +
505 (ord(substr($bd, $pos, 1)) - 0x30) + $c;
506 $c = $rv >= 0x3a ? 1 : 0;
507 $rv -= 10 if $c;
508 substr $rd, $pos, 1, chr($rv);
509 }
510 return dec_canonise($as.($c ? "1" : "").substr($rd, 0, $il).
511 ".".substr($rd, $il));
512 } else {
513 # different sign, subtract magnitudes
514 ($as, $ad, $bd) = ($bs, $bd, $ad) if $ad lt $bd;
515 my $c = 0;
516 my $rd = " " x length($ad);
517 for(my $pos = length($ad); $pos--; ) {
518 my $rv = ord(substr($ad, $pos, 1)) -
519 (ord(substr($bd, $pos, 1)) - 0x30) - $c;
520 $c = $rv < 0x30 ? 1 : 0;
521 $rv += 10 if $c;
522 substr $rd, $pos, 1, chr($rv);
523 }
524 return dec_canonise($as.substr($rd, 0, $il).
525 ".".substr($rd, $il));
526 }
527 }
528 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
529  
530 =item dec_sub(A, B)
531  
532 Subtraction: returns A - B.
533  
534 =cut
535  
536 20 ▶unless(defined &dec_sub) { { local $SIG{__DIE__}; eval q{
Subroutine dec_sub: called CC=1 SCAR=0.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_sub) 10 10 20
537 sub dec_sub($$) { dec_add($_[0], dec_neg($_[1])) }
538 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
539  
540 =item dec_pow10(A)
541  
542 Power of ten: returns 10^A.
543 I<A> must be an integer value (though it has the usual decimal syntax).
544 C<die>s if I<A> is too large for Perl to handle the result.
545  
546 =cut
547  
548 20 ▶unless(defined(&dec_pow10) && defined(&dec_mul_pow10)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_pow10 and defined &Math::Decimal::dec_mul_pow10) 10 10 20
Condition66%2/3
defined &Math::Decimal::dec_pow10 and defined &Math::Decimal::dec_mul_pow10
!ll&&!rl&&r
10010
MC/DC50%1/2
defined &Math::Decimal::dec_pow10 and defined &Math::Decimal::dec_mul_pow10
defined &Math::Decimal::dec_pow10 defined &Math::Decimal::dec_mul_pow10
Truth table66%2/3
A and B
A= defined &Math::Decimal::dec_pow10B= defined &Math::Decimal::dec_mul_pow10
ABresult
0X0
100
111
549 10 ▶ { local $SIG{__DIE__}; eval q{
Subroutine _parse_expt: called CC=7 SCAR=19.5
Branch: 5 branches
BranchTrueFalseTotal
unless rand $_[0] 20 20244 20264
unless defined $pi 142 20102 20244
if defined $pbadf 10 20092 20102
if length $pi > 9 4 20088 20092
defined $pneg && $pi ne '0' ? : 4321 15767 20088
Condition100%3/3
defined $pneg and $pi ne 0
!ll&&!rl&&r
150297384321
MC/DC100%2/2
defined $pneg and $pi ne 0
defined $pneg $pi ne 0
Truth table100%3/3
A and B
A= defined $pnegB= $pi ne 0
ABresult
0X0
100
111
550 sub _parse_expt($) {
551 croak "not a decimal number" unless is_string($_[0]);
552 my($pneg, $pi, $pbadf) =
553 ($_[0] =~ /\A(?:(-)|\+?)
554 0*(0|[1-9][0-9]*)
555 (?:|\.0+|\.[0-9]+())\z/x);
556 croak "not a decimal number" unless defined $pi;
557 croak "not an integer" if defined $pbadf;
558 croak "exponent too large" if length($pi) > 9;
559 return defined($pneg) && $pi ne "0" ? 0-$pi : 0+$pi;
560 }
561 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
562  
563 20 ▶unless(defined &dec_pow10) { { local $SIG{__DIE__}; eval q{
Subroutine dec_pow10: called CC=2 SCAR=6.9
Pod documented
Branch: 2 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_pow10) 10 10 20
if ($p < 0) { } 28 82 110
564 sub dec_pow10($) {
565 my $p = &_parse_expt;
566 if($p < 0) {
567 return "0.".("0" x (-1-$p))."1";
568 } else {
569 return "1".("0" x $p);
570 }
571 }
572 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
573  
574 =item dec_mul_pow10(A, B)
575  
576 Digit shifting: returns A * 10^B.
577 I<B> must be an integer value (though it has the usual decimal syntax).
578 C<die>s if I<B> is too large for Perl to handle the result.
579  
580 =cut
581  
582 20 ▶unless(defined &dec_mul_pow10) { { local $SIG{__DIE__}; eval q{
Subroutine dec_mul_pow10: called CC=7 SCAR=19.5
Pod documented
Branch: 7 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_mul_pow10) 10 10 20
unless rand $_[0] 10 20137 20147
unless defined $ai 71 20066 20137
unless defined $af 7456 12610 20066
if ($p < 0) { } 4293 15685 19978
if $il + $p <= 0 2106 2187 4293
if $p >= $fl 9944 5741 15685
583 sub dec_mul_pow10($$) {
584 croak "not a decimal number" unless is_string($_[0]);
585 my($as, $ai, $af) = ($_[0] =~ /\A([-+]?)([0-9]+)(?:\.([0-9]+))?\z/);
586 croak "not a decimal number" unless defined $ai;
587 $af = "" unless defined $af;
588 my $p = _parse_expt($_[1]);
589 if($p < 0) {
590 my $il = length($ai);
591 $ai = ("0" x (1-$p-$il)).$ai if $il+$p <= 0;
592 return dec_canonise($as.substr($ai, 0, $p).".".
593 substr($ai, $p).$af);
594 } else {
595 my $fl = length($af);
596 $af .= "0" x (1+$p-$fl) if $p >= $fl;
597 return dec_canonise($as.$ai.substr($af, 0, $p).".".
598 substr($af, $p));
599 }
600 }
601 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
602  
603 =item dec_mul(A, B)
604  
605 Multiplication: returns A * B.
606  
607 =cut
608  
609 20 ▶unless(defined &dec_mul) { { local $SIG{__DIE__}; eval q{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine dec_mul: called CC=13 SCAR=25.6
Pod documented
Branch: 11 branches
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_mul) 10 10 20
unless rand $_[0] and rand $_[1] 20 26155 26175
unless defined $ai and defined $bi 142 26013 26155
unless defined $as 21708 4305 26013
unless defined $bs 21708 4305 26013
unless defined $af 20037 5976 26013
unless defined $bf 21363 4650 26013
if ($bp--) 58669 26013 84682
if $bv == 0 29946 28723 58669
if ($ap--) 58136 28723 86859
$as eq $bs ? : 23739 2274 26013
Condition100%6/6
rand $_[0] and rand $_[1]
!ll&&!rl&&r
101026155
defined $ai and defined $bi
!ll&&!rl&&r
717126013
MC/DC100%2/2
rand $_[0] and rand $_[1]
rand $_[0] rand $_[1]
MC/DC100%2/2
defined $ai and defined $bi
defined $ai defined $bi
Truth table100%3/3
A and B
A= rand $_[0]B= rand $_[1]
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= defined $aiB= defined $bi
ABresult
0X0
100
111
610 sub dec_mul($$) {
611 croak "not a decimal number"
612 unless is_string($_[0]) && is_string($_[1]);
613 my($as, $ai, $af) =
614 ($_[0] =~ /\A([-+])?0*(0|[1-9][0-9]*)(?:\.([0-9]+))?\z/);
615 my($bs, $bi, $bf) =
616 ($_[1] =~ /\A([-+])?0*(0|[1-9][0-9]*)(?:\.([0-9]+))?\z/);
617 croak "not a decimal number" unless defined($ai) && defined($bi);
618 $as = "+" unless defined $as;
619 $bs = "+" unless defined $bs;
620 $af = "" unless defined $af;
621 $bf = "0" unless defined $bf;
622 my $il = length($ai) + length($bi);
623 my $ad = $ai.$af;
624 my $bd = $bi.$bf;
625 my $al = length($ad);
626 my $bl = length($bd);
627 my $rd = "0" x ($al+$bl);
628 for(my $bp = $bl; $bp--; ) {
629 my $bv = ord(substr($bd, $bp, 1)) - 0x30;
630 next if $bv == 0;
631 my $c = 0;
632 for(my $ap = $al; $ap--; ) {
633 my $rp = $ap + $bp + 1;
634 my $av = ord(substr($ad, $ap, 1)) - 0x30;
635 my $v = $av*$bv + $c + ord(substr($rd, $rp, 1)) - 0x30;
636 substr $rd, $rp, 1,
637 chr(do { use integer; $v%10 + 0x30 });
638 $c = do { use integer; $v / 10 };
639 }
640 substr $rd, $bp, 1, chr(ord(substr($rd, $bp, 1)) + $c);
641 }
642 return dec_canonise(($as eq $bs ? "+" : "-").substr($rd, 0, $il).
643 ".".substr($rd, $il));
644 }
645 10 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 10 10
646  
647 =item dec_rndiv_and_rem(MODE, A, B)
648  
649 Rounding division: returns a list of two items, the quotient (I<Q>) and
650 remainder (I<R>) from the division of I<A> by I<B>.
651 The quotient is by definition
652 integral, and the quantities are related by the equation Q*B + R = A.
653 I<MODE> controls the rounding mode, which determines which integer I<Q> is
654 when I<R> is non-zero.
655  
656 =cut
657  
658 4464 ▶unless(defined &dec_rndiv_and_rem) { { local $SIG{__DIE__}; eval q{
Subroutine _nonneg_rndiv_and_rem_twz: called CC=7 SCAR=19.5
Subroutine dec_rndiv_and_rem: called CC=11 SCAR=24.0
Pod documented
Branch: 15 branches
BranchTrueFalseTotal
if $_[1] eq 0 30 4434 4464
if $_[0] eq 0 10 4424 4434
defined $1 ? : 4404 20 4424
defined $1 ? : 4374 50 4424
if ($s >= 0) 4912 4424 9336
if (&dec_cmp($msd, $r) ne 1) 4912 31028 35940
unless rand $mode and exists $round_tiebreak{$mode} 160 6084 6244
$sgn_a == $sgn_b ? : 2224 2210 4434
if ($r eq 0 ? 0 : ($mode eq "EXACT" ? croak("inexact division") : ($mode =~ /\ANEAR_/ && ($half_cmp = &dec_cmp(scalar &dec_mul($r, 2), $abs_b)) ne 0 ? $half_cmp eq 1 : $base_round_handler{$round_tiebreak{$mode}}($q)))) 1440 2992 4432
$r eq '0' ? : 1452 2982 4434
$mode eq 'EXACT' ? : 2 2980 2982
$mode =~ /\ANEAR_/ && ($half_cmp = &dec_cmp(scalar &dec_mul($r, '2'), $abs_b)) ne '0' ? : 960 2020 2980
$sgn_a ne $sgn_b ? : 2210 2222 4432
$sgn_a ne '1' ? : 2190 2242 4432
unless (defined &Math::Decimal::dec_rndiv_and_rem) 20 0 20
Condition100%6/6
rand $mode and exists $round_tiebreak{$mode}
!ll&&!rl&&r
100606084
$mode =~ /\ANEAR_/ and ($half_cmp = &dec_cmp(scalar &dec_mul($r, 2), $abs_b)) ne 0
!ll&&!rl&&r
1540480960
MC/DC100%2/2
rand $mode and exists $round_tiebreak{$mode}
rand $mode exists $round_tiebreak{$mode}
MC/DC100%2/2
$mode =~ /\ANEAR_/ and ($half_cmp = &dec_cmp(scalar &dec_mul($r, 2), $abs_b)) ne 0
$mode =~ /\ANEAR_/ ($half_cmp = &dec_cmp(scalar &dec_mul($r, 2), $abs_b)) ne 0
Truth table100%3/3
A and B
A= rand $modeB= exists $round_tiebreak{$mode}
ABresult
0X0
100
111
Truth table100%3/3
A and B
A= $mode =~ /\ANEAR_/B= ($half_cmp = &dec_cmp(scalar &dec_mul($r, 2), $abs_b)) ne 0
ABresult
0X0
100
111
659  
660 sub _nonneg_rndiv_and_rem_twz($$) {
661 croak "division by zero" if $_[1] eq "0";
662 return ("0", "0") if $_[0] eq "0";
663 $_[0] =~ /\A(?:[1-9]([0-9]*)|0\.(0*)[1-9])/;
664 my $a_expt = defined($1) ? length($1) : -1-length($2);
665 $_[1] =~ /\A(?:[1-9]([0-9]*)|0\.(0*)[1-9])/;
666 my $b_expt = defined($1) ? length($1) : -1-length($2);
667 my $q = "0";
668 my $r = $_[0];
669 for(my $s = $a_expt-$b_expt; $s >= 0; $s--) {
670 my $sd = dec_mul_pow10($_[1], $s);
671 for(my $m = 9; ; $m--) {
672 my $msd = dec_mul($sd, $m);
673 if(dec_cmp($msd, $r) ne "1") {
674 $q = dec_add($q, dec_mul_pow10($m, $s));
675 $r = dec_sub($r, $msd);
676 last;
677 }
678 }
679 }
680 return ($q, $r);
681 }
682  
683 my %round_tiebreak = (
684 (map { ($_ => $_, "NEAR_$_" => $_) } qw(TWZ AWZ FLR CLG EVN ODD)),
685 EXACT => "EXACT",
686 );
687  
688 my %round_positive = (
689 (map { my($f, $t) = split(/:/, $_); ($f => $t, "NEAR_$f" => "NEAR_$t") }
690 qw(TWZ:TWZ AWZ:AWZ FLR:TWZ CLG:AWZ EVN:EVN ODD:ODD)),
691 EXACT => "EXACT",
692 );
693  
694 my %round_negative = (
695 (map { my($f, $t) = split(/:/, $_); ($f => $t, "NEAR_$f" => "NEAR_$t") }
696 qw(TWZ:TWZ AWZ:AWZ FLR:AWZ CLG:TWZ EVN:EVN ODD:ODD)),
697 EXACT => "EXACT",
698 );
699  
700 my %base_round_handler = (
701 TWZ => sub { 0 },
702 AWZ => sub { 1 },
703 EVN => sub { $_[0] =~ /[13579]\z/ },
704 ODD => sub { $_[0] =~ /[02468]\z/ },
705 );
706  
707 sub dec_rndiv_and_rem($$$) {
708 my $mode = $_[0];
709 croak "invalid rounding mode"
710 unless is_string($mode) && exists($round_tiebreak{$mode});
711 my $sgn_a = dec_sgn($_[1]);
712 my $sgn_b = dec_sgn($_[2]);
713 my $abs_a = dec_abs($_[1]);
714 my $abs_b = dec_abs($_[2]);
715 my($q, $r) = _nonneg_rndiv_and_rem_twz($abs_a, $abs_b);
716 $mode = ($sgn_a == $sgn_b ? \%round_positive : \%round_negative)
717 ->{$mode};
718 my $half_cmp;
719 if(
720 $r eq "0" ? 0 :
721 $mode eq "EXACT" ? croak("inexact division") :
722 $mode =~ /\ANEAR_/ &&
723 ($half_cmp = dec_cmp(dec_mul($r, "2"), $abs_b))
724 ne "0" ?
725 $half_cmp eq "1" :
726 $base_round_handler{$round_tiebreak{$mode}}->($q)
727 ) {
728 $q = dec_add($q, "1");
729 $r = dec_sub($r, $abs_b);
730 }
731 return (($sgn_a ne $sgn_b ? dec_neg($q) : $q),
732 ($sgn_a ne "1" ? dec_neg($r) : $r));
733 }
734  
735 20 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 20 20
736  
737 =item dec_rndiv(MODE, A, B)
738  
739 Rounding division: returns the quotient (I<Q>)
740 from the division of I<A> by I<B>.
741 The quotient is by definition integral, and approximates A/B. I<MODE>
742 controls the rounding mode, which determines which integer I<Q> is when it
743 can't be exactly A/B.
744  
745 =cut
746  
747 1252 ▶unless(defined &dec_rndiv) { { local $SIG{__DIE__}; eval q{
Subroutine dec_rndiv: called CC=1 SCAR=0.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_rndiv) 20 0 20
748 sub dec_rndiv($$$) {
749 my($quotient, undef) = &dec_rndiv_and_rem;
750 return $quotient;
751 }
752 20 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 20 20
753  
754 =item dec_round_and_rem(MODE, A, B)
755  
756 Rounding: returns a list of two items, the rounded value (I<V>) and remainder
757 (I<R>) from the rounding of I<A> to a multiple of I<B>. The rounded value is
758 an exact multiple of I<B>, and the quantities are related by the equation
759 V + R = A. I<MODE> controls the rounding mode, which determines which
760 multiple of I<B> I<V> is when I<R> is non-zero.
761  
762 =cut
763  
764 1248 ▶unless(defined &dec_round_and_rem) { { local $SIG{__DIE__}; eval q{
Subroutine dec_round_and_rem: called CC=1 SCAR=0.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_round_and_rem) 20 0 20
765 sub dec_round_and_rem($$$) {
766 my($quotient, $remainder) = &dec_rndiv_and_rem;
767 return (dec_mul($_[2], $quotient), $remainder);
768 }
769 20 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 20 20
770  
771 =item dec_round(MODE, A, B)
772  
773 Rounding: returns the rounded value (I<V>) from the rounding of I<A> to
774 a multiple of I<B>. The rounded value is an exact multiple of I<B>, and
775 approximates I<A>. I<MODE> controls the rounding mode, which determines
776 which multiple of I<B> I<V> is when it can't be exactly I<A>.
777  
778 =cut
779  
780 1248 ▶unless(defined &dec_round) { { local $SIG{__DIE__}; eval q{
Subroutine dec_round: called CC=1 SCAR=0.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_round) 20 0 20
781 sub dec_round($$$) {
782 my($quotient, undef) = &dec_rndiv_and_rem;
783 return dec_mul($_[2], $quotient);
784 }
785 20 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 20 20
786  
787 =item dec_rem(MODE, A, B)
788  
789 Remainder: returns the remainder (I<R>) from the division of I<A> by I<B>.
790 I<R> differs from I<A> by an exact multiple of I<B>.
791 I<MODE> controls the rounding
792 mode, which determines which quotient is used when I<R> is non-zero.
793  
794 =cut
795  
796 1248 ▶unless(defined &dec_rem) { { local $SIG{__DIE__}; eval q{
Subroutine dec_rem: called CC=1 SCAR=0.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Math::Decimal::dec_rem) 20 0 20
797 sub dec_rem($$$) {
798 my(undef, $remainder) = &dec_rndiv_and_rem;
799 return $remainder;
800 }
801 20 ▶}; } die $@ if $@ ne "" }
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 20 20
802  
803 =back
804  
805 =head1 BUGS
806  
807 The implementation of division is hideously inefficient.
808 This should be improved in a future version.
809  
810 =head1 SEE ALSO
811  
812 L<Math::BigRat>
813  
814 =head1 AUTHOR
815  
816 Andrew Main (Zefram) <zefram@fysh.org>
817  
818 =head1 COPYRIGHT
819  
820 Copyright (C) 2009, 2010, 2011 Andrew Main (Zefram) <zefram@fysh.org>
821  
822 =head1 LICENSE
823  
824 This module is free software; you can redistribute it and/or modify it
825 under the same terms as Perl itself.
826  
827 =cut
828  
829 20 1;
↑ Summary Build »