blib/lib/Math/GoldenBigMath.pm

filter: Statementstmt 84.1% 313 / 372 Branchbran 74.3% 116 / 156 Conditioncond 33.3% 6 / 18 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 97.4% 38 / 39 Podpod 0.0% 0 / 24 totaltotal 76.7% 475 / 619 CC 86 SCAR 35.0
CC
86
Cov
80%
CalcDivisionWithoutSignPointAndExponent
240.0
AdditionWithoutSignPointAndExponent
8.0
Compare
7.0
CRAP 33.0
↑ Summary
1 # perl 5
2 #
3 # GoldenBigMath.pm
4 #
5 # calc + - * / % with unbounded integers/decimals
6 #
7 # + - * / % == != > < <=> implemented
8 #
9 # handling of exponents ([eE][+-]?123456789) and decimal point "[,.]" still missing
10 #
11 # Ralf Peine, Mon Aug 18 17:01:06 2014
12
13 1 $VERSION = "0.804";
14
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 1 $|=1;
19
20 package Math::GoldenBigMath; # shorthand: GBM
21
22 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23
24 1 ▶use vars qw($version $_MAX_DIV_DIGITS $_COEFF_MAX_DIGITS);
Subroutine BEGIN: called CC=1 SCAR=0.0
25
26 # stop calculation of division if digit count >= $_maxDivDigits
27 1 $_MAX_DIV_DIGITS = 100000;
28 # $_MAX_DIV_DIGITS = 100;
29 1 $_COEFF_MAX_DIGITS = 1000000;
30
31 # --- overloads ------------------- sub ----------------------------------------
32
33 # define usual mathematic operators
34 use overload
35 1 ▶ '+' => \&Addition, # sub
Subroutine BEGIN: called CC=1 SCAR=0.0
36 '-' => \&Subtraction, # sub
37 '*' => \&Multiplication, # sub
38 '/' => \&Division, # sub
39 '%' => \&DivisionModulus, # sub
40 '==' => \&CompareEqual, # sub
41 '!=' => \&CompareNotEqual, # sub
42 '>' => \&Greater, # sub
43 '<' => \&Smaller, # sub
44 '>=' => \&GreaterEqual, # sub
45 '<=' => \&SmallerEqual, # sub
46 '<=>' => \&Compare, # sub
47 1 '""' => \&GetValue; # sub
48
49 # --- creating, getting and setting sub (s) ------------------------------------
50
51 1110 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
52 1110 my $self = shift;
53
54 1110 ▶ my $type = ref($self) || $self;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
2608500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
55 1110 my $number = shift;
56 1110 ▶ $number = "0" unless $number;
Branch: 1 branch
BranchTrueFalseTotal
unless $number 65 1045 1110
57 1110 my $elem = bless {}, $type;
58 1110 $elem->SetValue($number);
59 1110 return $elem;
60 }
61
62 # --- set coefficient (value before 'e') ------------------------
63 2230 ▶sub _setCoeff {
Subroutine _setCoeff: called CC=1 SCAR=0.0
64 2230 my $self = shift;
65 2230 $self->{_coeff} = shift;
66 2230 return $self;
67 }
68
69 # --- get coefficient (value before 'e') ------------------------
70 2366 ▶sub _getCoeff {
Subroutine _getCoeff: called CC=1 SCAR=0.0
71 2366 my $self = shift;
72 2366 return $self->{_coeff};
73 }
74
75 # --- set exponent (value after 'e') ------------------------
76 1463 ▶sub _setExp {
Subroutine _setExp: called CC=1 SCAR=0.0
77 1463 my $self = shift;
78 1463 $self->{_exp} = shift;
79 1463 return $self;
80 }
81
82 # --- get exponent (value after 'e') ------------------------
83 1566 ▶sub _getExp {
Subroutine _getExp: called CC=1 SCAR=0.0
84 1566 my $self = shift;
85 1566 return $self->{_exp};
86 }
87
88 # --- get char for sign ('+', '-' for +1, -1)
89 2379 ▶sub _getSignChar {
Subroutine _getSignChar: called CC=3 SCAR=11.4
90 2379 my $sign = shift;
91
92 2379 ▶ return '+' if $sign == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $sign == 1 2110 269 2379
93 269 ▶ return '-' if $sign == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $sign == -1 269 0 269
94
95 0 croak "wrong sign given: $sign";
96 }
97
98 # --- Get whole value ---
99 403 ▶sub GetValue {
Subroutine GetValue: called CC=1 SCAR=0.0
Pod undocumented
100 403 my $self = shift;
101
102 403 my $coeff_sign = _getSignChar($self->_getCoeffSign());
103 403 my $exp_sign = _getSignChar($self->_getExpSign());
104
105 403 return $coeff_sign.$self->_getCoeff().'e'.$self->GetExpValue();
106 }
107
108 # --- Get exponent value ---
109 1085 ▶sub GetExpValue {
Subroutine GetExpValue: called CC=1 SCAR=0.0
Pod undocumented
110 1085 my $self = shift;
111
112 1085 my $exp_sign = _getSignChar($self->_getExpSign());
113
114 1085 return $exp_sign.$self->_getExp();
115 }
116
117 # --- Set sign of coeff ---
118 1229 ▶sub _setCoeffSign {
Subroutine _setCoeffSign: called CC=1 SCAR=0.0
119 1229 my $self = shift;
120 1229 $self->{_coeff_sign} = shift;
121 1229 return $self;
122 }
123
124 # --- Get sign of coeff ---
125 1104 ▶sub _getCoeffSign {
Subroutine _getCoeffSign: called CC=1 SCAR=0.0
126 1104 my $self = shift;
127 1104 return $self->{_coeff_sign};
128 }
129
130 # --- Set sign of exp ---
131 1463 ▶sub _setExpSign {
Subroutine _setExpSign: called CC=1 SCAR=0.0
132 1463 my $self = shift;
133 1463 $self->{_exp_sign} = shift;
134 1463 return $self;
135 }
136
137 # --- Get sign of exponent ---
138 1948 ▶sub _getExpSign {
Subroutine _getExpSign: called CC=1 SCAR=0.0
139 1948 my $self = shift;
140 1948 return $self->{_exp_sign};
141 }
142
143 # --- extract Sign out of $number_ref ---
144 3564 ▶sub _extractSign {
Subroutine _extractSign: called CC=4 SCAR=13.9
145 3564 my $number = shift;
146
147 3564 my $sign = 1;
148
149 3564 my $signStr = substr($number,0,1);
150 3564 ▶ if ($signStr eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($signStr eq '-') { } 312 3252 3564
elsif ($signStr eq '+') { } 617 2635 3252
151 312 $number = substr($number, 1);
152 312 $sign = -1;
153 }
154 elsif ($signStr eq '+') {
155 617 $number = substr($number, 1);
156 617 $sign = 1;
157 }
158
159 3564 ▶ $sign = 1 unless $number;
Branch: 1 branch
BranchTrueFalseTotal
unless $number 2256 1308 3564
160
161 3564 return ($sign, $number);
162 }
163
164 # --- split ExponentValue into sign and value and set ---
165 234 ▶sub SetExpValue {
Subroutine SetExpValue: called CC=1 SCAR=0.0
Pod undocumented
166 234 my $self = shift;
167 234 my $expValue = shift;
168
169 234 my ($expSign, $exp) = _extractSign($expValue);
170
171 234 return $self
172 ->_setExp($exp)
173 ->_setExpSign($expSign);
174 }
175
176 # --- Parse and set value ---
177 1110 ▶sub SetValue {
Subroutine SetValue: called CC=7 SCAR=19.5
Pod undocumented
178 1110 my $self = shift;
179 1110 my $number = lc(shift);
180
181 # --- Split up into coeffStr, expStr and frac ---
182 1110 my $coeffStr;
183 my $expStr;
184 1110 my $frac;
185
186 # 1.e1
187 # +13.0e+2
188 # -123.010e-3
189 1110 ▶ if ($number =~ /^([\+\-]?\d+)(\.\d*)e([\+\-]?\d+)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($number =~ /^([\+\-]?\d+)(\.\d*)e([\+\-]?\d+)$/) { } 38 1072 1110
elsif ($number =~ /^([\+\-]?\d+)e([\+\-]?\d+)$/) { } 347 725 1072
elsif ($number =~ /^([\+\-]?\d+)(\.\d*)$/) { } 57 668 725
elsif ($number =~ /^([\+\-]?\d+)$/) { } 668 0 668
190 38 $coeffStr = $1;
191 38 $frac = $2;
192 38 $expStr = $3;
193 }
194 # 1e1
195 # +12e+2
196 # -123e-3
197 elsif ($number =~ /^([\+\-]?\d+)e([\+\-]?\d+)$/) {
198 347 $coeffStr = $1;
199 347 $frac = '.';
200 347 $expStr = $2;
201 }
202 # 1.
203 # +12.01
204 # -123.120
205 elsif ($number =~ /^([\+\-]?\d+)(\.\d*)$/) {
206 57 $coeffStr = $1;
207 57 $frac = $2;
208 57 $expStr = '0';
209 }
210 # 1
211 # +12
212 # -123
213 elsif ($number =~ /^([\+\-]?\d+)$/) {
214 668 $coeffStr = $1;
215 668 $frac = '.';
216 668 $expStr = '0';
217 }
218 else {
219 0 croak "wrong number format: '$number'";
220 }
221
222 # --- extract signs ---
223
224 1110 my ($exp_sign, $exp) = _extractSign($expStr);
225
226 # --- remove not needed '0's ---
227
228 1110 $frac .= '0';
229 1110 $frac = substr($frac, 1); # throw away '.' in first char
230 1110 $frac =~ s/0+$//o; # throw away 0 at end
231
232 1110 ▶ $exp = 0 unless $exp; # in case exp not set
Branch: 1 branch
BranchTrueFalseTotal
unless $exp 1052 58 1110
233
234 # --- move decimal point '.' off to right to get empty fractional part ---
235
236 1110 my $exp_value = $exp;
237 1110 ▶ $exp_value = '-' . $exp
Branch: 1 branch
BranchTrueFalseTotal
if $exp_sign < 0 32 1078 1110
238 if $exp_sign < 0;
239
240 # TODO: replace standard '-' operator by subtraction of GBM, when available
241 # --- move '.' ---
242 1110 $exp_value -= length($frac);
243
244 1110 ($exp_sign, $exp) = _extractSign($exp_value);
245
246 1110 $coeffStr .= $frac;
247
248 1110 my ($coeff_sign, $coeff) = _extractSign($coeffStr);
249
250 # --- store values in instance ---
251
252 1110 $self->_setCoeff($coeff);
253 1110 $self->_setExp($exp);
254 1110 $self->_setCoeffSign($coeff_sign);
255 1110 $self->_setExpSign($exp_sign);
256
257 1110 return $self->Simplify();
258 }
259
260 # --- Simplify: Remove 0 at front and ascend exponent for every 0 removed at end ---
261 1120 ▶sub Simplify {
Subroutine Simplify: called CC=4 SCAR=13.9
Pod undocumented
262 1120 my $self = shift;
263 1120 my $coeff = $self->_getCoeff();
264
265 1120 ▶ if ($coeff =~ /(\d)(0+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($coeff =~ /(\d)(0+)$/) 124 996 1120
266 124 $coeff = $`.$1;
267 124 my $expAdd = length($2);
268
269 124 $self->SetExpValue($self->_getExpSign() * $self->_getExp() + $expAdd);
270 }
271
272 1120 $coeff =~ s/^0+//o; # throw away 0 from beginning
273 1120 ▶ $coeff = '0' if $coeff eq ''; # coeff was 0
Branch: 1 branch
BranchTrueFalseTotal
if $coeff eq "" 119 1001 1120
274
275 1120 ▶ if ($coeff eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($coeff eq 0) 119 1001 1120
276 119 $self->_setExp(0);
277 119 $self->_setCoeffSign(1);
278 119 $self->_setExpSign(1);
279 }
280
281 1120 return $self->_setCoeff($coeff);
282 }
283
284 # --- move decimal point to the right until exponent is 0 ----
285 32 ▶sub DispenseExponent {
Subroutine DispenseExponent: called CC=4 SCAR=14.0
Pod undocumented
286 32 my $self = shift;
287
288 32 ▶ return $self if $self->_getExpSign() < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_getExpSign < 0 0 32 32
289 32 my $exp = $self->_getExp();
290
291 32 ▶ croak __PACKAGE__.'::'.(caller(0))[3].'(): '.
Branch: 1 branch
BranchTrueFalseTotal
if $exp > $Math::GoldenBigMath::_COEFF_MAX_DIGITS 0 32 32
292 "Cannot deal numbers with more than $_COEFF_MAX_DIGITS"
293 if $exp > $_COEFF_MAX_DIGITS;
294
295 32 ▶ return $self if $exp eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $exp eq 0 21 11 32
296 11 return $self->MoveDecimalPointToRight($exp);
297 }
298
299 # --- MoveDecimalPointToRight for bigger exponent until exponents are equal ---
300 286 ▶sub AdoptExponents {
Subroutine AdoptExponents: called CC=3 SCAR=11.0
Pod undocumented
301 286 my $gbm1 = shift; # GoldenBigMath 1
302 286 my $gbm2 = shift; # GoldenBigMath 2
303
304 # print $gbm1->GetValue()."\n";
305 # print $gbm2->GetValue()."\n\n";
306
307 286 my $exp1 = $gbm1->GetExpValue();
308 286 my $exp2 = $gbm2->GetExpValue();
309
310 286 my $expDiff = 0;
311 286 ▶ if ($exp1 > $exp2) {
Branch: 2 branches
BranchTrueFalseTotal
if ($exp1 > $exp2) { } 68 218 286
elsif ($exp1 < $exp2) { } 28 190 218
312 68 $expDiff = $exp1 - $exp2;
313 68 $gbm1->MoveDecimalPointToRight($expDiff);
314
315 # print "expDiff $expDiff\n";
316 # print $gbm1->GetValue()."\n";
317 # print $gbm2->GetValue()."\n\n";
318
319 68 return $gbm1;
320 }
321 elsif ($exp1 < $exp2) {
322 28 $expDiff = $exp2 - $exp1;
323 28 $gbm2->MoveDecimalPointToRight($expDiff);
324
325 # print "expDiff $expDiff\n";
326 # print $gbm1->GetValue()."\n";
327 # print $gbm2->GetValue()."\n\n";
328
329 28 return $gbm2;
330 }
331
332 # print $gbm1->GetValue()."\n";
333 # print $gbm2->GetValue()."\n\n";
334
335 190 return $gbm1;
336 }
337
338 # --- move decimal point to the right and reduce exponent ----
339 114 ▶sub MoveDecimalPointToRight {
Subroutine MoveDecimalPointToRight: called CC=5 SCAR=16.7
Pod undocumented
340 114 my $self = shift;
341 114 my $moves = shift; # number of '0' digits to add at right end
342
343 114 ▶ croak __PACKAGE__.'::'.(caller(0))[3].'(): '.
Branch: 1 branch
BranchTrueFalseTotal
unless $moves =~ /^[1-9][0-9]*$/ 4 110 114
344 "Wrong number format for moving decimal point, should be [1-9][0-9]*, "
345 ."but was '$moves'"
346 unless $moves =~ /^[1-9][0-9]*$/;
347
348 110 ▶ croak __PACKAGE__.'::'.(caller(0))[3].'(): '.
Branch: 1 branch
BranchTrueFalseTotal
if $moves > $Math::GoldenBigMath::_COEFF_MAX_DIGITS 0 110 110
349 "Cannot deal numbers with more than $_COEFF_MAX_DIGITS"
350 if $moves > $_COEFF_MAX_DIGITS;
351
352 110 ▶ return $self
Branch: 1 branch
BranchTrueFalseTotal
if $moves <= 0 or $moves + length($self->_getCoeff) > $Math::GoldenBigMath::_COEFF_MAX_DIGITS 0 110 110
Condition33%1/3
$moves <= 0 or $moves + length($self->_getCoeff) > $Math::GoldenBigMath::_COEFF_MAX_DIGITS
l!l&&r!l&&!r
00110
MC/DC0%0/2
$moves <= 0 or $moves + length($self->_getCoeff) > $Math::GoldenBigMath::_COEFF_MAX_DIGITS
$moves <= 0 $moves + length($self->_getCoeff) > $Math::GoldenBigMath::_COEFF_MAX_DIGITS
Truth table33%1/3
A or B
A= $moves <= 0B= $moves + length($self->_getCoeff) > $Math::GoldenBigMath::_COEFF_MAX_DIGITS
ABresult
1X1
011
000
353 if $moves <= 0 or $moves + length($self->_getCoeff()) > $_COEFF_MAX_DIGITS;
354
355 # TODO: replace standard '-' operator by subtraction of GBM, when available
356 110 $self->SetExpValue($self->GetExpValue() - $moves);
357 110 $self->{_coeff} .= '0' x $moves;
358
359 110 return $self;
360 }
361
362 #=== Calculation sub (s) =======================================================
363
364 # --- comparison sub (s) -------------------------------------------------------
365
366 1 ▶sub CompareEqual {
Subroutine CompareEqual: called CC=2 SCAR=7.0
Pod undocumented
367 1 my $gbm1 = shift;
368 1 my $gbm2 = shift;
369
370 1 $gbm1->Simplify();
371 1 $gbm2->Simplify();
372 1 ▶ return $gbm1->GetValue() eq $gbm2->GetValue() ? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$gbm1->GetValue eq $gbm2->GetValue ? : 1 0 1
373 }
374
375 4 ▶sub CompareNotEqual {
Subroutine CompareNotEqual: called CC=2 SCAR=6.9
Pod undocumented
376 4 my $gbm1 = shift;
377 4 my $gbm2 = shift;
378
379 4 $gbm1->Simplify();
380 4 $gbm2->Simplify();
381 4 ▶ return $gbm1->GetValue() ne $gbm2->GetValue() ? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
$gbm1->GetValue ne $gbm2->GetValue ? : 3 1 4
382 }
383
384 1 ▶sub Greater {
Subroutine Greater: called CC=2 SCAR=7.0
Pod undocumented
385 1 my $gbm1 = shift;
386 1 my $gbm2 = shift;
387
388 1 ▶ my $result = ($gbm1 <=> $gbm2) > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($gbm1 <=> $gbm2) > 0 ? : 1 0 1
389 1 return $result;
390 }
391
392 1 ▶sub Smaller {
Subroutine Smaller: called CC=2 SCAR=7.0
Pod undocumented
393 1 my $gbm1 = shift;
394 1 my $gbm2 = shift;
395 1 ▶ my $result = ($gbm1 <=> $gbm2) < 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($gbm1 <=> $gbm2) < 0 ? : 1 0 1
396 1 return $result;
397 }
398
399 1 ▶sub GreaterEqual {
Subroutine GreaterEqual: called CC=2 SCAR=7.0
Pod undocumented
400 1 my $gbm1 = shift;
401 1 my $gbm2 = shift;
402
403 1 ▶ my $result = ($gbm1 <=> $gbm2) >= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($gbm1 <=> $gbm2) >= 0 ? : 1 0 1
404 1 return $result;
405 }
406
407 2 ▶sub SmallerEqual {
Subroutine SmallerEqual: called CC=2 SCAR=7.0
Pod undocumented
408 2 my $gbm1 = shift;
409 2 my $gbm2 = shift;
410 2 ▶ my $result = ($gbm1 <=> $gbm2) <= 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($gbm1 <=> $gbm2) <= 0 ? : 2 0 2
411 2 return $result;
412 }
413
414 # --- start Addition, handle exponents and sign ----
415 216 ▶sub Addition {
Subroutine Addition: called CC=6 SCAR=17.9
Pod undocumented
416 216 my $gbm1 = shift;
417 216 my $gbm2 = shift;
418
419 216 ▶ return new Math::GoldenBigMath($gbm1)->Addition($gbm2) unless ref $gbm1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gbm1 55 161 216
420 161 ▶ return $gbm1->Addition(new Math::GoldenBigMath($gbm2)) unless ref $gbm2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gbm2 55 106 161
421
422 106 AdoptExponents($gbm1, $gbm2);
423
424 106 my $resultString;
425
426 106 my $sign1 = $gbm1->_getCoeffSign();
427 106 my $sign2 = $gbm2->_getCoeffSign();
428
429 106 my $z1 = $gbm1->_getCoeff();
430 106 my $z2 = $gbm2->_getCoeff();
431
432 106 ▶ if ($sign1 == $sign2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign1 == $sign2) { } 85 21 106
433 85 $resultString = AdditionWithoutSignPointAndExponent($z1, $z2);
434 }
435 else {
436 21 ▶ my $result = $sign1 > 0
Branch: 1 branch
BranchTrueFalseTotal
$sign1 > 0 ? : 11 10 21
437 ? Subtraction($z1, $z2)
438 : Subtraction($z2, $z1);
439
440 21 $result->DispenseExponent();
441 21 ▶ croak "something went wrong, exponent was not dispensed" if $result->_getExp() ne '0';
Branch: 1 branch
BranchTrueFalseTotal
if $result->_getExp ne 0 0 21 21
442
443 21 $resultString = $result->_getCoeff();
444 21 $sign1 = $result->_getCoeffSign();
445 }
446
447 106 my $signChar = _getSignChar($sign1);
448 106 my $exponent = _getSignChar($gbm1->_getExpSign()) . $gbm1->_getExp();
449
450 106 return $gbm1->new("$signChar${resultString}e$exponent");
451 }
452
453 # --- start Subtraction, handle exponents and sign ----
454 230 ▶sub Subtraction {
Subroutine Subtraction: called CC=7 SCAR=19.5
Pod undocumented
455 230 my $gbm1 = shift;
456 230 my $gbm2 = shift;
457
458 230 ▶ return new Math::GoldenBigMath($gbm1)->Subtraction($gbm2) unless ref $gbm1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gbm1 76 154 230
459 154 ▶ return $gbm1->Subtraction(new Math::GoldenBigMath($gbm2)) unless ref $gbm2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gbm2 76 78 154
460
461 78 AdoptExponents($gbm1, $gbm2);
462
463 78 my $resultString;
464
465 78 my $sign1 = $gbm1->_getCoeffSign();
466 78 my $sign2 = $gbm2->_getCoeffSign();
467
468 78 my $z = $gbm1->_getCoeff();
469 78 my $subtr = $gbm2->_getCoeff();
470
471 78 ▶ if ($sign1 == $sign2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign1 == $sign2) { } 58 20 78
472 58 ▶ my $swap = Compare ($z, $subtr) < 0 ? 1: 0;
Branch: 1 branch
BranchTrueFalseTotal
Compare($z, $subtr) < 0 ? : 19 39 58
473
474 58 ▶ if ($swap) { # Second is greater, so swap args and set $sign1 = -$sign1
Branch: 1 branch
BranchTrueFalseTotal
if ($swap) { } 19 39 58
475 19 $resultString = SubtractionWithoutSignPointAndExponentAndFirstGreater($subtr, $z);
476 19 ▶ $sign1 = $sign1 < 0 ? 1: -1;
Branch: 1 branch
BranchTrueFalseTotal
$sign1 < 0 ? : 2 17 19
477 # print "# swap\n";
478 }
479 else {
480 39 $resultString = SubtractionWithoutSignPointAndExponentAndFirstGreater($z, $subtr);
481 }
482 }
483 else {
484 20 $resultString = AdditionWithoutSignPointAndExponent($z, $subtr);
485 }
486
487 # print "# sign $sign1\n";
488
489 78 my $signChar = _getSignChar($sign1);
490 78 my $exponent = _getSignChar($gbm1->_getExpSign()) . $gbm1->_getExp();
491
492 78 return $gbm1->new("$signChar${resultString}e$exponent");
493 }
494
495 # --- start multiplication, handle exponents and sign ---------------------
496 128 ▶sub Multiplication {
Subroutine Multiplication: called CC=6 SCAR=17.9
Pod undocumented
497 128 my $gbm1 = shift;
498 128 my $gbm2 = shift;
499
500 128 ▶ return new Math::GoldenBigMath($gbm1)->Multiplication($gbm2) unless ref $gbm1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gbm1 26 102 128
501 102 ▶ return $gbm1->Multiplication(new Math::GoldenBigMath($gbm2)) unless ref $gbm2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gbm2 26 76 102
502
503 76 my $coeff1 = $gbm1->_getCoeff();
504 76 my $coeff2 = $gbm2->_getCoeff();
505
506 76 ▶ return $gbm1->new(0)
Branch: 1 branch
BranchTrueFalseTotal
if $coeff1 == 0 or $coeff2 == 0 16 60 76
Condition100%3/3
$coeff1 == 0 or $coeff2 == 0
l!l&&r!l&&!r
9760
MC/DC100%2/2
$coeff1 == 0 or $coeff2 == 0
$coeff1 == 0 $coeff2 == 0
Truth table100%3/3
A or B
A= $coeff1 == 0B= $coeff2 == 0
ABresult
1X1
011
000
507 if $coeff1 == 0 or $coeff2 == 0;
508
509 60 my $resultString = MultiplicationWithoutSignPointAndExponent
510 ($coeff1, $coeff2);
511
512 60 ▶ my $signChar = $gbm1->_getCoeffSign() eq $gbm2->_getCoeffSign()
Branch: 1 branch
BranchTrueFalseTotal
$gbm1->_getCoeffSign eq $gbm2->_getCoeffSign ? : 55 5 60
513 ? '+' : '-';
514
515 60 my $exp1 = _getSignChar($gbm1->_getExpSign()) . $gbm1->_getExp();
516 60 my $exp2 = _getSignChar($gbm2->_getExpSign()) . $gbm2->_getExp();
517
518 # TODO: replace standard '+' operator by addition of GBM
519 60 my $exponent = $exp1 + $exp2;
520
521 # Add call of Simplify()
522 60 return $gbm1->new("$signChar${resultString}e$exponent");
523 }
524
525 # --- Do Compare ----------------------------------------------------------
526 269 ▶sub Compare {
Subroutine Compare: called CC=7 SCAR=19.5
Pod undocumented
527 269 my $gbm1 = shift;
528 269 my $gbm2 = shift;
529
530 269 ▶ return new Math::GoldenBigMath($gbm1)->Compare($gbm2) unless ref $gbm1;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gbm1 86 183 269
531 183 ▶ return $gbm1->Compare(new Math::GoldenBigMath($gbm2)) unless ref $gbm2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $gbm2 87 96 183
532
533 96 my $sign = $gbm1->_getCoeffSign();
534 96 my $sign2 = $gbm2->_getCoeffSign();
535
536 96 ▶ if ($sign ne $sign2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign ne $sign2) 0 96 96
537 0 return $sign cmp $sign2;
538 }
539
540 96 AdoptExponents($gbm1, $gbm2);
541
542 96 my $coeff1 = $gbm1->_getCoeff();
543 96 my $coeff2 = $gbm2->_getCoeff();
544
545 96 my $l1 = length($coeff1);
546 96 my $l2 = length($coeff2);
547
548 96 my $coeffCmp = "";
549 96 ▶ if ($l1 == $l2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l1 == $l2) { } 71 25 96
550 71 $coeffCmp = $coeff1 cmp $coeff2;
551 }
552 else {
553 25 ▶ $coeffCmp = $l1 > $l2 ? 1:-1;
Branch: 1 branch
BranchTrueFalseTotal
$l1 > $l2 ? : 16 9 25
554 }
555
556 96 ▶ return $sign > 0 ? $coeffCmp: -$coeffCmp;
Branch: 1 branch
BranchTrueFalseTotal
$sign > 0 ? : 96 0 96
557 }
558
559 # --- create multiplication table for fast multiplication and division ---
560 # internal, but should be tested
561 91 ▶sub buildMultiplicationTableAsString {
Subroutine buildMultiplicationTableAsString: called CC=3 SCAR=11.0
Pod undocumented
562 91 my $z = shift; # coeff without sign as string
563
564 91 ▶ croak "not a number string " unless $z =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $z =~ /^\d+$/ 5 86 91
565
566 86 my @mulTab;
567 86 $mulTab[9] = 0;
568 86 $mulTab[0] = 0;
569 86 $mulTab[1] = $z;
570 86 foreach my $i (2..9) {
571 688 $mulTab[$i] = AdditionWithoutSignPointAndExponent($mulTab[$i-1], $z);
572 }
573
574 86 return \@mulTab;
575 }
576
577 #=== Worker subs with real calculation ====================================
578
579 # FB-SCHR+ =====================================================================
580 # Addition without exponent, decimal point and sign
581 # Returns string containing [0-9]+
582 894 ▶sub AdditionWithoutSignPointAndExponent {
Subroutine AdditionWithoutSignPointAndExponent: called CC=8 SCAR=20.8
Pod undocumented
583 894 my $z1 = shift; # Number as string
584 894 my $z2 = shift; # Number as string
585
586 894 my $i1 = length($z1) - 1;
587 894 my $i2 = length($z2) - 1;
588 894 ▶ my $maxIdx = $i1 > $i2 ? $i1: $i2;
Branch: 1 branch
BranchTrueFalseTotal
$i1 > $i2 ? : 362 532 894
589
590 # print "# maxIdx $maxIdx\n";
591
592 # result variables
593 894 my $result = ''; # result as string
594
595 # index variables
596 894 my $i; # running index in GoldenBigMath
597 my $s; # sum of two digits
598 894 my $u = 0; # store carry (u for german uebertrag)
599 894 my $d1; # one digit
600 my $d2; # one digit
601
602 # --- now calculate sum by schriftliche addition ---------------------------
603 894 ▶ for ($i = $maxIdx; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 4546 894 5440
604 4546 $d1 = 0;
605 4546 $d2 = 0;
606
607 4546 ▶ $d1 = substr($z1, $i1, 1) if $i1 >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i1 >= 0 4381 165 4546
608 4546 ▶ $d2 = substr($z2, $i2, 1) if $i2 >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i2 >= 0 4103 443 4546
609
610 4546 $s = $d1 + $d2 + $u;
611 # print "# $s = $d1 + $d2\n";
612 4546 ▶ if ($s > 9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s > 9) { } 1467 3079 4546
613 1467 $u = 1; # don't use slow divide or modulo
614 1467 $s -= 10; # don't use slow divide or modulo
615 }
616 else {
617 3079 $u = 0;
618 }
619 # print "# $u$s = $d1 + $d2\n";
620 4546 $result .= $s; # much faster than $result = $s . $result
621
622 4546 $i1--;
623 4546 $i2--;
624 }
625
626 894 ▶ $result .= $u if $u;
Branch: 1 branch
BranchTrueFalseTotal
if $u 93 801 894
627
628 # --- reverse strings to get the highest number at first position
629 894 $result = reverse $result;
630
631 # replace starting zeroes
632 894 $result =~ s/^0+//go;
633 894 ▶ $result = 0 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 10 884 894
634
635 894 return $result;
636 }
637
638 # FB-SCHR- =====================================================================
639 # Subtraction without exponent, decimal point and sign
640 # and first number is greater than second
641 58 ▶sub SubtractionWithoutSignPointAndExponentAndFirstGreater {
Subroutine SubtractionWithoutSignPointAndExponentAndFirstGreater: called CC=6 SCAR=17.9
Pod undocumented
642 58 my $z1 = shift; # Number as string
643 58 my $z2 = shift; # Number as string
644
645 58 my $maxIdx = length($z1) - 1;
646 58 my $addZeroCount = $maxIdx - length($z2) + 1;
647
648 58 ▶ croak "second longer than first" if $addZeroCount < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $addZeroCount < 0 0 58 58
649
650 # Add zeros in front of second to get both strings same length
651 58 ▶ $z2 = '0' x $addZeroCount . $z2 if $addZeroCount > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $addZeroCount > 0 19 39 58
652
653 # print "\n $z1\n-$z2\n\n";
654
655 # result variables
656 58 my $result = ''; # result as string
657 58 my $resultObj; # result as object
658
659 # index variables
660 my $i; # running index in GoldenBigMath
661 58 my $d; # difference of two digits
662 58 my $u = 0; # store carry (u for german uebertrag)
663
664 # --- now calculate difference by schriftliche subtraction -----------------
665 58 ▶ for ($i = $maxIdx; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 348 58 406
666 348 $d = substr($z1, $i, 1) - substr($z2, $i, 1) - $u;
667 348 ▶ if ($d < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 0) { } 21 327 348
668 21 $u = 1; # don't use slow divide or modulo
669 21 $d += 10; # don't use slow divide or modulo
670 }
671 else {
672 327 $u = 0;
673 }
674 348 $result .= $d; # much faster than $result = $d . $result
675 }
676
677 # --- reverse strings to get the highest number at first position
678 58 $result = reverse $result;
679
680 # replace starting zeroes by ' '
681 58 $result =~ s/^0+//go;
682 58 ▶ $result = 0 unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 16 42 58
683
684 58 return $result;
685 }
686
687 # FB-SCHR* =====================================================================
688 # Multiplication without exponent, decimal point and sign
689 60 ▶sub MultiplicationWithoutSignPointAndExponent {
Subroutine MultiplicationWithoutSignPointAndExponent: called CC=5 SCAR=16.4
Pod undocumented
690 60 my $z1 = shift; # Number as string
691 60 my $z2 = shift; # Number as string
692
693 60 my $mulTabRef = buildMultiplicationTableAsString($z2);
694
695 # result variables
696 60 my $result = '';
697
698 # help variables
699 60 my $c = 1; # dot counter
700
701 # intermediate multiplication values
702 60 my $d; # next digit
703 60 my $addZeros = ''; # store zeros to append to multab value
704 60 my $add; # GoldenBigMath to be added as
705 # next multiplication by single digit
706
707 # --- now calculate mul by schriftliche multiplication ---------------------
708
709 60 ▶ for (my $i = length($z1)-1; $i >= 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 101 60 161
710 101 $d = substr($z1, $i, 1);
711 101 ▶ if ($d != '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($d != 0) 101 0 101
712 # --- $add = $d * $d2, much faster is using table as follows
713 101 $add = $mulTabRef->[$d] . $addZeros;
714 101 $result = AdditionWithoutSignPointAndExponent($result, $add);
715 }
716 101 $addZeros .= '0'; # next number position, mulTabRef *= 10;
717
718 # --- print dots (.) to see its still running and not hanging ----------
719 101 ▶ if ($c > 16383) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c > 16383) 0 101 101
720 0 print "\n";
721 0 $c = 1;
722 }
723 101 ▶ print "." unless $c++ & 127;
Branch: 1 branch
BranchTrueFalseTotal
unless $c++ & 127 0 101 101
724 }
725
726 60 return $result;
727 }
728
729 # FB-SCHR/ =====================================================================
730 # FB-SCHR% =====================================================================
731 # Really calc the division (without exponent, decimal point and sign)
732 0 ▶sub CalcDivisionWithoutSignPointAndExponent {
Subroutine CalcDivisionWithoutSignPointAndExponent: not called CC=15 SCAR=54.8
Pod undocumented
733 0 my $gbm1 = shift;
734 0 my $z1 = $gbm1; # to get symmetric names
735 0 my $z2 = shift; # to get symmetric names
736
737 0 $z1->prepareMulDiv($z2);
738
739 0 my @mulTab = $gbm1->ConvertMultiplicationTableToGoldenBigMath();
740 0 my $maxDivDigits = $gbm1->GetMaxDivideDigits();
741
742 # use string references for faster access
743 0 my $bm1Ref = \$z1->GetValue();
744 0 my $bm1Len = length($$bm1Ref);
745
746 # result variables
747 0 my $result = '';
748 0 my $resultObj = new GoldenBigMath->new(0);
749
750 # index and help variables
751 0 my $i = 0; # running index in GoldenBigMath
752 0 my $z; # next digit (z for german ziffer)
753 0 my $u = 0; # store carry (u for german uebertrag)
754 0 my $c = 1; # dot counter
755 0 my $firstIter = 'true'; # One iteration is needed !!
756
757 # intermediate division values
758 0 my $rest = '0'; # Residue of actual division step,
759 # but rest is shorter and same in german
760 0 my %restHash = (); # will actually not be used,
761 # coming later to identify periods
762
763 # --- now calculate div by schriftliche division ---------------------------
764
765 0 ▶ while ($i < $maxDivDigits
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $maxDivDigits and $rest != 0 || $firstIter || $i <= $bm1Len) 0 0 0
Condition0%0/9
$i < $maxDivDigits and $rest != 0 || $firstIter || $i <= $bm1Len
!ll&&!rl&&r
000
$rest != 0 or $firstIter or $i <= $bm1Len
l!l&&r!l&&!r
000
$rest != 0 or $firstIter
l!l&&r!l&&!r
000
MC/DC0%0/4
$i < $maxDivDigits and $rest != 0 || $firstIter || $i <= $bm1Len
$i < $maxDivDigits $rest != 0 $firstIter $i <= $bm1Len
Truth table0%0/5
A and B or C or D
A= $i < $maxDivDigitsB= $rest != 0C= $firstIterD= $i <= $bm1Len
ABCDresult
0XXX0
10000
11XX1
101X1
10011
766 && ($rest != 0 || $firstIter || $i <= $bm1Len)) {
767
768 0 $firstIter = '';
769 0 my $bmRest = GoldenBigMath->new($rest); # Rest as GoldenBigMath
770 0 my $z = 0; # next digit of result
771
772 # --- find next result digit -------------------------------------------
773
774 0 ▶ unless ($bmRest < $mulTab[1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($bmRest < $mulTab[1]) 0 0 0
775 0 ▶ while ($z < 9) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z < 9) 0 0 0
776 0 ▶ if ($mulTab[$z+1] > $bmRest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mulTab[$z + 1] > $bmRest) 0 0 0
777 0 last;
778 }
779 0 ▶ $z++ if $z < 9;
Branch: 1 branch
BranchTrueFalseTotal
if $z < 9 0 0 0
780 }
781
782 # --- fire exit, should never be reached!! ---
783 0 ▶ if ($mulTab[$z] > $bmRest) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mulTab[$z] > $bmRest) 0 0 0
784 0 print $bmRest->GetValue() . " < " . $mulTab[$z]->GetValue()
785 . "\n";
786 0 croak "problem during search for multiplication factor\n";
787 }
788
789 # --- calc next rest for division ----------------------------------
790
791 0 $bmRest = $bmRest - $mulTab[$z];
792 }
793
794 # --- add digit found --------------------------------------------------
795
796 0 $result .= $z;
797
798 # --- Check, if decimal point reached ----------------------------------
799
800 0 my $lz = '0';
801 0 ▶ if ($i < $bm1Len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < $bm1Len) { } 0 0 0
elsif ($i == $bm1Len) { } 0 0 0
802 0 $lz = substr($$bm1Ref, $i, 1);
803 }
804 # --- end of number reached --------------------------------------------
805 elsif ($i == $bm1Len) {
806 # --- modulo wanted ? ----------------------------------------------
807 0 ▶ if ($gbm1->GetOperator() eq '%') {
Branch: 1 branch
BranchTrueFalseTotal
if ($gbm1->GetOperator eq "%") 0 0 0
808 0 $result = $bmRest->GetValue();
809 0 last;
810 }
811
812 # --- add decimal point --------------------------------------------
813
814 0 $result .= '.';
815 }
816
817 # --- Append next digit to rest ----------------------------------------
818
819 0 $rest = $bmRest->GetValue() . $lz;
820
821 # --- print dots (.) to see its still running and not hanging ----------
822 0 ▶ if ($c > 16383) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c > 16383) 0 0 0
823 0 print "\n";
824 0 $c = 1;
825 }
826 0 ▶ print "." unless $c++ %127;
Branch: 1 branch
BranchTrueFalseTotal
unless $c++ % 127 0 0 0
827
828 0 $i++;
829 }
830
831 # --- create and fill result object ----------------------------------------
832 0 $z1->Normalize();
833 0 $z2->Normalize();
834 0 $resultObj = GoldenBigMath->new($result);
835 0 $resultObj->Normalize();
836 0 $resultObj->_storeOperator($gbm1->GetOperator());
837 0 $resultObj->_storeOperatorName("div string");
838 0 $resultObj->_storeU('...');
839 0 $resultObj->_setZ1($z1); #TODO: clean up storage??
840 0 $resultObj->_setZ2($z2); #TODO: clean up storage??
841
842 0 return $resultObj;
843 }
844
845 __END__
↑ Summary