blib/lib/Math/NoCarry.pm

filter: Statementstmt 100% 61 / 61 Branchbran 87.5% 21 / 24 Conditioncond 76.4% 13 / 17 MC/DCmcdc 44.4% 4 / 9 Subroutinesub 100% 7 / 7 Podpod 100% 3 / 3 totaltotal 90.0% 109 / 121 CC 21 SCAR 19.2
CC
21
Cov
93%
multiply
9.0
subtract
7.2
add
7.0
CRAP 6.8
↑ Summary
1 package Math::NoCarry;
2 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 5 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 5 our @EXPORT_OK = qw(add subtract multiply);
10 5 our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
11  
12 5 our $VERSION = '1.117';
13  
14 =encoding utf8
15  
16 =head1 NAME
17  
18 Math::NoCarry - Perl extension for no-carry arithmetic
19  
20 =head1 SYNOPSIS
21  
22 use Math::NoCarry qw(:all);
23  
24 my $sum = add( 123, 456 );
25  
26 my $difference = subtract( 123, 456 );
27  
28 my $product = multiply( 123, 456 );
29  
30 =head1 DESCRIPTION
31  
32 No-carry arithmetic doesn't allow you to carry digits to the
33 next column. For example, if you add 8 and 4, you normally
34 expect the answer to be 12, but that 1 digit is a carry.
35 In no-carry arithmetic you can't do that, so the sum of
36 8 and 4 is just 2. In effect, this is addition modulo 10
37 in each column. I discard all of the carry digits in
38 this example:
39  
40 1234
41 + 5678
42 ------
43 6802
44  
45 For multiplication, the result of pair-wise multiplication
46 of digits is the modulo 10 value of their normal, everyday
47 multiplication.
48  
49 123
50 x 456
51 -----
52 8 6 x 3
53 2 6 x 2
54 6 6 x 1
55  
56 5 5 x 3
57 0 5 x 2
58 5 5 x 1
59  
60 2 4 x 3
61 8 4 x 2
62 + 4 4 x 1
63 -------
64 43878
65  
66 Since multiplication and subtraction are actually types of
67 additions, you can multiply and subtract like this as well.
68  
69 No carry arithmetic is both associative and commutative.
70  
71 =head2 Functions
72  
73 As of version 1.11, all of these functions are exportable on
74 demand, or with the tag C<:all> to get them all at once.
75  
76 =over 4
77  
78 =item multiply( A, B )
79  
80 Returns the no carry product of A and B.
81  
82 Return A if it is the only argument ( A x 1 );
83  
84 =cut
85  
86 57 ▶sub multiply {
Subroutine multiply: called CC=9 SCAR=22.0
Pod documented
87 57 ▶ return $_[0] if $#_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 1 43 14 57
88  
89 14 @_ = map { $_ += 0 } @_;
90  
91 14 ▶ my $sign = ($_[0] > 0 and $_[1] < 0 ) ||
Condition100%9/9
$_[0] > 0 && $_[1] < 0 || $_[1] > 0 && $_[0] < 0
l!l&&r!l&&!r
2210
$_[0] > 0 and $_[1] < 0
!ll&&!rl&&r
482
$_[1] > 0 and $_[0] < 0
!ll&&!rl&&r
282
MC/DC75%3/4
$_[0] > 0 && $_[1] < 0 || $_[1] > 0 && $_[0] < 0
$_[0] > 0 $_[1] < 0 $_[1] > 0 $_[0] < 0
Truth table57%4/7
A and B || C and D
A= $_[0] > 0B= $_[1] < 0C= $_[1] > 0D= $_[0] < 0
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
92 ($_[1] > 0 and $_[0] < 0 );
93  
94 14 my @p0 = reverse split //, abs $_[0];
95 14 my @p1 = reverse split //, abs $_[1];
96  
97 14 my @m;
98  
99 14 foreach my $i ( 0 .. $#p0 ) {
100 42 foreach my $j ( 0 .. $#p1 ) {
101 126 push @m, ( ( $p1[$j] * $p0[$i] ) % 10 ) * ( 10**($i+$j) );
102 }
103 }
104  
105 14 ▶ while( @m > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@m > 1) 112 14 126
106 112 unshift @m, Math::NoCarry::add( shift @m, shift @m );
107 }
108  
109 14 ▶ $m[0] *= -1 if $sign;
Branch: 1 branch
BranchTrueFalseTotal
if $sign 4 10 14
110  
111 14 return $m[0];
112 }
113  
114 =item add( A, B )
115  
116 Returns the no carry sum of the positive numbers A and B.
117  
118 Returns A if it is the only argument ( A + 0 )
119  
120 Returns false if either number is negative.
121  
122 =cut
123  
124 128 ▶sub add {
Subroutine add: called CC=7 SCAR=19.5
Pod documented
125 128 ▶ return $_[0] if $#_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 1 10 118 128
126  
127 118 @_ = map { local $^W; $_ += 0 } @_;
128  
129 118 ▶ return unless( $_[0] >= 0 and $_[1] >= 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] >= 0 and $_[1] >= 0 0 118 118
Condition33%1/3
$_[0] >= 0 and $_[1] >= 0
!ll&&!rl&&r
00118
MC/DC0%0/2
$_[0] >= 0 and $_[1] >= 0
$_[0] >= 0 $_[1] >= 0
Truth table33%1/3
A and B
A= $_[0] >= 0B= $_[1] >= 0
ABresult
0X0
100
111
130  
131 118 my @addends = map scalar reverse, @_;
132  
133 118 my $string = '';
134  
135 118 my $max = length $addends[0];
136 118 ▶ $max = length $addends[1] if length $addends[1] > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $addends[1] > $max 52 66 118
137  
138 118 ▶ for( my $i = 0; $i < $max ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 380 118 498
139 380 ▶ my @digits = map { local $^W = 0; substr( $_, $i, 1) or 0 } @addends;
Condition100%2/2
substr($_, $i, 1) || 0
l!l
354406
MC/DC100%1/1
substr($_, $i, 1) || 0
substr($_, $i, 1)
Truth table100%2/2
A
A= substr($_, $i, 1)
Aresult
11
00
140  
141 380 my $sum = ( $digits[0] + $digits[1] ) % 10;
142  
143 380 $string .= $sum;
144 }
145  
146 118 $string =~ s/0*$//;
147  
148 118 $string = scalar reverse $string;
149  
150 118 return $string;
151 }
152  
153 =item subtract( A, B )
154  
155 Returns the no carry difference of the positive numbers A and B.
156  
157 Returns A if it is the only argument ( A - 0 )
158  
159 Returns false if either number is negative.
160  
161 =cut
162  
163 16 ▶sub subtract {
Subroutine subtract: called CC=7 SCAR=19.7
Pod documented
164 16 ▶ return $_[0] if $#_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $#_ < 1 10 6 16
165  
166 6 ▶ return unless( $_[0] >= 0 and $_[1] >= 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] >= 0 and $_[1] >= 0 0 6 6
Condition33%1/3
$_[0] >= 0 and $_[1] >= 0
!ll&&!rl&&r
006
MC/DC0%0/2
$_[0] >= 0 and $_[1] >= 0
$_[0] >= 0 $_[1] >= 0
Truth table33%1/3
A and B
A= $_[0] >= 0B= $_[1] >= 0
ABresult
0X0
100
111
167  
168 6 my @addends = map scalar reverse, @_;
169  
170 6 my $string = '';
171  
172 6 my $max = length $addends[0];
173 6 ▶ $max = length $addends[1] if length $addends[1] > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $addends[1] > $max 0 6 6
174  
175 6 ▶ for( my $i = 0; $i < $max ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $max) 18 6 24
176 18 my @digits = map { substr $_, $i, 1 } @addends;
177  
178 18 ▶ $digits[0] += 10 if $digits[0] < $digits[1];
Branch: 1 branch
BranchTrueFalseTotal
if $digits[0] < $digits[1] 8 10 18
179  
180 18 my $sum = ( $digits[0] - $digits[1] ) % 10;
181  
182 18 $string .= $sum;
183 }
184  
185 6 return scalar reverse $string;
186 }
187  
188 5 1;
189  
190 __END__
↑ Summary