BigInt.pm

filter: Statementstmt 71.3% 1569 / 2198 Branchbran 59.5% 1231 / 2066 Conditioncond 60.5% 683 / 1128 MC/DCmcdc 35.1% 229 / 651 Subroutinesub 77.0% 181 / 235 Podpod 99.2% 135 / 136 totaltotal 62.8% 4028 / 6414 SCAR 114.3
CC
1431
Cov
65%
bmuladd
503.4
bfib
342.0
to_base
342.0
CRAP 92306.6
↑ Summary BigFloat.pm »
1 # -*- coding: utf-8-unix -*-
2  
3 package Math::BigInt;
4  
5 #
6 # "Mike had an infinite amount to do and a negative amount of time in which
7 # to do it." - Before and After
8 #
9  
10 # The following hash values are used:
11 # value: unsigned int with actual value (as a Math::BigInt::Calc or similar)
12 # sign : +, -, NaN, +inf, -inf
13 # _a : accuracy
14 # _p : precision
15  
16 # Remember not to take shortcuts ala $xs = $x->{value}; $LIB->foo($xs); since
17 # underlying lib might change the reference!
18  
19 51 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 51 ▶use Carp qw< carp croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 51 ▶use Scalar::Util qw< blessed refaddr >;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 51 our $VERSION = '1.999842';
27 51 $VERSION =~ tr/_//d;
28  
29 51 require Exporter;
30 51 our @ISA = qw(Exporter);
31 51 our @EXPORT_OK = qw(objectify bgcd blcm);
32  
33 # Inside overload, the first arg is always an object. If the original code had
34 # it reversed (like $x = 2 * $y), then the third parameter is true.
35 # In some cases (like add, $x = $x + 2 is the same as $x = 2 + $x) this makes
36 # no difference, but in some cases it does.
37  
38 # For overloaded ops with only one argument we simple use $_[0]->copy() to
39 # preserve the argument.
40  
41 # Thus inheritance of overload operators becomes possible and transparent for
42 # our subclasses without the need to repeat the entire overload section there.
43  
44 use overload
45  
46 # overload key: with_assign
47  
48 309 ▶ '+' => sub { $_[0] -> copy() -> badd($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
49  
50 356 ▶ '-' => sub { my $c = $_[0] -> copy();
Subroutine __ANON__: called CC=2 SCAR=6.9
51 356 ▶ $_[2] ? $c -> bneg() -> badd($_[1])
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 56 300 356
52 : $c -> bsub($_[1]); },
53  
54 960 ▶ '*' => sub { $_[0] -> copy() -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56 341 ▶ '/' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bdiv($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 3 338 341
57 : $_[0] -> copy() -> bdiv($_[1]); },
58  
59 353 ▶ '%' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bmod($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 3 350 353
60 : $_[0] -> copy() -> bmod($_[1]); },
61  
62 439 ▶ '**' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bpow($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 3 436 439
63 : $_[0] -> copy() -> bpow($_[1]); },
64  
65 20 ▶ '<<' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bblsft($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 20 20
66 : $_[0] -> copy() -> bblsft($_[1]); },
67  
68 20 ▶ '>>' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bbrsft($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 20 20
69 : $_[0] -> copy() -> bbrsft($_[1]); },
70  
71 # overload key: assign
72  
73 27 ▶ '+=' => sub { $_[0] -> badd($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
74  
75 29 ▶ '-=' => sub { $_[0] -> bsub($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
76  
77 17 ▶ '*=' => sub { $_[0] -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
78  
79 14 ▶ '/=' => sub { scalar $_[0] -> bdiv($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
80  
81 17 ▶ '%=' => sub { $_[0] -> bmod($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
82  
83 6 ▶ '**=' => sub { $_[0] -> bpow($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
84  
85 3 ▶ '<<=' => sub { $_[0] -> bblsft($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
86  
87 3 ▶ '>>=' => sub { $_[0] -> bbrsft($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
88  
89 # 'x=' => sub { },
90  
91 # '.=' => sub { },
92  
93 # overload key: num_comparison
94  
95 318 ▶ '<' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> blt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 318 318
96 : $_[0] -> blt($_[1]); },
97  
98 621 ▶ '<=' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> ble($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 4 617 621
99 : $_[0] -> ble($_[1]); },
100  
101 506 ▶ '>' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bgt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 506 506
102 : $_[0] -> bgt($_[1]); },
103  
104 140 ▶ '>=' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bge($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 140 140
105 : $_[0] -> bge($_[1]); },
106  
107 241 ▶ '==' => sub { $_[0] -> beq($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
108  
109 9 ▶ '!=' => sub { $_[0] -> bne($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
110  
111 # overload key: 3way_comparison
112  
113 0 ▶ '<=>' => sub { my $cmp = $_[0] -> bcmp($_[1]);
Subroutine __ANON__: not called CC=3 SCAR=24.8
114 0 ▶ defined($cmp) && $_[2] ? -$cmp : $cmp; },
Branch: 1 branch
BranchTrueFalseTotal
defined $cmp && $_[2] ? : 0 0 0
Condition0%0/3
defined $cmp and $_[2]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cmp and $_[2]
defined $cmp $_[2]
Truth table0%0/3
A and B
A= defined $cmpB= $_[2]
ABresult
0X0
100
111
115  
116 7947 ▶ 'cmp' => sub { $_[2] ? "$_[1]" cmp $_[0] -> bstr()
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 7947 7947
117 : $_[0] -> bstr() cmp "$_[1]"; },
118  
119 # overload key: str_comparison
120  
121 # 'lt' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrlt($_[0])
122 # : $_[0] -> bstrlt($_[1]); },
123 #
124 # 'le' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrle($_[0])
125 # : $_[0] -> bstrle($_[1]); },
126 #
127 # 'gt' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrgt($_[0])
128 # : $_[0] -> bstrgt($_[1]); },
129 #
130 # 'ge' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrge($_[0])
131 # : $_[0] -> bstrge($_[1]); },
132 #
133 # 'eq' => sub { $_[0] -> bstreq($_[1]); },
134 #
135 # 'ne' => sub { $_[0] -> bstrne($_[1]); },
136  
137 # overload key: binary
138  
139 140 ▶ '&' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> band($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 3 137 140
140 : $_[0] -> copy() -> band($_[1]); },
141  
142 4 ▶ '&=' => sub { $_[0] -> band($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
143  
144 201 ▶ '|' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bior($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 3 198 201
145 : $_[0] -> copy() -> bior($_[1]); },
146  
147 4 ▶ '|=' => sub { $_[0] -> bior($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
148  
149 199 ▶ '^' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bxor($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 3 196 199
150 : $_[0] -> copy() -> bxor($_[1]); },
151  
152 4 ▶ '^=' => sub { $_[0] -> bxor($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
153  
154 # '&.' => sub { },
155  
156 # '&.=' => sub { },
157  
158 # '|.' => sub { },
159  
160 # '|.=' => sub { },
161  
162 # '^.' => sub { },
163  
164 # '^.=' => sub { },
165  
166 # overload key: unary
167  
168 285 ▶ 'neg' => sub { $_[0] -> copy() -> bneg(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
169  
170 # '!' => sub { },
171  
172 0 ▶ '~' => sub { $_[0] -> copy() -> bnot(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
173  
174 # '~.' => sub { },
175  
176 # overload key: mutators
177  
178 23 ▶ '++' => sub { $_[0] -> binc() },
Subroutine __ANON__: called CC=1 SCAR=0.0
179  
180 3 ▶ '--' => sub { $_[0] -> bdec() },
Subroutine __ANON__: called CC=1 SCAR=0.0
181  
182 # overload key: func
183  
184 0 ▶ 'atan2' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> batan2($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
185 : $_[0] -> copy() -> batan2($_[1]); },
186  
187 0 ▶ 'cos' => sub { $_[0] -> copy() -> bcos(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
188  
189 0 ▶ 'sin' => sub { $_[0] -> copy() -> bsin(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
190  
191 0 ▶ 'exp' => sub { $_[0] -> copy() -> bexp($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
192  
193 3 ▶ 'abs' => sub { $_[0] -> copy() -> babs(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
194  
195 30 ▶ 'log' => sub { $_[0] -> copy() -> blog(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
196  
197 1 ▶ 'sqrt' => sub { $_[0] -> copy() -> bsqrt(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
198  
199 6 ▶ 'int' => sub { $_[0] -> copy() -> bint(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
200  
201 # overload key: conversion
202  
203 6 ▶ 'bool' => sub { $_[0] -> is_zero() ? '' : 1; },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_zero ? : 3 3 6
204  
205 1855 ▶ '""' => sub { $_[0] -> bstr(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
206  
207 51 ▶ '0+' => sub { $_[0] -> numify(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
208  
209 0 ▶ '=' => sub { $_[0] -> copy(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
210  
211 51 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
212  
213 ##############################################################################
214 # global constants, flags and accessory
215  
216 # These vars are public, but their direct usage is not recommended, use the
217 # accessor methods instead
218  
219 # $round_mode is 'even', 'odd', '+inf', '-inf', 'zero', 'trunc', or 'common'.
220 51 our $round_mode = 'even';
221 51 our $accuracy = undef;
222 51 our $precision = undef;
223 51 our $div_scale = 40;
224 51 our $upgrade = undef; # default is no upgrade
225 51 our $downgrade = undef; # default is no downgrade
226  
227 # These are internally, and not to be used from the outside at all
228  
229 51 our $_trap_nan = 0; # are NaNs ok? set w/ config()
230 51 our $_trap_inf = 0; # are infs ok? set w/ config()
231  
232 51 my $nan = 'NaN'; # constants for easier life
233  
234 # Module to do the low level math.
235  
236 51 my $DEFAULT_LIB = 'Math::BigInt::Calc';
237 51 my $LIB;
238  
239 # Has import() been called yet? Needed to make "require" work.
240  
241 51 my $IMPORT = 0;
242  
243 ##############################################################################
244 # the old code had $rnd_mode, so we need to support it, too
245  
246 51 our $rnd_mode = 'even';
247  
248 sub TIESCALAR {
249 51 ▶ my ($class) = @_;
Subroutine TIESCALAR: called CC=1 SCAR=0.0
250 51 bless \$round_mode, $class;
251 }
252  
253 sub FETCH {
254 3 ▶ return $round_mode;
Subroutine FETCH: called CC=1 SCAR=0.0
255 }
256  
257 sub STORE {
258 52 ▶ $rnd_mode = $_[0]->round_mode($_[1]);
Subroutine STORE: called CC=1 SCAR=0.0
259 }
260  
261 BEGIN {
262 # tie to enable $rnd_mode to work transparently
263 51 ▶ tie $rnd_mode, 'Math::BigInt';
Subroutine BEGIN: called CC=1 SCAR=0.0
264  
265 # set up some handy alias names
266 51 *is_pos = \&is_positive;
267 51 *is_neg = \&is_negative;
268 51 *as_number = \&as_int;
269 }
270  
271 ###############################################################################
272 # Configuration methods
273 ###############################################################################
274  
275 sub round_mode {
276 410 ▶ my $self = shift;
Subroutine round_mode: called CC=7 SCAR=19.5
Pod documented
277 410 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition60%3/5
ref $self || $self || __PACKAGE__
l!l
4100
ref $self or $self
l!l&&r!l&&!r
673430
MC/DC0%0/2
ref $self || $self || __PACKAGE__
ref $self $self
Truth table66%2/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
278  
279 410 ▶ if (@_) { # setter
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 367 43 410
280 367 my $m = shift;
281 367 ▶ croak("The value for 'round_mode' must be defined")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 0 367 367
282 unless defined $m;
283 367 ▶ croak("Unknown round mode '$m'")
Branch: 1 branch
BranchTrueFalseTotal
unless $m =~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/ 4 363 367
284 unless $m =~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/;
285 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
286 363 ${"${class}::round_mode"} = $m;
287 }
288  
289 else { # getter
290 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
291 43 my $m = ${"${class}::round_mode"};
292 43 ▶ defined($m) ? $m : $round_mode;
Branch: 1 branch
BranchTrueFalseTotal
defined $m ? : 34 9 43
293 }
294 }
295  
296 sub upgrade {
297 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
298 # make Class->upgrade() work
299 3332 ▶ my $self = shift;
Subroutine upgrade: called CC=4 SCAR=14.2
Pod documented
300 3332 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition40%2/5
ref $self || $self || __PACKAGE__
l!l
33320
ref $self or $self
l!l&&r!l&&!r
033320
MC/DC0%0/2
ref $self || $self || __PACKAGE__
ref $self $self
Truth table33%1/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
301  
302 # need to set new value?
303 3332 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 2023 1309 3332
304 2023 return ${"${class}::upgrade"} = $_[0];
305 }
306 1309 ${"${class}::upgrade"};
307 }
308  
309 sub downgrade {
310 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
311 # make Class->downgrade() work
312 3140 ▶ my $self = shift;
Subroutine downgrade: called CC=4 SCAR=14.2
Pod documented
313 3140 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition40%2/5
ref $self || $self || __PACKAGE__
l!l
31400
ref $self or $self
l!l&&r!l&&!r
031400
MC/DC0%0/2
ref $self || $self || __PACKAGE__
ref $self $self
Truth table33%1/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
314 # need to set new value?
315 3140 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 2093 1047 3140
316 2093 return ${"${class}::downgrade"} = $_[0];
317 }
318 1047 ${"${class}::downgrade"};
319 }
320  
321 sub div_scale {
322 946 ▶ my $self = shift;
Subroutine div_scale: called CC=7 SCAR=19.6
Pod documented
323 946 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition60%3/5
ref $self || $self || __PACKAGE__
l!l
9460
ref $self or $self
l!l&&r!l&&!r
149320
MC/DC0%0/2
ref $self || $self || __PACKAGE__
ref $self $self
Truth table66%2/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
324  
325 946 ▶ if (@_) { # setter
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 15 931 946
326 15 my $ds = shift;
327 15 ▶ croak("The value for 'div_scale' must be defined") unless defined $ds;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ds 0 15 15
328 # It is not documented what div_scale <= 0 means, but Astro::Units sets
329 # div_scale to 0 and fails its tests if this is not supported.
330 #croak("The value for 'div_scale' must be positive") unless $ds > 0;
331 15 ▶ $ds = $ds -> numify() if defined(blessed($ds));
Branch: 1 branch
BranchTrueFalseTotal
if defined builtin::blessed($ds) 0 15 15
332 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
333 15 ${"${class}::div_scale"} = $ds;
334 }
335  
336 else { # getter
337 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
338 931 my $ds = ${"${class}::div_scale"};
339 931 ▶ defined($ds) ? $ds : $div_scale;
Branch: 1 branch
BranchTrueFalseTotal
defined $ds ? : 922 9 931
340 }
341 }
342  
343 sub accuracy {
344 # $x->accuracy($a); ref($x) $a
345 # $x->accuracy(); ref($x)
346 # Class->accuracy(); class
347 # Class->accuracy($a); class $a
348  
349 8021 ▶ my $x = shift;
Subroutine accuracy: called CC=14 SCAR=26.9
Pod documented
350 8021 ▶ my $class = ref($x) || $x || __PACKAGE__;
Condition60%3/5
ref $x || $x || __PACKAGE__
l!l
80210
ref $x or $x
l!l&&r!l&&!r
52874930
MC/DC0%0/2
ref $x || $x || __PACKAGE__
ref $x $x
Truth table66%2/3
A or B
A= ref $xB= $x
ABresult
1X1
011
000
351  
352 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
353 8021 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 526 7495 8021
354 526 my $a = shift;
355 526 ▶ if (defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a) 433 93 526
356 433 ▶ $a = $a -> can('numify') ? $a -> numify() : 0 + "$a" if ref($a);
Branch: 2 branches
BranchTrueFalseTotal
if ref $a 0 433 433
$a->can('numify') ? : 0 0 0
357 # also croak on non-numerical
358 433 ▶ croak "accuracy must be a number, not '$a'"
Branch: 1 branch
BranchTrueFalseTotal
unless $a =~ /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/ 0 433 433
359 unless $a =~/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/;
360 433 ▶ croak "accuracy must be an integer, not '$a'"
Branch: 1 branch
BranchTrueFalseTotal
if $a != int $a 0 433 433
361 if $a != int $a;
362 433 ▶ croak "accuracy must be greater than zero, not '$a'"
Branch: 1 branch
BranchTrueFalseTotal
if $a <= 0 0 433 433
363 if $a <= 0;
364 }
365  
366 526 ▶ if (ref($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x) { } 442 84 526
367 # Set instance variable.
368 442 ▶ $x = $x->bround($a) if defined $a;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a 389 53 442
369 442 $x->{_a} = $a; # set/overwrite, even if not rounded
370 442 $x->{_p} = undef; # clear P
371 # Why return class variable here? Fixme!
372 442 ▶ $a = ${"${class}::accuracy"} unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 53 389 442
373 } else {
374 # Set class variable.
375 84 ${"${class}::accuracy"} = $a; # set global A
376 84 ${"${class}::precision"} = undef; # clear global P
377 }
378  
379 526 return $a; # shortcut
380 }
381  
382 # Return instance variable.
383 7495 ▶ return $x->{_a} if ref($x);
Branch: 1 branch
BranchTrueFalseTotal
if ref $x 86 7409 7495
384  
385 # Return class variable.
386 7409 return ${"${class}::accuracy"};
387 }
388  
389 sub precision {
390 # $x->precision($p); ref($x) $p
391 # $x->precision(); ref($x)
392 # Class->precision(); class
393 # Class->precision($p); class $p
394  
395 7795 ▶ my $x = shift;
Subroutine precision: called CC=13 SCAR=26.0
Pod documented
396 7795 ▶ my $class = ref($x) || $x || __PACKAGE__;
Condition60%3/5
ref $x || $x || __PACKAGE__
l!l
77950
ref $x or $x
l!l&&r!l&&!r
29475010
MC/DC0%0/2
ref $x || $x || __PACKAGE__
ref $x $x
Truth table66%2/3
A or B
A= ref $xB= $x
ABresult
1X1
011
000
397  
398 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
399 7795 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 293 7502 7795
400 293 my $p = shift;
401 293 ▶ if (defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p) 208 85 293
402 208 ▶ $p = $p -> can('numify') ? $p -> numify() : 0 + "$p" if ref($p);
Branch: 2 branches
BranchTrueFalseTotal
if ref $p 0 208 208
$p->can('numify') ? : 0 0 0
403 208 ▶ croak "precision must be a number, not '$p'"
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/ 0 208 208
404 unless $p =~/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/;
405 208 ▶ croak "precision must be an integer, not '$p'"
Branch: 1 branch
BranchTrueFalseTotal
if $p != int $p 0 208 208
406 if $p != int $p;
407 }
408  
409 293 ▶ if (ref($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x) { } 207 86 293
410 # Set instance variable.
411 207 ▶ $x = $x->bfround($p) if defined $p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p 158 49 207
412 207 $x->{_p} = $p; # set/overwrite, even if not rounded
413 207 $x->{_a} = undef; # clear A
414 # Why return class variable here? Fixme!
415 207 ▶ $p = ${"${class}::precision"} unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 49 158 207
416 } else {
417 # Set class variable.
418 86 ${"${class}::precision"} = $p; # set global P
419 86 ${"${class}::accuracy"} = undef; # clear global A
420 }
421  
422 293 return $p; # shortcut
423 }
424  
425 # Return instance variable.
426 7502 ▶ return $x->{_p} if ref($x);
Branch: 1 branch
BranchTrueFalseTotal
if ref $x 87 7415 7502
427  
428 # Return class variable.
429 7415 return ${"${class}::precision"};
430 }
431  
432 sub config {
433 # return (or set) configuration data.
434 286 ▶ my $class = shift || __PACKAGE__;
Subroutine config: called CC=15 SCAR=27.1
Pod documented
Condition50%1/2
shift() || __PACKAGE__
l!l
2860
MC/DC0%0/1
shift() || __PACKAGE__
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
435  
436 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
437 286 ▶ if (@_ > 1 || (@_ == 1 && (ref($_[0]) eq 'HASH'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1 or @_ == 1 and ref $_[0] eq "HASH") 30 256 286
Condition100%6/6
@_ > 1 or @_ == 1 and ref $_[0] eq "HASH"
l!l&&r!l&&!r
284254
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
262302
MC/DC66%2/3
@_ > 1 or @_ == 1 and ref $_[0] eq "HASH"
@_ > 1 @_ == 1 ref $_[0] eq "HASH"
Truth table75%3/4
A or B and C
A= @_ > 1B= @_ == 1C= ref $_[0] eq "HASH"
ABCresult
1XX1
0111
00X0
0100
438 # try to set given options as arguments from hash
439  
440 30 my $args = $_[0];
441 30 ▶ if (ref($args) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args ne "HASH") 28 2 30
442 28 $args = { @_ };
443 }
444 # these values can be "set"
445 30 my $set_args = {};
446 30 foreach my $key (qw/
447 accuracy precision
448 round_mode div_scale
449 upgrade downgrade
450 trap_inf trap_nan
451 /)
452 {
453 240 ▶ $set_args->{$key} = $args->{$key} if exists $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$key} 28 212 240
454 240 delete $args->{$key};
455 }
456 30 ▶ if (keys %$args > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$args > 0) 2 28 30
457 2 croak("Illegal key(s) '", join("', '", keys %$args),
458 "' passed to $class\->config()");
459 }
460 28 foreach my $key (keys %$set_args) {
461 28 ▶ if ($key =~ /^trap_(inf|nan)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^trap_(inf|nan)\z/) 16 12 28
462 16 ▶ ${"${class}::_trap_$1"} = ($set_args->{"trap_$1"} ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$$set_args{"trap_$1"} ? : 12 4 16
463 16 next;
464 }
465 # use a call instead of just setting the $variable to check argument
466 12 $class->$key($set_args->{$key});
467 }
468 }
469  
470 # now return actual configuration
471  
472 my $cfg = {
473 lib => $LIB,
474 284 lib_version => ${"${LIB}::VERSION"},
475 class => $class,
476 284 trap_nan => ${"${class}::_trap_nan"},
477 284 trap_inf => ${"${class}::_trap_inf"},
478 284 version => ${"${class}::VERSION"},
479 };
480 284 foreach my $key (qw/
481 accuracy precision
482 round_mode div_scale
483 upgrade downgrade
484 /)
485 {
486 1704 $cfg->{$key} = ${"${class}::$key"};
487 }
488 284 ▶ if (@_ == 1 && (ref($_[0]) ne 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] ne "HASH") 230 54 284
Condition100%3/3
@_ == 1 and ref $_[0] ne "HASH"
!ll&&!rl&&r
522230
MC/DC100%2/2
@_ == 1 and ref $_[0] ne "HASH"
@_ == 1 ref $_[0] ne "HASH"
Truth table100%3/3
A and B
A= @_ == 1B= ref $_[0] ne "HASH"
ABresult
0X0
100
111
489 # calls of the style config('lib') return just this value
490 230 return $cfg->{$_[0]};
491 }
492 54 $cfg;
493 }
494  
495 sub _scale_a {
496 # select accuracy parameter based on precedence,
497 # used by bround() and bfround(), may return undef for scale (means no op)
498 67505 ▶ my ($x, $scale, $mode) = @_;
Subroutine _scale_a: called CC=7 SCAR=19.5
499  
500 67505 ▶ $scale = $x->{_a} unless defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 3894 63611 67505
501  
502 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
503 67505 my $class = ref($x);
504  
505 67505 ▶ $scale = ${ $class . '::accuracy' } unless defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 3891 63614 67505
506 67505 ▶ $mode = ${ $class . '::round_mode' } unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 12203 55302 67505
507  
508 67505 ▶ if (defined $scale) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scale) 63614 3891 67505
509 63614 ▶ $scale = $scale->can('numify') ? $scale->numify()
Branch: 2 branches
BranchTrueFalseTotal
if ref $scale 434 63180 63614
$scale->can('numify') ? : 434 0 434
510 : "$scale" if ref($scale);
511 63614 $scale = int($scale);
512 }
513  
514 67505 ($scale, $mode);
515 }
516  
517 sub _scale_p {
518 # select precision parameter based on precedence,
519 # used by bround() and bfround(), may return undef for scale (means no op)
520 936 ▶ my ($x, $scale, $mode) = @_;
Subroutine _scale_p: called CC=7 SCAR=19.6
521  
522 936 ▶ $scale = $x->{_p} unless defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 4 932 936
523  
524 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
525 936 my $class = ref($x);
526  
527 936 ▶ $scale = ${ $class . '::precision' } unless defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 4 932 936
528 936 ▶ $mode = ${ $class . '::round_mode' } unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 716 220 936
529  
530 936 ▶ if (defined $scale) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scale) 932 4 936
531 932 ▶ $scale = $scale->can('numify') ? $scale->numify()
Branch: 2 branches
BranchTrueFalseTotal
if ref $scale 0 932 932
$scale->can('numify') ? : 0 0 0
532 : "$scale" if ref($scale);
533 932 $scale = int($scale);
534 }
535  
536 936 ($scale, $mode);
537 }
538  
539 ###############################################################################
540 # Constructor methods
541 ###############################################################################
542  
543 sub new {
544 # Create a new Math::BigInt object from a string or another Math::BigInt
545 # object. See hash keys documented at top.
546  
547 # The argument could be an object, so avoid ||, && etc. on it. This would
548 # cause costly overloaded code to be called. The only allowed ops are ref()
549 # and defined.
550  
551 19924 ▶ my $self = shift;
Subroutine new: called CC=31 SCAR=34.9
Pod documented
552 19924 my $selfref = ref $self;
553 19924 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
0199240
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
554  
555 # Make "require" work.
556  
557 19924 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 2 19922 19924
558  
559 # Calling new() with no input arguments has been discouraged for more than
560 # 10 years, but people apparently still use it, so we still support it.
561  
562 19924 ▶ return $class -> bzero() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 8 19916 19924
563  
564 19916 my ($wanted, @r) = @_;
565  
566 19916 ▶ if (!defined($wanted)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wanted) 0 19916 19916
567 #carp("Use of uninitialized value in new()")
568 # if warnings::enabled("uninitialized");
569 0 return $class -> bzero(@r);
570 }
571  
572 19916 ▶ if (!ref($wanted) && $wanted eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $wanted and $wanted eq "") 4 19912 19916
Condition100%3/3
not ref $wanted and $wanted eq ""
!ll&&!rl&&r
39198734
MC/DC100%2/2
not ref $wanted and $wanted eq ""
not ref $wanted $wanted eq ""
Truth table100%3/3
not(A) and B
A= not ref $wantedB= $wanted eq ""
ABresult
0X0
100
111
573 #carp(q|Argument "" isn't numeric in new()|)
574 # if warnings::enabled("numeric");
575 #return $class -> bzero(@r);
576 4 return $class -> bnan(@r);
577 }
578  
579 # Initialize a new object.
580  
581 19912 $self = bless {}, $class;
582  
583 # Math::BigInt or subclass
584  
585 19912 ▶ if (defined(blessed($wanted)) && $wanted -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($wanted) and $wanted->isa(__PACKAGE__)) 5 19907 19912
Condition100%3/3
defined builtin::blessed($wanted) and $wanted->isa(__PACKAGE__)
!ll&&!rl&&r
19873345
MC/DC100%2/2
defined builtin::blessed($wanted) and $wanted->isa(__PACKAGE__)
defined builtin::blessed($wanted) $wanted->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= defined builtin::blessed($wanted)B= $wanted->isa(__PACKAGE__)
ABresult
0X0
100
111
586  
587 # Don't copy the accuracy and precision, because a new object should get
588 # them from the global configuration.
589  
590 5 $self -> {sign} = $wanted -> {sign};
591 5 $self -> {value} = $LIB -> _copy($wanted -> {value});
592 5 ▶ $self = $self->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 0 1 1
Condition66%4/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
401
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
401
MC/DC33%1/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table50%2/4
A and not(B) and not(C)
A= @r >= 2B= not defined $r[0]C= not defined $r[1]
ABCresult
0XX0
10X0
1100
1111
593 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
594 5 return $self;
595 }
596  
597 # Shortcut for non-zero scalar integers with no non-zero exponent.
598  
599 19907 ▶ if ($wanted =~
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ ( [+-]? ) # optional sign ( [1-9] [0-9]* ) # non-zero significand ( \.0* )? # ... with optional zero fraction ( [Ee] [+-]? 0+ )? # optional zero exponent \z /x) 12829 7078 19907
600 / ^
601 ( [+-]? ) # optional sign
602 ( [1-9] [0-9]* ) # non-zero significand
603 ( \.0* )? # ... with optional zero fraction
604 ( [Ee] [+-]? 0+ )? # optional zero exponent
605 \z
606 /x)
607 {
608 12829 my $sgn = $1;
609 12829 my $abs = $2;
610 12829 ▶ $self->{sign} = $sgn || '+';
Condition100%2/2
$sgn || '+'
l!l
41618668
MC/DC100%1/1
$sgn || '+'
$sgn
Truth table100%2/2
A
A= $sgn
Aresult
11
00
611 12829 $self->{value} = $LIB->_new($abs);
612 12829 $self = $self->round(@r);
613 12829 return $self;
614 }
615  
616 # Handle Infs.
617  
618 7078 ▶ if ($wanted =~ / ^
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ \s* ( [+-]? ) inf (?: inity )? \s* \z /xi) 1755 5323 7078
619 \s*
620 ( [+-]? )
621 inf (?: inity )?
622 \s*
623 \z
624 /ix)
625 {
626 1755 ▶ my $sgn = $1 || '+';
Condition100%2/2
$1 || '+'
l!l
1173582
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
627 1755 return $class -> binf($sgn, @r);
628 }
629  
630 # Handle explicit NaNs (not the ones returned due to invalid input).
631  
632 5323 ▶ if ($wanted =~ / ^
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ \s* ( [+-]? ) nan \s* \z /xi) 391 4932 5323
633 \s*
634 ( [+-]? )
635 nan
636 \s*
637 \z
638 /ix)
639 {
640 391 return $class -> bnan(@r);
641 }
642  
643 4932 my @parts;
644  
645 4932 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted) or @parts = $class->_dec_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)) 3026 639 3665
Condition75%18/24
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted) or @parts = $class->_dec_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
l!l&&r!l&&!r
42930639
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted) or @parts = $class->_dec_str_to_flt_lib_parts($wanted)
l!l&&r!l&&!r
12673026639
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted)
l!l&&r!l&&!r
10871803665
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
l!l&&r!l&&!r
108703845
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
384141087
$wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
384500
$wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
364223180
$wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
63900
MC/DC0%0/9
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted) or @parts = $class->_dec_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
$wanted =~ /^\s*[+-]?0?[Xx]/ @parts = $class->_hex_str_to_flt_lib_parts($wanted) $wanted =~ /^\s*[+-]?0?[Oo]/ @parts = $class->_oct_str_to_flt_lib_parts($wanted) $wanted =~ /^\s*[+-]?0?[Bb]/ @parts = $class->_bin_str_to_flt_lib_parts($wanted) @parts = $class->_dec_str_to_flt_lib_parts($wanted) $wanted =~ /^\s*[+-]?0_*\d/ @parts = $class->_oct_str_to_flt_lib_parts($wanted)
Truth table7%3/39
A and B or C and D or E and F or G or H and I
A= $wanted =~ /^\s*[+-]?0?[Xx]/B= @parts = $class->_hex_str_to_flt_lib_parts($wanted)C= $wanted =~ /^\s*[+-]?0?[Oo]/D= @parts = $class->_oct_str_to_flt_lib_parts($wanted)E= $wanted =~ /^\s*[+-]?0?[Bb]/F= @parts = $class->_bin_str_to_flt_lib_parts($wanted)G= @parts = $class->_dec_str_to_flt_lib_parts($wanted)H= $wanted =~ /^\s*[+-]?0_*\d/I= @parts = $class->_oct_str_to_flt_lib_parts($wanted)
ABCDEFGHIresult
11XXXXXXX1
0X11XXXXX1
1011XXXXX1
0X0X11XXX1
0X1011XXX1
100X11XXX1
101011XXX1
0X0X0X1XX1
0X0X101XX1
0X100X1XX1
0X10101XX1
100X0X1XX1
100X101XX1
10100X1XX1
1010101XX1
0X0X0X0111
0X0X100111
0X100X0111
0X10100111
100X0X0111
100X100111
10100X0111
1010100111
0X0X0X00X0
0X0X0X0100
0X0X1000X0
0X0X100100
0X100X00X0
0X100X0100
0X101000X0
0X10100100
100X0X00X0
100X0X0100
100X1000X0
100X100100
10100X00X0
10100X0100
10101000X0
1010100100
646 # Handle hexadecimal numbers. We auto-detect hexadecimal numbers if they
647 # have a "0x", "0X", "x", or "X" prefix, cf. CORE::oct().
648  
649 $wanted =~ /^\s*[+-]?0?[Xx]/ and
650 @parts = $class -> _hex_str_to_flt_lib_parts($wanted)
651  
652 or
653  
654 # Handle octal numbers. We auto-detect octal numbers if they have a
655 # "0o", "0O", "o", "O" prefix, cf. CORE::oct().
656  
657 $wanted =~ /^\s*[+-]?0?[Oo]/ and
658 @parts = $class -> _oct_str_to_flt_lib_parts($wanted)
659  
660 or
661  
662 # Handle binary numbers. We auto-detect binary numbers if they have a
663 # "0b", "0B", "b", or "B" prefix, cf. CORE::oct().
664  
665 $wanted =~ /^\s*[+-]?0?[Bb]/ and
666 @parts = $class -> _bin_str_to_flt_lib_parts($wanted)
667  
668 or
669  
670 # At this point, what is left are decimal numbers that aren't handled
671 # above and octal floating point numbers that don't have any of the
672 # "0o", "0O", "o", or "O" prefixes. First see if it is a decimal number.
673  
674 @parts = $class -> _dec_str_to_flt_lib_parts($wanted)
675 or
676  
677 # See if it is an octal floating point number. The extra check is
678 # included because _oct_str_to_flt_lib_parts() accepts octal numbers
679 # that don't have a prefix (this is needed to make it work with, e.g.,
680 # from_oct() that don't require a prefix). However, Perl requires a
681 # prefix for octal floating point literals. For example, "1p+0" is not
682 # valid, but "01p+0" and "0__1p+0" are.
683  
684 $wanted =~ /^\s*[+-]?0_*\d/ and
685 @parts = $class -> _oct_str_to_flt_lib_parts($wanted))
686 {
687 # The value is an integer iff the exponent is non-negative.
688  
689 4293 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 4252 41 4293
690 4252 $self -> {sign} = $parts[0];
691 4252 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
692 4252 ▶ $self = $self->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 2 1124 1126
Condition83%5/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
312801124
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
312621124
MC/DC66%2/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table75%3/4
A and not(B) and not(C)
A= @r >= 2B= not defined $r[0]C= not defined $r[1]
ABCresult
0XX0
10X0
1100
1111
693 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
694 4252 return $self;
695 }
696  
697 # The value is not an integer, so upgrade if upgrading is enabled.
698  
699 41 ▶ return $upgrade -> new($wanted, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 10 31 41
700 }
701  
702 # If we get here, the value is neither a valid decimal, binary, octal, or
703 # hexadecimal number. It is not explicit an Inf or a NaN either.
704  
705 670 return $class -> bnan(@r);
706 }
707  
708 # Create a Math::BigInt from a decimal string. This is an equivalent to
709 # from_hex(), from_oct(), and from_bin(). It is like new() except that it does
710 # not accept anything but a string representing a finite decimal number.
711  
712 sub from_dec {
713 0 ▶ my $self = shift;
Subroutine from_dec: not called CC=8 SCAR=42.8
Pod documented
714 0 my $selfref = ref $self;
715 0 ▶ my $class = $selfref || $self;
Condition0%0/3
$selfref || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table0%0/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
716  
717 # Don't modify constant (read-only) objects.
718  
719 0 ▶ return $self if $selfref && $self->modify('from_dec');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_dec") 0 0 0
Condition0%0/3
$selfref and $self->modify("from_dec")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("from_dec")
$selfref $self->modify("from_dec")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("from_dec")
ABresult
0X0
100
111
720  
721 0 my $str = shift;
722 0 my @r = @_;
723  
724 # If called as a class method, initialize a new object.
725  
726 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
727  
728 0 ▶ if (my @parts = $class -> _dec_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_dec_str_to_flt_lib_parts($str)) 0 0 0
729  
730 # The value is an integer iff the exponent is non-negative.
731  
732 0 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 0 0 0
733 0 $self -> {sign} = $parts[0];
734 0 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
735 0 return $self -> round(@r);
736 }
737  
738 # The value is not an integer, so upgrade if upgrading is enabled.
739  
740 0 ▶ return $upgrade -> new($str, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 0 0
741 }
742  
743 0 return $self -> bnan(@r);
744 }
745  
746 # Create a Math::BigInt from a hexadecimal string.
747  
748 sub from_hex {
749 2 ▶ my $self = shift;
Subroutine from_hex: called CC=8 SCAR=24.9
Pod documented
750 2 my $selfref = ref $self;
751 2 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
020
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
752  
753 # Don't modify constant (read-only) objects.
754  
755 2 ▶ return $self if $selfref && $self->modify('from_hex');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_hex") 0 2 2
Condition33%1/3
$selfref and $self->modify("from_hex")
!ll&&!rl&&r
200
MC/DC0%0/2
$selfref and $self->modify("from_hex")
$selfref $self->modify("from_hex")
Truth table33%1/3
A and B
A= $selfrefB= $self->modify("from_hex")
ABresult
0X0
100
111
756  
757 2 my $str = shift;
758 2 my @r = @_;
759  
760 # If called as a class method, initialize a new object.
761  
762 2 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 2 0 2
763  
764 2 ▶ if (my @parts = $class -> _hex_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_hex_str_to_flt_lib_parts($str)) 2 0 2
765  
766 # The value is an integer iff the exponent is non-negative.
767  
768 2 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 2 0 2
769 2 $self -> {sign} = $parts[0];
770 2 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
771 2 return $self -> round(@r);
772 }
773  
774 # The value is not an integer, so upgrade if upgrading is enabled.
775  
776 0 ▶ return $upgrade -> new($str, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 0 0
777 }
778  
779 0 return $self -> bnan(@r);
780 }
781  
782 # Create a Math::BigInt from an octal string.
783  
784 sub from_oct {
785 2 ▶ my $self = shift;
Subroutine from_oct: called CC=8 SCAR=24.9
Pod documented
786 2 my $selfref = ref $self;
787 2 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
020
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
788  
789 # Don't modify constant (read-only) objects.
790  
791 2 ▶ return $self if $selfref && $self->modify('from_oct');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_oct") 0 2 2
Condition33%1/3
$selfref and $self->modify("from_oct")
!ll&&!rl&&r
200
MC/DC0%0/2
$selfref and $self->modify("from_oct")
$selfref $self->modify("from_oct")
Truth table33%1/3
A and B
A= $selfrefB= $self->modify("from_oct")
ABresult
0X0
100
111
792  
793 2 my $str = shift;
794 2 my @r = @_;
795  
796 # If called as a class method, initialize a new object.
797  
798 2 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 2 0 2
799  
800 2 ▶ if (my @parts = $class -> _oct_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_oct_str_to_flt_lib_parts($str)) 2 0 2
801  
802 # The value is an integer iff the exponent is non-negative.
803  
804 2 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 2 0 2
805 2 $self -> {sign} = $parts[0];
806 2 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
807 2 return $self -> round(@r);
808 }
809  
810 # The value is not an integer, so upgrade if upgrading is enabled.
811  
812 0 ▶ return $upgrade -> new($str, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 0 0
813 }
814  
815 0 return $self -> bnan(@r);
816 }
817  
818 # Create a Math::BigInt from a binary string.
819  
820 sub from_bin {
821 53 ▶ my $self = shift;
Subroutine from_bin: called CC=8 SCAR=24.9
Pod documented
822 53 my $selfref = ref $self;
823 53 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
0530
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
824  
825 # Don't modify constant (read-only) objects.
826  
827 53 ▶ return $self if $selfref && $self->modify('from_bin');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_bin") 0 53 53
Condition33%1/3
$selfref and $self->modify("from_bin")
!ll&&!rl&&r
5300
MC/DC0%0/2
$selfref and $self->modify("from_bin")
$selfref $self->modify("from_bin")
Truth table33%1/3
A and B
A= $selfrefB= $self->modify("from_bin")
ABresult
0X0
100
111
828  
829 53 my $str = shift;
830 53 my @r = @_;
831  
832 # If called as a class method, initialize a new object.
833  
834 53 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 53 0 53
835  
836 53 ▶ if (my @parts = $class -> _bin_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_bin_str_to_flt_lib_parts($str)) 53 0 53
837  
838 # The value is an integer iff the exponent is non-negative.
839  
840 53 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 53 0 53
841 53 $self -> {sign} = $parts[0];
842 53 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
843 53 return $self -> round(@r);
844 }
845  
846 # The value is not an integer, so upgrade if upgrading is enabled.
847  
848 0 ▶ return $upgrade -> new($str, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 0 0
849 }
850  
851 0 return $self -> bnan(@r);
852 }
853  
854 # Create a Math::BigInt from a byte string.
855  
856 sub from_bytes {
857 0 ▶ my $self = shift;
Subroutine from_bytes: not called CC=6 SCAR=37.4
Pod documented
858 0 my $selfref = ref $self;
859 0 ▶ my $class = $selfref || $self;
Condition0%0/3
$selfref || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table0%0/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
860  
861 # Don't modify constant (read-only) objects.
862  
863 0 ▶ return $self if $selfref && $self->modify('from_bytes');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_bytes") 0 0 0
Condition0%0/3
$selfref and $self->modify("from_bytes")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("from_bytes")
$selfref $self->modify("from_bytes")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("from_bytes")
ABresult
0X0
100
111
864  
865 0 ▶ croak("from_bytes() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_from_bytes") 0 0 0
866 unless $LIB->can('_from_bytes');
867  
868 0 my $str = shift;
869 0 my @r = @_;
870  
871 # If called as a class method, initialize a new object.
872  
873 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
874 0 $self -> {sign} = '+';
875 0 $self -> {value} = $LIB -> _from_bytes($str);
876 0 return $self -> round(@r);
877 }
878  
879 sub from_base {
880 0 ▶ my $self = shift;
Subroutine from_base: not called CC=15 SCAR=54.8
Pod documented
881 0 my $selfref = ref $self;
882 0 ▶ my $class = $selfref || $self;
Condition0%0/3
$selfref || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table0%0/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
883  
884 # Don't modify constant (read-only) objects.
885  
886 0 ▶ return $self if $selfref && $self->modify('from_base');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_base") 0 0 0
Condition0%0/3
$selfref and $self->modify("from_base")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("from_base")
$selfref $self->modify("from_base")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("from_base")
ABresult
0X0
100
111
887  
888 0 my ($str, $base, $cs, @r) = @_; # $cs is the collation sequence
889  
890 0 ▶ $base = $class->new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
891  
892 0 ▶ croak("the base must be a finite integer >= 2")
Branch: 1 branch
BranchTrueFalseTotal
if $base < 2 or not $base->is_int 0 0 0
Condition0%0/3
$base < 2 or not $base->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$base < 2 or not $base->is_int
$base < 2 not $base->is_int
Truth table0%0/3
A or not(B)
A= $base < 2B= not $base->is_int
ABresult
1X1
011
000
893 if $base < 2 || ! $base -> is_int();
894  
895 # If called as a class method, initialize a new object.
896  
897 0 ▶ $self = $class -> bzero() unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
898  
899 # If no collating sequence is given, pass some of the conversions to
900 # methods optimized for those cases.
901  
902 0 ▶ unless (defined $cs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cs) 0 0 0
903 0 ▶ return $self -> from_bin($str, @r) if $base == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 2 0 0 0
904 0 ▶ return $self -> from_oct($str, @r) if $base == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 8 0 0 0
905 0 ▶ return $self -> from_hex($str, @r) if $base == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 16 0 0 0
906 0 ▶ if ($base == 10) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base == 10) 0 0 0
907 0 my $tmp = $class -> from_dec($str, @r);
908 0 $self -> {value} = $tmp -> {value};
909 0 $self -> {sign} = '+';
910 0 return $self -> bround(@r);
911 }
912 }
913  
914 0 ▶ croak("from_base() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_from_base") 0 0 0
915 unless $LIB->can('_from_base');
916  
917 0 $self -> {sign} = '+';
918 $self -> {value}
919 0 ▶ = $LIB->_from_base($str, $base -> {value}, defined($cs) ? $cs : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $cs ? : 0 0 0
920 0 return $self -> bround(@r);
921 }
922  
923 sub from_base_num {
924 0 ▶ my $self = shift;
Subroutine from_base_num: not called CC=13 SCAR=52.0
Pod documented
925 0 my $selfref = ref $self;
926 0 ▶ my $class = $selfref || $self;
Condition0%0/3
$selfref || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table0%0/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
927  
928 # Don't modify constant (read-only) objects.
929  
930 0 ▶ return $self if $selfref && $self->modify('from_base_num');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_base_num") 0 0 0
Condition0%0/3
$selfref and $self->modify("from_base_num")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("from_base_num")
$selfref $self->modify("from_base_num")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("from_base_num")
ABresult
0X0
100
111
931  
932 # Make sure we have an array of non-negative, finite, numerical objects.
933  
934 0 my $nums = shift;
935 0 $nums = [ @$nums ]; # create new reference
936  
937 0 for my $i (0 .. $#$nums) {
938 # Make sure we have an object.
939 0 ▶ $nums -> [$i] = $class -> new($nums -> [$i])
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($nums->[$i]) and $nums->[$i]->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined builtin::blessed($nums->[$i]) and $nums->[$i]->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined builtin::blessed($nums->[$i]) and $nums->[$i]->isa(__PACKAGE__)
defined builtin::blessed($nums->[$i]) $nums->[$i]->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= defined builtin::blessed($nums->[$i])B= $nums->[$i]->isa(__PACKAGE__)
ABresult
0X0
100
111
940 unless defined(blessed($nums -> [$i]))
941 && $nums -> [$i] -> isa(__PACKAGE__);
942 # Make sure we have a finite, non-negative integer.
943 0 ▶ croak "the elements must be finite non-negative integers"
Branch: 1 branch
BranchTrueFalseTotal
if $nums->[$i]->is_neg or not $nums->[$i]->is_int 0 0 0
Condition0%0/3
$nums->[$i]->is_neg or not $nums->[$i]->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$nums->[$i]->is_neg or not $nums->[$i]->is_int
$nums->[$i]->is_neg not $nums->[$i]->is_int
Truth table0%0/3
A or not(B)
A= $nums->[$i]->is_negB= not $nums->[$i]->is_int
ABresult
1X1
011
000
944 if $nums -> [$i] -> is_neg() || ! $nums -> [$i] -> is_int();
945 }
946  
947 0 my $base = shift;
948 0 ▶ $base = $class -> new($base)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($base) and $base->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined builtin::blessed($base) and $base->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined builtin::blessed($base) and $base->isa(__PACKAGE__)
defined builtin::blessed($base) $base->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= defined builtin::blessed($base)B= $base->isa(__PACKAGE__)
ABresult
0X0
100
111
949 unless defined(blessed($base)) && $base -> isa(__PACKAGE__);
950  
951 0 my @r = @_;
952  
953 # If called as a class method, initialize a new object.
954  
955 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
956  
957 0 ▶ croak("from_base_num() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_from_base_num") 0 0 0
958 unless $LIB->can('_from_base_num');
959  
960 0 $self -> {sign} = '+';
961 0 $self -> {value} = $LIB -> _from_base_num([ map { $_ -> {value} } @$nums ],
962 0 $base -> {value});
963  
964 0 return $self -> round(@r);
965 }
966  
967 sub bzero {
968 # create/assign '+0'
969  
970 # Class::method(...) -> Class->method(...)
971 2343 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bzero: called CC=15 SCAR=28.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 2343 2343
Condition55%5/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
002343
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
44019030
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
19030440
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table16%1/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
972 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
973 {
974 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
975 # " use is as a method instead";
976 0 unshift @_, __PACKAGE__;
977 }
978  
979 2343 my $self = shift;
980 2343 my $selfref = ref $self;
981 2343 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
44019030
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
982  
983 2343 ▶ $self->import() if $IMPORT == 0; # make require work
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 2343 2343
984  
985 # Don't modify constant (read-only) objects.
986  
987 2343 ▶ return $self if $selfref && $self->modify('bzero');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bzero") 0 2343 2343
Condition66%2/3
$selfref and $self->modify("bzero")
!ll&&!rl&&r
19034400
MC/DC0%0/2
$selfref and $self->modify("bzero")
$selfref $self->modify("bzero")
Truth table66%2/3
A and B
A= $selfrefB= $self->modify("bzero")
ABresult
0X0
100
111
988  
989 # Get the rounding parameters, if any.
990  
991 2343 my @r = @_;
992  
993 # If called as a class method, initialize a new object.
994  
995 2343 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1903 440 2343
996  
997 2343 $self->{sign} = '+';
998 2343 $self->{value} = $LIB->_zero();
999  
1000 # If rounding parameters are given as arguments, use them. If no rounding
1001 # parameters are given, and if called as a class method, initialize the new
1002 # instance with the class variables.
1003  
1004 2343 ▶ if (@r) {
Branch: 2 branches
BranchTrueFalseTotal
if (@r) { } 12 2331 2343
elsif (not $selfref) { } 1897 434 2331
1005 12 ▶ croak "can't specify both accuracy and precision"
Branch: 1 branch
BranchTrueFalseTotal
if @r >= 2 and defined $r[0] and defined $r[1] 0 12 12
Condition83%5/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
930
@r >= 2 and defined $r[0]
!ll&&!rl&&r
363
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table75%3/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
1006 if @r >= 2 && defined($r[0]) && defined($r[1]);
1007 12 $self->{_a} = $_[0];
1008 12 $self->{_p} = $_[1];
1009 } elsif (!$selfref) {
1010 1897 $self->{_a} = $class -> accuracy();
1011 1897 $self->{_p} = $class -> precision();
1012 }
1013  
1014 2343 return $self;
1015 }
1016  
1017 sub bone {
1018 # Create or assign '+1' (or -1 if given sign '-').
1019  
1020 # Class::method(...) -> Class->method(...)
1021 476 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bone: called CC=17 SCAR=29.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 476 476
Condition66%6/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
027449
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
2042720
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
2720204
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table16%1/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1022 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1023 {
1024 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1025 # " use is as a method instead";
1026 0 unshift @_, __PACKAGE__;
1027 }
1028  
1029 476 my $self = shift;
1030 476 my $selfref = ref $self;
1031 476 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
2042720
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1032  
1033 476 ▶ $self->import() if $IMPORT == 0; # make require work
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 476 476
1034  
1035 # Don't modify constant (read-only) objects.
1036  
1037 476 ▶ return $self if $selfref && $self->modify('bone');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bone") 0 476 476
Condition66%2/3
$selfref and $self->modify("bone")
!ll&&!rl&&r
2722040
MC/DC0%0/2
$selfref and $self->modify("bone")
$selfref $self->modify("bone")
Truth table66%2/3
A and B
A= $selfrefB= $self->modify("bone")
ABresult
0X0
100
111
1038  
1039 476 my ($sign, @r) = @_;
1040  
1041 # Get the sign.
1042  
1043 476 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/) { } 104 372 476
Condition100%3/3
defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/
!ll&&!rl&&r
3675104
MC/DC100%2/2
defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/
defined $_[0] $_[0] =~ /^\s*([+-])\s*$/
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] =~ /^\s*([+-])\s*$/
ABresult
0X0
100
111
1044 104 $sign = $1;
1045 104 shift;
1046 } else {
1047 372 $sign = '+';
1048 }
1049  
1050 # If called as a class method, initialize a new object.
1051  
1052 476 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 272 204 476
1053  
1054 476 $self->{sign} = $sign;
1055 476 $self->{value} = $LIB->_one();
1056  
1057 # If rounding parameters are given as arguments, use them. If no rounding
1058 # parameters are given, and if called as a class method, initialize the new
1059 # instance with the class variables.
1060  
1061 476 ▶ if (@r) {
Branch: 2 branches
BranchTrueFalseTotal
if (@r) { } 18 458 476
elsif (not $selfref) { } 266 192 458
1062 18 ▶ croak "can't specify both accuracy and precision"
Branch: 1 branch
BranchTrueFalseTotal
if @r >= 2 and defined $r[0] and defined $r[1] 0 18 18
Condition83%5/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
1260
@r >= 2 and defined $r[0]
!ll&&!rl&&r
396
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table75%3/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
1063 if @r >= 2 && defined($r[0]) && defined($r[1]);
1064 18 $self->{_a} = $_[0];
1065 18 $self->{_p} = $_[1];
1066 } elsif (!$selfref) {
1067 266 $self->{_a} = $class -> accuracy();
1068 266 $self->{_p} = $class -> precision();
1069 }
1070  
1071 476 return $self;
1072 }
1073  
1074 sub binf {
1075 # create/assign a '+inf' or '-inf'
1076  
1077 # Class::method(...) -> Class->method(...)
1078 2088 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine binf: called CC=18 SCAR=30.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 2088 2088
Condition66%6/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
0132075
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
28818000
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
18000288
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table16%1/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1079 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1080 {
1081 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1082 # " use is as a method instead";
1083 0 unshift @_, __PACKAGE__;
1084 }
1085  
1086 2088 my $self = shift;
1087 2088 my $selfref = ref $self;
1088 2088 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
28818000
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1089  
1090 {
1091 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1092 2088 ▶ if (${"${class}::_trap_inf"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_inf";}) 5 2083 2088
1093 5 croak("Tried to create +-inf in $class->binf()");
1094 }
1095 }
1096  
1097 2083 ▶ $self->import() if $IMPORT == 0; # make require work
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 2083 2083
1098  
1099 # Don't modify constant (read-only) objects.
1100  
1101 2083 ▶ return $self if $selfref && $self->modify('binf');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("binf") 0 2083 2083
Condition66%2/3
$selfref and $self->modify("binf")
!ll&&!rl&&r
17982850
MC/DC0%0/2
$selfref and $self->modify("binf")
$selfref $self->modify("binf")
Truth table66%2/3
A and B
A= $selfrefB= $self->modify("binf")
ABresult
0X0
100
111
1102  
1103 # Get the sign.
1104  
1105 2083 my $sign = '+'; # default is to return positive infinity
1106 2083 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])(inf|$)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i) 2032 51 2083
Condition66%2/3
defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i
!ll&&!rl&&r
5102032
MC/DC50%1/2
defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i
defined $_[0] $_[0] =~ /^\s*([+-])(inf|$)/i
Truth table66%2/3
A and B
A= defined $_[0]B= $_[0] =~ /^\s*([+-])(inf|$)/i
ABresult
0X0
100
111
1107 2032 $sign = $1;
1108 2032 shift;
1109 }
1110  
1111 # Get the rounding parameters, if any.
1112  
1113 2083 my @r = @_;
1114  
1115 # If called as a class method, initialize a new object.
1116  
1117 2083 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1798 285 2083
1118  
1119 2083 $self -> {sign} = $sign . 'inf';
1120 2083 $self -> {value} = $LIB -> _zero();
1121  
1122 # If rounding parameters are given as arguments, use them. If no rounding
1123 # parameters are given, and if called as a class method, initialize the new
1124 # instance with the class variables.
1125  
1126 2083 ▶ if (@r) {
Branch: 2 branches
BranchTrueFalseTotal
if (@r) { } 575 1508 2083
elsif (not $selfref) { } 1235 273 1508
1127 575 ▶ croak "can't specify both accuracy and precision"
Branch: 1 branch
BranchTrueFalseTotal
if @r >= 2 and defined $r[0] and defined $r[1] 0 575 575
Condition33%2/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
57500
@r >= 2 and defined $r[0]
!ll&&!rl&&r
05750
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table25%1/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
1128 if @r >= 2 && defined($r[0]) && defined($r[1]);
1129 575 $self->{_a} = $_[0];
1130 575 $self->{_p} = $_[1];
1131 } elsif (!$selfref) {
1132 1235 $self->{_a} = $class -> accuracy();
1133 1235 $self->{_p} = $class -> precision();
1134 }
1135  
1136 2083 return $self;
1137 }
1138  
1139 sub bnan {
1140 # create/assign a 'NaN'
1141  
1142 # Class::method(...) -> Class->method(...)
1143 2246 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bnan: called CC=16 SCAR=29.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 2246 2246
Condition66%6/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
0492197
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
101412320
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
123201014
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1144 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1145 {
1146 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1147 # " use is as a method instead";
1148 0 unshift @_, __PACKAGE__;
1149 }
1150  
1151 2246 my $self = shift;
1152 2246 my $selfref = ref($self);
1153 2246 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
101412320
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1154  
1155 {
1156 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1157 2246 ▶ if (${"${class}::_trap_nan"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_nan";}) 4 2242 2246
1158 4 croak("Tried to create NaN in $class->bnan()");
1159 }
1160 }
1161  
1162 2242 ▶ $self->import() if $IMPORT == 0; # make require work
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 2242 2242
1163  
1164 # Don't modify constant (read-only) objects.
1165  
1166 2242 ▶ return $self if $selfref && $self->modify('bnan');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bnan") 0 2242 2242
Condition66%2/3
$selfref and $self->modify("bnan")
!ll&&!rl&&r
123010120
MC/DC0%0/2
$selfref and $self->modify("bnan")
$selfref $self->modify("bnan")
Truth table66%2/3
A and B
A= $selfrefB= $self->modify("bnan")
ABresult
0X0
100
111
1167  
1168 # Get the rounding parameters, if any.
1169  
1170 2242 my @r = @_;
1171  
1172 2242 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1230 1012 2242
1173  
1174 2242 $self -> {sign} = $nan;
1175 2242 $self -> {value} = $LIB -> _zero();
1176  
1177 # If rounding parameters are given as arguments, use them. If no rounding
1178 # parameters are given, and if called as a class method, initialize the new
1179 # instance with the class variables.
1180  
1181 2242 ▶ if (@r) {
Branch: 2 branches
BranchTrueFalseTotal
if (@r) { } 541 1701 2242
elsif (not $selfref) { } 900 801 1701
1182 541 ▶ croak "can't specify both accuracy and precision"
Branch: 1 branch
BranchTrueFalseTotal
if @r >= 2 and defined $r[0] and defined $r[1] 0 541 541
Condition50%3/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
54100
@r >= 2 and defined $r[0]
!ll&&!rl&&r
25390
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table50%2/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
1183 if @r >= 2 && defined($r[0]) && defined($r[1]);
1184 541 $self->{_a} = $_[0];
1185 541 $self->{_p} = $_[1];
1186 } elsif (!$selfref) {
1187 900 $self->{_a} = $class -> accuracy();
1188 900 $self->{_p} = $class -> precision();
1189 }
1190  
1191 2242 return $self;
1192 }
1193  
1194 sub bpi {
1195  
1196 # Class::method(...) -> Class->method(...)
1197 9 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bpi: called CC=9 SCAR=27.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 9 9
Condition33%3/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
009
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
090
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
900
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table16%1/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1198 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1199 {
1200 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1201 # " use is as a method instead";
1202 0 unshift @_, __PACKAGE__;
1203 }
1204  
1205 # Called as Argument list
1206 # --------- -------------
1207 # Math::BigFloat->bpi() ("Math::BigFloat")
1208 # Math::BigFloat->bpi(10) ("Math::BigFloat", 10)
1209 # $x->bpi() ($x)
1210 # $x->bpi(10) ($x, 10)
1211 # Math::BigFloat::bpi() ()
1212 # Math::BigFloat::bpi(10) (10)
1213 #
1214 # In ambiguous cases, we favour the OO-style, so the following case
1215 #
1216 # $n = Math::BigFloat->new("10");
1217 # $x = Math::BigFloat->bpi($n);
1218 #
1219 # which gives an argument list with the single element $n, is resolved as
1220 #
1221 # $n->bpi();
1222  
1223 9 my $self = shift;
1224 9 my $selfref = ref $self;
1225 9 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
090
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1226 9 my @r = @_; # rounding paramters
1227  
1228 9 ▶ if ($selfref) { # bpi() called as an instance method
Branch: 1 branch
BranchTrueFalseTotal
if ($selfref) { } 0 9 9
1229 0 ▶ return $self if $self -> modify('bpi');
Branch: 1 branch
BranchTrueFalseTotal
if $self->modify("bpi") 0 0 0
1230 } else { # bpi() called as a class method
1231 9 $self = bless {}, $class; # initialize new instance
1232 }
1233  
1234 9 ▶ return $upgrade -> bpi(@r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 9 9
1235  
1236 # hard-wired to "3"
1237 9 $self -> {sign} = '+';
1238 9 $self -> {value} = $LIB -> _new("3");
1239 9 $self = $self -> round(@r);
1240 9 return $self;
1241 }
1242  
1243 sub copy {
1244 4824 ▶ my ($x, $class);
Subroutine copy: called CC=5 SCAR=16.4
Pod documented
1245 4824 ▶ if (ref($_[0])) { # $y = $x -> copy()
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 4824 0 4824
1246 4824 $x = shift;
1247 4824 $class = ref($x);
1248 } else { # $y = Math::BigInt -> copy($y)
1249 0 $class = shift;
1250 0 $x = shift;
1251 }
1252  
1253 4824 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4824 4824
1254  
1255 4824 my $copy = bless {}, $class;
1256  
1257 4824 $copy->{sign} = $x->{sign};
1258 4824 $copy->{value} = $LIB->_copy($x->{value});
1259 4824 ▶ $copy->{_a} = $x->{_a} if exists $x->{_a};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{"_a"} 1302 3522 4824
1260 4824 ▶ $copy->{_p} = $x->{_p} if exists $x->{_p};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{"_p"} 1288 3536 4824
1261  
1262 4824 return $copy;
1263 }
1264  
1265 sub as_int {
1266 3 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_int: called CC=4 SCAR=21.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 0 3
1267 3 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 3 3
1268  
1269 # If called as an instance method, and the instance class is something we
1270 # upgrade to, $x might not be a Math::BigInt, so don't just call copy().
1271  
1272 3 ▶ return $x -> copy() if $x -> isa("Math::BigInt");
Branch: 1 branch
BranchTrueFalseTotal
if $x->isa("Math::BigInt") 3 0 3
1273  
1274 # disable upgrading and downgrading
1275  
1276 0 my $upg = Math::BigInt -> upgrade();
1277 0 my $dng = Math::BigInt -> downgrade();
1278 0 Math::BigInt -> upgrade(undef);
1279 0 Math::BigInt -> downgrade(undef);
1280  
1281 0 my $y = Math::BigInt -> new($x);
1282  
1283 # reset upgrading and downgrading
1284  
1285 0 Math::BigInt -> upgrade($upg);
1286 0 Math::BigInt -> downgrade($dng);
1287  
1288 0 return $y;
1289 }
1290  
1291 sub as_float {
1292 343 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_float: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 343 0 343
1293 343 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 343 343
1294  
1295 # disable upgrading and downgrading
1296  
1297 343 require Math::BigFloat;
1298 343 my $upg = Math::BigFloat -> upgrade();
1299 343 my $dng = Math::BigFloat -> downgrade();
1300 343 Math::BigFloat -> upgrade(undef);
1301 343 Math::BigFloat -> downgrade(undef);
1302  
1303 343 my $y = Math::BigFloat -> new($x);
1304  
1305 # reset upgrading and downgrading
1306  
1307 343 Math::BigFloat -> upgrade($upg);
1308 343 Math::BigFloat -> downgrade($dng);
1309  
1310 343 return $y;
1311 }
1312  
1313 sub as_rat {
1314 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_rat: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
1315 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
1316  
1317 # disable upgrading and downgrading
1318  
1319 0 require Math::BigRat;
1320 0 my $upg = Math::BigRat -> upgrade();
1321 0 my $dng = Math::BigRat -> downgrade();
1322 0 Math::BigRat -> upgrade(undef);
1323 0 Math::BigRat -> downgrade(undef);
1324  
1325 0 my $y = Math::BigRat -> new($x);
1326  
1327 # reset upgrading and downgrading
1328  
1329 0 Math::BigRat -> upgrade($upg);
1330 0 Math::BigRat -> downgrade($dng);
1331  
1332 0 return $y;
1333 }
1334  
1335 ###############################################################################
1336 # Boolean methods
1337 ###############################################################################
1338  
1339 sub is_zero {
1340 # return true if arg (BINT or num_str) is zero (array '+', '0')
1341 32532 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_zero: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 32528 4 32532
1342  
1343 32532 ▶ return 0 if $x->{sign} !~ /^\+$/; # -, NaN & +-inf aren't
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^\+$/ 2807 29725 32532
1344 29725 $LIB->_is_zero($x->{value});
1345 }
1346  
1347 sub is_one {
1348 # return true if arg (BINT or num_str) is +1, or -1 if sign is given
1349 1731 ▶ my (undef, $x, $sign) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_one: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1727 4 1731
1350  
1351 1731 ▶ $sign = '+' if !defined($sign) || $sign ne '-';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $sign or $sign ne "-" 1127 604 1731
Condition100%3/3
not defined $sign or $sign ne "-"
l!l&&r!l&&!r
507620604
MC/DC100%2/2
not defined $sign or $sign ne "-"
not defined $sign $sign ne "-"
Truth table100%3/3
not(A) or B
A= not defined $signB= $sign ne "-"
ABresult
1X1
011
000
1352  
1353 1731 ▶ return 0 if $x->{sign} ne $sign; # -1 != +1, NaN, +-inf aren't either
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $sign 606 1125 1731
1354 1125 $LIB->_is_one($x->{value});
1355 }
1356  
1357 sub is_finite {
1358 364 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_finite: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 364 0 364
1359 364 ▶ return $x->{sign} eq '+' || $x->{sign} eq '-';
Condition100%3/3
$x->{'sign'} eq '+' || $x->{'sign'} eq '-'
l!l&&r!l&&!r
35248
MC/DC100%2/2
$x->{'sign'} eq '+' || $x->{'sign'} eq '-'
$x->{'sign'} eq '+' $x->{'sign'} eq '-'
Truth table100%3/3
A || B
A= $x->{'sign'} eq '+'B= $x->{'sign'} eq '-'
ABresult
1X1
011
000
1360 }
1361  
1362 sub is_inf {
1363 # return true if arg (BINT or num_str) is +-inf
1364 38693 ▶ my (undef, $x, $sign) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_inf: called CC=7 SCAR=19.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 38685 8 38693
1365  
1366 38693 ▶ if (defined $sign) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sign) 7080 31613 38693
1367 7080 ▶ $sign = '[+-]inf' if $sign eq ''; # +- doesn't matter, only that's inf
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "" 64 7016 7080
1368 7080 ▶ $sign = "[$1]inf" if $sign =~ /^([+-])(inf)?$/; # extract '+' or '-'
Branch: 1 branch
BranchTrueFalseTotal
if $sign =~ /^([+-])(inf)?$/ 7016 64 7080
1369 7080 ▶ return $x->{sign} =~ /^$sign$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^$sign$/ ? : 528 6552 7080
1370 }
1371 31613 ▶ $x->{sign} =~ /^[+-]inf$/ ? 1 : 0; # only +-inf is infinity
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^[+-]inf$/ ? : 926 30687 31613
1372 }
1373  
1374 sub is_nan {
1375 # return true if arg (BINT or num_str) is NaN
1376 48850 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_nan: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 48842 8 48850
1377  
1378 48850 ▶ $x->{sign} eq $nan ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $nan ? : 639 48211 48850
1379 }
1380  
1381 sub is_positive {
1382 # return true when arg (BINT or num_str) is positive (> 0)
1383 454 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_positive: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 446 8 454
1384  
1385 454 ▶ return 1 if $x->{sign} eq '+inf'; # +inf is positive
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 15 439 454
1386  
1387 # 0+ is neither positive nor negative
1388 439 ▶ ($x->{sign} eq '+' && !$x->is_zero()) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '+' && !$x->is_zero ? : 364 75 439
Condition100%3/3
$x->{"sign"} eq "+" and not $x->is_zero
!ll&&!rl&&r
6015364
MC/DC100%2/2
$x->{"sign"} eq "+" and not $x->is_zero
$x->{"sign"} eq "+" not $x->is_zero
Truth table100%3/3
A and not(B)
A= $x->{"sign"} eq "+"B= not $x->is_zero
ABresult
0X0
100
111
1389 }
1390  
1391 sub is_negative {
1392 # return true when arg (BINT or num_str) is negative (< 0)
1393 2826 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_negative: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2818 8 2826
1394  
1395 2826 ▶ $x->{sign} =~ /^-/ ? 1 : 0; # -inf is negative, but NaN is not
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^-/ ? : 616 2210 2826
1396 }
1397  
1398 sub is_non_negative {
1399 # Return true if argument is non-negative (>= 0).
1400 64 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_non_negative: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 56 8 64
1401  
1402 64 ▶ return 1 if $x->{sign} =~ /^\+/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^\+/ 32 32 64
1403 32 ▶ return 1 if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 32 32
1404 32 return 0;
1405 }
1406  
1407 sub is_non_positive {
1408 # Return true if argument is non-positive (<= 0).
1409 64 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_non_positive: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 56 8 64
1410  
1411 64 ▶ return 1 if $x->{sign} =~ /^\-/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^\-/ 24 40 64
1412 40 ▶ return 1 if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 32 40
1413 32 return 0;
1414 }
1415  
1416 sub is_odd {
1417 # return true when arg (BINT or num_str) is odd, false for even
1418 185 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_odd: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 185 0 185
1419  
1420 185 ▶ return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 7 178 185
1421 178 $LIB->_is_odd($x->{value});
1422 }
1423  
1424 sub is_even {
1425 # return true when arg (BINT or num_str) is even, false for odd
1426 44 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_even: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 44 0 44
1427  
1428 44 ▶ return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 4 40 44
1429 40 $LIB->_is_even($x->{value});
1430 }
1431  
1432 sub is_int {
1433 # return true when arg (BINT or num_str) is an integer
1434 46 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_int: called CC=3 SCAR=11.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 46 0 46
1435  
1436 46 ▶ $x->{sign} =~ /^[+-]$/ ? 1 : 0; # inf/-inf/NaN aren't
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^[+-]$/ ? : 30 16 46
1437 }
1438  
1439 ###############################################################################
1440 # Comparison methods
1441 ###############################################################################
1442  
1443 sub bcmp {
1444 # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
1445 # (BINT or num_str, BINT or num_str) return cond_code
1446  
1447 # set up parameters
1448 2401 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bcmp: called CC=21 SCAR=30.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 2001 400 2401
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
04002001
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1449 ? (ref($_[0]), @_)
1450 : objectify(2, @_);
1451  
1452 2401 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 2401 2401
1453  
1454 2401 ▶ return $upgrade->bcmp($x, $y)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 1 2400 2401
Condition83%5/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
22501501
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
01150
MC/DC66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table75%3/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
1455 if defined($upgrade) && (!$x->isa(__PACKAGE__) || !$y->isa(__PACKAGE__));
1456  
1457 2400 ▶ if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/) 320 2080 2400
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
280402080
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
$x->{"sign"} =~ /^[+-]$/ $y->{"sign"} =~ /^[+-]$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $y->{"sign"} =~ /^[+-]$/
ABresult
0X0
100
111
1458 # handle +-inf and NaN
1459 320 ▶ return if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 64 256 320
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
3529256
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
1460 256 ▶ return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $y->{"sign"} and $x->{"sign"} =~ /^[+-]inf$/ 27 229 256
Condition66%2/3
$x->{"sign"} eq $y->{"sign"} and $x->{"sign"} =~ /^[+-]inf$/
!ll&&!rl&&r
229027
MC/DC50%1/2
$x->{"sign"} eq $y->{"sign"} and $x->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} eq $y->{"sign"} $x->{"sign"} =~ /^[+-]inf$/
Truth table66%2/3
A and B
A= $x->{"sign"} eq $y->{"sign"}B= $x->{"sign"} =~ /^[+-]inf$/
ABresult
0X0
100
111
1461 229 ▶ return +1 if $x->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 96 133 229
1462 133 ▶ return -1 if $x->{sign} eq '-inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-inf" 114 19 133
1463 19 ▶ return -1 if $y->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} eq "+inf" 8 11 19
1464 11 return +1;
1465 }
1466  
1467 # check sign for speed first
1468 2080 ▶ return 1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # does also 0 <=> -y
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+" and $y->{"sign"} eq "-" 262 1818 2080
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "-"
!ll&&!rl&&r
2841534262
MC/DC100%2/2
$x->{"sign"} eq "+" and $y->{"sign"} eq "-"
$x->{"sign"} eq "+" $y->{"sign"} eq "-"
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $y->{"sign"} eq "-"
ABresult
0X0
100
111
1469 1818 ▶ return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # does also -x <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" and $y->{"sign"} eq "+" 194 1624 1818
Condition100%3/3
$x->{"sign"} eq "-" and $y->{"sign"} eq "+"
!ll&&!rl&&r
153490194
MC/DC100%2/2
$x->{"sign"} eq "-" and $y->{"sign"} eq "+"
$x->{"sign"} eq "-" $y->{"sign"} eq "+"
Truth table100%3/3
A and B
A= $x->{"sign"} eq "-"B= $y->{"sign"} eq "+"
ABresult
0X0
100
111
1470  
1471 # have same sign, so compare absolute values. Don't make tests for zero
1472 # here because it's actually slower than testing in Calc (especially w/ Pari
1473 # et al)
1474  
1475 # post-normalized compare for internal use (honors signs)
1476 1624 ▶ if ($x->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} eq "+") 1534 90 1624
1477 # $x and $y both > 0
1478 1534 return $LIB->_acmp($x->{value}, $y->{value});
1479 }
1480  
1481 # $x && $y both < 0
1482 90 $LIB->_acmp($y->{value}, $x->{value}); # swapped acmp (lib returns 0, 1, -1)
1483 }
1484  
1485 sub bacmp {
1486 # Compares 2 values, ignoring their signs.
1487 # Returns one of undef, <0, =0, >0. (suitable for sort)
1488 # (BINT, BINT) return cond_code
1489  
1490 # set up parameters
1491 238 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bacmp: called CC=15 SCAR=28.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 238 0 238
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00238
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1492 ? (ref($_[0]), @_)
1493 : objectify(2, @_);
1494  
1495 238 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 238 238
1496  
1497 238 ▶ return $upgrade->bacmp($x, $y)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 0 238 238
Condition50%3/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
212260
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
0026
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table50%2/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
1498 if defined($upgrade) && (!$x->isa(__PACKAGE__) || !$y->isa(__PACKAGE__));
1499  
1500 238 ▶ if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/) 72 166 238
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
5616166
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
$x->{"sign"} =~ /^[+-]$/ $y->{"sign"} =~ /^[+-]$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $y->{"sign"} =~ /^[+-]$/
ABresult
0X0
100
111
1501 # handle +-inf and NaN
1502 72 ▶ return if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 28 44 72
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
161244
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
1503 44 ▶ return 0 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/ 16 28 44
Condition100%3/3
$x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/
!ll&&!rl&&r
121616
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} =~ /^[+-]inf$/ $y->{"sign"} =~ /^[+-]inf$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]inf$/B= $y->{"sign"} =~ /^[+-]inf$/
ABresult
0X0
100
111
1504 28 ▶ return 1 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} !~ /^[+-]inf$/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^[+-]inf$/ and not $y->{"sign"} =~ /^[+-]inf$/ 16 12 28
Condition66%2/3
$x->{"sign"} =~ /^[+-]inf$/ and not $y->{"sign"} =~ /^[+-]inf$/
!ll&&!rl&&r
12016
MC/DC50%1/2
$x->{"sign"} =~ /^[+-]inf$/ and not $y->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} =~ /^[+-]inf$/ not $y->{"sign"} =~ /^[+-]inf$/
Truth table66%2/3
A and not(B)
A= $x->{"sign"} =~ /^[+-]inf$/B= not $y->{"sign"} =~ /^[+-]inf$/
ABresult
0X0
100
111
1505 12 return -1;
1506 }
1507 166 $LIB->_acmp($x->{value}, $y->{value}); # lib does only 0, 1, -1
1508 }
1509  
1510 sub beq {
1511 427 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine beq: called CC=5 SCAR=16.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 157 270 427
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0270157
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1512 ? (undef, @_)
1513 : objectify(2, @_);
1514  
1515 427 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 427 427
1516  
1517 427 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
1518 427 ▶ return defined($cmp) && !$cmp;
Condition100%3/3
defined $cmp && !$cmp
!ll&&!rl&&r
18277132
MC/DC100%2/2
defined $cmp && !$cmp
defined $cmp !$cmp
Truth table100%3/3
A && not(B)
A= defined $cmpB= !$cmp
ABresult
0X0
100
111
1519 }
1520  
1521 sub bne {
1522 18 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bne: called CC=6 SCAR=22.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 18 0 18
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0018
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1523 ? (undef, @_)
1524 : objectify(2, @_);
1525  
1526 18 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 18 18
1527  
1528 18 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
1529 18 ▶ return defined($cmp) && !$cmp ? '' : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $cmp && !$cmp ? : 0 18 18
Condition33%1/3
defined $cmp and not $cmp
!ll&&!rl&&r
1800
MC/DC0%0/2
defined $cmp and not $cmp
defined $cmp not $cmp
Truth table33%1/3
A and not(B)
A= defined $cmpB= not $cmp
ABresult
0X0
100
111
1530 }
1531  
1532 sub blt {
1533 619 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine blt: called CC=5 SCAR=16.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 5 614 619
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
06145
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1534 ? (undef, @_)
1535 : objectify(2, @_);
1536  
1537 619 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 619 619
1538  
1539 619 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
1540 619 ▶ return defined($cmp) && $cmp < 0;
Condition100%3/3
defined $cmp && $cmp < 0
!ll&&!rl&&r
453283
MC/DC100%2/2
defined $cmp && $cmp < 0
defined $cmp $cmp < 0
Truth table100%3/3
A && B
A= defined $cmpB= $cmp < 0
ABresult
0X0
100
111
1541 }
1542  
1543 sub ble {
1544 1478 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine ble: called CC=5 SCAR=16.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1207 271 1478
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
02711207
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1545 ? (undef, @_)
1546 : objectify(2, @_);
1547  
1548 1478 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 1478 1478
1549  
1550 1478 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
1551 1478 ▶ return defined($cmp) && $cmp <= 0;
Condition100%3/3
defined $cmp && $cmp <= 0
!ll&&!rl&&r
131205260
MC/DC100%2/2
defined $cmp && $cmp <= 0
defined $cmp $cmp <= 0
Truth table100%3/3
A && B
A= defined $cmpB= $cmp <= 0
ABresult
0X0
100
111
1552 }
1553  
1554 sub bgt {
1555 1385 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bgt: called CC=5 SCAR=16.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1072 313 1385
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
03131072
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1556 ? (undef, @_)
1557 : objectify(2, @_);
1558  
1559 1385 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 1385 1385
1560  
1561 1385 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
1562 1385 ▶ return defined($cmp) && $cmp > 0;
Condition66%2/3
defined $cmp && $cmp > 0
!ll&&!rl&&r
0583802
MC/DC50%1/2
defined $cmp && $cmp > 0
defined $cmp $cmp > 0
Truth table66%2/3
A && B
A= defined $cmpB= $cmp > 0
ABresult
0X0
100
111
1563 }
1564  
1565 sub bge {
1566 317 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bge: called CC=5 SCAR=16.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 150 167 317
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0167150
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1567 ? (undef, @_)
1568 : objectify(2, @_);
1569  
1570 317 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 317 317
1571  
1572 317 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
1573 317 ▶ return defined($cmp) && $cmp >= 0;
Condition100%3/3
defined $cmp && $cmp >= 0
!ll&&!rl&&r
1363241
MC/DC100%2/2
defined $cmp && $cmp >= 0
defined $cmp $cmp >= 0
Truth table100%3/3
A && B
A= defined $cmpB= $cmp >= 0
ABresult
0X0
100
111
1574 }
1575  
1576 ###############################################################################
1577 # Arithmetic methods
1578 ###############################################################################
1579  
1580 sub bneg {
1581 # (BINT or num_str) return BINT
1582 # negate number or make a negated number from string
1583 486 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bneg: called CC=7 SCAR=20.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 486 0 486
1584  
1585 486 ▶ return $x if $x->modify('bneg');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bneg") 0 486 486
1586  
1587 486 ▶ return $upgrade -> bneg($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 486 486
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
458280
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
1588 if defined($upgrade) && !$x->isa(__PACKAGE__);
1589  
1590 # Don't negate +0 so we always have the normalized form +0. Does nothing for
1591 # 'NaN'.
1592 $x->{sign} =~ tr/+-/-+/
1593 486 ▶ unless $x->{sign} eq '+' && $LIB->_is_zero($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+" and $LIB->_is_zero($x->{"value"}) 478 8 486
Condition100%3/3
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"value"})
!ll&&!rl&&r
983808
MC/DC100%2/2
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"value"})
$x->{"sign"} eq "+" $LIB->_is_zero($x->{"value"})
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $LIB->_is_zero($x->{"value"})
ABresult
0X0
100
111
1594  
1595 486 $x -> round(@r);
1596 }
1597  
1598 sub babs {
1599 # (BINT or num_str) return BINT
1600 # make number absolute, or return absolute BINT from string
1601 321 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine babs: called CC=6 SCAR=19.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 313 8 321
1602  
1603 321 ▶ return $x if $x->modify('babs');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("babs") 0 321 321
1604  
1605 321 ▶ return $upgrade -> babs($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) and not $x->isa($Math::BigInt::upgrade) 0 321 321
Condition50%3/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) and not $x->isa($Math::BigInt::upgrade)
!ll&&!rl&&r
32100
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
296250
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) and not $x->isa($Math::BigInt::upgrade)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__) not $x->isa($Math::BigInt::upgrade)
Truth table50%2/4
A and not(B) and not(C)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)C= not $x->isa($Math::BigInt::upgrade)
ABCresult
0XX0
10X0
1100
1111
1606 if defined($upgrade) && !$x->isa(__PACKAGE__) && !$x -> isa($upgrade);
1607  
1608 321 $x->{sign} =~ s/^-/+/;
1609  
1610 321 $x -> round(@r);
1611 }
1612  
1613 sub bsgn {
1614 # Signum function.
1615 18 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bsgn: called CC=8 SCAR=23.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 18 0 18
1616  
1617 18 ▶ return $x if $x->modify('bsgn');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsgn") 0 18 18
1618  
1619 18 ▶ return $upgrade -> bsgn($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) and not $x->isa($Math::BigInt::upgrade) 0 18 18
Condition33%2/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) and not $x->isa($Math::BigInt::upgrade)
!ll&&!rl&&r
1800
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
1800
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) and not $x->isa($Math::BigInt::upgrade)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__) not $x->isa($Math::BigInt::upgrade)
Truth table25%1/4
A and not(B) and not(C)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)C= not $x->isa($Math::BigInt::upgrade)
ABCresult
0XX0
10X0
1100
1111
1620 if defined($upgrade) && !$x->isa(__PACKAGE__) && !$x -> isa($upgrade);
1621  
1622 18 ▶ return $x -> bone("+", @r) if $x -> is_pos();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_pos 6 12 18
1623 12 ▶ return $x -> bone("-", @r) if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 6 6 12
1624  
1625 6 $x -> round(@r);
1626 }
1627  
1628 sub bnorm {
1629 # (numstr or BINT) return BINT
1630 # Normalize number -- no-op here
1631 792 ▶ my ($class, $x, @r) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine bnorm: called CC=3 SCAR=11.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 792 792
1632  
1633 # This method is called from the rounding methods, so if this method
1634 # supports rounding by calling the rounding methods, we get an infinite
1635 # recursion.
1636  
1637 792 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 792 792
1638  
1639 792 $x;
1640 }
1641  
1642 sub binc {
1643 # increment arg by one
1644 191 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine binc: called CC=10 SCAR=23.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 191 0 191
1645  
1646 191 ▶ return $x if $x->modify('binc');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("binc") 0 191 191
1647  
1648 191 ▶ return $x->round(@r) if $x -> is_inf() || $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf or $x->is_nan 16 175 191
Condition100%3/3
$x->is_inf or $x->is_nan
l!l&&r!l&&!r
88175
MC/DC100%2/2
$x->is_inf or $x->is_nan
$x->is_inf $x->is_nan
Truth table100%3/3
A or B
A= $x->is_infB= $x->is_nan
ABresult
1X1
011
000
1649  
1650 175 ▶ return $upgrade -> binc($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 175 175
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
16690
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
1651 if defined($upgrade) && !$x -> isa(__PACKAGE__);
1652  
1653 175 ▶ if ($x->{sign} eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '+') { } 101 74 175
elsif ($x->{'sign'} eq '-') { } 74 0 74
1654 101 $x->{value} = $LIB->_inc($x->{value});
1655 } elsif ($x->{sign} eq '-') {
1656 74 $x->{value} = $LIB->_dec($x->{value});
1657 74 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{value}); # -1 +1 => -0 => +0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 8 66 74
1658 }
1659  
1660 175 return $x->round(@r);
1661 }
1662  
1663 sub bdec {
1664 # decrement arg by one
1665 31 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bdec: called CC=10 SCAR=23.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 31 0 31
1666  
1667 31 ▶ return $x if $x->modify('bdec');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdec") 0 31 31
1668  
1669 31 ▶ return $x->round(@r) if $x -> is_inf() || $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf or $x->is_nan 12 19 31
Condition100%3/3
$x->is_inf or $x->is_nan
l!l&&r!l&&!r
8419
MC/DC100%2/2
$x->is_inf or $x->is_nan
$x->is_inf $x->is_nan
Truth table100%3/3
A or B
A= $x->is_infB= $x->is_nan
ABresult
1X1
011
000
1670  
1671 19 ▶ return $upgrade -> bdec($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 19 19
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
1540
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
1672 if defined($upgrade) && !$x -> isa(__PACKAGE__);;
1673  
1674 19 ▶ if ($x->{sign} eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '-') { } 4 15 19
elsif ($x->{'sign'} eq '+') { } 15 0 15
1675 4 $x->{value} = $LIB->_inc($x->{value});
1676 } elsif ($x->{sign} eq '+') {
1677 15 ▶ if ($LIB->_is_zero($x->{value})) { # +1 - 1 => +0
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{'value'})) { } 4 11 15
1678 4 $x->{value} = $LIB->_one();
1679 4 $x->{sign} = '-';
1680 } else {
1681 11 $x->{value} = $LIB->_dec($x->{value});
1682 }
1683 }
1684  
1685 19 return $x->round(@r);
1686 }
1687  
1688 #sub bstrcmp {
1689 # my $self = shift;
1690 # my $selfref = ref $self;
1691 # my $class = $selfref || $self;
1692 #
1693 # croak 'bstrcmp() is an instance method, not a class method'
1694 # unless $selfref;
1695 # croak 'Wrong number of arguments for bstrcmp()' unless @_ == 1;
1696 #
1697 # return $self -> bstr() CORE::cmp shift;
1698 #}
1699 #
1700 #sub bstreq {
1701 # my $self = shift;
1702 # my $selfref = ref $self;
1703 # my $class = $selfref || $self;
1704 #
1705 # croak 'bstreq() is an instance method, not a class method'
1706 # unless $selfref;
1707 # croak 'Wrong number of arguments for bstreq()' unless @_ == 1;
1708 #
1709 # my $cmp = $self -> bstrcmp(shift);
1710 # return defined($cmp) && ! $cmp;
1711 #}
1712 #
1713 #sub bstrne {
1714 # my $self = shift;
1715 # my $selfref = ref $self;
1716 # my $class = $selfref || $self;
1717 #
1718 # croak 'bstrne() is an instance method, not a class method'
1719 # unless $selfref;
1720 # croak 'Wrong number of arguments for bstrne()' unless @_ == 1;
1721 #
1722 # my $cmp = $self -> bstrcmp(shift);
1723 # return defined($cmp) && ! $cmp ? '' : 1;
1724 #}
1725 #
1726 #sub bstrlt {
1727 # my $self = shift;
1728 # my $selfref = ref $self;
1729 # my $class = $selfref || $self;
1730 #
1731 # croak 'bstrlt() is an instance method, not a class method'
1732 # unless $selfref;
1733 # croak 'Wrong number of arguments for bstrlt()' unless @_ == 1;
1734 #
1735 # my $cmp = $self -> bstrcmp(shift);
1736 # return defined($cmp) && $cmp < 0;
1737 #}
1738 #
1739 #sub bstrle {
1740 # my $self = shift;
1741 # my $selfref = ref $self;
1742 # my $class = $selfref || $self;
1743 #
1744 # croak 'bstrle() is an instance method, not a class method'
1745 # unless $selfref;
1746 # croak 'Wrong number of arguments for bstrle()' unless @_ == 1;
1747 #
1748 # my $cmp = $self -> bstrcmp(shift);
1749 # return defined($cmp) && $cmp <= 0;
1750 #}
1751 #
1752 #sub bstrgt {
1753 # my $self = shift;
1754 # my $selfref = ref $self;
1755 # my $class = $selfref || $self;
1756 #
1757 # croak 'bstrgt() is an instance method, not a class method'
1758 # unless $selfref;
1759 # croak 'Wrong number of arguments for bstrgt()' unless @_ == 1;
1760 #
1761 # my $cmp = $self -> bstrcmp(shift);
1762 # return defined($cmp) && $cmp > 0;
1763 #}
1764 #
1765 #sub bstrge {
1766 # my $self = shift;
1767 # my $selfref = ref $self;
1768 # my $class = $selfref || $self;
1769 #
1770 # croak 'bstrge() is an instance method, not a class method'
1771 # unless $selfref;
1772 # croak 'Wrong number of arguments for bstrge()' unless @_ == 1;
1773 #
1774 # my $cmp = $self -> bstrcmp(shift);
1775 # return defined($cmp) && $cmp >= 0;
1776 #}
1777  
1778 sub badd {
1779 # add second arg (BINT or string) to first (BINT) (modifies first)
1780 # return result as BINT
1781  
1782 # set up parameters
1783 1818 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine badd: called CC=15 SCAR=27.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1705 113 1818
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
15981705
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1784 ? (ref($_[0]), @_)
1785 : objectify(2, @_);
1786  
1787 1818 ▶ return $x if $x->modify('badd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("badd") 0 1818 1818
1788  
1789 1818 $r[3] = $y; # no push!
1790  
1791 1818 ▶ return $upgrade->badd($x, $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 2 1816 1818
Condition83%5/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
1726902
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
0290
MC/DC66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table75%3/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
1792 if defined($upgrade) && (!$x->isa(__PACKAGE__) || !$y->isa(__PACKAGE__));
1793  
1794 # Inf and NaN handling
1795 1816 ▶ if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/) 197 1619 1816
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
138591619
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
$x->{"sign"} =~ /^[+-]$/ $y->{"sign"} =~ /^[+-]$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $y->{"sign"} =~ /^[+-]$/
ABresult
0X0
100
111
1796 # NaN first
1797 197 ▶ return $x->bnan(@r) if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 88 109 197
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
4840109
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
1798 # Inf handling
1799 109 ▶ if (($x->{sign} =~ /^[+-]inf$/) && ($y->{sign} =~ /^[+-]inf$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/) 54 55 109
Condition100%3/3
$x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/
!ll&&!rl&&r
352054
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} =~ /^[+-]inf$/ $y->{"sign"} =~ /^[+-]inf$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]inf$/B= $y->{"sign"} =~ /^[+-]inf$/
ABresult
0X0
100
111
1800 # +Inf + +Inf or -Inf + -Inf => same, rest is NaN
1801 54 ▶ return $x->round(@r) if $x->{sign} eq $y->{sign};
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $y->{"sign"} 30 24 54
1802 24 return $x->bnan(@r);
1803 }
1804 # ±Inf + something => ±Inf
1805 # something + ±Inf => ±Inf
1806 55 ▶ if ($y->{sign} =~ /^[+-]inf$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{"sign"} =~ /^[+-]inf$/) 35 20 55
1807 35 $x->{sign} = $y->{sign};
1808 }
1809 55 return $x -> round(@r);
1810 }
1811  
1812 ($x->{value}, $x->{sign})
1813 1619 = $LIB -> _sadd($x->{value}, $x->{sign}, $y->{value}, $y->{sign});
1814 1619 $x->round(@r);
1815 }
1816  
1817 sub bsub {
1818 # (BINT or num_str, BINT or num_str) return BINT
1819 # subtract second arg from first, modify first
1820  
1821 # set up parameters
1822 1116 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bsub: called CC=10 SCAR=23.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1062 54 1116
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
12421062
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1823 ? (ref($_[0]), @_)
1824 : objectify(2, @_);
1825  
1826 1116 ▶ return $x if $x -> modify('bsub');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsub") 0 1116 1116
1827  
1828 1116 ▶ return $upgrade -> bsub($x, $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 0 1116 1116
Condition50%3/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
1072440
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
0044
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table50%2/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
1829 if defined($upgrade) && (!$x->isa(__PACKAGE__) || !$y->isa(__PACKAGE__));
1830  
1831 1116 ▶ return $x -> round(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 33 1083 1116
1832  
1833 # To correctly handle the lone special case $x -> bsub($x), we note the
1834 # sign of $x, then flip the sign from $y, and if the sign of $x did change,
1835 # too, then we caught the special case:
1836  
1837 1083 my $xsign = $x -> {sign};
1838 1083 $y -> {sign} =~ tr/+-/-+/; # does nothing for NaN
1839 1083 ▶ if ($xsign ne $x -> {sign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsign ne $x->{"sign"}) 12 1071 1083
1840 # special case of $x -> bsub($x) results in 0
1841 12 ▶ return $x -> bzero(@r) if $xsign =~ /^[+-]$/;
Branch: 1 branch
BranchTrueFalseTotal
if $xsign =~ /^[+-]$/ 6 6 12
1842 6 return $x -> bnan(@r); # NaN, -inf, +inf
1843 }
1844  
1845 1071 $x = $x -> badd($y, @r); # badd() does not leave internal zeros
1846 1071 $y -> {sign} =~ tr/+-/-+/; # refix $y (does nothing for NaN)
1847 1071 $x; # already rounded by badd() or no rounding
1848 }
1849  
1850 sub bmul {
1851 # multiply the first number by the second number
1852 # (BINT or num_str, BINT or num_str) return BINT
1853  
1854 # set up parameters
1855 1644 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bmul: called CC=19 SCAR=29.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1608 36 1644
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
12241608
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1856 ? (ref($_[0]), @_)
1857 : objectify(2, @_);
1858  
1859 1644 ▶ return $x if $x->modify('bmul');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmul") 0 1644 1644
1860  
1861 1644 ▶ return $x->bnan(@r) if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 52 1592 1644
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
28241592
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
1862  
1863 # inf handling
1864 1592 ▶ if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/) 52 1540 1592
Condition100%3/3
$x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/
l!l&&r!l&&!r
36161540
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} =~ /^[+-]inf$/ $y->{"sign"} =~ /^[+-]inf$/
Truth table100%3/3
A or B
A= $x->{"sign"} =~ /^[+-]inf$/B= $y->{"sign"} =~ /^[+-]inf$/
ABresult
1X1
011
000
1865 52 ▶ return $x->bnan(@r) if $x->is_zero() || $y->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $y->is_zero 12 40 52
Condition100%3/3
$x->is_zero or $y->is_zero
l!l&&r!l&&!r
8440
MC/DC100%2/2
$x->is_zero or $y->is_zero
$x->is_zero $y->is_zero
Truth table100%3/3
A or B
A= $x->is_zeroB= $y->is_zero
ABresult
1X1
011
000
1866 # result will always be +-inf:
1867 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
1868 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
1869 40 ▶ return $x->binf(@r) if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/ 10 30 40
Condition100%3/3
$x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/
!ll&&!rl&&r
201010
MC/DC100%2/2
$x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/
$x->{"sign"} =~ /^\+/ $y->{"sign"} =~ /^\+/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^\+/B= $y->{"sign"} =~ /^\+/
ABresult
0X0
100
111
1870 30 ▶ return $x->binf(@r) if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/ 10 20 30
Condition100%3/3
$x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/
!ll&&!rl&&r
101010
MC/DC100%2/2
$x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/
$x->{"sign"} =~ /^-/ $y->{"sign"} =~ /^-/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^-/B= $y->{"sign"} =~ /^-/
ABresult
0X0
100
111
1871 20 return $x->binf('-', @r);
1872 }
1873  
1874 1540 ▶ return $upgrade->bmul($x, $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 8 1532 1540
Condition83%5/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
1482508
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
0850
MC/DC66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table75%3/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
1875 if defined($upgrade) && (!$x->isa(__PACKAGE__) || !$y->isa(__PACKAGE__));
1876  
1877 1532 $r[3] = $y; # no push here
1878  
1879 1532 ▶ $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; # +1 * +1 or -1 * -1 => +
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 1467 65 1532
1880  
1881 1532 $x->{value} = $LIB->_mul($x->{value}, $y->{value}); # do actual math
1882 1532 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{value}); # no -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 312 1220 1532
1883  
1884 1532 $x->round(@r);
1885 }
1886  
1887 sub bmuladd {
1888 # multiply two numbers and then add the third to the result
1889 # (BINT or num_str, BINT or num_str, BINT or num_str) return BINT
1890  
1891 # set up parameters
1892 177 ▶ my ($class, $x, $y, $z, @r)
Subroutine bmuladd: called CC=47 SCAR=62.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 177 0 177
Condition33%2/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
00177
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00177
MC/DC0%0/3
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
ref $_[0] ref $_[0] eq ref $_[1] ref $_[1] eq ref $_[2]
Truth table25%1/4
A and B and C
A= ref $_[0]B= ref $_[0] eq ref $_[1]C= ref $_[1] eq ref $_[2]
ABCresult
0XX0
10X0
1100
1111
1893 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
1894 ? (ref($_[0]), @_)
1895 : objectify(3, @_);
1896  
1897 177 ▶ return $x if $x->modify('bmuladd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmuladd") 0 177 177
1898  
1899 # x, y, and z are finite numbers
1900  
1901 177 ▶ if ($x->{sign} =~ /^[+-]$/ &&
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/ and $z->{"sign"} =~ /^[+-]$/) 141 36 177
Condition100%6/6
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/ and $z->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
333141
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
303144
MC/DC100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/ and $z->{"sign"} =~ /^[+-]$/
$x->{"sign"} =~ /^[+-]$/ $y->{"sign"} =~ /^[+-]$/ $z->{"sign"} =~ /^[+-]$/
Truth table100%4/4
A and B and C
A= $x->{"sign"} =~ /^[+-]$/B= $y->{"sign"} =~ /^[+-]$/C= $z->{"sign"} =~ /^[+-]$/
ABCresult
0XX0
10X0
1100
1111
1902 $y->{sign} =~ /^[+-]$/ &&
1903 $z->{sign} =~ /^[+-]$/)
1904 {
1905 141 ▶ return $upgrade->bmuladd($x, $y, $z, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $z->isa(__PACKAGE__) 0 141 141
Condition11%1/9
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $z->isa(__PACKAGE__)
!ll&&!rl&&r
14100
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $z->isa(__PACKAGE__)
!ll&&!rl&&r
000
$x->isa(__PACKAGE__) and $y->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/4
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $z->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__) $z->isa(__PACKAGE__)
Truth table20%1/5
A and not(B and C && D)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)D= $z->isa(__PACKAGE__)
ABCDresult
0XXX0
11110
10XX1
110X1
11101
1906 if defined($upgrade) && (!$x->isa(__PACKAGE__) ||
1907 !$y->isa(__PACKAGE__) ||
1908 !$z->isa(__PACKAGE__));
1909  
1910 # TODO: what if $y and $z have A or P set?
1911 141 $r[3] = $z; # no push here
1912  
1913 141 my $zs = $z->{sign};
1914 141 my $zv = $z->{value};
1915 141 ▶ $zv = $LIB -> _copy($zv) if refaddr($x) eq refaddr($z);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($x) eq builtin::refaddr($z) 0 141 141
1916  
1917 141 ▶ $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-'; # +1 * +1 or -1 * -1 => +
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 111 30 141
1918 141 $x->{value} = $LIB->_mul($x->{value}, $y->{value}); # do actual math
1919 141 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{value}); # no -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 21 120 141
1920  
1921 ($x->{value}, $x->{sign})
1922 141 = $LIB -> _sadd($x->{value}, $x->{sign}, $zv, $zs);
1923 141 return $x->round(@r);
1924 }
1925  
1926 # At least one of x, y, and z is a NaN
1927  
1928 return $x->bnan(@r) if (($x->{sign} eq $nan) ||
1929 ($y->{sign} eq $nan) ||
1930 36 ▶ ($z->{sign} eq $nan));
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan or $z->{"sign"} eq $nan 12 12 24
Condition100%6/6
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan or $z->{"sign"} eq $nan
l!l&&r!l&&!r
21312
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
12915
MC/DC100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan or $z->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan $z->{"sign"} eq $nan
Truth table100%4/4
A or B or C
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nanC= $z->{"sign"} eq $nan
ABCresult
1XX1
01X1
0011
0000
1931  
1932 # At least one of x, y, and z is an Inf
1933  
1934 12 ▶ if ($x->{sign} eq "-inf") {
Branch: 5 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '-inf') { } 6 6 12
elsif ($x->{'sign'} eq '+inf') { } 6 0 6
elsif ($x->is_neg) { } 0 0 0
elsif ($x->is_zero) { } 0 0 0
elsif ($x->is_pos) { } 0 0 0
1935  
1936 6 ▶ if ($y -> is_neg()) { # x = -inf, y < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_neg) { } 3 3 6
elsif ($y->is_zero) { } 0 3 3
1937 3 ▶ if ($z->{sign} eq "-inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '-inf') { } 0 3 3
1938 0 return $x->bnan(@r);
1939 } else {
1940 3 return $x->binf("+", @r);
1941 }
1942 } elsif ($y -> is_zero()) { # x = -inf, y = 0
1943 0 return $x->bnan(@r);
1944 } else { # x = -inf, y > 0
1945 3 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 3 3
1946 0 return $x->bnan(@r);
1947 } else {
1948 3 return $x->binf("-", @r);
1949 }
1950 }
1951  
1952 } elsif ($x->{sign} eq "+inf") {
1953  
1954 6 ▶ if ($y -> is_neg()) { # x = +inf, y < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_neg) { } 3 3 6
elsif ($y->is_zero) { } 0 3 3
1955 3 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 3 3
1956 0 return $x->bnan(@r);
1957 } else {
1958 3 return $x->binf("-", @r);
1959 }
1960 } elsif ($y -> is_zero()) { # x = +inf, y = 0
1961 0 return $x->bnan(@r);
1962 } else { # x = +inf, y > 0
1963 3 ▶ if ($z->{sign} eq "-inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '-inf') { } 0 3 3
1964 0 return $x->bnan(@r);
1965 } else {
1966 3 return $x->binf("+", @r);
1967 }
1968 }
1969  
1970 } elsif ($x -> is_neg()) {
1971  
1972 0 ▶ if ($y->{sign} eq "-inf") { # -inf < x < 0, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->{'sign'} eq '-inf') { } 0 0 0
elsif ($y->{'sign'} eq '+inf') { } 0 0 0
1973 0 ▶ if ($z->{sign} eq "-inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '-inf') { } 0 0 0
1974 0 return $x->bnan(@r);
1975 } else {
1976 0 return $x->binf("+", @r);
1977 }
1978 } elsif ($y->{sign} eq "+inf") { # -inf < x < 0, y = +inf
1979 0 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 0 0
1980 0 return $x->bnan(@r);
1981 } else {
1982 0 return $x->binf("-", @r);
1983 }
1984 } else { # -inf < x < 0, -inf < y < +inf
1985 0 ▶ if ($z->{sign} eq "-inf") {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->{'sign'} eq '-inf') { } 0 0 0
elsif ($z->{'sign'} eq '+inf') { } 0 0 0
1986 0 return $x->binf("-", @r);
1987 } elsif ($z->{sign} eq "+inf") {
1988 0 return $x->binf("+", @r);
1989 }
1990 }
1991  
1992 } elsif ($x -> is_zero()) {
1993  
1994 0 ▶ if ($y->{sign} eq "-inf") { # x = 0, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->{'sign'} eq '-inf') { } 0 0 0
elsif ($y->{'sign'} eq '+inf') { } 0 0 0
1995 0 return $x->bnan(@r);
1996 } elsif ($y->{sign} eq "+inf") { # x = 0, y = +inf
1997 0 return $x->bnan(@r);
1998 } else { # x = 0, -inf < y < +inf
1999 0 ▶ if ($z->{sign} eq "-inf") {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->{'sign'} eq '-inf') { } 0 0 0
elsif ($z->{'sign'} eq '+inf') { } 0 0 0
2000 0 return $x->binf("-", @r);
2001 } elsif ($z->{sign} eq "+inf") {
2002 0 return $x->binf("+", @r);
2003 }
2004 }
2005  
2006 } elsif ($x -> is_pos()) {
2007  
2008 0 ▶ if ($y->{sign} eq "-inf") { # 0 < x < +inf, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->{'sign'} eq '-inf') { } 0 0 0
elsif ($y->{'sign'} eq '+inf') { } 0 0 0
2009 0 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 0 0
2010 0 return $x->bnan(@r);
2011 } else {
2012 0 return $x->binf("-", @r);
2013 }
2014 } elsif ($y->{sign} eq "+inf") { # 0 < x < +inf, y = +inf
2015 0 ▶ if ($z->{sign} eq "-inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '-inf') { } 0 0 0
2016 0 return $x->bnan(@r);
2017 } else {
2018 0 return $x->binf("+", @r);
2019 }
2020 } else { # 0 < x < +inf, -inf < y < +inf
2021 0 ▶ if ($z->{sign} eq "-inf") {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->{'sign'} eq '-inf') { } 0 0 0
elsif ($z->{'sign'} eq '+inf') { } 0 0 0
2022 0 return $x->binf("-", @r);
2023 } elsif ($z->{sign} eq "+inf") {
2024 0 return $x->binf("+", @r);
2025 }
2026 }
2027 }
2028  
2029 0 die;
2030 }
2031  
2032 sub bdiv {
2033 # This does floored division, where the quotient is floored, i.e., rounded
2034 # towards negative infinity. As a consequence, the remainder has the same
2035 # sign as the divisor.
2036  
2037 # Set up parameters.
2038 1467 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bdiv: called CC=32 SCAR=34.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1446 21 1467
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
6151446
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2039 ? (ref($_[0]), @_)
2040 : objectify(2, @_);
2041  
2042 1467 ▶ return $x if $x -> modify('bdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdiv") 0 1467 1467
2043  
2044 1467 my $wantarray = wantarray; # call only once
2045  
2046 # At least one argument is NaN. Return NaN for both quotient and the
2047 # modulo/remainder.
2048  
2049 1467 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 51 1416 1467
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
26251416
MC/DC100%2/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table100%3/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
2050 51 ▶ return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 24 27 51
2051 : $x -> bnan(@r);
2052 }
2053  
2054 # Divide by zero and modulo zero.
2055 #
2056 # Division: Use the common convention that x / 0 is inf with the same sign
2057 # as x, except when x = 0, where we return NaN. This is also what earlier
2058 # versions did.
2059 #
2060 # Modulo: In modular arithmetic, the congruence relation z = x (mod y)
2061 # means that there is some integer k such that z - x = k y. If y = 0, we
2062 # get z - x = 0 or z = x. This is also what earlier versions did, except
2063 # that 0 % 0 returned NaN.
2064 #
2065 # inf / 0 = inf inf % 0 = inf
2066 # 5 / 0 = inf 5 % 0 = 5
2067 # 0 / 0 = NaN 0 % 0 = 0
2068 # -5 / 0 = -inf -5 % 0 = -5
2069 # -inf / 0 = -inf -inf % 0 = -inf
2070  
2071 1416 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 67 1349 1416
2072 67 my $rem;
2073 67 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 32 35 67
2074 32 $rem = $x -> copy() -> round(@r);
2075 }
2076 67 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 17 50 67
2077 17 $x = $x -> bnan(@r);
2078 } else {
2079 50 $x = $x -> binf($x -> {sign}, @r);
2080 }
2081 64 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 32 32 64
2082 }
2083  
2084 # Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
2085 # The divide by zero cases are covered above. In all of the cases listed
2086 # below we return the same as core Perl.
2087 #
2088 # inf / -inf = NaN inf % -inf = NaN
2089 # inf / -5 = -inf inf % -5 = NaN
2090 # inf / 5 = inf inf % 5 = NaN
2091 # inf / inf = NaN inf % inf = NaN
2092 #
2093 # -inf / -inf = NaN -inf % -inf = NaN
2094 # -inf / -5 = inf -inf % -5 = NaN
2095 # -inf / 5 = -inf -inf % 5 = NaN
2096 # -inf / inf = NaN -inf % inf = NaN
2097  
2098 1349 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 96 1253 1349
2099 96 my $rem;
2100 96 ▶ $rem = $class -> bnan(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 48 48 96
2101 96 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 48 48 96
2102 48 $x = $x -> bnan(@r);
2103 } else {
2104 48 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 24 24 48
2105 48 $x = $x -> binf($sign, @r);
2106 }
2107 96 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 48 48 96
2108 }
2109  
2110 # Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
2111 # are covered above. In the modulo cases (in the right column) we return
2112 # the same as core Perl, which does floored division, so for consistency we
2113 # also do floored division in the division cases (in the left column).
2114 #
2115 # -5 / inf = -1 -5 % inf = inf
2116 # 0 / inf = 0 0 % inf = 0
2117 # 5 / inf = 0 5 % inf = 5
2118 #
2119 # -5 / -inf = 0 -5 % -inf = -5
2120 # 0 / -inf = 0 0 % -inf = 0
2121 # 5 / -inf = -1 5 % -inf = -inf
2122  
2123 1253 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 80 1173 1253
2124 80 my $rem;
2125 80 ▶ if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero or $x->bcmp(0) == $y->bcmp(0)) { } 56 24 80
Condition100%3/3
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
l!l&&r!l&&!r
322424
MC/DC100%2/2
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
$x->is_zero $x->bcmp(0) == $y->bcmp(0)
Truth table100%3/3
A or B
A= $x->is_zeroB= $x->bcmp(0) == $y->bcmp(0)
ABresult
1X1
011
000
2126 56 ▶ $rem = $x -> copy() -> round(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 28 28 56
2127 56 $x = $x -> bzero(@r);
2128 } else {
2129 24 ▶ $rem = $class -> binf($y -> {sign}, @r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 12 12 24
2130 24 $x = $x -> bone('-', @r);
2131 }
2132 80 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 40 40 80
2133 }
2134  
2135 # At this point, both the numerator and denominator are finite numbers, and
2136 # the denominator (divisor) is non-zero.
2137  
2138 # Division might return a non-integer result, so upgrade unconditionally, if
2139 # upgrading is enabled.
2140  
2141 1173 ▶ return $upgrade -> bdiv($x, $y, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 70 1103 1173
2142  
2143 1103 $r[3] = $y; # no push!
2144  
2145 # Inialize remainder.
2146  
2147 1103 my $rem = $class -> bzero();
2148  
2149 # Are both operands the same object, i.e., like $x -> bdiv($x)? If so,
2150 # flipping the sign of $y also flips the sign of $x.
2151  
2152 1103 my $xsign = $x -> {sign};
2153 1103 my $ysign = $y -> {sign};
2154  
2155 1103 $y -> {sign} =~ tr/+-/-+/; # Flip the sign of $y, and see ...
2156 1103 my $same = $xsign ne $x -> {sign}; # ... if that changed the sign of $x.
2157 1103 $y -> {sign} = $ysign; # Re-insert the original sign.
2158  
2159 1103 ▶ if ($same) {
Branch: 1 branch
BranchTrueFalseTotal
if ($same) { } 6 1097 1103
2160 6 $x = $x -> bone();
2161 } else {
2162 ($x -> {value}, $rem -> {value}) =
2163 1097 $LIB -> _div($x -> {value}, $y -> {value});
2164  
2165 1097 ▶ if ($LIB -> _is_zero($rem -> {value})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($rem->{'value'})) { } 522 575 1097
2166 522 ▶ if ($xsign eq $ysign || $LIB -> _is_zero($x -> {value})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsign eq $ysign or $LIB->_is_zero($x->{'value'})) { } 469 53 522
Condition100%3/3
$xsign eq $ysign or $LIB->_is_zero($x->{"value"})
l!l&&r!l&&!r
4492053
MC/DC100%2/2
$xsign eq $ysign or $LIB->_is_zero($x->{"value"})
$xsign eq $ysign $LIB->_is_zero($x->{"value"})
Truth table100%3/3
A or B
A= $xsign eq $ysignB= $LIB->_is_zero($x->{"value"})
ABresult
1X1
011
000
2167 469 $x -> {sign} = '+';
2168 } else {
2169 53 $x -> {sign} = '-';
2170 }
2171 } else {
2172 575 ▶ if ($xsign eq $ysign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsign eq $ysign) { } 524 51 575
2173 524 $x -> {sign} = '+';
2174 } else {
2175 51 ▶ if ($xsign eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsign eq '+') { } 24 27 51
2176 24 $x = $x -> badd(1);
2177 } else {
2178 27 $x = $x -> bsub(1);
2179 }
2180 51 $x -> {sign} = '-';
2181 }
2182 }
2183 }
2184  
2185 1103 $x = $x -> round(@r);
2186  
2187 1103 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 491 612 1103
2188 491 ▶ unless ($LIB -> _is_zero($rem -> {value})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($rem->{"value"})) 379 112 491
2189 379 ▶ if ($xsign ne $ysign) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsign ne $ysign) 24 355 379
2190 24 $rem = $y -> copy() -> babs() -> bsub($rem);
2191 }
2192 379 $rem -> {sign} = $ysign;
2193 }
2194 491 $rem -> {_a} = $x -> {_a};
2195 491 $rem -> {_p} = $x -> {_p};
2196 491 $rem = $rem -> round(@r);
2197 491 return ($x, $rem);
2198 }
2199  
2200 612 return $x;
2201 }
2202  
2203 sub btdiv {
2204 # This does truncated division, where the quotient is truncted, i.e.,
2205 # rounded towards zero.
2206 #
2207 # ($q, $r) = $x -> btdiv($y) returns $q and $r so that $q is int($x / $y)
2208 # and $q * $y + $r = $x.
2209  
2210 # Set up parameters
2211 366 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine btdiv: called CC=25 SCAR=32.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 366 0 366
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00366
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2212 ? (ref($_[0]), @_)
2213 : objectify(2, @_);
2214  
2215 366 ▶ return $x if $x -> modify('btdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btdiv") 0 366 366
2216  
2217 366 my $wantarray = wantarray; # call only once
2218  
2219 # At least one argument is NaN. Return NaN for both quotient and the
2220 # modulo/remainder.
2221  
2222 366 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 0 366 366
Condition33%1/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
00366
MC/DC0%0/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table33%1/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
2223 0 ▶ return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
2224 : $x -> bnan(@r);
2225 }
2226  
2227 # Divide by zero and modulo zero.
2228 #
2229 # Division: Use the common convention that x / 0 is inf with the same sign
2230 # as x, except when x = 0, where we return NaN. This is also what earlier
2231 # versions did.
2232 #
2233 # Modulo: In modular arithmetic, the congruence relation z = x (mod y)
2234 # means that there is some integer k such that z - x = k y. If y = 0, we
2235 # get z - x = 0 or z = x. This is also what earlier versions did, except
2236 # that 0 % 0 returned NaN.
2237 #
2238 # inf / 0 = inf inf % 0 = inf
2239 # 5 / 0 = inf 5 % 0 = 5
2240 # 0 / 0 = NaN 0 % 0 = 0
2241 # -5 / 0 = -inf -5 % 0 = -5
2242 # -inf / 0 = -inf -inf % 0 = -inf
2243  
2244 366 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 30 336 366
2245 30 my $rem;
2246 30 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 15 15 30
2247 15 $rem = $x -> copy(@r);
2248 }
2249 30 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 6 24 30
2250 6 $x = $x -> bnan(@r);
2251 } else {
2252 24 $x = $x -> binf($x -> {sign}, @r);
2253 }
2254 30 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 15 15 30
2255 }
2256  
2257 # Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
2258 # The divide by zero cases are covered above. In all of the cases listed
2259 # below we return the same as core Perl.
2260 #
2261 # inf / -inf = NaN inf % -inf = NaN
2262 # inf / -5 = -inf inf % -5 = NaN
2263 # inf / 5 = inf inf % 5 = NaN
2264 # inf / inf = NaN inf % inf = NaN
2265 #
2266 # -inf / -inf = NaN -inf % -inf = NaN
2267 # -inf / -5 = inf -inf % -5 = NaN
2268 # -inf / 5 = -inf -inf % 5 = NaN
2269 # -inf / inf = NaN -inf % inf = NaN
2270  
2271 336 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 48 288 336
2272 48 my $rem;
2273 48 ▶ $rem = $class -> bnan(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 24 24 48
2274 48 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 24 24 48
2275 24 $x = $x -> bnan(@r);
2276 } else {
2277 24 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 12 12 24
2278 24 $x = $x -> binf($sign,@r );
2279 }
2280 48 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 24 24 48
2281 }
2282  
2283 # Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
2284 # are covered above. In the modulo cases (in the right column) we return
2285 # the same as core Perl, which does floored division, so for consistency we
2286 # also do floored division in the division cases (in the left column).
2287 #
2288 # -5 / inf = 0 -5 % inf = -5
2289 # 0 / inf = 0 0 % inf = 0
2290 # 5 / inf = 0 5 % inf = 5
2291 #
2292 # -5 / -inf = 0 -5 % -inf = -5
2293 # 0 / -inf = 0 0 % -inf = 0
2294 # 5 / -inf = 0 5 % -inf = 5
2295  
2296 288 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 36 252 288
2297 36 my $rem;
2298 36 ▶ $rem = $x -> copy() -> round(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 18 18 36
2299 36 $x = $x -> bzero(@r);
2300 36 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 18 18 36
2301 }
2302  
2303 # Division might return a non-integer result, so upgrade unconditionally, if
2304 # upgrading is enabled.
2305  
2306 252 ▶ return $upgrade -> btdiv($x, $y, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 252 252
2307  
2308 252 $r[3] = $y; # no push!
2309  
2310 # Inialize remainder.
2311  
2312 252 my $rem = $class -> bzero();
2313  
2314 # Are both operands the same object, i.e., like $x -> bdiv($x)? If so,
2315 # flipping the sign of $y also flips the sign of $x.
2316  
2317 252 my $xsign = $x -> {sign};
2318 252 my $ysign = $y -> {sign};
2319  
2320 252 $y -> {sign} =~ tr/+-/-+/; # Flip the sign of $y, and see ...
2321 252 my $same = $xsign ne $x -> {sign}; # ... if that changed the sign of $x.
2322 252 $y -> {sign} = $ysign; # Re-insert the original sign.
2323  
2324 252 ▶ if ($same) {
Branch: 1 branch
BranchTrueFalseTotal
if ($same) { } 0 252 252
2325 0 $x = $x -> bone(@r);
2326 } else {
2327 ($x -> {value}, $rem -> {value}) =
2328 252 $LIB -> _div($x -> {value}, $y -> {value});
2329  
2330 252 ▶ $x -> {sign} = $xsign eq $ysign ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$xsign eq $ysign ? : 126 126 252
2331 252 ▶ $x -> {sign} = '+' if $LIB -> _is_zero($x -> {value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 108 144 252
2332 252 $x = $x -> round(@r);
2333 }
2334  
2335 252 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 126 126 252
2336 126 $rem -> {sign} = $xsign;
2337 126 ▶ $rem -> {sign} = '+' if $LIB -> _is_zero($rem -> {value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($rem->{"value"}) 78 48 126
2338 126 $rem -> {_a} = $x -> {_a};
2339 126 $rem -> {_p} = $x -> {_p};
2340 126 $rem = $rem -> round(@r);
2341 126 return ($x, $rem);
2342 }
2343  
2344 126 return $x;
2345 }
2346  
2347 sub bmod {
2348 # This is the remainder after floored division.
2349  
2350 # Set up parameters.
2351 700 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bmod: called CC=16 SCAR=27.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 679 21 700
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
129679
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2352 ? (ref($_[0]), @_)
2353 : objectify(2, @_);
2354  
2355 700 ▶ return $x if $x -> modify('bmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmod") 0 700 700
2356  
2357 700 $r[3] = $y; # no push!
2358  
2359 # At least one argument is NaN.
2360  
2361 700 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 27 673 700
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1413673
MC/DC100%2/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table100%3/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
2362 27 return $x -> bnan(@r);
2363 }
2364  
2365 # Modulo zero. See documentation for bdiv().
2366  
2367 673 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 34 639 673
2368 34 return $x -> round(@r);
2369 }
2370  
2371 # Numerator (dividend) is +/-inf.
2372  
2373 639 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 48 591 639
2374 48 return $x -> bnan(@r);
2375 }
2376  
2377 # Denominator (divisor) is +/-inf.
2378  
2379 591 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 40 551 591
2380 40 ▶ if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero or $x->bcmp(0) == $y->bcmp(0)) { } 28 12 40
Condition100%3/3
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
l!l&&r!l&&!r
161212
MC/DC100%2/2
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
$x->is_zero $x->bcmp(0) == $y->bcmp(0)
Truth table100%3/3
A or B
A= $x->is_zeroB= $x->bcmp(0) == $y->bcmp(0)
ABresult
1X1
011
000
2381 28 return $x -> round(@r);
2382 } else {
2383 12 return $x -> binf($y -> sign(), @r);
2384 }
2385 }
2386  
2387 551 ▶ return $upgrade -> bmod($x, $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 0 551 551
Condition50%3/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
502490
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
0049
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table50%2/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
2388 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
2389 !$y -> isa(__PACKAGE__));
2390  
2391 # Calc new sign and in case $y == +/- 1, return $x.
2392  
2393 551 $x -> {value} = $LIB -> _mod($x -> {value}, $y -> {value});
2394 551 ▶ if ($LIB -> _is_zero($x -> {value})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{'value'})) { } 154 397 551
2395 154 $x -> {sign} = '+'; # do not leave -0
2396 } else {
2397 $x -> {value} = $LIB -> _sub($y -> {value}, $x -> {value}, 1) # $y-$x
2398 397 ▶ if ($x -> {sign} ne $y -> {sign});
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $y->{"sign"} 138 259 397
2399 397 $x -> {sign} = $y -> {sign};
2400 }
2401  
2402 551 $x -> round(@r);
2403 }
2404  
2405 sub btmod {
2406 # Remainder after truncated division.
2407  
2408 # set up parameters
2409 0 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine btmod: not called CC=13 SCAR=52.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2410 ? (ref($_[0]), @_)
2411 : objectify(2, @_);
2412  
2413 0 ▶ return $x if $x -> modify('btmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btmod") 0 0 0
2414  
2415 # At least one argument is NaN.
2416  
2417 0 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 0 0 0
Condition0%0/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table0%0/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
2418 0 return $x -> bnan(@r);
2419 }
2420  
2421 # Modulo zero. See documentation for btdiv().
2422  
2423 0 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 0 0 0
2424 0 return $x -> round(@r);
2425 }
2426  
2427 # Numerator (dividend) is +/-inf.
2428  
2429 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
2430 0 return $x -> bnan(@r);
2431 }
2432  
2433 # Denominator (divisor) is +/-inf.
2434  
2435 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 0 0 0
2436 0 return $x -> round(@r);
2437 }
2438  
2439 0 ▶ return $upgrade -> btmod($x, $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 0 0 0
Condition0%0/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
000
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table0%0/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
2440 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
2441 !$y -> isa(__PACKAGE__));
2442  
2443 0 $r[3] = $y; # no push!
2444  
2445 0 my $xsign = $x -> {sign};
2446  
2447 0 $x -> {value} = $LIB -> _mod($x -> {value}, $y -> {value});
2448  
2449 0 $x -> {sign} = $xsign;
2450 0 ▶ $x -> {sign} = '+' if $LIB -> _is_zero($x -> {value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 0 0 0
2451 0 $x -> round(@r);
2452 }
2453  
2454 sub bmodinv {
2455 # Return modular multiplicative inverse:
2456 #
2457 # z is the modular inverse of x (mod y) if and only if
2458 #
2459 # x*z ≡ 1 (mod y)
2460 #
2461 # If the modulus y is larger than one, x and z are relative primes (i.e.,
2462 # their greatest common divisor is one).
2463 #
2464 # If no modular multiplicative inverse exists, NaN is returned.
2465  
2466 # set up parameters
2467 243 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bmodinv: called CC=17 SCAR=30.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 243 0 243
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00243
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2468 ? (ref($_[0]), @_)
2469 : objectify(2, @_);
2470  
2471 243 ▶ return $x if $x->modify('bmodinv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmodinv") 0 243 243
2472  
2473 # Return NaN if one or both arguments is +inf, -inf, or nan.
2474  
2475 return $x->bnan(@r) if ($y->{sign} !~ /^[+-]$/ ||
2476 243 ▶ $x->{sign} !~ /^[+-]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $y->{"sign"} =~ /^[+-]$/ and $x->{"sign"} =~ /^[+-]$/ 21 222 243
Condition100%3/3
$y->{"sign"} =~ /^[+-]$/ and $x->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
129222
MC/DC100%2/2
$y->{"sign"} =~ /^[+-]$/ and $x->{"sign"} =~ /^[+-]$/
$y->{"sign"} =~ /^[+-]$/ $x->{"sign"} =~ /^[+-]$/
Truth table100%3/3
A and B
A= $y->{"sign"} =~ /^[+-]$/B= $x->{"sign"} =~ /^[+-]$/
ABresult
0X0
100
111
2477  
2478 # Return NaN if $y is zero; 1 % 0 makes no sense.
2479  
2480 222 ▶ return $x->bnan(@r) if $y->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 0 222 222
2481  
2482 # Return 0 in the trivial case. $x % 1 or $x % -1 is zero for all finite
2483 # integers $x.
2484  
2485 222 ▶ return $x->bzero(@r) if ($y->is_one('+') ||
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_one("+") or $y->is_one("-") 63 159 222
Condition66%2/3
$y->is_one("+") or $y->is_one("-")
l!l&&r!l&&!r
630159
MC/DC50%1/2
$y->is_one("+") or $y->is_one("-")
$y->is_one("+") $y->is_one("-")
Truth table66%2/3
A or B
A= $y->is_one("+")B= $y->is_one("-")
ABresult
1X1
011
000
2486 $y->is_one('-'));
2487  
2488 159 ▶ return $upgrade -> bmodinv($x, $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 0 159 159
Condition16%1/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
15900
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table25%1/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
2489 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
2490 !$y -> isa(__PACKAGE__));
2491  
2492 # Return NaN if $x = 0, or $x modulo $y is zero. The only valid case when
2493 # $x = 0 is when $y = 1 or $y = -1, but that was covered above.
2494 #
2495 # Note that computing $x modulo $y here affects the value we'll feed to
2496 # $LIB->_modinv() below when $x and $y have opposite signs. E.g., if $x =
2497 # 5 and $y = 7, those two values are fed to _modinv(), but if $x = -5 and
2498 # $y = 7, the values fed to _modinv() are $x = 2 (= -5 % 7) and $y = 7.
2499 # The value if $x is affected only when $x and $y have opposite signs.
2500  
2501 159 $x = $x->bmod($y);
2502 159 ▶ return $x->bnan(@r) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 36 123 159
2503  
2504 # Compute the modular multiplicative inverse of the absolute values. We'll
2505 # correct for the signs of $x and $y later. Return NaN if no GCD is found.
2506  
2507 123 ($x->{value}, $x->{sign}) = $LIB->_modinv($x->{value}, $y->{value});
2508 123 ▶ return $x->bnan(@r) if !defined($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x->{"value"} 21 102 123
2509  
2510 # Library inconsistency workaround: _modinv() in Math::BigInt::GMP versions
2511 # <= 1.32 return undef rather than a "+" for the sign.
2512  
2513 102 ▶ $x->{sign} = '+' unless defined $x->{sign};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x->{"sign"} 0 102 102
2514  
2515 # When one or both arguments are negative, we have the following
2516 # relations. If x and y are positive:
2517 #
2518 # modinv(-x, -y) = -modinv(x, y)
2519 # modinv(-x, y) = y - modinv(x, y) = -modinv(x, y) (mod y)
2520 # modinv( x, -y) = modinv(x, y) - y = modinv(x, y) (mod -y)
2521  
2522 # We must swap the sign of the result if the original $x is negative.
2523 # However, we must compensate for ignoring the signs when computing the
2524 # inverse modulo. The net effect is that we must swap the sign of the
2525 # result if $y is negative.
2526  
2527 102 ▶ $x = $x -> bneg() if $y->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} eq "-" 3 99 102
2528  
2529 # Compute $x modulo $y again after correcting the sign.
2530  
2531 102 ▶ $x = $x -> bmod($y) if $x->{sign} ne $y->{sign};
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $y->{"sign"} 54 48 102
2532  
2533 102 $x -> round(@r);
2534 }
2535  
2536 sub bmodpow {
2537 # Modular exponentiation. Raises a very large number to a very large
2538 # exponent in a given very large modulus quickly, thanks to binary
2539 # exponentiation. Supports negative exponents.
2540 501 ▶ my ($class, $num, $exp, $mod, @r)
Subroutine bmodpow: called CC=20 SCAR=33.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 501 0 501
Condition33%2/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
00501
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00501
MC/DC0%0/3
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
ref $_[0] ref $_[0] eq ref $_[1] ref $_[1] eq ref $_[2]
Truth table25%1/4
A and B and C
A= ref $_[0]B= ref $_[0] eq ref $_[1]C= ref $_[1] eq ref $_[2]
ABCresult
0XX0
10X0
1100
1111
2541 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
2542 ? (ref($_[0]), @_)
2543 : objectify(3, @_);
2544  
2545 501 ▶ return $num if $num->modify('bmodpow');
Branch: 1 branch
BranchTrueFalseTotal
if $num->modify("bmodpow") 0 501 501
2546  
2547 # When the exponent 'e' is negative, use the following relation, which is
2548 # based on finding the multiplicative inverse 'd' of 'b' modulo 'm':
2549 #
2550 # b^(-e) (mod m) = d^e (mod m) where b*d = 1 (mod m)
2551  
2552 501 ▶ $num = $num -> bmodinv($mod) if ($exp->{sign} eq '-');
Branch: 1 branch
BranchTrueFalseTotal
if $exp->{"sign"} eq "-" 135 366 501
2553  
2554 # Check for valid input. All operands must be finite, and the modulus must
2555 # be non-zero.
2556  
2557 return $num->bnan(@r) if ($num->{sign} =~ /NaN|inf/ || # NaN, -inf, +inf
2558 $exp->{sign} =~ /NaN|inf/ || # NaN, -inf, +inf
2559 501 ▶ $mod->{sign} =~ /NaN|inf/); # NaN, -inf, +inf
Branch: 1 branch
BranchTrueFalseTotal
if $num->{"sign"} =~ /NaN|inf/ or $exp->{"sign"} =~ /NaN|inf/ or $mod->{"sign"} =~ /NaN|inf/ 12 435 447
Condition100%6/6
$num->{"sign"} =~ /NaN|inf/ or $exp->{"sign"} =~ /NaN|inf/ or $mod->{"sign"} =~ /NaN|inf/
l!l&&r!l&&!r
633435
$num->{"sign"} =~ /NaN|inf/ or $exp->{"sign"} =~ /NaN|inf/
l!l&&r!l&&!r
549438
MC/DC100%3/3
$num->{"sign"} =~ /NaN|inf/ or $exp->{"sign"} =~ /NaN|inf/ or $mod->{"sign"} =~ /NaN|inf/
$num->{"sign"} =~ /NaN|inf/ $exp->{"sign"} =~ /NaN|inf/ $mod->{"sign"} =~ /NaN|inf/
Truth table100%4/4
A or B or C
A= $num->{"sign"} =~ /NaN|inf/B= $exp->{"sign"} =~ /NaN|inf/C= $mod->{"sign"} =~ /NaN|inf/
ABCresult
1XX1
01X1
0011
0000
2560  
2561 # Modulo zero. See documentation for Math::BigInt's bmod() method.
2562  
2563 435 ▶ if ($mod -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->is_zero) 3 432 435
2564 3 ▶ if ($num -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num->is_zero) { } 0 3 3
2565 0 return $class -> bnan(@r);
2566 } else {
2567 3 return $num -> copy(@r);
2568 }
2569 }
2570  
2571 432 ▶ return $upgrade -> bmodinv($num, $exp, $mod, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $num->isa(__PACKAGE__) && $exp->isa(__PACKAGE__) && &$mod($class) 0 432 432
Condition11%1/9
defined $Math::BigInt::upgrade and not $num->isa(__PACKAGE__) && $exp->isa(__PACKAGE__) && &$mod($class)
!ll&&!rl&&r
43200
$num->isa(__PACKAGE__) && $exp->isa(__PACKAGE__) && &$mod($class)
!ll&&!rl&&r
000
$num->isa(__PACKAGE__) and $exp->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/4
defined $Math::BigInt::upgrade and not $num->isa(__PACKAGE__) && $exp->isa(__PACKAGE__) && &$mod($class)
defined $Math::BigInt::upgrade $num->isa(__PACKAGE__) $exp->isa(__PACKAGE__) &$mod($class)
Truth table20%1/5
A and not(B and C && D)
A= defined $Math::BigInt::upgradeB= $num->isa(__PACKAGE__)C= $exp->isa(__PACKAGE__)D= &$mod($class)
ABCDresult
0XXX0
11110
10XX1
110X1
11101
2572 if defined($upgrade) && (!$num -> isa(__PACKAGE__) ||
2573 !$exp -> isa(__PACKAGE__) ||
2574 !$mod -> ($class));
2575  
2576 # Compute 'a (mod m)', ignoring the signs on 'a' and 'm'. If the resulting
2577 # value is zero, the output is also zero, regardless of the signs on 'a' and
2578 # 'm'.
2579  
2580 432 my $value = $LIB->_modpow($num->{value}, $exp->{value}, $mod->{value});
2581 432 my $sign = '+';
2582  
2583 # If the resulting value is non-zero, we have four special cases, depending
2584 # on the signs on 'a' and 'm'.
2585  
2586 432 ▶ unless ($LIB->_is_zero($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($value)) 213 219 432
2587  
2588 # There is a negative sign on 'a' (= $num**$exp) only if the number we
2589 # are exponentiating ($num) is negative and the exponent ($exp) is odd.
2590  
2591 213 ▶ if ($num->{sign} eq '-' && $exp->is_odd()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num->{'sign'} eq '-' and $exp->is_odd) { } 21 192 213
Condition100%3/3
$num->{"sign"} eq "-" and $exp->is_odd
!ll&&!rl&&r
1623021
MC/DC100%2/2
$num->{"sign"} eq "-" and $exp->is_odd
$num->{"sign"} eq "-" $exp->is_odd
Truth table100%3/3
A and B
A= $num->{"sign"} eq "-"B= $exp->is_odd
ABresult
0X0
100
111
2592  
2593 # When both the number 'a' and the modulus 'm' have a negative sign,
2594 # use this relation:
2595 #
2596 # -a (mod -m) = -(a (mod m))
2597  
2598 21 ▶ if ($mod->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->{'sign'} eq '-') { } 0 21 21
2599 0 $sign = '-';
2600 }
2601  
2602 # When only the number 'a' has a negative sign, use this relation:
2603 #
2604 # -a (mod m) = m - (a (mod m))
2605  
2606 else {
2607 # Use copy of $mod since _sub() modifies the first argument.
2608 21 my $mod = $LIB->_copy($mod->{value});
2609 21 $value = $LIB->_sub($mod, $value);
2610 21 $sign = '+';
2611 }
2612  
2613 } else {
2614  
2615 # When only the modulus 'm' has a negative sign, use this relation:
2616 #
2617 # a (mod -m) = (a (mod m)) - m
2618 # = -(m - (a (mod m)))
2619  
2620 192 ▶ if ($mod->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->{"sign"} eq "-") 3 189 192
2621 # Use copy of $mod since _sub() modifies the first argument.
2622 3 my $mod = $LIB->_copy($mod->{value});
2623 3 $value = $LIB->_sub($mod, $value);
2624 3 $sign = '-';
2625 }
2626  
2627 # When neither the number 'a' nor the modulus 'm' have a negative
2628 # sign, directly return the already computed value.
2629 #
2630 # (a (mod m))
2631  
2632 }
2633  
2634 }
2635  
2636 432 $num->{value} = $value;
2637 432 $num->{sign} = $sign;
2638  
2639 432 return $num -> round(@r);
2640 }
2641  
2642 sub bpow {
2643 # (BINT or num_str, BINT or num_str) return BINT
2644 # compute power of two numbers -- stolen from Knuth Vol 2 pg 233
2645 # modifies first argument
2646  
2647 # set up parameters
2648 575 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bpow: called CC=32 SCAR=34.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 504 71 575
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
1259504
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2649 ? (ref($_[0]), @_)
2650 : objectify(2, @_);
2651  
2652 575 ▶ return $x if $x -> modify('bpow');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bpow") 0 575 575
2653  
2654 # $x and/or $y is a NaN
2655 575 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 65 510 575
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
3431510
MC/DC100%2/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table100%3/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
2656  
2657 # $x and/or $y is a +/-Inf
2658 510 ▶ if ($x -> is_inf("-")) {
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 39 471 510
elsif ($x->is_inf('+')) { } 35 436 471
elsif ($y->is_inf('-')) { } 21 415 436
elsif ($y->is_inf('+')) { } 21 394 415
2659 39 ▶ return $x -> bzero(@r) if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 16 23 39
2660 23 ▶ return $x -> bnan(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 3 20 23
2661 20 ▶ return $x -> round(@r) if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 10 10 20
2662 10 return $x -> bneg(@r);
2663 } elsif ($x -> is_inf("+")) {
2664 35 ▶ return $x -> bzero(@r) if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 16 19 35
2665 19 ▶ return $x -> bnan(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 3 16 19
2666 16 return $x -> round(@r);
2667 } elsif ($y -> is_inf("-")) {
2668 21 ▶ return $x -> bnan(@r) if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 3 18 21
2669 18 ▶ return $x -> binf("+", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 3 15 18
2670 15 ▶ return $x -> bone(@r) if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 3 12 15
2671 12 return $x -> bzero(@r);
2672 } elsif ($y -> is_inf("+")) {
2673 21 ▶ return $x -> bnan(@r) if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 3 18 21
2674 18 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 3 15 18
2675 15 ▶ return $x -> bone(@r) if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 3 12 15
2676 12 return $x -> binf("+", @r);
2677 }
2678  
2679 394 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 26 368 394
2680 26 ▶ return $x -> bone(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 22 26
2681 22 ▶ return $x -> binf(@r) if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 11 11 22
2682 11 return $x -> round(@r);
2683 }
2684  
2685 368 ▶ if ($x -> is_one("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("+")) 28 340 368
2686 28 return $x -> round(@r);
2687 }
2688  
2689 340 ▶ if ($x -> is_one("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("-")) 31 309 340
2690 31 ▶ return $x -> round(@r) if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 17 14 31
2691 14 return $x -> bneg(@r);
2692 }
2693  
2694 309 ▶ return $upgrade -> bpow($x, $y, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 38 271 309
2695  
2696 # We don't support finite non-integers, so return zero. The reason for
2697 # returning zero, not NaN, is that all output is in the open interval (0,1),
2698 # and truncating that to integer gives zero.
2699  
2700 271 ▶ if ($y->{sign} eq '-' || !$y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{"sign"} eq "-" or not $y->isa(__PACKAGE__)) 36 235 271
Condition66%2/3
$y->{"sign"} eq "-" or not $y->isa(__PACKAGE__)
l!l&&r!l&&!r
360235
MC/DC50%1/2
$y->{"sign"} eq "-" or not $y->isa(__PACKAGE__)
$y->{"sign"} eq "-" not $y->isa(__PACKAGE__)
Truth table66%2/3
A or not(B)
A= $y->{"sign"} eq "-"B= not $y->isa(__PACKAGE__)
ABresult
1X1
011
000
2701 36 return $x -> bzero(@r);
2702 }
2703  
2704 235 $r[3] = $y; # no push!
2705  
2706 235 $x->{value} = $LIB -> _pow($x->{value}, $y->{value});
2707 235 ▶ $x->{sign} = $x -> is_negative() && $y -> is_odd() ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative && $y->is_odd ? : 21 214 235
Condition100%3/3
$x->is_negative and $y->is_odd
!ll&&!rl&&r
1961821
MC/DC100%2/2
$x->is_negative and $y->is_odd
$x->is_negative $y->is_odd
Truth table100%3/3
A and B
A= $x->is_negativeB= $y->is_odd
ABresult
0X0
100
111
2708 235 $x -> round(@r);
2709 }
2710  
2711 sub blog {
2712 # Return the logarithm of the operand. If a second operand is defined, that
2713 # value is used as the base, otherwise the base is assumed to be Euler's
2714 # constant.
2715  
2716 199 ▶ my ($class, $x, $base, @r);
Subroutine blog: called CC=29 SCAR=34.4
Pod documented
2717  
2718 # Only objectify the base if it is defined, since an undefined base, as in
2719 # $x->blog() or $x->blog(undef) signals that the base is Euler's number =
2720 # 2.718281828...
2721  
2722 199 ▶ if (!ref($_[0]) && $_[0] =~ /^[a-z]\w*(?:::\w+)*$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and $_[0] =~ /^[a-z]\w*(?:::\w+)*$/i) { } 0 199 199
Condition33%1/3
not ref $_[0] and $_[0] =~ /^[a-z]\w*(?:::\w+)*$/i
!ll&&!rl&&r
19900
MC/DC0%0/2
not ref $_[0] and $_[0] =~ /^[a-z]\w*(?:::\w+)*$/i
not ref $_[0] $_[0] =~ /^[a-z]\w*(?:::\w+)*$/i
Truth table33%1/3
not(A) and B
A= not ref $_[0]B= $_[0] =~ /^[a-z]\w*(?:::\w+)*$/i
ABresult
0X0
100
111
2723 # E.g., Math::BigInt->blog(256, 2)
2724 0 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
2725 defined $_[2] ? objectify(2, @_) : objectify(1, @_);
2726 } else {
2727 # E.g., $x->blog(2) or the deprecated Math::BigInt::blog(256, 2)
2728 199 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 166 33 199
2729 defined $_[1] ? objectify(2, @_) : objectify(1, @_);
2730 }
2731  
2732 199 ▶ return $x if $x->modify('blog');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blog") 0 199 199
2733  
2734 # Handle all exception cases and all trivial cases. I have used Wolfram
2735 # Alpha (http://www.wolframalpha.com) as the reference for these cases.
2736  
2737 199 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 9 190 199
2738  
2739 190 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 160 30 190
2740 160 ▶ $base = $class -> new($base)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($base) and $base->isa(__PACKAGE__) 0 160 160
Condition33%1/3
defined builtin::blessed($base) and $base->isa(__PACKAGE__)
!ll&&!rl&&r
00160
MC/DC0%0/2
defined builtin::blessed($base) and $base->isa(__PACKAGE__)
defined builtin::blessed($base) $base->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($base)B= $base->isa(__PACKAGE__)
ABresult
0X0
100
111
2741 unless defined(blessed($base)) && $base -> isa(__PACKAGE__);
2742 160 ▶ if ($base -> is_nan() || $base -> is_one()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($base->is_nan or $base->is_one) { } 12 148 160
elsif ($base->is_inf or $base->is_zero) { } 36 112 148
elsif ($base->is_negative) { } 12 100 112
Condition100%3/3
$base->is_nan or $base->is_one
l!l&&r!l&&!r
39148
MC/DC100%2/2
$base->is_nan or $base->is_one
$base->is_nan $base->is_one
Truth table100%3/3
A or B
A= $base->is_nanB= $base->is_one
ABresult
1X1
011
000
2743 12 return $x -> bnan(@r);
2744 ▶ } elsif ($base -> is_inf() || $base -> is_zero()) {
Condition100%3/3
$base->is_inf or $base->is_zero
l!l&&r!l&&!r
279112
MC/DC100%2/2
$base->is_inf or $base->is_zero
$base->is_inf $base->is_zero
Truth table100%3/3
A or B
A= $base->is_infB= $base->is_zero
ABresult
1X1
011
000
2745 36 ▶ return $x -> bnan(@r) if $x -> is_inf() || $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf or $x->is_zero 21 15 36
Condition100%3/3
$x->is_inf or $x->is_zero
l!l&&r!l&&!r
12915
MC/DC100%2/2
$x->is_inf or $x->is_zero
$x->is_inf $x->is_zero
Truth table100%3/3
A or B
A= $x->is_infB= $x->is_zero
ABresult
1X1
011
000
2746 15 return $x -> bzero(@r);
2747 } elsif ($base -> is_negative()) { # -inf < base < 0
2748 12 ▶ return $x -> bzero(@r) if $x -> is_one(); # x = 1
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 3 9 12
2749 9 ▶ return $x -> bone('+', @r) if $x == $base; # x = base
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 0 9 9
2750 # we can't handle these cases, so upgrade, if we can
2751 9 ▶ return $upgrade -> blog($x, $base, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 9 9
2752 9 return $x -> bnan(@r);
2753 }
2754 100 ▶ return $x -> bone(@r) if $x == $base; # 0 < base && 0 < x < inf
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 3 97 100
2755 }
2756  
2757 # We now know that the base is either undefined or >= 2 and finite.
2758  
2759 127 ▶ if ($x -> is_inf()) { # x = +/-inf
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 15 112 127
elsif ($x->is_neg) { } 6 106 112
elsif ($x->is_one) { } 9 97 106
elsif ($x->is_zero) { } 6 91 97
2760 15 return $x -> binf('+', @r);
2761 } elsif ($x -> is_neg()) { # -inf < x < 0
2762 6 ▶ return $upgrade -> blog($x, $base, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 6 6
2763 6 return $x -> bnan(@r);
2764 } elsif ($x -> is_one()) { # x = 1
2765 9 return $x -> bzero(@r);
2766 } elsif ($x -> is_zero()) { # x = 0
2767 6 return $x -> binf('-', @r);
2768 }
2769  
2770 # At this point we are done handling all exception cases and trivial cases.
2771  
2772 91 ▶ return $upgrade -> blog($x, $base, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 2 89 91
2773  
2774 # fix for bug #24969:
2775 # the default base is e (Euler's number) which is not an integer
2776 89 ▶ if (!defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base) 15 74 89
2777 15 require Math::BigFloat;
2778  
2779 # disable upgrading and downgrading
2780  
2781 15 my $upg = Math::BigFloat -> upgrade();
2782 15 my $dng = Math::BigFloat -> downgrade();
2783 15 Math::BigFloat -> upgrade(undef);
2784 15 Math::BigFloat -> downgrade(undef);
2785  
2786 15 my $u = Math::BigFloat -> blog($x) -> as_int();
2787  
2788 # reset upgrading and downgrading
2789  
2790 15 Math::BigFloat -> upgrade($upg);
2791 15 Math::BigFloat -> downgrade($dng);
2792  
2793 # modify $x in place
2794  
2795 15 $x->{value} = $u->{value};
2796 15 $x->{sign} = $u->{sign};
2797  
2798 15 return $x -> round(@r);
2799 }
2800  
2801 74 my ($rc) = $LIB -> _log_int($x->{value}, $base->{value});
2802 74 ▶ return $x -> bnan(@r) unless defined $rc; # not possible to take log?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 0 74 74
2803 74 $x->{value} = $rc;
2804 74 $x = $x -> round(@r);
2805 }
2806  
2807 sub bexp {
2808 # Calculate e ** $x (Euler's number to the power of X), truncated to
2809 # an integer value.
2810 15 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bexp: called CC=8 SCAR=21.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 15 0 15
2811  
2812 15 ▶ return $x if $x->modify('bexp');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bexp") 0 15 15
2813  
2814 # inf, -inf, NaN, <0 => NaN
2815 15 ▶ return $x -> bnan(@r) if $x->{sign} eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "NaN" 3 12 15
2816 12 ▶ return $x -> bone(@r) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 12 12
2817 12 ▶ return $x -> round(@r) if $x->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 3 9 12
2818 9 ▶ return $x -> bzero(@r) if $x->{sign} eq '-inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-inf" 0 9 9
2819  
2820 9 ▶ return $upgrade -> bexp($x, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 9 9
2821  
2822 9 require Math::BigFloat;
2823 9 my $tmp = Math::BigFloat -> bexp($x, @r) -> as_int();
2824 9 $x->{value} = $tmp->{value};
2825 9 return $x -> round(@r);
2826 }
2827  
2828 sub bnok {
2829 # Calculate n over k (binomial coefficient or "choose" function) as
2830 # integer.
2831  
2832 # Set up parameters.
2833 93 ▶ my ($class, $n, $k, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bnok: called CC=23 SCAR=44.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 93 0 93
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0093
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2834 ? (ref($_[0]), @_)
2835 : objectify(2, @_);
2836  
2837 93 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 93 93
2838  
2839 93 ▶ return $n if $n->modify('bnok');
Branch: 1 branch
BranchTrueFalseTotal
if $n->modify("bnok") 0 93 93
2840  
2841 # All cases where at least one argument is NaN.
2842  
2843 93 ▶ return $n->bnan(@r) if $n->{sign} eq 'NaN' || $k->{sign} eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $n->{"sign"} eq "NaN" or $k->{"sign"} eq "NaN" 9 84 93
Condition100%3/3
$n->{"sign"} eq "NaN" or $k->{"sign"} eq "NaN"
l!l&&r!l&&!r
6384
MC/DC100%2/2
$n->{"sign"} eq "NaN" or $k->{"sign"} eq "NaN"
$n->{"sign"} eq "NaN" $k->{"sign"} eq "NaN"
Truth table100%3/3
A or B
A= $n->{"sign"} eq "NaN"B= $k->{"sign"} eq "NaN"
ABresult
1X1
011
000
2844  
2845 # All cases where at least one argument is +/-inf.
2846  
2847 84 ▶ if ($n -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n->is_inf) { } 7 77 84
elsif ($k->is_inf) { } 0 77 77
2848 7 ▶ if ($k -> is_inf()) { # bnok(+/-inf,+/-inf)
Branch: 3 branches
BranchTrueFalseTotal
if ($k->is_inf) { } 0 7 7
elsif ($k->is_neg) { } 0 7 7
elsif ($k->is_zero) { } 0 7 7
2849 0 return $n -> bnan(@r);
2850 } elsif ($k -> is_neg()) { # bnok(+/-inf,k), k < 0
2851 0 return $n -> bzero(@r);
2852 } elsif ($k -> is_zero()) { # bnok(+/-inf,k), k = 0
2853 0 return $n -> bone(@r);
2854 } else {
2855 7 ▶ if ($n -> is_inf("+", @r)) { # bnok(+inf,k), 0 < k < +inf
Branch: 1 branch
BranchTrueFalseTotal
if ($n->is_inf('+', @r)) { } 7 0 7
2856 7 return $n -> binf("+");
2857 } else { # bnok(-inf,k), k > 0
2858 0 ▶ my $sign = $k -> is_even() ? "+" : "-";
Branch: 1 branch
BranchTrueFalseTotal
$k->is_even ? : 0 0 0
2859 0 return $n -> binf($sign, @r);
2860 }
2861 }
2862 }
2863  
2864 elsif ($k -> is_inf()) { # bnok(n,+/-inf), -inf <= n <= inf
2865 0 return $n -> bnan(@r);
2866 }
2867  
2868 # At this point, both n and k are real numbers.
2869  
2870 77 ▶ return $upgrade -> bnok($n, $k, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $n->isa(__PACKAGE__) && $k->isa(__PACKAGE__) 0 77 77
Condition16%1/6
defined $Math::BigInt::upgrade and not $n->isa(__PACKAGE__) && $k->isa(__PACKAGE__)
!ll&&!rl&&r
7700
$n->isa(__PACKAGE__) && $k->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $n->isa(__PACKAGE__) && $k->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $n->isa(__PACKAGE__) $k->isa(__PACKAGE__)
Truth table25%1/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $n->isa(__PACKAGE__)C= $k->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
2871 if defined($upgrade) && (!$n -> isa(__PACKAGE__) ||
2872 !$k -> isa(__PACKAGE__));
2873  
2874 77 my $sign = 1;
2875  
2876 77 ▶ if ($n >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 0) { } 77 0 77
2877 77 ▶ if ($k < 0 || $k > $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < 0 or $k > $n) 21 56 77
Condition100%3/3
$k < 0 or $k > $n
l!l&&r!l&&!r
71456
MC/DC0%0/2
$k < 0 or $k > $n
$k < 0 $k > $n
Truth table33%1/3
A or B
A= $k < 0B= $k > $n
ABresult
1X1
011
000
2878 21 return $n -> bzero(@r);
2879 }
2880 } else {
2881  
2882 0 ▶ if ($k >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k >= 0) { } 0 0 0
elsif ($k <= $n) { } 0 0 0
2883  
2884 # n < 0 and k >= 0: bnok(n,k) = (-1)^k * bnok(-n+k-1,k)
2885  
2886 0 $sign = (-1) ** $k;
2887 0 $n = $n -> bneg() -> badd($k) -> bdec();
2888  
2889 } elsif ($k <= $n) {
2890  
2891 # n < 0 and k <= n: bnok(n,k) = (-1)^(n-k) * bnok(-k-1,n-k)
2892  
2893 0 $sign = (-1) ** ($n - $k);
2894 0 my $x0 = $n -> copy();
2895 0 $n = $n -> bone() -> badd($k) -> bneg();
2896 0 $k = $k -> copy();
2897 0 $k = $k -> bneg() -> badd($x0);
2898  
2899 } else {
2900  
2901 # n < 0 and n < k < 0:
2902  
2903 0 return $n -> bzero(@r);
2904 }
2905 }
2906  
2907 56 $n->{value} = $LIB->_nok($n->{value}, $k->{value});
2908 56 ▶ $n = $n -> bneg() if $sign == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $sign == -1 0 56 56
2909  
2910 56 $n -> round(@r);
2911 }
2912  
2913 sub buparrow {
2914 0 ▶ my $a = shift;
Subroutine buparrow: not called CC=1 SCAR=6.9
Pod documented
2915 0 my $y = $a -> uparrow(@_);
2916 0 $a -> {value} = $y -> {value};
2917 0 return $a;
2918 }
2919  
2920 sub uparrow {
2921 # Knuth's up-arrow notation buparrow(a, n, b)
2922 #
2923 # The following is a simple, recursive implementation of the up-arrow
2924 # notation, just to show the idea. Such implementations cause "Deep
2925 # recursion on subroutine ..." warnings, so we use a faster, non-recursive
2926 # algorithm below with @_ as a stack.
2927 #
2928 # sub buparrow {
2929 # my ($a, $n, $b) = @_;
2930 # return $a ** $b if $n == 1;
2931 # return $a * $b if $n == 0;
2932 # return 1 if $b == 0;
2933 # return buparrow($a, $n - 1, buparrow($a, $n, $b - 1));
2934 # }
2935  
2936 0 ▶ my ($a, $b, $n) = @_;
Subroutine uparrow: not called CC=8 SCAR=42.8
Pod documented
2937 0 my $class = ref $a;
2938 0 ▶ croak("a must be non-negative") if $a < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $a < 0 0 0 0
2939 0 ▶ croak("n must be non-negative") if $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 0 0 0
2940 0 ▶ croak("b must be non-negative") if $b < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $b < 0 0 0 0
2941  
2942 0 ▶ while (@_ >= 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 3) 0 0 0
2943  
2944 # return $a ** $b if $n == 1;
2945  
2946 0 ▶ if ($_[-2] == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[-2] == 1) 0 0 0
2947 0 my ($a, $n, $b) = splice @_, -3;
2948 0 push @_, $a ** $b;
2949 0 next;
2950 }
2951  
2952 # return $a * $b if $n == 0;
2953  
2954 0 ▶ if ($_[-2] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[-2] == 0) 0 0 0
2955 0 my ($a, $n, $b) = splice @_, -3;
2956 0 push @_, $a * $b;
2957 0 next;
2958 }
2959  
2960 # return 1 if $b == 0;
2961  
2962 0 ▶ if ($_[-1] == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[-1] == 0) 0 0 0
2963 0 splice @_, -3;
2964 0 push @_, $class -> bone();
2965 0 next;
2966 }
2967  
2968 # return buparrow($a, $n - 1, buparrow($a, $n, $b - 1));
2969  
2970 0 my ($a, $n, $b) = splice @_, -3;
2971 0 push @_, ($a, $n - 1,
2972 $a, $n, $b - 1);
2973  
2974 }
2975  
2976 0 pop @_;
2977 }
2978  
2979 sub backermann {
2980 0 ▶ my $m = shift;
Subroutine backermann: not called CC=1 SCAR=6.9
Pod documented
2981 0 my $y = $m -> ackermann(@_);
2982 0 $m -> {value} = $y -> {value};
2983 0 return $m;
2984 }
2985  
2986 sub ackermann {
2987 # Ackermann's function ackermann(m, n)
2988 #
2989 # The following is a simple, recursive implementation of the ackermann
2990 # function, just to show the idea. Such implementations cause "Deep
2991 # recursion on subroutine ..." warnings, so we use a faster, non-recursive
2992 # algorithm below with @_ as a stack.
2993 #
2994 # sub ackermann {
2995 # my ($m, $n) = @_;
2996 # return $n + 1 if $m == 0;
2997 # return ackermann($m - 1, 1) if $m > 0 && $n == 0;
2998 # return ackermann($m - 1, ackermann($m, $n - 1) if $m > 0 && $n > 0;
2999 # }
3000  
3001 0 ▶ my ($m, $n) = @_;
Subroutine ackermann: not called CC=10 SCAR=47.0
Pod documented
3002 0 my $class = ref $m;
3003 0 ▶ croak("m must be non-negative") if $m < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $m < 0 0 0 0
3004 0 ▶ croak("n must be non-negative") if $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 0 0 0
3005  
3006 0 my $two = $class -> new("2");
3007 0 my $three = $class -> new("3");
3008 0 my $thirteen = $class -> new("13");
3009  
3010 0 $n = pop;
3011 0 ▶ $n = $class -> new($n) unless ref($n);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $n 0 0 0
3012 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
3013 0 my $m = pop;
3014 0 ▶ if ($m > $three) {
Branch: 4 branches
BranchTrueFalseTotal
if ($m > $three) { } 0 0 0
elsif ($m == $three) { } 0 0 0
elsif ($m == $two) { } 0 0 0
elsif ($m >= 0) { } 0 0 0
3015 0 push @_, (--$m) x $n;
3016 0 ▶ while (--$m >= $three) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$m >= $three) 0 0 0
3017 0 push @_, $m;
3018 }
3019 0 $n = $thirteen;
3020 } elsif ($m == $three) {
3021 0 $n = $class -> bone() -> blsft($n + $three) -> bsub($three);
3022 } elsif ($m == $two) {
3023 0 $n = $n -> bmul($two) -> badd($three);
3024 } elsif ($m >= 0) {
3025 0 $n = $n -> badd($m) -> binc();
3026 } else {
3027 0 die "negative m!";
3028 }
3029 }
3030 0 $n;
3031 }
3032  
3033 sub bsin {
3034 # Calculate sin(x) to N digits. Unless upgrading is in effect, returns the
3035 # result truncated to an integer.
3036 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bsin: not called CC=8 SCAR=42.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3037  
3038 0 ▶ return $x if $x->modify('bsin');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsin") 0 0 0
3039  
3040 0 ▶ return $x->bnan(@r) if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]\z/ 0 0 0
3041 0 ▶ return $x->bzero(@r) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3042  
3043 0 ▶ return $upgrade -> bsin($x, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 0 0
3044  
3045 0 require Math::BigFloat;
3046 # calculate the result and truncate it to integer
3047 0 my $t = Math::BigFloat->new($x)->bsin(@r)->as_int();
3048  
3049 0 ▶ $x = $x->bone(@r) if $t->is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $t->is_one 0 0 0
3050 0 ▶ $x = $x->bzero(@r) if $t->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $t->is_zero 0 0 0
3051 0 $x->round(@r);
3052 }
3053  
3054 sub bcos {
3055 # Calculate cos(x) to N digits. Unless upgrading is in effect, returns the
3056 # result truncated to an integer.
3057 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bcos: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3058  
3059 0 ▶ return $x if $x->modify('bcos');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bcos") 0 0 0
3060  
3061 0 ▶ return $x->bnan(@r) if $x->{sign} !~ /^[+-]\z/; # -inf +inf or NaN => NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]\z/ 0 0 0
3062 0 ▶ return $x->bone(@r) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3063  
3064 0 ▶ return $upgrade -> bcos($x, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 0 0
3065  
3066 0 require Math::BigFloat;
3067 0 my $tmp = Math::BigFloat -> bcos($x, @r) -> as_int();
3068 0 $x->{value} = $tmp->{value};
3069 0 return $x -> round(@r);
3070 }
3071  
3072 sub batan {
3073 # Calculate arctan(x) to N digits. Unless upgrading is in effect, returns
3074 # the result truncated to an integer.
3075 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine batan: not called CC=8 SCAR=42.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3076  
3077 0 ▶ return $x if $x->modify('batan');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("batan") 0 0 0
3078  
3079 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
3080 0 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3081  
3082 0 ▶ return $upgrade -> batan($x, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 0 0
3083  
3084 0 ▶ return $x -> bone("+", @r) if $x -> bgt("1");
Branch: 1 branch
BranchTrueFalseTotal
if $x->bgt(1) 0 0 0
3085 0 ▶ return $x -> bone("-", @r) if $x -> blt("-1");
Branch: 1 branch
BranchTrueFalseTotal
if $x->blt(-1) 0 0 0
3086  
3087 0 $x -> bzero(@r);
3088 }
3089  
3090 sub batan2 {
3091 # calculate arcus tangens of ($y/$x)
3092  
3093 84 ▶ my ($class, $y, $x, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine batan2: called CC=13 SCAR=25.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 84 0 84
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0084
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
3094 ? (ref($_[0]), @_) : objectify(2, @_);
3095  
3096 84 ▶ return $y if $y->modify('batan2');
Branch: 1 branch
BranchTrueFalseTotal
if $y->modify("batan2") 0 84 84
3097  
3098 84 ▶ return $y->bnan() if ($y->{sign} eq $nan) || ($x->{sign} eq $nan);
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} eq $nan or $x->{"sign"} eq $nan 9 75 84
Condition100%3/3
$y->{"sign"} eq $nan or $x->{"sign"} eq $nan
l!l&&r!l&&!r
6375
MC/DC100%2/2
$y->{"sign"} eq $nan or $x->{"sign"} eq $nan
$y->{"sign"} eq $nan $x->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $y->{"sign"} eq $nanB= $x->{"sign"} eq $nan
ABresult
1X1
011
000
3099  
3100 75 ▶ return $upgrade->batan2($y, $x, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 75 75
3101  
3102 # Y X
3103 # != 0 -inf result is +- pi
3104 75 ▶ if ($x->is_inf() || $y->is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf or $y->is_inf) 30 45 75
Condition100%3/3
$x->is_inf or $y->is_inf
l!l&&r!l&&!r
24645
MC/DC100%2/2
$x->is_inf or $y->is_inf
$x->is_inf $y->is_inf
Truth table100%3/3
A or B
A= $x->is_infB= $y->is_inf
ABresult
1X1
011
000
3105 30 ▶ if ($y->is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 18 12 30
3106 18 ▶ if ($x->{sign} eq '-inf') {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '-inf') { } 6 12 18
elsif ($x->{'sign'} eq '+inf') { } 6 6 12
3107 # calculate 3 pi/4 => 2.3.. => 2
3108 6 $y = $y->bone(substr($y->{sign}, 0, 1));
3109 6 $y = $y->bmul($class->new(2));
3110 } elsif ($x->{sign} eq '+inf') {
3111 # calculate pi/4 => 0.7 => 0
3112 6 $y = $y->bzero();
3113 } else {
3114 # calculate pi/2 => 1.5 => 1
3115 6 $y = $y->bone(substr($y->{sign}, 0, 1));
3116 }
3117 } else {
3118 12 ▶ if ($x->{sign} eq '+inf') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '+inf') { } 3 9 12
3119 # calculate pi/4 => 0.7 => 0
3120 3 $y = $y->bzero();
3121 } else {
3122 # PI => 3.1415.. => 3
3123 9 $y = $y->bone(substr($y->{sign}, 0, 1));
3124 9 $y = $y->bmul($class->new(3));
3125 }
3126 }
3127 30 return $y;
3128 }
3129  
3130 45 require Math::BigFloat;
3131 45 my $r = Math::BigFloat->new($y)
3132 ->batan2(Math::BigFloat->new($x), @r)
3133 ->as_int();
3134  
3135 45 $x->{value} = $r->{value};
3136 45 $x->{sign} = $r->{sign};
3137  
3138 45 $x->round(@r);
3139 }
3140  
3141 sub bsqrt {
3142 # calculate square root of $x
3143 523 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bsqrt: called CC=6 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 520 3 523
3144  
3145 523 ▶ return $x if $x->modify('bsqrt');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsqrt") 0 523 523
3146  
3147 523 ▶ return $x->bnan(@r) if $x->{sign} !~ /^\+/; # -x or -inf or NaN => NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^\+/ 16 507 523
3148 507 ▶ return $x->round(@r) if $x->{sign} eq '+inf'; # sqrt(+inf) == inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 4 503 507
3149  
3150 503 ▶ return $upgrade->bsqrt($x, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 22 481 503
3151  
3152 481 $x->{value} = $LIB->_sqrt($x->{value});
3153 481 $x->round(@r);
3154 }
3155  
3156 sub broot {
3157 # calculate $y'th root of $x
3158  
3159 # set up parameters
3160  
3161 174 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine broot: called CC=13 SCAR=25.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 166 8 174
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
08166
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
3162 ? (ref($_[0]), @_) : objectify(2, @_);
3163  
3164 174 ▶ $y = $class->new(2) unless defined $y;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y 0 174 174
3165  
3166 174 ▶ return $x if $x->modify('broot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("broot") 0 174 174
3167  
3168 # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0
3169 return $x->bnan(@r) if $x->{sign} !~ /^\+/ || $y->is_zero() ||
3170 174 ▶ $y->{sign} !~ /^\+$/;
Branch: 1 branch
BranchTrueFalseTotal
if not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/ 27 99 126
Condition100%6/6
not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/
l!l&&r!l&&!r
571899
not $x->{"sign"} =~ /^\+/ or $y->is_zero
l!l&&r!l&&!r
489117
MC/DC100%3/3
not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/
not $x->{"sign"} =~ /^\+/ $y->is_zero not $y->{"sign"} =~ /^\+$/
Truth table100%4/4
not(A) or B or not(C)
A= not $x->{"sign"} =~ /^\+/B= $y->is_zeroC= not $y->{"sign"} =~ /^\+$/
ABCresult
1XX1
01X1
0011
0000
3171  
3172 99 ▶ return $x->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one or $x->is_inf or $y->is_one 6 87 93
Condition100%9/9
$x->is_zero or $x->is_one or $x->is_inf or $y->is_one
l!l&&r!l&&!r
9387
$x->is_zero or $x->is_one or $x->is_inf
l!l&&r!l&&!r
6390
$x->is_zero or $x->is_one
l!l&&r!l&&!r
3393
MC/DC100%4/4
$x->is_zero or $x->is_one or $x->is_inf or $y->is_one
$x->is_zero $x->is_one $x->is_inf $y->is_one
Truth table100%5/5
A or B or C or D
A= $x->is_zeroB= $x->is_oneC= $x->is_infD= $y->is_one
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3173 if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one();
3174  
3175 87 ▶ return $upgrade->broot($x, $y, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 2 85 87
3176  
3177 85 $x->{value} = $LIB->_root($x->{value}, $y->{value});
3178 85 $x->round(@r);
3179 }
3180  
3181 sub bfac {
3182 # (BINT or num_str, BINT or num_str) return BINT
3183 # compute factorial number from $x, modify $x in place
3184 81 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bfac: called CC=7 SCAR=20.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 81 0 81
3185  
3186 81 ▶ return $x if $x->modify('bfac') || $x->{sign} eq '+inf'; # inf => inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfac") or $x->{"sign"} eq "+inf" 3 78 81
Condition66%2/3
$x->modify("bfac") or $x->{"sign"} eq "+inf"
l!l&&r!l&&!r
0378
MC/DC50%1/2
$x->modify("bfac") or $x->{"sign"} eq "+inf"
$x->modify("bfac") $x->{"sign"} eq "+inf"
Truth table66%2/3
A or B
A= $x->modify("bfac")B= $x->{"sign"} eq "+inf"
ABresult
1X1
011
000
3187  
3188 78 ▶ return $x->bnan(@r) if $x->{sign} ne '+'; # NaN, <0 => NaN
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne "+" 9 69 78
3189  
3190 69 ▶ return $upgrade -> bfac($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 69 69
Condition33%1/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
6900
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3191 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3192  
3193 69 $x->{value} = $LIB->_fac($x->{value});
3194 69 $x->round(@r);
3195 }
3196  
3197 sub bdfac {
3198 # compute double factorial, modify $x in place
3199 54 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bdfac: called CC=10 SCAR=23.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 54 0 54
3200  
3201 54 ▶ return $x if $x->modify('bdfac') || $x->{sign} eq '+inf'; # inf => inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdfac") or $x->{"sign"} eq "+inf" 3 51 54
Condition66%2/3
$x->modify("bdfac") or $x->{"sign"} eq "+inf"
l!l&&r!l&&!r
0351
MC/DC50%1/2
$x->modify("bdfac") or $x->{"sign"} eq "+inf"
$x->modify("bdfac") $x->{"sign"} eq "+inf"
Truth table66%2/3
A or B
A= $x->modify("bdfac")B= $x->{"sign"} eq "+inf"
ABresult
1X1
011
000
3202  
3203 51 ▶ return $x->bnan(@r) if $x->is_nan() || $x <= -2;
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x <= -2 9 42 51
Condition100%3/3
$x->is_nan or $x <= -2
l!l&&r!l&&!r
3642
MC/DC0%0/2
$x->is_nan or $x <= -2
$x->is_nan $x <= -2
Truth table33%1/3
A or B
A= $x->is_nanB= $x <= -2
ABresult
1X1
011
000
3204 42 ▶ return $x->bone(@r) if $x <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= 1 9 33 42
3205  
3206 33 ▶ return $upgrade -> bdfac($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 33 33
Condition33%1/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
3300
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3207 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3208  
3209 33 ▶ croak("bdfac() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_dfac") 0 33 33
3210 unless $LIB->can('_dfac');
3211  
3212 33 $x->{value} = $LIB->_dfac($x->{value});
3213 33 $x->round(@r);
3214 }
3215  
3216 sub btfac {
3217 # compute triple factorial, modify $x in place
3218 57 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine btfac: called CC=10 SCAR=23.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 57 0 57
3219  
3220 57 ▶ return $x if $x->modify('btfac') || $x->{sign} eq '+inf'; # inf => inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btfac") or $x->{"sign"} eq "+inf" 3 54 57
Condition66%2/3
$x->modify("btfac") or $x->{"sign"} eq "+inf"
l!l&&r!l&&!r
0354
MC/DC50%1/2
$x->modify("btfac") or $x->{"sign"} eq "+inf"
$x->modify("btfac") $x->{"sign"} eq "+inf"
Truth table66%2/3
A or B
A= $x->modify("btfac")B= $x->{"sign"} eq "+inf"
ABresult
1X1
011
000
3221  
3222 54 ▶ return $x->bnan(@r) if $x->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 3 51 54
3223  
3224 51 ▶ return $upgrade -> btfac($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 51 51
Condition33%1/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
5100
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3225 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3226  
3227 51 my $k = $class -> new("3");
3228 51 ▶ return $x->bnan(@r) if $x <= -$k;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= -$k 6 45 51
3229  
3230 45 my $one = $class -> bone();
3231 45 ▶ return $x->bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 12 33 45
3232  
3233 33 my $f = $x -> copy();
3234 33 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 45 33 78
3235 45 $x = $x -> bmul($f);
3236 }
3237 33 $x->round(@r);
3238 }
3239  
3240 sub bmfac {
3241 # compute multi-factorial
3242  
3243 270 ▶ my ($class, $x, $k, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bmfac: called CC=13 SCAR=26.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 270 0 270
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00270
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
3244 ? (ref($_[0]), @_) : objectify(2, @_);
3245  
3246 270 ▶ return $x if $x->modify('bmfac') || $x->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmfac") or $x->{"sign"} eq "+inf" 15 255 270
Condition66%2/3
$x->modify("bmfac") or $x->{"sign"} eq "+inf"
l!l&&r!l&&!r
015255
MC/DC50%1/2
$x->modify("bmfac") or $x->{"sign"} eq "+inf"
$x->modify("bmfac") $x->{"sign"} eq "+inf"
Truth table66%2/3
A or B
A= $x->modify("bmfac")B= $x->{"sign"} eq "+inf"
ABresult
1X1
011
000
3247 255 ▶ return $x->bnan(@r) if $x->is_nan() || $k->is_nan() || $k < 1 || $x <= -$k;
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $k->is_nan or $k < 1 or $x <= -$k 39 198 237
Condition100%9/9
$x->is_nan or $k->is_nan or $k < 1 or $x <= -$k
l!l&&r!l&&!r
2730198
$x->is_nan or $k->is_nan or $k < 1
l!l&&r!l&&!r
189228
$x->is_nan or $k->is_nan
l!l&&r!l&&!r
153237
MC/DC0%0/4
$x->is_nan or $k->is_nan or $k < 1 or $x <= -$k
$x->is_nan $k->is_nan $k < 1 $x <= -$k
Truth table40%2/5
A or B or C or D
A= $x->is_nanB= $k->is_nanC= $k < 1D= $x <= -$k
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3248  
3249 198 ▶ return $upgrade -> bmfac($x, $k, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 198 198
Condition33%1/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
19800
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3250 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3251  
3252 198 my $one = $class -> bone();
3253 198 ▶ return $x->bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 60 138 198
3254  
3255 138 my $f = $x -> copy();
3256 138 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 213 138 351
3257 213 $x = $x -> bmul($f);
3258 }
3259 138 $x->round(@r);
3260 }
3261  
3262 sub bfib {
3263 # compute Fibonacci number(s)
3264 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bfib: not called CC=18 SCAR=58.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3265  
3266 0 ▶ croak("bfib() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_fib") 0 0 0
3267 unless $LIB->can('_fib');
3268  
3269 0 ▶ return $x if $x->modify('bfib');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfib") 0 0 0
3270  
3271 0 ▶ return $upgrade -> bfib($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3272 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3273  
3274 # List context.
3275  
3276 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
3277 0 ▶ return () if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
3278 0 ▶ croak("bfib() can't return an infinitely long list of numbers")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
3279 if $x -> is_inf();
3280  
3281 # Use the backend library to compute the first $x Fibonacci numbers.
3282  
3283 0 my @values = $LIB->_fib($x->{value});
3284  
3285 # Make objects out of them. The last element in the array is the
3286 # invocand.
3287  
3288 0 ▶ for (my $i = 0 ; $i < $#values ; ++ $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#values) 0 0 0
3289 0 my $fib = $class -> bzero();
3290 0 $fib -> {value} = $values[$i];
3291 0 $values[$i] = $fib;
3292 }
3293  
3294 0 $x -> {value} = $values[-1];
3295 0 $values[-1] = $x;
3296  
3297 # If negative, insert sign as appropriate.
3298  
3299 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
3300 0 ▶ for (my $i = 2 ; $i <= $#values ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#values) 0 0 0
3301 0 $values[$i]{sign} = '-';
3302 }
3303 }
3304  
3305 0 @values = map { $_ -> round(@r) } @values;
3306 0 return @values;
3307 }
3308  
3309 # Scalar context.
3310  
3311 else {
3312 0 ▶ return $x if $x->modify('bdfac') || $x -> is_inf('+');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdfac") or $x->is_inf("+") 0 0 0
Condition0%0/3
$x->modify("bdfac") or $x->is_inf("+")
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->modify("bdfac") or $x->is_inf("+")
$x->modify("bdfac") $x->is_inf("+")
Truth table0%0/3
A or B
A= $x->modify("bdfac")B= $x->is_inf("+")
ABresult
1X1
011
000
3313 0 ▶ return $x->bnan() if $x -> is_nan() || $x -> is_inf('-');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 0 0 0
Condition0%0/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table0%0/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
3314  
3315 0 ▶ $x->{sign} = $x -> is_neg() && $x -> is_even() ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->is_neg && $x->is_even ? : 0 0 0
Condition0%0/3
$x->is_neg and $x->is_even
!ll&&!rl&&r
000
MC/DC0%0/2
$x->is_neg and $x->is_even
$x->is_neg $x->is_even
Truth table0%0/3
A and B
A= $x->is_negB= $x->is_even
ABresult
0X0
100
111
3316 0 $x->{value} = $LIB->_fib($x->{value});
3317 0 return $x->round(@r);
3318 }
3319 }
3320  
3321 sub blucas {
3322 # compute Lucas number(s)
3323 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine blucas: not called CC=17 SCAR=57.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3324  
3325 0 ▶ croak("blucas() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_lucas") 0 0 0
3326 unless $LIB->can('_lucas');
3327  
3328 0 ▶ return $x if $x->modify('blucas');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blucas") 0 0 0
3329  
3330 0 ▶ return $upgrade -> blucas($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3331 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3332  
3333 # List context.
3334  
3335 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
3336 0 ▶ return () if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
3337 0 ▶ croak("blucas() can't return an infinitely long list of numbers")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
3338 if $x -> is_inf();
3339  
3340 # Use the backend library to compute the first $x Lucas numbers.
3341  
3342 0 my @values = $LIB->_lucas($x->{value});
3343  
3344 # Make objects out of them. The last element in the array is the
3345 # invocand.
3346  
3347 0 ▶ for (my $i = 0 ; $i < $#values ; ++ $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#values) 0 0 0
3348 0 my $lucas = $class -> bzero();
3349 0 $lucas -> {value} = $values[$i];
3350 0 $values[$i] = $lucas;
3351 }
3352  
3353 0 $x -> {value} = $values[-1];
3354 0 $values[-1] = $x;
3355  
3356 # If negative, insert sign as appropriate.
3357  
3358 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
3359 0 ▶ for (my $i = 2 ; $i <= $#values ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#values) 0 0 0
3360 0 $values[$i]{sign} = '-';
3361 }
3362 }
3363  
3364 0 @values = map { $_ -> round(@r) } @values;
3365 0 return @values;
3366 }
3367  
3368 # Scalar context.
3369  
3370 else {
3371 0 ▶ return $x if $x -> is_inf('+');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
3372 0 ▶ return $x->bnan() if $x -> is_nan() || $x -> is_inf('-');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 0 0 0
Condition0%0/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table0%0/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
3373  
3374 0 ▶ $x->{sign} = $x -> is_neg() && $x -> is_even() ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->is_neg && $x->is_even ? : 0 0 0
Condition0%0/3
$x->is_neg and $x->is_even
!ll&&!rl&&r
000
MC/DC0%0/2
$x->is_neg and $x->is_even
$x->is_neg $x->is_even
Truth table0%0/3
A and B
A= $x->is_negB= $x->is_even
ABresult
0X0
100
111
3375 0 $x->{value} = $LIB->_lucas($x->{value});
3376 0 return $x->round(@r);
3377 }
3378 }
3379  
3380 sub blsft {
3381 # (BINT or num_str, BINT or num_str) return BINT
3382 # compute $x << $y, base $n
3383  
3384 62 ▶ my ($class, $x, $y, $b, @r);
Subroutine blsft: called CC=27 SCAR=52.9
Pod documented
3385  
3386 # Objectify the base only when it is defined, since an undefined base, as
3387 # in $x->blsft(3) or $x->blog(3, undef) means use the default base 2.
3388  
3389 62 ▶ if (!ref($_[0]) && $_[0] =~ /^[A-Za-z]|::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/) { } 12 50 62
Condition66%2/3
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
!ll&&!rl&&r
50012
MC/DC50%1/2
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
not ref $_[0] $_[0] =~ /^[A-Za-z]|::/
Truth table66%2/3
not(A) and B
A= not ref $_[0]B= $_[0] =~ /^[A-Za-z]|::/
ABresult
0X0
100
111
3390 # E.g., Math::BigInt->blog(256, 5, 2)
3391 12 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 12 12
3392 defined $_[3] ? objectify(3, @_) : objectify(2, @_);
3393 } else {
3394 # E.g., Math::BigInt::blog(256, 5, 2) or $x->blog(5, 2)
3395 50 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 44 6 50
3396 defined $_[2] ? objectify(3, @_) : objectify(2, @_);
3397 }
3398  
3399 62 ▶ return $x if $x -> modify('blsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blsft") 0 62 62
3400  
3401 62 ▶ $b = 2 unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 18 44 62
3402 62 ▶ $b = $class -> new($b) unless defined(blessed($b));
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($b) 18 44 62
3403  
3404 62 ▶ return $upgrade -> blsft($x, $y, $b, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $b->isa(__PACKAGE__) 0 62 62
Condition44%4/9
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $b->isa(__PACKAGE__)
!ll&&!rl&&r
5480
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $b->isa(__PACKAGE__)
!ll&&!rl&&r
008
$x->isa(__PACKAGE__) and $y->isa(__PACKAGE__)
!ll&&!rl&&r
008
MC/DC0%0/4
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $b->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__) $b->isa(__PACKAGE__)
Truth table40%2/5
A and not(B and C && D)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)D= $b->isa(__PACKAGE__)
ABCDresult
0XXX0
11110
10XX1
110X1
11101
3405 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
3406 !$y -> isa(__PACKAGE__) ||
3407 !$b -> isa(__PACKAGE__));
3408  
3409 62 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan() || $b -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan or $b->is_nan 0 62 62
Condition33%2/6
$x->is_nan or $y->is_nan or $b->is_nan
l!l&&r!l&&!r
0062
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
0062
MC/DC0%0/3
$x->is_nan or $y->is_nan or $b->is_nan
$x->is_nan $y->is_nan $b->is_nan
Truth table25%1/4
A or B or C
A= $x->is_nanB= $y->is_nanC= $b->is_nan
ABCresult
1XX1
01X1
0011
0000
3410  
3411 # blsft($x, -$y, $b) = brsft($x, $y, $b)
3412  
3413 62 ▶ return $x -> brsft($y -> copy() -> bneg(), $b, @r) if $y -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 4 58 62
3414  
3415 58 return $x -> bmul($b -> bpow($y));
3416  
3417 # Base $b = 1 changes nothing, not even when $b = Inf. Shifting zero places
3418 # ($y = 0) doesn't change anything either.
3419 0 ▶ return $x -> bround(@r) if $b -> is_one("+") || $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $b->is_one("+") or $y->is_zero 0 0 0
Condition0%0/3
$b->is_one("+") or $y->is_zero
l!l&&r!l&&!r
000
MC/DC0%0/2
$b->is_one("+") or $y->is_zero
$b->is_one("+") $y->is_zero
Truth table0%0/3
A or B
A= $b->is_one("+")B= $y->is_zero
ABresult
1X1
011
000
3420  
3421 # Shifting infinitely far to the left.
3422 0 ▶ if ($y -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf("+")) 0 0 0
3423 0 ▶ return $x -> binf("+", @r) if $x -> is_pos();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_pos 0 0 0
3424 0 ▶ return $x -> binf("-", @r) if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
3425 0 return $x -> bnan(@r); # Inf * 0 = NaN
3426 }
3427  
3428 # At this point we know that $b > 1, so we are essentially computing 0 *
3429 # Inf = NaN.
3430 0 ▶ return $x -> bnan(@r) if $x -> is_zero() && $y -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero and $y->is_inf("+") 0 0 0
Condition0%0/3
$x->is_zero and $y->is_inf("+")
!ll&&!rl&&r
000
MC/DC0%0/2
$x->is_zero and $y->is_inf("+")
$x->is_zero $y->is_inf("+")
Truth table0%0/3
A and B
A= $x->is_zeroB= $y->is_inf("+")
ABresult
0X0
100
111
3431  
3432 # Handle trivial zero case.
3433 0 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3434  
3435 0 ▶ return $x -> binf("+", @r) if $y -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_inf("+") 0 0 0
3436 0 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3437  
3438 # While some of the libraries support an arbitrarily large base, not all of
3439 # them do, so rather than returning an incorrect result in those cases,
3440 # disallow bases that don't work with all libraries.
3441  
3442 0 my $uintmax = ~0;
3443 0 ▶ if ($x -> bcmp($uintmax) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bcmp($uintmax) > 0) { } 0 0 0
3444 0 $x = $x -> bmul($b -> bpow($y));
3445 } else {
3446 0 $b = $b -> numify();
3447 0 $x -> {value} = $LIB -> _lsft($x -> {value}, $y -> {value}, $b);
3448 }
3449 0 $x -> round(@r);
3450 }
3451  
3452 sub brsft {
3453 # (BINT or num_str, BINT or num_str) return BINT
3454 # compute $x >> $y, base $n
3455  
3456 134 ▶ my ($class, $x, $y, $b, @r) = (ref($_[0]), @_);
Subroutine brsft: called CC=26 SCAR=35.2
Pod documented
3457  
3458 # Objectify the base only when it is defined, since an undefined base, as
3459 # in $x->blsft(3) or $x->blog(3, undef) means use the default base 2.
3460  
3461 134 ▶ if (!ref($_[0]) && $_[0] =~ /^[A-Za-z]|::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/) { } 12 122 134
Condition66%2/3
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
!ll&&!rl&&r
122012
MC/DC50%1/2
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
not ref $_[0] $_[0] =~ /^[A-Za-z]|::/
Truth table66%2/3
not(A) and B
A= not ref $_[0]B= $_[0] =~ /^[A-Za-z]|::/
ABresult
0X0
100
111
3462 # E.g., Math::BigInt->blog(256, 5, 2)
3463 12 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 12 12
3464 defined $_[3] ? objectify(3, @_) : objectify(2, @_);
3465 } else {
3466 # E.g., Math::BigInt::blog(256, 5, 2) or $x->blog(5, 2)
3467 122 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 116 6 122
3468 defined $_[2] ? objectify(3, @_) : objectify(2, @_);
3469 }
3470  
3471 134 ▶ return $x if $x -> modify('brsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("brsft") 0 134 134
3472  
3473 134 ▶ $b = 2 unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 18 116 134
3474 134 ▶ $b = $class -> new($b) unless defined(blessed($b));
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($b) 18 116 134
3475  
3476 134 ▶ return $upgrade -> brsft($x, $y, $b, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $b->isa(__PACKAGE__) 0 134 134
Condition44%4/9
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $b->isa(__PACKAGE__)
!ll&&!rl&&r
120140
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $b->isa(__PACKAGE__)
!ll&&!rl&&r
0014
$x->isa(__PACKAGE__) and $y->isa(__PACKAGE__)
!ll&&!rl&&r
0014
MC/DC0%0/4
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) && $b->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__) $b->isa(__PACKAGE__)
Truth table40%2/5
A and not(B and C && D)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)D= $b->isa(__PACKAGE__)
ABCDresult
0XXX0
11110
10XX1
110X1
11101
3477 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
3478 !$y -> isa(__PACKAGE__) ||
3479 !$b -> isa(__PACKAGE__));
3480  
3481 134 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan() || $b -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan or $b->is_nan 0 134 134
Condition33%2/6
$x->is_nan or $y->is_nan or $b->is_nan
l!l&&r!l&&!r
00134
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
00134
MC/DC0%0/3
$x->is_nan or $y->is_nan or $b->is_nan
$x->is_nan $y->is_nan $b->is_nan
Truth table25%1/4
A or B or C
A= $x->is_nanB= $y->is_nanC= $b->is_nan
ABCresult
1XX1
01X1
0011
0000
3482  
3483 # brsft($x, -$y, $b) = blsft($x, $y, $b)
3484  
3485 134 ▶ return $x -> blsft($y -> copy() -> bneg(), $b, @r) if $y -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 4 130 134
3486  
3487 130 ▶ return $x -> round(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 8 122 130
3488 122 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 122 122
3489  
3490 # Shifting right by a positive amount might lead to a non-integer result.
3491  
3492 122 ▶ return $upgrade -> brsft($x, $y, $b, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and $y->is_pos 11 111 122
Condition66%2/3
defined $Math::BigInt::upgrade and $y->is_pos
!ll&&!rl&&r
111011
MC/DC50%1/2
defined $Math::BigInt::upgrade and $y->is_pos
defined $Math::BigInt::upgrade $y->is_pos
Truth table66%2/3
A and B
A= defined $Math::BigInt::upgradeB= $y->is_pos
ABresult
0X0
100
111
3493 if defined($upgrade) && $y -> is_pos();
3494  
3495 # This only works for negative numbers when shifting in base 2.
3496 111 ▶ if ($x -> is_neg() && $b -> bcmp("2") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg and $b->bcmp(2) == 0) 57 54 111
Condition66%2/3
$x->is_neg and $b->bcmp(2) == 0
!ll&&!rl&&r
54057
MC/DC50%1/2
$x->is_neg and $b->bcmp(2) == 0
$x->is_neg $b->bcmp(2) == 0
Truth table66%2/3
A and B
A= $x->is_negB= $b->bcmp(2) == 0
ABresult
0X0
100
111
3497 57 ▶ return $x -> round(@r) if $x -> is_one('-'); # -1 => -1
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 3 54 57
3498 # Although this is O(N*N) in Math::BigInt::Calc->_as_bin(), it is O(N)
3499 # in Pari et al., but perhaps there is a better emulation for two's
3500 # complement shift ... if $y != 1, we must simulate it by doing:
3501 # convert to bin, flip all bits, shift, and be done
3502 54 $x = $x -> binc(); # -3 => -2
3503 54 my $bin = $x -> to_bin(); # convert to string
3504 54 $bin =~ s/^-//; # strip leading minus
3505 54 $bin =~ tr/10/01/; # flip bits
3506 54 my $nbits = CORE::length($bin);
3507 54 ▶ return $x -> bone("-", @r) if $y >= $nbits;
Branch: 1 branch
BranchTrueFalseTotal
if $y >= $nbits 3 51 54
3508 51 $bin = substr $bin, 0, $nbits - $y; # keep most significant bits
3509 51 $bin = '1' . $bin; # prepend one dummy '1'
3510 51 $bin =~ tr/10/01/; # flip bits back
3511 51 my $res = $class -> from_bin($bin); # convert back from string
3512 51 $res = $res -> binc(); # remember to increment
3513 51 $x -> {value} = $res -> {value}; # take over value
3514 51 return $x -> round(@r);
3515 }
3516  
3517 # While some of the libraries support an arbitrarily large base, not all of
3518 # them do, so rather than returning an incorrect result in those cases, use
3519 # division.
3520  
3521 54 my $uintmax = ~0;
3522 54 ▶ if ($x -> bcmp($uintmax) > 0 || $x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bcmp($uintmax) > 0 or $x->is_neg) { } 0 54 54
Condition33%1/3
$x->bcmp($uintmax) > 0 or $x->is_neg
l!l&&r!l&&!r
0054
MC/DC0%0/2
$x->bcmp($uintmax) > 0 or $x->is_neg
$x->bcmp($uintmax) > 0 $x->is_neg
Truth table33%1/3
A or B
A= $x->bcmp($uintmax) > 0B= $x->is_neg
ABresult
1X1
011
000
3523 0 $x = $x -> bdiv($b -> bpow($y));
3524 } else {
3525 54 $b = $b -> numify();
3526 54 $x -> {value} = $LIB -> _rsft($x -> {value}, $y -> {value}, $b);
3527 }
3528  
3529 54 return $x -> round(@r);
3530 }
3531  
3532 ###############################################################################
3533 # Bitwise methods
3534 ###############################################################################
3535  
3536 # Bitwise left shift.
3537  
3538 sub bblsft {
3539 # We don't call objectify(), because the bitwise methods should not
3540 # upgrade/downgrade, even when upgrading/downgrading is enabled.
3541  
3542 35 ▶ my ($class, $x, $y, @r);
Subroutine bblsft: called CC=21 SCAR=39.9
Pod documented
3543  
3544 # $x -> bblsft($y)
3545  
3546 35 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 27 8 35
3547 27 ($class, $x, $y, @r) = (ref($_[0]), @_);
3548 27 ▶ $y = $y -> as_int()
Branch: 1 branch
BranchTrueFalseTotal
if ref $y and not $y->isa(__PACKAGE__) and $y->can("as_int") 0 27 27
Condition33%2/6
ref $y and not $y->isa(__PACKAGE__) and $y->can("as_int")
!ll&&!rl&&r
2700
ref $y and not $y->isa(__PACKAGE__)
!ll&&!rl&&r
0270
MC/DC0%0/3
ref $y and not $y->isa(__PACKAGE__) and $y->can("as_int")
ref $y not $y->isa(__PACKAGE__) $y->can("as_int")
Truth table25%1/4
A and not(B) and C
A= ref $yB= not $y->isa(__PACKAGE__)C= $y->can("as_int")
ABCresult
0XX0
10X0
1100
1111
3549 if ref($y) && !$y -> isa(__PACKAGE__) && $y -> can('as_int');
3550 27 ▶ $y = $class -> new(int($y)) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 27 27
3551 }
3552  
3553 # $class -> bblsft($x, $y)
3554  
3555 else {
3556 8 ($class, $x, $y, @r) = @_;
3557 8 for ($x, $y) {
3558 16 ▶ $_ = $_ -> as_int()
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ and not $_->isa(__PACKAGE__) and $_->can("as_int") 16 0 16
Condition33%2/6
ref $_ and not $_->isa(__PACKAGE__) and $_->can("as_int")
!ll&&!rl&&r
0016
ref $_ and not $_->isa(__PACKAGE__)
!ll&&!rl&&r
0016
MC/DC0%0/3
ref $_ and not $_->isa(__PACKAGE__) and $_->can("as_int")
ref $_ not $_->isa(__PACKAGE__) $_->can("as_int")
Truth table25%1/4
A and not(B) and C
A= ref $_B= not $_->isa(__PACKAGE__)C= $_->can("as_int")
ABCresult
0XX0
10X0
1100
1111
3559 if ref($_) && !$_ -> isa(__PACKAGE__) && $_ -> can('as_int');
3560 16 ▶ $_ = $class -> new(int($_)) unless ref($_);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 16 16
3561 }
3562 }
3563  
3564 35 ▶ return $x if $x -> modify('bblsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bblsft") 0 35 35
3565  
3566 35 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 4 31 35
Condition66%2/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
4031
MC/DC50%1/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table66%2/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
3567  
3568 # bblsft($x, -$y) = bbrsft($x, $y)
3569  
3570 31 ▶ return $x -> bbrsft($y -> copy() -> bneg()) if $y -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 4 27 31
3571  
3572 # Shifting infinitely far to the left.
3573  
3574 27 ▶ if ($y -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf("+")) 0 27 27
3575 0 ▶ return $x -> binf("+", @r) if $x -> is_pos();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_pos 0 0 0
3576 0 ▶ return $x -> binf("-", @r) if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
3577 0 return $x -> bnan(@r);
3578 }
3579  
3580 # These cases change nothing.
3581  
3582 27 ▶ return $x -> round(@r) if $x -> is_zero() || $x -> is_inf() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_inf or $y->is_zero 0 27 27
Condition33%2/6
$x->is_zero or $x->is_inf or $y->is_zero
l!l&&r!l&&!r
0027
$x->is_zero or $x->is_inf
l!l&&r!l&&!r
0027
MC/DC0%0/3
$x->is_zero or $x->is_inf or $y->is_zero
$x->is_zero $x->is_inf $y->is_zero
Truth table25%1/4
A or B or C
A= $x->is_zeroB= $x->is_infC= $y->is_zero
ABCresult
1XX1
01X1
0011
0000
3583 $y -> is_zero();
3584  
3585 27 $x -> {value} = $LIB -> _lsft($x -> {value}, $y -> {value}, 2);
3586 27 $x -> round(@r);
3587 }
3588  
3589 # Bitwise right shift.
3590  
3591 sub bbrsft {
3592 # We don't call objectify(), because the bitwise methods should not
3593 # upgrade/downgrade, even when upgrading/downgrading is enabled.
3594  
3595 35 ▶ my ($class, $x, $y, @r);
Subroutine bbrsft: called CC=23 SCAR=43.7
Pod documented
3596  
3597 # $x -> bblsft($y)
3598  
3599 35 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 27 8 35
3600 27 ($class, $x, $y, @r) = (ref($_[0]), @_);
3601 27 ▶ $y = $y -> as_int()
Branch: 1 branch
BranchTrueFalseTotal
if ref $y and not $y->isa(__PACKAGE__) and $y->can("as_int") 0 27 27
Condition33%2/6
ref $y and not $y->isa(__PACKAGE__) and $y->can("as_int")
!ll&&!rl&&r
2700
ref $y and not $y->isa(__PACKAGE__)
!ll&&!rl&&r
0270
MC/DC0%0/3
ref $y and not $y->isa(__PACKAGE__) and $y->can("as_int")
ref $y not $y->isa(__PACKAGE__) $y->can("as_int")
Truth table25%1/4
A and not(B) and C
A= ref $yB= not $y->isa(__PACKAGE__)C= $y->can("as_int")
ABCresult
0XX0
10X0
1100
1111
3602 if ref($y) && !$y -> isa(__PACKAGE__) && $y -> can('as_int');
3603 27 ▶ $y = $class -> new(int($y)) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 27 27
3604 }
3605  
3606 # $class -> bblsft($x, $y)
3607  
3608 else {
3609 8 ($class, $x, $y, @r) = @_;
3610 8 for ($x, $y) {
3611 16 ▶ $_ = $_ -> as_int()
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ and not $_->isa(__PACKAGE__) and $_->can("as_int") 16 0 16
Condition33%2/6
ref $_ and not $_->isa(__PACKAGE__) and $_->can("as_int")
!ll&&!rl&&r
0016
ref $_ and not $_->isa(__PACKAGE__)
!ll&&!rl&&r
0016
MC/DC0%0/3
ref $_ and not $_->isa(__PACKAGE__) and $_->can("as_int")
ref $_ not $_->isa(__PACKAGE__) $_->can("as_int")
Truth table25%1/4
A and not(B) and C
A= ref $_B= not $_->isa(__PACKAGE__)C= $_->can("as_int")
ABCresult
0XX0
10X0
1100
1111
3612 if ref($_) && !$_ -> isa(__PACKAGE__) && $_ -> can('as_int');
3613 16 ▶ $_ = $class -> new(int($_)) unless ref($_);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 16 16
3614 }
3615 }
3616  
3617 35 ▶ return $x if $x -> modify('bbrsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bbrsft") 0 35 35
3618  
3619 35 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 4 31 35
Condition66%2/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
4031
MC/DC50%1/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table66%2/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
3620  
3621 # bbrsft($x, -$y) = bblsft($x, $y)
3622  
3623 31 ▶ return $x -> bblsft($y -> copy() -> bneg()) if $y -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 4 27 31
3624  
3625 # Shifting infinitely far to the right.
3626  
3627 27 ▶ if ($y -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf("+")) 0 27 27
3628 0 ▶ return $x -> bnan(@r) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
3629 0 ▶ return $x -> bone("-", @r) if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
3630 0 return $x -> bzero(@r);
3631 }
3632  
3633 # These cases change nothing.
3634  
3635 27 ▶ return $x -> round(@r) if $x -> is_zero() || $x -> is_inf() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_inf or $y->is_zero 0 27 27
Condition33%2/6
$x->is_zero or $x->is_inf or $y->is_zero
l!l&&r!l&&!r
0027
$x->is_zero or $x->is_inf
l!l&&r!l&&!r
0027
MC/DC0%0/3
$x->is_zero or $x->is_inf or $y->is_zero
$x->is_zero $x->is_inf $y->is_zero
Truth table25%1/4
A or B or C
A= $x->is_zeroB= $x->is_infC= $y->is_zero
ABCresult
1XX1
01X1
0011
0000
3636 $y -> is_zero();
3637  
3638 # At this point, $x is either positive or negative, not zero.
3639  
3640 27 ▶ if ($x -> is_pos()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_pos) { } 27 0 27
3641 27 $x -> {value} = $LIB -> _rsft($x -> {value}, $y -> {value}, 2);
3642 } else {
3643 0 my $n = $x -> {value};
3644 0 my $d = $LIB -> _pow($LIB -> _new("2"), $y -> {value});
3645 0 my ($p, $q) = $LIB -> _div($n, $d);
3646 0 ▶ $p = $LIB -> _inc($p) unless $LIB -> _is_zero($q);
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_zero($q) 0 0 0
3647 0 $x -> {value} = $p;
3648 }
3649  
3650 27 $x -> round(@r);
3651 }
3652  
3653 sub band {
3654 #(BINT or num_str, BINT or num_str) return BINT
3655 # compute x & y
3656  
3657 175 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine band: called CC=11 SCAR=24.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 174 1 175
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
01174
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
3658 ? (ref($_[0]), @_) : objectify(2, @_);
3659  
3660 175 ▶ return $x if $x->modify('band');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("band") 0 175 175
3661  
3662 175 ▶ return $upgrade -> band($x, $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 1 174 175
Condition83%5/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
141331
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
0133
MC/DC66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table75%3/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
3663 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
3664 !$y -> isa(__PACKAGE__));
3665  
3666 174 $r[3] = $y; # no push!
3667  
3668 174 ▶ return $x->bnan(@r) if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/ 12 162 174
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
84162
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
$x->{"sign"} =~ /^[+-]$/ $y->{"sign"} =~ /^[+-]$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $y->{"sign"} =~ /^[+-]$/
ABresult
0X0
100
111
3669  
3670 162 ▶ if ($x->{sign} eq '+' && $y->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '+' and $y->{'sign'} eq '+') { } 129 33 162
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "+"
!ll&&!rl&&r
249129
MC/DC100%2/2
$x->{"sign"} eq "+" and $y->{"sign"} eq "+"
$x->{"sign"} eq "+" $y->{"sign"} eq "+"
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $y->{"sign"} eq "+"
ABresult
0X0
100
111
3671 129 $x->{value} = $LIB->_and($x->{value}, $y->{value});
3672 } else {
3673 ($x->{value}, $x->{sign}) = $LIB->_sand($x->{value}, $x->{sign},
3674 33 $y->{value}, $y->{sign});
3675 }
3676 162 return $x->round(@r);
3677 }
3678  
3679 sub bior {
3680 #(BINT or num_str, BINT or num_str) return BINT
3681 # compute x | y
3682  
3683 236 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bior: called CC=11 SCAR=24.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 235 1 236
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
01235
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
3684 ? (ref($_[0]), @_) : objectify(2, @_);
3685  
3686 236 ▶ return $x if $x->modify('bior');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bior") 0 236 236
3687  
3688 236 ▶ return $upgrade -> bior($x, $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 1 235 236
Condition83%5/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
189461
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
0146
MC/DC66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table75%3/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
3689 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
3690 !$y -> isa(__PACKAGE__));
3691  
3692 235 $r[3] = $y; # no push!
3693  
3694 235 ▶ return $x->bnan() if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/ 12 223 235
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
84223
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
$x->{"sign"} =~ /^[+-]$/ $y->{"sign"} =~ /^[+-]$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $y->{"sign"} =~ /^[+-]$/
ABresult
0X0
100
111
3695  
3696 223 ▶ if ($x->{sign} eq '+' && $y->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '+' and $y->{'sign'} eq '+') { } 188 35 223
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "+"
!ll&&!rl&&r
2312188
MC/DC100%2/2
$x->{"sign"} eq "+" and $y->{"sign"} eq "+"
$x->{"sign"} eq "+" $y->{"sign"} eq "+"
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $y->{"sign"} eq "+"
ABresult
0X0
100
111
3697 188 $x->{value} = $LIB->_or($x->{value}, $y->{value});
3698 } else {
3699 ($x->{value}, $x->{sign}) = $LIB->_sor($x->{value}, $x->{sign},
3700 35 $y->{value}, $y->{sign});
3701 }
3702 223 return $x->round(@r);
3703 }
3704  
3705 sub bxor {
3706 #(BINT or num_str, BINT or num_str) return BINT
3707 # compute x ^ y
3708  
3709 246 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bxor: called CC=11 SCAR=24.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 245 1 246
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
01245
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
3710 ? (ref($_[0]), @_) : objectify(2, @_);
3711  
3712 246 ▶ return $x if $x->modify('bxor');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bxor") 0 246 246
3713  
3714 246 ▶ return $upgrade -> bxor($x, $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__) 1 245 246
Condition83%5/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
198471
$x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
!ll&&!rl&&r
0147
MC/DC66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $y->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $y->isa(__PACKAGE__)
Truth table75%3/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $y->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
3715 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
3716 !$y -> isa(__PACKAGE__));
3717  
3718 245 $r[3] = $y; # no push!
3719  
3720 245 ▶ return $x->bnan(@r) if $x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/ 12 233 245
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
84233
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
$x->{"sign"} =~ /^[+-]$/ $y->{"sign"} =~ /^[+-]$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $y->{"sign"} =~ /^[+-]$/
ABresult
0X0
100
111
3721  
3722 233 ▶ if ($x->{sign} eq '+' && $y->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '+' and $y->{'sign'} eq '+') { } 193 40 233
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "+"
!ll&&!rl&&r
2713193
MC/DC100%2/2
$x->{"sign"} eq "+" and $y->{"sign"} eq "+"
$x->{"sign"} eq "+" $y->{"sign"} eq "+"
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $y->{"sign"} eq "+"
ABresult
0X0
100
111
3723 193 $x->{value} = $LIB->_xor($x->{value}, $y->{value});
3724 } else {
3725 ($x->{value}, $x->{sign}) = $LIB->_sxor($x->{value}, $x->{sign},
3726 40 $y->{value}, $y->{sign});
3727 }
3728 233 return $x->round(@r);
3729 }
3730  
3731 sub bnot {
3732 # (num_str or BINT) return BINT
3733 # represent ~x as twos-complement number
3734 39 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bnot: called CC=5 SCAR=17.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 39 0 39
3735  
3736 39 ▶ return $x if $x->modify('bnot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bnot") 0 39 39
3737  
3738 39 ▶ return $upgrade -> bnot($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 39 39
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
3270
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3739 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3740  
3741 39 $x -> binc() -> bneg(@r);
3742 }
3743  
3744 ###############################################################################
3745 # Rounding methods
3746 ###############################################################################
3747  
3748 sub round {
3749 # Round $self according to given parameters, or given second argument's
3750 # parameters or global defaults
3751  
3752 71804 ▶ my ($class, $self, @args) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine round: called CC=35 SCAR=35.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 71804 0 71804
3753  
3754 # $x->round(undef, undef) signals no rounding
3755  
3756 71804 ▶ if (@args >= 2 && @args <= 3 && !defined($args[0]) && !defined($args[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args >= 2 and @args <= 3 and not defined $args[0] and not defined $args[1]) 3599 68205 71804
Condition100%9/9
@args >= 2 and @args <= 3 and not defined $args[0] and not defined $args[1]
!ll&&!rl&&r
68149563599
@args >= 2 and @args <= 3 and not defined $args[0]
!ll&&!rl&&r
68102473655
@args >= 2 and @args <= 3
!ll&&!rl&&r
27844402583702
MC/DC100%4/4
@args >= 2 and @args <= 3 and not defined $args[0] and not defined $args[1]
@args >= 2 @args <= 3 not defined $args[0] not defined $args[1]
Truth table100%5/5
A and B and not(C) and not(D)
A= @args >= 2B= @args <= 3C= not defined $args[0]D= not defined $args[1]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
3757 3599 $self->{_a} = undef;
3758 3599 $self->{_p} = undef;
3759 3599 return $self;
3760 }
3761  
3762 68205 my ($a, $p, $r) = splice @args, 0, 3;
3763  
3764 # $a accuracy, if given by caller
3765 # $p precision, if given by caller
3766 # $r round_mode, if given by caller
3767 # @args all 'other' arguments (0 for unary, 1 for binary ops)
3768  
3769 68205 ▶ if (defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a) 304 67901 68205
3770 304 ▶ croak "accuracy must be a number, not '$a'"
Branch: 1 branch
BranchTrueFalseTotal
unless $a =~ /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/ 0 304 304
3771 unless $a =~/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/;
3772 }
3773  
3774 68205 ▶ if (defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p) 92 68113 68205
3775 92 ▶ croak "precision must be a number, not '$p'"
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/ 0 92 92
3776 unless $p =~/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/;
3777 }
3778  
3779 # now pick $a or $p, but only if we have got "arguments"
3780 68205 ▶ if (!defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a) 67901 304 68205
3781 67901 foreach ($self, @args) {
3782 # take the defined one, or if both defined, the one that is smaller
3783 $a = $_->{_a}
3784 108121 ▶ if (defined $_->{_a}) && (!defined $a || $_->{_a} < $a);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"_a"} and !defined($a) || $_->{"_a"} < $a 20886 87235 108121
Condition100%6/6
defined $_->{"_a"} and !defined($a) || $_->{"_a"} < $a
!ll&&!rl&&r
737653427086
not defined $a or $_->{"_a"} < $a
l!l&&r!l&&!r
208691713470
MC/DC66%2/3
defined $_->{"_a"} and !defined($a) || $_->{"_a"} < $a
defined $_->{"_a"} not defined $a $_->{"_a"} < $a
Truth table75%3/4
A and not(B) or C
A= defined $_->{"_a"}B= not defined $aC= $_->{"_a"} < $a
ABCresult
0XX0
1000
11X1
1011
3785 }
3786 }
3787 68205 ▶ if (!defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p) 68113 92 68205
3788 # even if $a is defined, take $p, to signal error for both defined
3789 68113 foreach ($self, @args) {
3790 # take the defined one, or if both defined, the one that is bigger
3791 # -2 > -3, and 3 > 2
3792 $p = $_->{_p}
3793 108356 ▶ if (defined $_->{_p}) && (!defined $p || $_->{_p} > $p);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"_p"} and !defined($p) || $_->{"_p"} > $p 111 108245 108356
Condition66%4/6
defined $_->{"_p"} and !defined($p) || $_->{"_p"} > $p
!ll&&!rl&&r
1082421140
not defined $p or $_->{"_p"} > $p
l!l&&r!l&&!r
11103
MC/DC0%0/3
defined $_->{"_p"} and !defined($p) || $_->{"_p"} > $p
defined $_->{"_p"} not defined $p $_->{"_p"} > $p
Truth table50%2/4
A and not(B) or C
A= defined $_->{"_p"}B= not defined $pC= $_->{"_p"} > $p
ABCresult
0XX0
1000
11X1
1011
3794 }
3795 }
3796  
3797 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
3798  
3799 # if still none defined, use globals
3800 68205 ▶ unless (defined $a || defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a or defined $p) 46880 21325 68205
Condition100%3/3
defined $a or defined $p
l!l&&r!l&&!r
2117315246880
MC/DC100%2/2
defined $a or defined $p
defined $a defined $p
Truth table100%3/3
A or B
A= defined $aB= defined $p
ABresult
1X1
011
000
3801 46880 $a = ${"$class\::accuracy"};
3802 46880 $p = ${"$class\::precision"};
3803 }
3804  
3805 # A == 0 is useless, so undef it to signal no rounding
3806 68205 ▶ $a = undef if defined $a && $a == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and $a == 0 6 68199 68205
Condition100%3/3
defined $a and $a == 0
!ll&&!rl&&r
47001211986
MC/DC100%2/2
defined $a and $a == 0
defined $a $a == 0
Truth table100%3/3
A and B
A= defined $aB= $a == 0
ABresult
0X0
100
111
3807  
3808 # no rounding today?
3809 68205 ▶ return $self unless defined $a || defined $p; # early out
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a or defined $p 46842 21363 68205
Condition100%3/3
defined $a or defined $p
l!l&&r!l&&!r
2119816546842
MC/DC100%2/2
defined $a or defined $p
defined $a defined $p
Truth table100%3/3
A or B
A= defined $aB= defined $p
ABresult
1X1
011
000
3810  
3811 # set A and set P is an fatal error
3812 21363 ▶ return $self->bnan() if defined $a && defined $p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and defined $p 58 21305 21363
Condition100%3/3
defined $a and defined $p
!ll&&!rl&&r
1652114058
MC/DC100%2/2
defined $a and defined $p
defined $a defined $p
Truth table100%3/3
A and B
A= defined $aB= defined $p
ABresult
0X0
100
111
3813  
3814 21305 ▶ $r = ${"$class\::round_mode"} unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 21244 61 21305
3815 21305 ▶ if ($r !~ /^(even|odd|[+-]inf|zero|trunc|common)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r =~ /^(even|odd|[+-]inf|zero|trunc|common)$/) 0 21305 21305
3816 0 croak("Unknown round mode '$r'");
3817 }
3818  
3819 # now round, by calling either bround or bfround:
3820 21305 ▶ if (defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a) { } 21140 165 21305
3821 $self = $self->bround(int($a), $r)
3822 21140 ▶ if !defined $self->{_a} || $self->{_a} >= $a;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"_a"} or $self->{"_a"} >= $a 21134 6 21140
Condition100%3/3
not defined $self->{"_a"} or $self->{"_a"} >= $a
l!l&&r!l&&!r
418207166
MC/DC100%2/2
not defined $self->{"_a"} or $self->{"_a"} >= $a
not defined $self->{"_a"} $self->{"_a"} >= $a
Truth table100%3/3
not(A) or B
A= not defined $self->{"_a"}B= $self->{"_a"} >= $a
ABresult
1X1
011
000
3823 } else { # both can't be undefined due to early out
3824 $self = $self->bfround(int($p), $r)
3825 165 ▶ if !defined $self->{_p} || $self->{_p} <= $p;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"_p"} or $self->{"_p"} <= $p 165 0 165
Condition66%2/3
not defined $self->{"_p"} or $self->{"_p"} <= $p
l!l&&r!l&&!r
123420
MC/DC0%0/2
not defined $self->{"_p"} or $self->{"_p"} <= $p
not defined $self->{"_p"} $self->{"_p"} <= $p
Truth table66%2/3
not(A) or B
A= not defined $self->{"_p"}B= $self->{"_p"} <= $p
ABresult
1X1
011
000
3826 }
3827  
3828 # bround() or bfround() already called bnorm() if nec.
3829 21305 $self;
3830 }
3831  
3832 sub bround {
3833 # accuracy: +$n preserve $n digits from left,
3834 # -$n preserve $n digits from right (f.i. for 0.1234 style in MBF)
3835 # no-op for $n == 0
3836 # and overwrite the rest with 0's, return normalized number
3837 # do not return $x->bnorm(), but $x
3838  
3839 26041 ▶ my ($class, $x, @a) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bround: called CC=45 SCAR=38.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 26041 0 26041
3840  
3841 26041 my ($scale, $mode) = $x->_scale_a(@a);
3842 26041 ▶ return $x if !defined $scale || $x->modify('bround'); # no-op
Branch: 1 branch
BranchTrueFalseTotal
if not defined $scale or $x->modify("bround") 2 26039 26041
Condition66%2/3
not defined $scale or $x->modify("bround")
l!l&&r!l&&!r
2026039
MC/DC50%1/2
not defined $scale or $x->modify("bround")
not defined $scale $x->modify("bround")
Truth table66%2/3
not(A) or B
A= not defined $scaleB= $x->modify("bround")
ABresult
1X1
011
000
3843  
3844 26039 ▶ if ($x->is_zero() || $scale == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero or $scale == 0) 101 25938 26039
Condition100%3/3
$x->is_zero or $scale == 0
l!l&&r!l&&!r
821925938
MC/DC100%2/2
$x->is_zero or $scale == 0
$x->is_zero $scale == 0
Truth table100%3/3
A or B
A= $x->is_zeroB= $scale == 0
ABresult
1X1
011
000
3845 101 ▶ $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; # 3 > 2
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x->{"_a"} or $x->{"_a"} > $scale 89 12 101
Condition66%2/3
not defined $x->{"_a"} or $x->{"_a"} > $scale
l!l&&r!l&&!r
89012
MC/DC50%1/2
not defined $x->{"_a"} or $x->{"_a"} > $scale
not defined $x->{"_a"} $x->{"_a"} > $scale
Truth table66%2/3
not(A) or B
A= not defined $x->{"_a"}B= $x->{"_a"} > $scale
ABresult
1X1
011
000
3846 101 return $x;
3847 }
3848 25938 ▶ return $x if $x->{sign} !~ /^[+-]$/; # inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 25926 25938
3849  
3850 # we have fewer digits than we want to scale to
3851 25926 my $len = $x->length();
3852 # convert $scale to a scalar in case it is an object (put's a limit on the
3853 # number length, but this would already limited by memory constraints),
3854 # makes it faster
3855 25926 ▶ $scale = $scale->numify() if ref ($scale);
Branch: 1 branch
BranchTrueFalseTotal
if ref $scale 0 25926 25926
3856  
3857 # scale < 0, but > -len (not >=!)
3858 25926 ▶ if (($scale < 0 && $scale < -$len-1) || ($scale >= $len)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < 0 and $scale < -$len - 1 or $scale >= $len) 194 25732 25926
Condition66%4/6
$scale < 0 and $scale < -$len - 1 or $scale >= $len
l!l&&r!l&&!r
019425732
$scale < 0 and $scale < -$len - 1
!ll&&!rl&&r
257921340
MC/DC33%1/3
$scale < 0 and $scale < -$len - 1 or $scale >= $len
$scale < 0 $scale < -$len - 1 $scale >= $len
Truth table60%3/5
A and B or C
A= $scale < 0B= $scale < -$len - 1C= $scale >= $len
ABCresult
11X1
0X11
1011
0X00
1000
3859 194 ▶ $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale; # 3 > 2
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x->{"_a"} or $x->{"_a"} > $scale 164 30 194
Condition66%2/3
not defined $x->{"_a"} or $x->{"_a"} > $scale
l!l&&r!l&&!r
164030
MC/DC50%1/2
not defined $x->{"_a"} or $x->{"_a"} > $scale
not defined $x->{"_a"} $x->{"_a"} > $scale
Truth table66%2/3
not(A) or B
A= not defined $x->{"_a"}B= $x->{"_a"} > $scale
ABresult
1X1
011
000
3860 194 return $x;
3861 }
3862  
3863 # count of 0's to pad, from left (+) or right (-): 9 - +6 => 3, or |-6| => 6
3864 25732 my ($pad, $digit_round, $digit_after);
3865 25732 $pad = $len - $scale;
3866 25732 ▶ $pad = abs($scale-1) if $scale < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $scale < 0 134 25598 25732
3867  
3868 # do not use digit(), it is very costly for binary => decimal
3869 # getting the entire string is also costly, but we need to do it only once
3870 25732 my $xs = $LIB->_str($x->{value});
3871 25732 my $pl = -$pad-1;
3872  
3873 # pad: 123: 0 => -1, at 1 => -2, at 2 => -3, at 3 => -4
3874 # pad+1: 123: 0 => 0, at 1 => -1, at 2 => -2, at 3 => -3
3875 25732 $digit_round = '0';
3876 25732 ▶ $digit_round = substr($xs, $pl, 1) if $pad <= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $pad <= $len 25610 122 25732
3877 25732 $pl++;
3878 25732 ▶ $pl ++ if $pad >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $pad >= $len 125 25607 25732
3879 25732 $digit_after = '0';
3880 25732 ▶ $digit_after = substr($xs, $pl, 1) if $pad > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pad > 0 25732 0 25732
3881  
3882 # in case of 01234 we round down, for 6789 up, and only in case 5 we look
3883 # closer at the remaining digits of the original $x, remember decision
3884 25732 my $round_up = 1; # default round up
3885 $round_up -- if
3886 ($mode eq 'trunc') || # trunc by round down
3887 ($digit_after =~ /[01234]/) || # round down anyway,
3888 # 6789 => round up
3889 ($digit_after eq '5') && # not 5000...0000
3890 ($x->_scan_for_nonzero($pad, $xs, $len) == 0) &&
3891 (
3892 ($mode eq 'even') && ($digit_round =~ /[24680]/) ||
3893 ($mode eq 'odd') && ($digit_round =~ /[13579]/) ||
3894 ($mode eq '+inf') && ($x->{sign} eq '-') ||
3895 25732 ▶ ($mode eq '-inf') && ($x->{sign} eq '+') ||
Branch: 1 branch
BranchTrueFalseTotal
if $mode eq "trunc" or $digit_after =~ /[01234]/ or $digit_after eq 5 and $x->_scan_for_nonzero($pad, $xs, $len) == 0 and $mode eq "even" && $digit_round =~ /[24680]/ || $mode eq "odd" && $digit_round =~ /[13579]/ || $mode eq "+inf" && $x->{"sign"} eq "-" || $mode eq "-inf" && $x->{"sign"} eq "+" || $mode eq "zero" 12871 12381 25252
Condition100%36/36
$mode eq "trunc" or $digit_after =~ /[01234]/ or $digit_after eq 5 and $x->_scan_for_nonzero($pad, $xs, $len) == 0 and $mode eq "even" && $digit_round =~ /[24680]/ || $mode eq "odd" && $digit_round =~ /[13579]/ || $mode eq "+inf" && $x->{"sign"} eq "-" || $mode eq "-inf" && $x->{"sign"} eq "+" || $mode eq "zero"
l!l&&r!l&&!r
1294077312019
$mode eq "trunc" or $digit_after =~ /[01234]/
l!l&&r!l&&!r
1481279212792
$digit_after eq 5 and $x->_scan_for_nonzero($pad, $xs, $len) == 0 and $mode eq "even" && $digit_round =~ /[24680]/ || $mode eq "odd" && $digit_round =~ /[13579]/ || $mode eq "+inf" && $x->{"sign"} eq "-" || $mode eq "-inf" && $x->{"sign"} eq "+" || $mode eq "zero"
!ll&&!rl&&r
1203070359
$digit_after eq 5 and $x->_scan_for_nonzero($pad, $xs, $len) == 0
!ll&&!rl&&r
98092221762
$mode eq "even" and $digit_round =~ /[24680]/ or $mode eq "odd" and $digit_round =~ /[13579]/ or $mode eq "+inf" and $x->{"sign"} eq "-" or $mode eq "-inf" and $x->{"sign"} eq "+" or $mode eq "zero"
l!l&&r!l&&!r
36051351
$mode eq "even" and $digit_round =~ /[24680]/ or $mode eq "odd" and $digit_round =~ /[13579]/ or $mode eq "+inf" and $x->{"sign"} eq "-" or $mode eq "-inf" and $x->{"sign"} eq "+"
l!l&&r!l&&!r
33228402
$mode eq "even" and $digit_round =~ /[24680]/ or $mode eq "odd" and $digit_round =~ /[13579]/ or $mode eq "+inf" and $x->{"sign"} eq "-"
l!l&&r!l&&!r
30824430
$mode eq "even" and $digit_round =~ /[24680]/ or $mode eq "odd" and $digit_round =~ /[13579]/
l!l&&r!l&&!r
25553454
$mode eq "even" and $digit_round =~ /[24680]/
!ll&&!rl&&r
266241255
$mode eq "odd" and $digit_round =~ /[13579]/
!ll&&!rl&&r
4054953
$mode eq "+inf" and $x->{"sign"} eq "-"
!ll&&!rl&&r
4032724
$mode eq "-inf" and $x->{"sign"} eq "+"
!ll&&!rl&&r
3822028
MC/DC23%3/13
$mode eq "trunc" or $digit_after =~ /[01234]/ or $digit_after eq 5 and $x->_scan_for_nonzero($pad, $xs, $len) == 0 and $mode eq "even" && $digit_round =~ /[24680]/ || $mode eq "odd" && $digit_round =~ /[13579]/ || $mode eq "+inf" && $x->{"sign"} eq "-" || $mode eq "-inf" && $x->{"sign"} eq "+" || $mode eq "zero"
$mode eq "trunc" $digit_after =~ /[01234]/ $digit_after eq 5 $x->_scan_for_nonzero($pad, $xs, $len) == 0 $mode eq "even" $digit_round =~ /[24680]/ $mode eq "odd" $digit_round =~ /[13579]/ $mode eq "+inf" $x->{"sign"} eq "-" $mode eq "-inf" $x->{"sign"} eq "+" $mode eq "zero"
Truth table15%8/51
A or B or C and D and E and F or G and H or I and J or K and L or M
A= $mode eq "trunc"B= $digit_after =~ /[01234]/C= $digit_after eq 5D= $x->_scan_for_nonzero($pad, $xs, $len) == 0E= $mode eq "even"F= $digit_round =~ /[24680]/G= $mode eq "odd"H= $digit_round =~ /[13579]/I= $mode eq "+inf"J= $x->{"sign"} eq "-"K= $mode eq "-inf"L= $x->{"sign"} eq "+"M= $mode eq "zero"
ABCDEFGHIJKLMresult
1XXXXXXXXXXXX1
01XXXXXXXXXXX1
001111XXXXXXX1
00110X11XXXXX1
00111011XXXXX1
00110X0X11XXX1
00110X1011XXX1
0011100X11XXX1
0011101011XXX1
00110X0X0X11X1
00110X0X1011X1
00110X100X11X1
00110X101011X1
0011100X0X11X1
0011100X1011X1
001110100X11X1
001110101011X1
00110X0X0X0X11
00110X0X0X1011
00110X0X100X11
00110X0X101011
00110X100X0X11
00110X100X1011
00110X10100X11
00110X10101011
0011100X0X0X11
0011100X0X1011
0011100X100X11
0011100X101011
001110100X0X11
001110100X1011
00111010100X11
00111010101011
000XXXXXXXXXX0
0010XXXXXXXXX0
00110X0X0X0X00
00110X0X0X1000
00110X0X100X00
00110X0X101000
00110X100X0X00
00110X100X1000
00110X10100X00
00110X10101000
0011100X0X0X00
0011100X0X1000
0011100X100X00
0011100X101000
001110100X0X00
001110100X1000
00111010100X00
00111010101000
3896 ($mode eq 'zero') # round down if zero, sign adjusted below
3897 );
3898 25732 my $put_back = 0; # not yet modified
3899  
3900 25732 ▶ if (($pad > 0) && ($pad <= $len)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pad > 0 and $pad <= $len) { } 25610 122 25732
elsif ($pad > $len) { } 122 0 122
Condition66%2/3
$pad > 0 and $pad <= $len
!ll&&!rl&&r
012225610
MC/DC50%1/2
$pad > 0 and $pad <= $len
$pad > 0 $pad <= $len
Truth table66%2/3
A and B
A= $pad > 0B= $pad <= $len
ABresult
0X0
100
111
3901 25610 substr($xs, -$pad, $pad) = '0' x $pad; # replace with '00...'
3902 25610 $xs =~ s/^0+(\d)/$1/; # "00000" -> "0"
3903 25610 $put_back = 1; # need to put back
3904 } elsif ($pad > $len) {
3905 122 $x = $x->bzero(); # round to '0'
3906 }
3907  
3908 25732 ▶ if ($round_up) { # what gave test above?
Branch: 1 branch
BranchTrueFalseTotal
if ($round_up) 12381 13351 25732
3909 12381 $put_back = 1; # need to put back
3910 12381 ▶ $pad = $len, $xs = '0' x $pad if $scale < 0; # tlr: whack 0.51=>1.0
Branch: 1 branch
BranchTrueFalseTotal
if $scale < 0 57 12324 12381
3911  
3912 # we modify directly the string variant instead of creating a number and
3913 # adding it, since that is faster (we already have the string)
3914 12381 my $c = 0;
3915 12381 $pad ++; # for $pad == $len case
3916 12381 ▶ while ($pad <= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad <= $len) 13696 57 13753
3917 13696 $c = substr($xs, -$pad, 1) + 1;
3918 13696 ▶ $c = '0' if $c eq '10';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq 10 1372 12324 13696
3919 13696 substr($xs, -$pad, 1) = $c;
3920 13696 $pad++;
3921 13696 ▶ last if $c != 0; # no overflow => early out
Branch: 1 branch
BranchTrueFalseTotal
if $c != 0 12324 1372 13696
3922 }
3923 12381 ▶ $xs = '1'.$xs if $c == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c == 0 57 12324 12381
3924 }
3925 25732 ▶ $x->{value} = $LIB->_new($xs) if $put_back == 1; # put back, if needed
Branch: 1 branch
BranchTrueFalseTotal
if $put_back == 1 25667 65 25732
3926  
3927 25732 ▶ $x->{_a} = $scale if $scale >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $scale >= 0 25598 134 25732
3928 25732 ▶ if ($scale < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < 0) 134 25598 25732
3929 134 $x->{_a} = $len+$scale;
3930 134 ▶ $x->{_a} = 0 if $scale < -$len;
Branch: 1 branch
BranchTrueFalseTotal
if $scale < -$len 3 131 134
3931 }
3932 25732 $x;
3933 }
3934  
3935 sub bfround {
3936 # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.'
3937 # $n == 0 || $n == 1 => round to integer
3938  
3939 212 ▶ my ($class, $x, @p) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bfround: called CC=5 SCAR=16.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 212 0 212
3940  
3941 212 my ($scale, $mode) = $x->_scale_p(@p);
3942  
3943 212 ▶ return $x if !defined $scale || $x->modify('bfround'); # no-op
Branch: 1 branch
BranchTrueFalseTotal
if not defined $scale or $x->modify("bfround") 0 212 212
Condition33%1/3
not defined $scale or $x->modify("bfround")
l!l&&r!l&&!r
00212
MC/DC0%0/2
not defined $scale or $x->modify("bfround")
not defined $scale $x->modify("bfround")
Truth table33%1/3
not(A) or B
A= not defined $scaleB= $x->modify("bfround")
ABresult
1X1
011
000
3944  
3945 # no-op for Math::BigInt objects if $n <= 0
3946 212 ▶ $x = $x->bround($x->length()-$scale, $mode) if $scale > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $scale > 0 104 108 212
3947  
3948 212 $x->{_a} = undef;
3949 212 $x->{_p} = $scale; # store new _p
3950 212 $x;
3951 }
3952  
3953 sub fround {
3954 # Exists to make life easier for switch between MBF and MBI (should we
3955 # autoload fxxx() like MBF does for bxxx()?)
3956 0 ▶ my $x = shift;
Subroutine fround: not called CC=2 SCAR=17.9
Pod undocumented
3957 0 ▶ $x = __PACKAGE__->new($x) unless ref $x;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 0 0 0
3958 0 $x->bround(@_);
3959 }
3960  
3961 sub bfloor {
3962 # round towards minus infinity; no-op since it's already integer
3963 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bfloor: called CC=4 SCAR=14.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
3964  
3965 36 ▶ return $upgrade -> bfloor($x)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 36 36
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2790
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3966 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3967  
3968 36 $x->round(@r);
3969 }
3970  
3971 sub bceil {
3972 # round towards plus infinity; no-op since it's already int
3973 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bceil: called CC=4 SCAR=14.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
3974  
3975 36 ▶ return $upgrade -> bceil($x)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 36 36
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2790
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3976 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3977  
3978 36 $x->round(@r);
3979 }
3980  
3981 sub bint {
3982 # round towards zero; no-op since it's already integer
3983 38 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bint: called CC=4 SCAR=14.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 38 0 38
3984  
3985 38 ▶ return $upgrade -> bint($x)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 38 38
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
3080
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3986 if defined($upgrade) && !$x -> isa(__PACKAGE__);
3987  
3988 38 $x->round(@r);
3989 }
3990  
3991 ###############################################################################
3992 # Other mathematical methods
3993 ###############################################################################
3994  
3995 sub bgcd {
3996 # (BINT or num_str, BINT or num_str) return BINT
3997 # does not modify arguments, but returns new object
3998 # GCD -- Euclid's algorithm, variant C (Knuth Vol 3, pg 341 ff)
3999  
4000 # Class::method(...) -> Class->method(...)
4001 97 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bgcd: called CC=17 SCAR=30.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 1 96 97
Condition77%7/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
0961
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
9511
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
2095
MC/DC25%1/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
4002 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
4003 {
4004 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
4005 # " use is as a method instead";
4006 1 unshift @_, __PACKAGE__;
4007 }
4008  
4009 97 my ($class, @args) = objectify(0, @_);
4010  
4011 # Upgrade?
4012  
4013 97 ▶ if (defined $upgrade) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Math::BigInt::upgrade) 15 82 97
4014 15 my $do_upgrade = 0;
4015 15 for my $arg (@args) {
4016 32 ▶ unless ($arg -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg->isa(__PACKAGE__)) 0 32 32
4017 0 $do_upgrade = 1;
4018 0 last;
4019 }
4020 }
4021 15 ▶ return $upgrade -> bgcd(@args) if $do_upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if $do_upgrade 0 15 15
4022 }
4023  
4024 97 my $x = shift @args;
4025 97 ▶ $x = defined(blessed($x)) && $x -> isa(__PACKAGE__) ? $x -> copy()
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($x) && $x->isa(__PACKAGE__) ? : 97 0 97
Condition33%1/3
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
!ll&&!rl&&r
0097
MC/DC0%0/2
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
defined builtin::blessed($x) $x->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($x)B= $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4026 : $class -> new($x);
4027  
4028 97 ▶ return $class->bnan() if $x->{sign} !~ /^[+-]$/; # x NaN?
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 23 74 97
4029  
4030 74 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 84 29 113
4031 84 my $y = shift @args;
4032 84 ▶ $y = $class->new($y)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($y) and $y->isa(__PACKAGE__) 0 84 84
Condition33%1/3
defined builtin::blessed($y) and $y->isa(__PACKAGE__)
!ll&&!rl&&r
0084
MC/DC0%0/2
defined builtin::blessed($y) and $y->isa(__PACKAGE__)
defined builtin::blessed($y) $y->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($y)B= $y->isa(__PACKAGE__)
ABresult
0X0
100
111
4033 unless defined(blessed($y)) && $y -> isa(__PACKAGE__);
4034 84 ▶ return $class->bnan() if $y->{sign} !~ /^[+-]$/; # y NaN?
Branch: 1 branch
BranchTrueFalseTotal
unless $y->{"sign"} =~ /^[+-]$/ 10 74 84
4035 74 $x->{value} = $LIB->_gcd($x->{value}, $y->{value});
4036 74 ▶ last if $LIB->_is_one($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_one($x->{"value"}) 35 39 74
4037 }
4038  
4039 64 return $x -> babs();
4040 }
4041  
4042 sub blcm {
4043 # (BINT or num_str, BINT or num_str) return BINT
4044 # does not modify arguments, but returns new object
4045 # Least Common Multiple
4046  
4047 # Class::method(...) -> Class->method(...)
4048 35 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine blcm: called CC=16 SCAR=29.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 1 34 35
Condition77%7/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
0341
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
3311
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
2033
MC/DC25%1/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
4049 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
4050 {
4051 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
4052 # " use is as a method instead";
4053 1 unshift @_, __PACKAGE__;
4054 }
4055  
4056 35 my ($class, @args) = objectify(0, @_);
4057  
4058 # Upgrade?
4059  
4060 35 ▶ if (defined $upgrade) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Math::BigInt::upgrade) 8 27 35
4061 8 my $do_upgrade = 0;
4062 8 for my $arg (@args) {
4063 16 ▶ unless ($arg -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg->isa(__PACKAGE__)) 0 16 16
4064 0 $do_upgrade = 1;
4065 0 last;
4066 }
4067 }
4068 8 ▶ return $upgrade -> blcm(@args) if $do_upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if $do_upgrade 0 8 8
4069 }
4070  
4071 35 my $x = shift @args;
4072 35 ▶ $x = defined(blessed($x)) && $x -> isa(__PACKAGE__) ? $x -> copy()
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($x) && $x->isa(__PACKAGE__) ? : 35 0 35
Condition33%1/3
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
!ll&&!rl&&r
0035
MC/DC0%0/2
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
defined builtin::blessed($x) $x->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($x)B= $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4073 : $class -> new($x);
4074 35 ▶ return $class->bnan() if $x->{sign} !~ /^[+-]$/; # x NaN?
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 8 27 35
4075  
4076 27 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 30 23 53
4077 30 my $y = shift @args;
4078 30 ▶ $y = $class -> new($y)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($y) and $y->isa(__PACKAGE__) 0 30 30
Condition33%1/3
defined builtin::blessed($y) and $y->isa(__PACKAGE__)
!ll&&!rl&&r
0030
MC/DC0%0/2
defined builtin::blessed($y) and $y->isa(__PACKAGE__)
defined builtin::blessed($y) $y->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($y)B= $y->isa(__PACKAGE__)
ABresult
0X0
100
111
4079 unless defined(blessed($y)) && $y -> isa(__PACKAGE__);
4080 30 ▶ return $x->bnan() if $y->{sign} !~ /^[+-]$/; # y not integer
Branch: 1 branch
BranchTrueFalseTotal
unless $y->{"sign"} =~ /^[+-]$/ 4 26 30
4081 26 $x -> {value} = $LIB->_lcm($x -> {value}, $y -> {value});
4082 }
4083  
4084 23 return $x -> babs();
4085 }
4086  
4087 ###############################################################################
4088 # Object property methods
4089 ###############################################################################
4090  
4091 sub sign {
4092 # return the sign of the number: +/-/-inf/+inf/NaN
4093 8837 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine sign: called CC=3 SCAR=11.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 8837 0 8837
4094  
4095 8837 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 8837 8837
4096  
4097 8837 $x->{sign};
4098 }
4099  
4100 sub digit {
4101 # return the nth decimal digit, negative values count backward, 0 is right
4102 87 ▶ my (undef, $x, $n, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine digit: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 84 3 87
4103  
4104 87 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 87 87
4105  
4106 87 ▶ $n = $n->numify() if ref($n);
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 63 24 87
4107 87 ▶ $LIB->_digit($x->{value}, $n || 0);
Condition100%2/2
$n || 0
l!l
6720
MC/DC100%1/1
$n || 0
$n
Truth table100%2/2
A
A= $n
Aresult
11
00
4108 }
4109  
4110 sub bdigitsum {
4111 # like digitsum(), but assigns the result to the invocand
4112 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bdigitsum: not called CC=5 SCAR=34.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4113  
4114 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4115  
4116 0 ▶ return $x if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4117 0 ▶ return $x -> bnan() if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4118  
4119 0 $x -> {value} = $LIB -> _digitsum($x -> {value});
4120 0 $x -> {sign} = '+';
4121 0 return $x;
4122 }
4123  
4124 sub digitsum {
4125 # compute sum of decimal digits and return it
4126 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine digitsum: not called CC=5 SCAR=34.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4127  
4128 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4129  
4130 0 ▶ return $class -> bnan() if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4131 0 ▶ return $class -> bnan() if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4132  
4133 0 my $y = $class -> bzero();
4134 0 $y -> {value} = $LIB -> _digitsum($x -> {value});
4135 0 $y -> round(@r);
4136 }
4137  
4138 sub length {
4139 26074 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine length: called CC=4 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 26074 0 26074
4140  
4141 26074 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 26074 26074
4142  
4143 26074 my $e = $LIB->_len($x->{value});
4144 26074 ▶ wantarray ? ($e, 0) : $e;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 26068 26074
4145 }
4146  
4147 sub exponent {
4148 # return a copy of the exponent (here always 0, NaN or 1 for $m == 0)
4149 72 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine exponent: called CC=7 SCAR=19.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 72 0 72
4150  
4151 72 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 72 72
4152  
4153 # Upgrade?
4154  
4155 72 ▶ return $upgrade -> exponent($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 72 72
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
54180
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4156 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4157  
4158 72 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 24 48 72
4159 24 my $s = $x->{sign};
4160 24 $s =~ s/^[+-]//; # NaN, -inf, +inf => NaN or inf
4161 24 return $class->new($s, @r);
4162 }
4163 48 ▶ return $class->bzero(@r) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 40 48
4164  
4165 # 12300 => 2 trailing zeros => exponent is 2
4166 40 $class->new($LIB->_zeros($x->{value}), @r);
4167 }
4168  
4169 sub mantissa {
4170 # return the mantissa (compatible to Math::BigFloat, e.g. reduced)
4171 68 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine mantissa: called CC=7 SCAR=19.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 68 0 68
4172  
4173 68 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 68 68
4174  
4175 # Upgrade?
4176  
4177 68 ▶ return $upgrade -> mantissa($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 68 68
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
51170
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4178 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4179  
4180 68 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 24 44 68
4181 # for NaN, +inf, -inf: keep the sign
4182 24 return $class->new($x->{sign}, @r);
4183 }
4184 44 my $m = $x->copy();
4185 44 $m -> precision(undef);
4186 44 $m -> accuracy(undef);
4187  
4188 # that's a bit inefficient:
4189 44 my $zeros = $LIB->_zeros($m->{value});
4190 44 ▶ $m = $m->brsft($zeros, 10) if $zeros != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $zeros != 0 8 36 44
4191 44 $m -> round(@r);
4192 }
4193  
4194 sub parts {
4195 # return a copy of both the exponent and the mantissa
4196 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine parts: called CC=5 SCAR=17.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
4197  
4198 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
4199  
4200 # Upgrade?
4201  
4202 36 ▶ return $upgrade -> parts($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 36 36
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2790
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4203 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4204  
4205 36 ($x->mantissa(@r), $x->exponent(@r));
4206 }
4207  
4208 # Parts used for scientific notation with significand/mantissa and exponent as
4209 # integers. E.g., "12345.6789" is returned as "123456789" (mantissa) and "-4"
4210 # (exponent).
4211  
4212 sub sparts {
4213 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine sparts: not called CC=11 SCAR=48.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4214  
4215 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4216  
4217 # Not-a-number.
4218  
4219 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
4220 0 my $mant = $class -> bnan(@r); # mantissa
4221 0 ▶ return $mant unless wantarray; # scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4222 0 my $expo = $class -> bnan(@r); # exponent
4223 0 return ($mant, $expo); # list context
4224 }
4225  
4226 # Infinity.
4227  
4228 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
4229 0 my $mant = $class -> binf($x->{sign}, @r); # mantissa
4230 0 ▶ return $mant unless wantarray; # scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4231 0 my $expo = $class -> binf('+', @r); # exponent
4232 0 return ($mant, $expo); # list context
4233 }
4234  
4235 # Upgrade?
4236  
4237 0 ▶ return $upgrade -> sparts($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4238 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4239  
4240 # Finite number.
4241  
4242 0 my $mant = $x -> copy();
4243 0 my $nzeros = $LIB -> _zeros($mant -> {value});
4244  
4245 $mant -> {value}
4246 0 ▶ = $LIB -> _rsft($mant -> {value}, $LIB -> _new($nzeros), 10)
Branch: 1 branch
BranchTrueFalseTotal
if $nzeros != 0 0 0 0
4247 if $nzeros != 0;
4248 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4249  
4250 0 my $expo = $class -> new($nzeros, @r);
4251 0 return ($mant, $expo);
4252 }
4253  
4254 # Parts used for normalized notation with significand/mantissa as either 0 or a
4255 # number in the semi-open interval [1,10). E.g., "12345.6789" is returned as
4256 # "1.23456789" and "4".
4257  
4258 sub nparts {
4259 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine nparts: not called CC=12 SCAR=50.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4260  
4261 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4262  
4263 # Not-a-Number and Infinity.
4264  
4265 0 ▶ return $x -> sparts(@r) if $x -> is_nan() || $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf 0 0 0
Condition0%0/3
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $x->is_inf
$x->is_nan $x->is_inf
Truth table0%0/3
A or B
A= $x->is_nanB= $x->is_inf
ABresult
1X1
011
000
4266  
4267 # Upgrade?
4268  
4269 0 ▶ return $upgrade -> nparts($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4270 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4271  
4272 # Finite number.
4273  
4274 0 my ($mant, $expo) = $x -> sparts(@r);
4275 0 ▶ if ($mant -> bcmp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant->bcmp(0)) 0 0 0
4276 0 my ($ndigtot, $ndigfrac) = $mant -> length();
4277 0 my $expo10adj = $ndigtot - $ndigfrac - 1;
4278  
4279 0 ▶ if ($expo10adj > 0) { # if mantissa is not an integer
Branch: 1 branch
BranchTrueFalseTotal
if ($expo10adj > 0) 0 0 0
4280 0 ▶ return $upgrade -> nparts($x, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 0 0
4281 0 $mant = $mant -> bnan(@r);
4282 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4283 0 $expo = $expo -> badd($expo10adj, @r);
4284 0 return ($mant, $expo);
4285 }
4286 }
4287  
4288 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4289 0 return ($mant, $expo);
4290 }
4291  
4292 # Parts used for engineering notation with significand/mantissa as either 0 or a
4293 # number in the semi-open interval [1,1000) and the exponent is a multiple of 3.
4294 # E.g., "12345.6789" is returned as "12.3456789" and "3".
4295  
4296 sub eparts {
4297 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine eparts: not called CC=12 SCAR=50.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4298  
4299 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4300  
4301 # Not-a-number and Infinity.
4302  
4303 0 ▶ return $x -> sparts(@r) if $x -> is_nan() || $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf 0 0 0
Condition0%0/3
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $x->is_inf
$x->is_nan $x->is_inf
Truth table0%0/3
A or B
A= $x->is_nanB= $x->is_inf
ABresult
1X1
011
000
4304  
4305 # Upgrade?
4306  
4307 0 ▶ return $upgrade -> eparts($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4308 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4309  
4310 # Finite number.
4311  
4312 0 my ($mant, $expo) = $x -> sparts(@r);
4313  
4314 0 ▶ if ($mant -> bcmp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant->bcmp(0)) 0 0 0
4315 0 my $ndigmant = $mant -> length();
4316 0 $expo = $expo -> badd($ndigmant, @r);
4317  
4318 # $c is the number of digits that will be in the integer part of the
4319 # final mantissa.
4320  
4321 0 my $c = $expo -> copy() -> bdec() -> bmod(3) -> binc();
4322 0 $expo = $expo -> bsub($c);
4323  
4324 0 ▶ if ($ndigmant > $c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigmant > $c) 0 0 0
4325 0 ▶ return $upgrade -> eparts($x, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade 0 0 0
4326 0 $mant = $mant -> bnan(@r);
4327 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4328 0 return ($mant, $expo);
4329 }
4330  
4331 0 $mant = $mant -> blsft($c - $ndigmant, 10, @r);
4332 }
4333  
4334 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4335 0 return ($mant, $expo);
4336 }
4337  
4338 # Parts used for decimal notation, e.g., "12345.6789" is returned as "12345"
4339 # (integer part) and "0.6789" (fraction part).
4340  
4341 sub dparts {
4342 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine dparts: not called CC=10 SCAR=47.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4343  
4344 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4345  
4346 # Not-a-number.
4347  
4348 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
4349 0 my $int = $class -> bnan(@r);
4350 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4351 0 my $frc = $class -> bzero(@r); # or NaN?
4352 0 return ($int, $frc);
4353 }
4354  
4355 # Infinity.
4356  
4357 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
4358 0 my $int = $class -> binf($x->{sign}, @r);
4359 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4360 0 my $frc = $class -> bzero(@r);
4361 0 return ($int, $frc);
4362 }
4363  
4364 # Upgrade?
4365  
4366 0 ▶ return $upgrade -> dparts($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4367 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4368  
4369 # Finite number.
4370  
4371 0 my $int = $x -> copy() -> round(@r);
4372 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4373  
4374 0 my $frc = $class -> bzero(@r);
4375 0 return ($int, $frc);
4376 }
4377  
4378 # Fractional parts with the numerator and denominator as integers. E.g.,
4379 # "123.4375" is returned as "1975" and "16".
4380  
4381 sub fparts {
4382 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine fparts: not called CC=10 SCAR=47.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4383  
4384 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4385  
4386 # NaN => NaN/NaN
4387  
4388 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
4389 0 ▶ return $class -> bnan(@r) unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4390 0 return $class -> bnan(@r), $class -> bnan(@r);
4391 }
4392  
4393 # ±Inf => ±Inf/1
4394  
4395 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
4396 0 my $numer = $class -> binf($x->{sign}, @r);
4397 0 ▶ return $numer unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4398 0 my $denom = $class -> bone(@r);
4399 0 return $numer, $denom;
4400 }
4401  
4402 # Upgrade?
4403  
4404 0 ▶ return $upgrade -> fparts($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4405 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4406  
4407 # N => N/1
4408  
4409 0 my $numer = $x -> copy() -> round(@r);
4410 0 ▶ return $numer unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4411 0 my $denom = $class -> bone(@r);
4412 0 return $numer, $denom;
4413 }
4414  
4415 sub numerator {
4416 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine numerator: not called CC=5 SCAR=34.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4417  
4418 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4419  
4420 0 ▶ return $upgrade -> numerator($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4421 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4422  
4423 0 return $x -> copy() -> round(@r);
4424 }
4425  
4426 sub denominator {
4427 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine denominator: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4428  
4429 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4430  
4431 0 ▶ return $upgrade -> denominator($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4432 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4433  
4434 0 ▶ return $x -> is_nan() ? $class -> bnan(@r) : $class -> bone(@r);
Branch: 1 branch
BranchTrueFalseTotal
$x->is_nan ? : 0 0 0
4435 }
4436  
4437 ###############################################################################
4438 # String conversion methods
4439 ###############################################################################
4440  
4441 sub bstr {
4442 12036 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bstr: called CC=9 SCAR=22.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 12024 12 12036
4443  
4444 12036 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 12036 12036
4445  
4446 # Inf and NaN
4447  
4448 12036 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 2703 9333 12036
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
819611372703
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
4449 2703 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 2159 544 2703
4450 544 return 'inf'; # +inf
4451 }
4452  
4453 # Upgrade?
4454  
4455 9333 ▶ return $upgrade -> bstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 9333 9333
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
86586750
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4456 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4457  
4458 # Finite number
4459  
4460 9333 my $str = $LIB->_str($x->{value});
4461 9333 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 1137 8196 9333
4462 }
4463  
4464 # Scientific notation with significand/mantissa as an integer, e.g., "12345" is
4465 # written as "1.2345e+4".
4466  
4467 sub bsstr {
4468 66 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bsstr: called CC=10 SCAR=23.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 50 16 66
4469  
4470 66 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 66 66
4471  
4472 # Inf and NaN
4473  
4474 66 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 18 48 66
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
341418
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
4475 18 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 11 7 18
4476 7 return 'inf'; # +inf
4477 }
4478  
4479 # Upgrade?
4480  
4481 48 ▶ return $upgrade -> bsstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 48 48
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
4530
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4482 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4483  
4484 # Finite number
4485  
4486 48 my $expo = $LIB -> _zeros($x->{value});
4487 48 my $mant = $LIB -> _str($x->{value});
4488 48 ▶ $mant = substr($mant, 0, -$expo) if $expo; # strip trailing zeros
Branch: 1 branch
BranchTrueFalseTotal
if $expo 25 23 48
4489  
4490 48 ▶ ($x->{sign} eq '-' ? '-' : '') . $mant . 'e+' . $expo;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 14 34 48
4491 }
4492  
4493 # Normalized notation, e.g., "12345" is written as "1.2345e+4".
4494  
4495 sub bnstr {
4496 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bnstr: not called CC=11 SCAR=48.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4497  
4498 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4499  
4500 # Inf and NaN
4501  
4502 0 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 0 0 0
Condition0%0/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
000
MC/DC0%0/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table0%0/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
4503 0 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 0 0 0
4504 0 return 'inf'; # +inf
4505 }
4506  
4507 # Upgrade?
4508  
4509 0 ▶ return $upgrade -> bnstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4510 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4511  
4512 # Finite number
4513  
4514 0 my $expo = $LIB -> _zeros($x->{value});
4515 0 my $mant = $LIB -> _str($x->{value});
4516 0 ▶ $mant = substr($mant, 0, -$expo) if $expo; # strip trailing zeros
Branch: 1 branch
BranchTrueFalseTotal
if $expo 0 0 0
4517  
4518 0 my $mantlen = CORE::length($mant);
4519 0 ▶ if ($mantlen > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mantlen > 1) 0 0 0
4520 0 $expo += $mantlen - 1; # adjust exponent
4521 0 substr $mant, 1, 0, "."; # insert decimal point
4522 }
4523  
4524 0 ▶ ($x->{sign} eq '-' ? '-' : '') . $mant . 'e+' . $expo;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
4525 }
4526  
4527 # Engineering notation, e.g., "12345" is written as "12.345e+3".
4528  
4529 sub bestr {
4530 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bestr: not called CC=12 SCAR=50.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4531  
4532 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4533  
4534 # Inf and NaN
4535  
4536 0 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 0 0 0
Condition0%0/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
000
MC/DC0%0/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table0%0/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
4537 0 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 0 0 0
4538 0 return 'inf'; # +inf
4539 }
4540  
4541 # Upgrade?
4542  
4543 0 ▶ return $upgrade -> bestr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4544 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4545  
4546 # Finite number
4547  
4548 0 my $expo = $LIB -> _zeros($x->{value}); # number of trailing zeros
4549 0 my $mant = $LIB -> _str($x->{value}); # mantissa as a string
4550 0 ▶ $mant = substr($mant, 0, -$expo) if $expo; # strip trailing zeros
Branch: 1 branch
BranchTrueFalseTotal
if $expo 0 0 0
4551 0 my $mantlen = CORE::length($mant); # length of mantissa
4552 0 $expo += $mantlen;
4553  
4554 0 my $dotpos = ($expo - 1) % 3 + 1; # offset of decimal point
4555 0 $expo -= $dotpos;
4556  
4557 0 ▶ if ($dotpos < $mantlen) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dotpos < $mantlen) { } 0 0 0
elsif ($dotpos > $mantlen) { } 0 0 0
4558 0 substr $mant, $dotpos, 0, "."; # insert decimal point
4559 } elsif ($dotpos > $mantlen) {
4560 0 $mant .= "0" x ($dotpos - $mantlen); # append zeros
4561 }
4562  
4563 0 ▶ ($x->{sign} eq '-' ? '-' : '') . $mant . 'e+' . $expo;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
4564 }
4565  
4566 # Decimal notation, e.g., "12345" (no exponent).
4567  
4568 sub bdstr {
4569 24 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bdstr: called CC=9 SCAR=22.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 24 0 24
4570  
4571 24 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 24 24
4572  
4573 # Inf and NaN
4574  
4575 24 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 8 16 24
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
1518
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
4576 8 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 7 1 8
4577 1 return 'inf'; # +inf
4578 }
4579  
4580 # Upgrade?
4581  
4582 16 ▶ return $upgrade -> bdstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 16 16
Condition33%1/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
0160
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4583 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4584  
4585 # Finite number
4586  
4587 16 ▶ ($x->{sign} eq '-' ? '-' : '') . $LIB->_str($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 1 15 16
4588 }
4589  
4590 # Fraction notation, e.g., "123.4375" is written as "1975/16", but "123" is
4591 # written as "123", not "123/1".
4592  
4593 sub bfstr {
4594 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bfstr: not called CC=9 SCAR=45.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4595  
4596 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4597  
4598 # Inf and NaN
4599  
4600 0 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 0 0 0
Condition0%0/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
000
MC/DC0%0/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table0%0/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
4601 0 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 0 0 0
4602 0 return 'inf'; # +inf
4603 }
4604  
4605 # Upgrade?
4606  
4607 0 ▶ return $upgrade -> bfstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4608 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4609  
4610 # Finite number
4611  
4612 0 ▶ ($x->{sign} eq '-' ? '-' : '') . $LIB->_str($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
4613 }
4614  
4615 sub to_hex {
4616 # return as hex string with no prefix
4617  
4618 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine to_hex: called CC=9 SCAR=22.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
4619  
4620 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
4621  
4622 # Inf and NaN
4623  
4624 36 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 12 24 36
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
20412
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
4625 12 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 8 4 12
4626 4 return 'inf'; # +inf
4627 }
4628  
4629 # Upgrade?
4630  
4631 24 ▶ return $upgrade -> to_hex($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 24 24
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
1860
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4632 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4633  
4634 # Finite number
4635  
4636 24 my $hex = $LIB->_to_hex($x->{value});
4637 24 ▶ return $x->{sign} eq '-' ? "-$hex" : $hex;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 20 24
4638 }
4639  
4640 sub to_oct {
4641 # return as octal string with no prefix
4642  
4643 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine to_oct: called CC=9 SCAR=22.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
4644  
4645 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
4646  
4647 # Inf and NaN
4648  
4649 40 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 12 28 40
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
24412
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
4650 12 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 8 4 12
4651 4 return 'inf'; # +inf
4652 }
4653  
4654 # Upgrade?
4655  
4656 28 ▶ return $upgrade -> to_oct($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 28 28
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2170
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4657 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4658  
4659 # Finite number
4660  
4661 28 my $oct = $LIB->_to_oct($x->{value});
4662 28 ▶ return $x->{sign} eq '-' ? "-$oct" : $oct;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 24 28
4663 }
4664  
4665 sub to_bin {
4666 # return as binary string with no prefix
4667  
4668 93 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine to_bin: called CC=9 SCAR=22.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 93 0 93
4669  
4670 93 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 93 93
4671  
4672 # Inf and NaN
4673  
4674 93 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 12 81 93
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
235812
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
4675 12 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 8 4 12
4676 4 return 'inf'; # +inf
4677 }
4678  
4679 # Upgrade?
4680  
4681 81 ▶ return $upgrade -> to_bin($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 81 81
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
7560
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4682 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4683  
4684 # Finite number
4685  
4686 81 my $bin = $LIB->_to_bin($x->{value});
4687 81 ▶ return $x->{sign} eq '-' ? "-$bin" : $bin;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 58 23 81
4688 }
4689  
4690 sub to_bytes {
4691 # return a byte string
4692  
4693 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine to_bytes: not called CC=8 SCAR=42.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4694  
4695 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4696  
4697 0 ▶ croak("to_bytes() requires a finite, non-negative integer")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg or not $x->is_int 0 0 0
Condition0%0/3
$x->is_neg or not $x->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_neg or not $x->is_int
$x->is_neg not $x->is_int
Truth table0%0/3
A or not(B)
A= $x->is_negB= not $x->is_int
ABresult
1X1
011
000
4698 if $x -> is_neg() || ! $x -> is_int();
4699  
4700 0 ▶ return $upgrade -> to_bytes($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4701 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4702  
4703 0 ▶ croak("to_bytes() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_to_bytes") 0 0 0
4704 unless $LIB->can('_to_bytes');
4705  
4706 0 return $LIB->_to_bytes($x->{value});
4707 }
4708  
4709 sub to_base {
4710 # return a base anything string
4711  
4712 # $cs is the collation sequence
4713 0 ▶ my ($class, $x, $base, $cs, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine to_base: not called CC=18 SCAR=58.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
4714 ? (ref($_[0]), @_) : objectify(2, @_);
4715  
4716 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4717  
4718 0 ▶ croak("the value to convert must be a finite, non-negative integer")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg or not $x->is_int 0 0 0
Condition0%0/3
$x->is_neg or not $x->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_neg or not $x->is_int
$x->is_neg not $x->is_int
Truth table0%0/3
A or not(B)
A= $x->is_negB= not $x->is_int
ABresult
1X1
011
000
4719 if $x -> is_neg() || !$x -> is_int();
4720  
4721 0 ▶ croak("the base must be a finite integer >= 2")
Branch: 1 branch
BranchTrueFalseTotal
if $base < 2 or not $base->is_int 0 0 0
Condition0%0/3
$base < 2 or not $base->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$base < 2 or not $base->is_int
$base < 2 not $base->is_int
Truth table0%0/3
A or not(B)
A= $base < 2B= not $base->is_int
ABresult
1X1
011
000
4722 if $base < 2 || ! $base -> is_int();
4723  
4724 # If no collating sequence is given, pass some of the conversions to
4725 # methods optimized for those cases.
4726  
4727 0 ▶ unless (defined $cs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cs) 0 0 0
4728 0 ▶ return $x -> to_bin() if $base == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 2 0 0 0
4729 0 ▶ return $x -> to_oct() if $base == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 8 0 0 0
4730 0 ▶ return uc $x -> to_hex() if $base == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 16 0 0 0
4731 0 ▶ return $x -> bstr() if $base == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 10 0 0 0
4732 }
4733  
4734 0 ▶ croak("to_base() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_to_base") 0 0 0
4735 unless $LIB->can('_to_base');
4736  
4737 0 ▶ return $upgrade -> to_base($x, $base, $cs, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__) 0 0 0
Condition0%0/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__)
!ll&&!rl&&r
000
$x->isa(__PACKAGE__) && $base->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $base->isa(__PACKAGE__)
Truth table0%0/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $base->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
4738 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
4739 !$base -> isa(__PACKAGE__));
4740  
4741 return $LIB->_to_base($x->{value}, $base -> {value},
4742 0 ▶ defined($cs) ? $cs : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $cs ? : 0 0 0
4743 }
4744  
4745 sub to_base_num {
4746 # return a base anything array ref, e.g.,
4747 # Math::BigInt -> new(255) -> to_base_num(10) returns [2, 5, 5];
4748  
4749 # $cs is the collation sequence
4750 0 ▶ my ($class, $x, $base, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine to_base_num: not called CC=13 SCAR=52.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
4751 ? (ref($_[0]), @_) : objectify(2, @_);
4752  
4753 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4754  
4755 0 ▶ croak("the value to convert must be a finite non-negative integer")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg or not $x->is_int 0 0 0
Condition0%0/3
$x->is_neg or not $x->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_neg or not $x->is_int
$x->is_neg not $x->is_int
Truth table0%0/3
A or not(B)
A= $x->is_negB= not $x->is_int
ABresult
1X1
011
000
4756 if $x -> is_neg() || !$x -> is_int();
4757  
4758 0 ▶ croak("the base must be a finite integer >= 2")
Branch: 1 branch
BranchTrueFalseTotal
if $base < 2 or not $base->is_int 0 0 0
Condition0%0/3
$base < 2 or not $base->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$base < 2 or not $base->is_int
$base < 2 not $base->is_int
Truth table0%0/3
A or not(B)
A= $base < 2B= not $base->is_int
ABresult
1X1
011
000
4759 if $base < 2 || ! $base -> is_int();
4760  
4761 0 ▶ croak("to_base() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_to_base") 0 0 0
4762 unless $LIB->can('_to_base');
4763  
4764 0 ▶ return $upgrade -> to_base_num($x, $base, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__) 0 0 0
Condition0%0/6
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__)
!ll&&!rl&&r
000
$x->isa(__PACKAGE__) && $base->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__)
defined $Math::BigInt::upgrade $x->isa(__PACKAGE__) $base->isa(__PACKAGE__)
Truth table0%0/4
A and not(B && C)
A= defined $Math::BigInt::upgradeB= $x->isa(__PACKAGE__)C= $base->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
4765 if defined($upgrade) && (!$x -> isa(__PACKAGE__) ||
4766 !$base -> isa(__PACKAGE__));
4767  
4768 # Get a reference to an array of library thingies, and replace each element
4769 # with a Math::BigInt object using that thingy.
4770  
4771 0 my $vals = $LIB -> _to_base_num($x->{value}, $base -> {value});
4772  
4773 0 for my $i (0 .. $#$vals) {
4774 0 my $x = $class -> bzero();
4775 0 $x -> {value} = $vals -> [$i];
4776 0 $vals -> [$i] = $x;
4777 }
4778  
4779 0 return $vals;
4780 }
4781  
4782 sub as_hex {
4783 # return as hex string, with prefixed 0x
4784  
4785 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_hex: called CC=7 SCAR=20.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
4786  
4787 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
4788  
4789 36 ▶ return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 24 36
4790  
4791 24 ▶ return $upgrade -> as_hex($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 24 24
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
1860
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4792 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4793  
4794 24 my $hex = $LIB->_as_hex($x->{value});
4795 24 ▶ return $x->{sign} eq '-' ? "-$hex" : $hex;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 20 24
4796 }
4797  
4798 sub as_oct {
4799 # return as octal string, with prefixed 0
4800  
4801 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_oct: called CC=7 SCAR=20.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
4802  
4803 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
4804  
4805 40 ▶ return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 28 40
4806  
4807 28 ▶ return $upgrade -> as_oct($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 28 28
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2170
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4808 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4809  
4810 28 my $oct = $LIB->_as_oct($x->{value});
4811 28 ▶ return $x->{sign} eq '-' ? "-$oct" : $oct;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 24 28
4812 }
4813  
4814 sub as_bin {
4815 # return as binary string, with prefixed 0b
4816  
4817 39 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_bin: called CC=7 SCAR=20.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 39 0 39
4818  
4819 39 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 39 39
4820  
4821 39 ▶ return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 27 39
4822  
4823 27 ▶ return $upgrade -> as_bin($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 27 27
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2160
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4824 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4825  
4826 27 my $bin = $LIB->_as_bin($x->{value});
4827 27 ▶ return $x->{sign} eq '-' ? "-$bin" : $bin;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 23 27
4828 }
4829  
4830 51 *as_bytes = \&to_bytes;
4831  
4832 ###############################################################################
4833 # Other conversion methods
4834 ###############################################################################
4835  
4836 sub numify {
4837 # Make a Perl scalar number from a Math::BigInt object.
4838 495 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine numify: called CC=9 SCAR=28.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 495 0 495
4839  
4840 495 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 495 495
4841  
4842 495 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 495 495
4843 0 require Math::Complex;
4844 0 my $inf = $Math::Complex::Inf;
4845 0 return $inf - $inf;
4846 }
4847  
4848 495 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 495 495
4849 0 require Math::Complex;
4850 0 my $inf = $Math::Complex::Inf;
4851 0 ▶ return $x -> is_negative() ? -$inf : $inf;
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative ? : 0 0 0
4852 }
4853  
4854 495 ▶ return $upgrade -> numify($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__) 0 495 495
Condition66%2/3
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
438570
MC/DC0%0/2
defined $Math::BigInt::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigInt::upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= defined $Math::BigInt::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4855 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4856  
4857 495 my $num = 0 + $LIB->_num($x->{value});
4858 495 ▶ return $x->{sign} eq '-' ? -$num : $num;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 39 456 495
4859 }
4860  
4861 ###############################################################################
4862 # Private methods and functions.
4863 ###############################################################################
4864  
4865 sub objectify {
4866 # Convert strings and "foreign objects" to the objects we want.
4867  
4868 # The first argument, $count, is the number of following arguments that
4869 # objectify() looks at and converts to objects. The first is a classname.
4870 # If the given count is 0, all arguments will be used.
4871  
4872 # After the count is read, objectify obtains the name of the class to which
4873 # the following arguments are converted. If the second argument is a
4874 # reference, use the reference type as the class name. Otherwise, if it is
4875 # a string that looks like a class name, use that. Otherwise, use $class.
4876  
4877 # Caller: Gives us:
4878 #
4879 # $x->badd(1); => ref x, scalar y
4880 # Class->badd(1, 2); => classname x (scalar), scalar x, scalar y
4881 # Class->badd(Class->(1), 2); => classname x (scalar), ref x, scalar y
4882 # Math::BigInt::badd(1, 2); => scalar x, scalar y
4883  
4884 # A shortcut for the common case $x->unary_op(), in which case the argument
4885 # list is (0, $x) or (1, $x).
4886  
4887 5151 ▶ return (ref($_[1]), $_[1]) if @_ == 2 && ($_[0] || 0) == 1 && ref($_[1]);
Subroutine objectify: called CC=26 SCAR=33.2
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 and ($_[0] || 0) == 1 and ref $_[1] 150 5001 5151
Condition87%7/8
@_ == 2 and ($_[0] || 0) == 1 and ref $_[1]
!ll&&!rl&&r
50010150
@_ == 2 and ($_[0] || 0) == 1
!ll&&!rl&&r
50001150
$_[0] || 0
l!l
1501
MC/DC66%2/3
@_ == 2 and ($_[0] || 0) == 1 and ref $_[1]
@_ == 2 $_[0] ref $_[1]
Truth table75%3/4
A and B and C
A= @_ == 2B= $_[0]C= ref $_[1]
ABCresult
0XX0
10X0
1100
1111
4888  
4889 # Check the context.
4890  
4891 5001 ▶ unless (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 0 5001 5001
4892 0 croak(__PACKAGE__ . "::objectify() needs list context");
4893 }
4894  
4895 # Get the number of arguments to objectify.
4896  
4897 5001 my $count = shift;
4898  
4899 # Initialize the output array.
4900  
4901 5001 my @a = @_;
4902  
4903 # If the first argument is a reference, use that reference type as our
4904 # class name. Otherwise, if the first argument looks like a class name,
4905 # then use that as our class name. Otherwise, use the default class name.
4906  
4907 5001 my $class;
4908 5001 ▶ if (ref($a[0])) { # reference?
Branch: 2 branches
BranchTrueFalseTotal
if (ref $a[0]) { } 3728 1273 5001
elsif ($a[0] =~ /^[A-Z].*::/) { } 1261 12 1273
4909 3728 $class = ref($a[0]);
4910 } elsif ($a[0] =~ /^[A-Z].*::/) { # string with class name?
4911 1261 $class = shift @a;
4912 } else {
4913 12 $class = __PACKAGE__; # default class name
4914 }
4915  
4916 5001 ▶ $count ||= @a;
Condition66%2/3
$count ||= @a
l!l&&r!l&&!r
46983030
MC/DC0%0/2
$count ||= @a
$count @a
Truth table66%2/3
A ||= B
A= $countB= @a
ABresult
1X1
011
000
4917 5001 unshift @a, $class;
4918  
4919 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
4920  
4921 # What we upgrade to, if anything. Note that we need the whole upgrade
4922 # chain, since there might be multiple levels of upgrading. E.g., class A
4923 # upgrades to class B, which upgrades to class C. Delay getting the chain
4924 # until we actually need it.
4925  
4926 5001 my @upg = ();
4927 5001 my $have_upgrade_chain = 0;
4928  
4929 # Disable downgrading, because Math::BigFloat -> foo('1.0', '2.0') needs
4930 # floats.
4931  
4932 5001 my $down;
4933 5001 ▶ if (defined ${"$a[0]::downgrade"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined ${"$a[0]::downgrade";}) 14 4987 5001
4934 14 $down = ${"$a[0]::downgrade"};
4935 14 ${"$a[0]::downgrade"} = undef;
4936 }
4937  
4938 5001 ARG: for my $i (1 .. $count) {
4939  
4940 9136 my $ref = ref $a[$i];
4941  
4942 # Perl scalars are fed to the appropriate constructor.
4943  
4944 9136 ▶ unless ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 4214 4922 9136
4945 4214 $a[$i] = $a[0] -> new($a[$i]);
4946 4214 next;
4947 }
4948  
4949 # If it is an object of the right class, all is fine.
4950  
4951 4922 ▶ next if $ref -> isa($a[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $ref->isa($a[0]) 4518 404 4922
4952  
4953 # Upgrading is OK, so skip further tests if the argument is upgraded,
4954 # but first get the whole upgrade chain if we haven't got it yet.
4955  
4956 404 ▶ unless ($have_upgrade_chain) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_upgrade_chain) 281 123 404
4957 281 my $cls = $class;
4958 281 my $upg = $cls -> upgrade();
4959 281 ▶ while (defined $upg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $upg) 17 281 298
4960 17 ▶ last if $upg eq $cls;
Branch: 1 branch
BranchTrueFalseTotal
if $upg eq $cls 0 17 17
4961 17 push @upg, $upg;
4962 17 $cls = $upg;
4963 17 $upg = $cls -> upgrade();
4964 }
4965 281 $have_upgrade_chain = 1;
4966 }
4967  
4968 404 for my $upg (@upg) {
4969 17 ▶ next ARG if $ref -> isa($upg);
Branch: 1 branch
BranchTrueFalseTotal
if $ref->isa($upg) 16 1 17
4970 }
4971  
4972 # See if we can call one of the as_xxx() methods. We don't know whether
4973 # the as_xxx() method returns an object or a scalar, so re-check
4974 # afterwards.
4975  
4976 388 my $recheck = 0;
4977  
4978 388 ▶ if ($a[0] -> isa('Math::BigInt')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($a[0]->isa('Math::BigInt')) { } 42 346 388
elsif ($a[0]->isa('Math::BigFloat')) { } 346 0 346
4979 42 ▶ if ($a[$i] -> can('as_int')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($a[$i]->can('as_int')) { } 42 0 42
elsif ($a[$i]->can('as_number')) { } 0 0 0
4980 42 $a[$i] = $a[$i] -> as_int();
4981 42 $recheck = 1;
4982 } elsif ($a[$i] -> can('as_number')) {
4983 0 $a[$i] = $a[$i] -> as_number();
4984 0 $recheck = 1;
4985 }
4986 }
4987  
4988 elsif ($a[0] -> isa('Math::BigFloat')) {
4989 346 ▶ if ($a[$i] -> can('as_float')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a[$i]->can("as_float")) 346 0 346
4990 346 $a[$i] = $a[$i] -> as_float();
4991 346 $recheck = $1;
4992 }
4993 }
4994  
4995 # If we called one of the as_xxx() methods, recheck.
4996  
4997 388 ▶ if ($recheck) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recheck) 44 344 388
4998 44 $ref = ref($a[$i]);
4999  
5000 # Perl scalars are fed to the appropriate constructor.
5001  
5002 44 ▶ unless ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 0 44 44
5003 0 $a[$i] = $a[0] -> new($a[$i]);
5004 0 next;
5005 }
5006  
5007 # If it is an object of the right class, all is fine.
5008  
5009 44 ▶ next if $ref -> isa($a[0]);
Branch: 1 branch
BranchTrueFalseTotal
if $ref->isa($a[0]) 43 1 44
5010 }
5011  
5012 # Last resort.
5013  
5014 345 $a[$i] = $a[0] -> new($a[$i]);
5015 }
5016  
5017 # Reset the downgrading.
5018  
5019 5001 ${"$a[0]::downgrade"} = $down;
5020  
5021 5001 return @a;
5022 }
5023  
5024 sub import {
5025 103 ▶ my $class = shift;
Subroutine import: called CC=28 SCAR=41.5
5026 103 $IMPORT++; # remember we did import()
5027 103 my @a; # unrecognized arguments
5028  
5029 103 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 91 103 194
5030 91 my $param = shift;
5031  
5032 # Enable overloading of constants.
5033  
5034 91 ▶ if ($param eq ':constant') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq ":constant") 1 90 91
5035 overload::constant
5036  
5037 integer => sub {
5038 2 ▶ $class -> new(shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
5039 },
5040  
5041 float => sub {
5042 0 ▶ $class -> new(shift);
Subroutine __ANON__: not called CC=1 SCAR=6.9
5043 },
5044  
5045 binary => sub {
5046 # E.g., a literal 0377 shall result in an object whose value
5047 # is decimal 255, but new("0377") returns decimal 377.
5048 0 ▶ return $class -> from_oct($_[0]) if $_[0] =~ /^0_*[0-7]/;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^0_*[0-7]/ 0 0 0
5049 0 $class -> new(shift);
5050 1 };
5051 1 next;
5052 }
5053  
5054 # Upgrading.
5055  
5056 90 ▶ if ($param eq 'upgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "upgrade") 2 88 90
5057 2 $class -> upgrade(shift);
5058 2 next;
5059 }
5060  
5061 # Downgrading.
5062  
5063 88 ▶ if ($param eq 'downgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "downgrade") 0 88 88
5064 0 $class -> downgrade(shift);
5065 0 next;
5066 }
5067  
5068 # Accuracy.
5069  
5070 88 ▶ if ($param eq 'accuracy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "accuracy") 0 88 88
5071 0 $class -> accuracy(shift);
5072 0 next;
5073 }
5074  
5075 # Precision.
5076  
5077 88 ▶ if ($param eq 'precision') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "precision") 0 88 88
5078 0 $class -> precision(shift);
5079 0 next;
5080 }
5081  
5082 # Rounding mode.
5083  
5084 88 ▶ if ($param eq 'round_mode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "round_mode") 0 88 88
5085 0 $class -> round_mode(shift);
5086 0 next;
5087 }
5088  
5089 # Backend library.
5090  
5091 88 ▶ if ($param =~ /^(lib|try|only)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(lib|try|only)\z/) 29 59 88
5092 # try => 0 (no warn if unavailable module)
5093 # lib => 1 (warn on fallback)
5094 # only => 2 (die on fallback)
5095  
5096 # Get the list of user-specified libraries.
5097  
5098 29 ▶ croak "Library argument for import parameter '$param' is missing"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 29 29
5099 unless @_;
5100 29 my $libs = shift;
5101 29 ▶ croak "Library argument for import parameter '$param' is undefined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $libs 0 29 29
5102 unless defined($libs);
5103  
5104 # Check and clean up the list of user-specified libraries.
5105  
5106 29 my @libs;
5107 29 for my $lib (split /,/, $libs) {
5108 29 $lib =~ s/^\s+//;
5109 29 $lib =~ s/\s+$//;
5110  
5111 29 ▶ if ($lib =~ /[^a-zA-Z0-9_:]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lib =~ /[^a-zA-Z0-9_:]/) 0 29 29
5112 0 carp "Library name '$lib' contains invalid characters";
5113 0 next;
5114 }
5115  
5116 29 ▶ if (! CORE::length $lib) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::length $lib) 0 29 29
5117 0 carp "Library name is empty";
5118 0 next;
5119 }
5120  
5121 29 ▶ $lib = "Math::BigInt::$lib" if $lib !~ /^Math::BigInt::/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $lib =~ /^Math::BigInt::/i 24 5 29
5122  
5123 # If a library has already been loaded, that is OK only if the
5124 # requested library is identical to the loaded one.
5125  
5126 29 ▶ if (defined($LIB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $LIB) 10 19 29
5127 10 ▶ if ($lib ne $LIB) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lib ne $LIB) 3 7 10
5128 #carp "Library '$LIB' has already been loaded, so",
5129 # " ignoring requested library '$lib'";
5130 }
5131 10 next;
5132 }
5133  
5134 19 push @libs, $lib;
5135 }
5136  
5137 29 ▶ next if defined $LIB;
Branch: 1 branch
BranchTrueFalseTotal
if defined $LIB 10 19 29
5138  
5139 19 ▶ croak "Library list contains no valid libraries" unless @libs;
Branch: 1 branch
BranchTrueFalseTotal
unless @libs 0 19 19
5140  
5141 # Try to load the specified libraries, if any.
5142  
5143 19 ▶ for (my $i = 0 ; $i <= $#libs ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#libs) 19 0 19
5144 19 my $lib = $libs[$i];
5145 19 eval "require $lib";
5146 19 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 19 0 19
5147 19 $LIB = $lib;
5148 19 last;
5149 }
5150 }
5151  
5152 19 ▶ next if defined $LIB;
Branch: 1 branch
BranchTrueFalseTotal
if defined $LIB 19 0 19
5153  
5154 # No library has been loaded, and none of the requested libraries
5155 # could be loaded, and fallback and the user doesn't allow fallback.
5156  
5157 0 ▶ if ($param eq 'only') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "only") 0 0 0
5158 0 croak "Couldn't load the specified math lib(s) ",
5159 join(", ", map "'$_'", @libs),
5160 ", and fallback to '$DEFAULT_LIB' is not allowed";
5161 }
5162  
5163 # No library has been loaded, and none of the requested libraries
5164 # could be loaded, but the user accepts the use of a fallback
5165 # library, so try to load it.
5166  
5167 0 eval "require $DEFAULT_LIB";
5168 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
5169 0 croak "Couldn't load the specified math lib(s) ",
5170 join(", ", map "'$_'", @libs),
5171 ", not even the fallback lib '$DEFAULT_LIB'";
5172 }
5173  
5174 # The fallback library was successfully loaded, but the user
5175 # might want to know that we are using the fallback.
5176  
5177 0 ▶ if ($param eq 'lib') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "lib") 0 0 0
5178 0 carp "Couldn't load the specified math lib(s) ",
5179 join(", ", map "'$_'", @libs),
5180 ", so using fallback lib '$DEFAULT_LIB'";
5181 }
5182  
5183 0 next;
5184 }
5185  
5186 # Unrecognized parameter.
5187  
5188 59 push @a, $param;
5189 }
5190  
5191 # Any non-':constant' stuff is handled by our parent, Exporter
5192  
5193 103 ▶ if (@a) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) 58 45 103
5194 58 $class->SUPER::import(@a); # need it for subclasses
5195 58 $class->export_to_level(1, $class, @a); # need it for Math::BigFloat
5196 }
5197  
5198 # We might not have loaded any backend library yet, either because the user
5199 # didn't specify any, or because the specified libraries failed to load and
5200 # the user allows the use of a fallback library.
5201  
5202 103 ▶ unless (defined $LIB) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $LIB) 32 71 103
5203 32 eval "require $DEFAULT_LIB";
5204 32 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 32 32
5205 0 croak "No lib specified, and couldn't load the default",
5206 " lib '$DEFAULT_LIB'";
5207 }
5208 32 $LIB = $DEFAULT_LIB;
5209 }
5210  
5211 # import done
5212 }
5213  
5214 sub _trailing_zeros {
5215 # return the amount of trailing zeros in $x (as scalar)
5216 0 ▶ my $x = shift;
Subroutine _trailing_zeros: not called CC=3 SCAR=24.8
5217 0 ▶ $x = __PACKAGE__->new($x) unless ref $x;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 0 0 0
5218  
5219 0 ▶ return 0 if $x->{sign} !~ /^[+-]$/; # NaN, inf, -inf etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 0 0 0
5220  
5221 0 $LIB->_zeros($x->{value}); # must handle odd values, 0 etc
5222 }
5223  
5224 sub _scan_for_nonzero {
5225 # internal, used by bround() to scan for non-zeros after a '5'
5226 2983 ▶ my ($x, $pad, $xs, $len) = @_;
Subroutine _scan_for_nonzero: called CC=5 SCAR=16.1
5227  
5228 2983 ▶ return 0 if $len == 1; # "5" is trailed by invisible zeros
Branch: 1 branch
BranchTrueFalseTotal
if $len == 1 23 2960 2983
5229 2960 my $follow = $pad - 1;
5230 2960 ▶ return 0 if $follow > $len || $follow < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $follow > $len or $follow < 1 653 2307 2960
Condition66%2/3
$follow > $len or $follow < 1
l!l&&r!l&&!r
06532307
MC/DC50%1/2
$follow > $len or $follow < 1
$follow > $len $follow < 1
Truth table66%2/3
A or B
A= $follow > $lenB= $follow < 1
ABresult
1X1
011
000
5231  
5232 # use the string form to check whether only '0's follow or not
5233 2307 ▶ substr ($xs, -$follow) =~ /[^0]/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
substr($xs, -$follow) =~ /[^0]/ ? : 2221 86 2307
5234 }
5235  
5236 sub _find_round_parameters {
5237 # After any operation or when calling round(), the result is rounded by
5238 # regarding the A & P from arguments, local parameters, or globals.
5239  
5240 # !!!!!!! If you change this, remember to change round(), too! !!!!!!!!!!
5241  
5242 # This procedure finds the round parameters, but it is for speed reasons
5243 # duplicated in round. Otherwise, it is tested by the testsuite and used
5244 # by bdiv().
5245  
5246 # returns ($self) or ($self, $a, $p, $r) - sets $self to NaN of both A and P
5247 # were requested/defined (locally or globally or both)
5248  
5249 10202 ▶ my ($self, $a, $p, $r, @args) = @_;
Subroutine _find_round_parameters: called CC=29 SCAR=34.2
5250 # $a accuracy, if given by caller
5251 # $p precision, if given by caller
5252 # $r round_mode, if given by caller
5253 # @args all 'other' arguments (0 for unary, 1 for binary ops)
5254  
5255 10202 my $class = ref($self); # find out class of argument(s)
5256 51 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
5257  
5258 # convert to normal scalar for speed and correctness in inner parts
5259 10202 ▶ $a = $a->can('numify') ? $a->numify() : "$a" if defined $a && ref($a);
Branch: 2 branches
BranchTrueFalseTotal
if defined $a and ref $a 309 9893 10202
$a->can('numify') ? : 309 0 309
Condition100%3/3
defined $a and ref $a
!ll&&!rl&&r
9948899309
MC/DC100%2/2
defined $a and ref $a
defined $a ref $a
Truth table100%3/3
A and B
A= defined $aB= ref $a
ABresult
0X0
100
111
5260 10202 ▶ $p = $p->can('numify') ? $p->numify() : "$p" if defined $p && ref($p);
Branch: 2 branches
BranchTrueFalseTotal
if defined $p and ref $p 0 10202 10202
$p->can('numify') ? : 0 0 0
Condition66%2/3
defined $p and ref $p
!ll&&!rl&&r
10150520
MC/DC0%0/2
defined $p and ref $p
defined $p ref $p
Truth table66%2/3
A and B
A= defined $pB= ref $p
ABresult
0X0
100
111
5261  
5262 # now pick $a or $p, but only if we have got "arguments"
5263 10202 ▶ if (!defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a) 994 9208 10202
5264 994 foreach ($self, @args) {
5265 # take the defined one, or if both defined, the one that is smaller
5266 $a = $_->{_a}
5267 1568 ▶ if (defined $_->{_a}) && (!defined $a || $_->{_a} < $a);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"_a"} and !defined($a) || $_->{"_a"} < $a 32 1536 1568
Condition50%3/6
defined $_->{"_a"} and !defined($a) || $_->{"_a"} < $a
!ll&&!rl&&r
1536320
not defined $a or $_->{"_a"} < $a
l!l&&r!l&&!r
3200
MC/DC0%0/3
defined $_->{"_a"} and !defined($a) || $_->{"_a"} < $a
defined $_->{"_a"} not defined $a $_->{"_a"} < $a
Truth table25%1/4
A and not(B) or C
A= defined $_->{"_a"}B= not defined $aC= $_->{"_a"} < $a
ABCresult
0XX0
1000
11X1
1011
5268 }
5269 }
5270 10202 ▶ if (!defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p) 10150 52 10202
5271 # even if $a is defined, take $p, to signal error for both defined
5272 10150 foreach ($self, @args) {
5273 # take the defined one, or if both defined, the one that is bigger
5274 # -2 > -3, and 3 > 2
5275 $p = $_->{_p}
5276 18981 ▶ if (defined $_->{_p}) && (!defined $p || $_->{_p} > $p);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"_p"} and !defined($p) || $_->{"_p"} > $p 10 18971 18981
Condition50%3/6
defined $_->{"_p"} and !defined($p) || $_->{"_p"} > $p
!ll&&!rl&&r
18971100
not defined $p or $_->{"_p"} > $p
l!l&&r!l&&!r
1000
MC/DC0%0/3
defined $_->{"_p"} and !defined($p) || $_->{"_p"} > $p
defined $_->{"_p"} not defined $p $_->{"_p"} > $p
Truth table25%1/4
A and not(B) or C
A= defined $_->{"_p"}B= not defined $pC= $_->{"_p"} > $p
ABCresult
0XX0
1000
11X1
1011
5277 }
5278 }
5279  
5280 # if still none defined, use globals (#2)
5281 10202 ▶ $a = ${"$class\::accuracy"} unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 962 9240 10202
5282 10202 ▶ $p = ${"$class\::precision"} unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 10140 62 10202
5283  
5284 # A == 0 is useless, so undef it to signal no rounding
5285 10202 ▶ $a = undef if defined $a && $a == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and $a == 0 16 10186 10202
Condition100%3/3
defined $a and $a == 0
!ll&&!rl&&r
959922716
MC/DC100%2/2
defined $a and $a == 0
defined $a $a == 0
Truth table100%3/3
A and B
A= defined $aB= $a == 0
ABresult
0X0
100
111
5286  
5287 # no rounding today?
5288 10202 ▶ return ($self) unless defined $a || defined $p; # early out
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a or defined $p 911 9291 10202
Condition100%3/3
defined $a or defined $p
l!l&&r!l&&!r
922764911
MC/DC100%2/2
defined $a or defined $p
defined $a defined $p
Truth table100%3/3
A or B
A= defined $aB= defined $p
ABresult
1X1
011
000
5289  
5290 # set A and set P is an fatal error
5291 9291 ▶ return ($self->bnan()) if defined $a && defined $p; # error
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and defined $p 9 9282 9291
Condition100%3/3
defined $a and defined $p
!ll&&!rl&&r
6492189
MC/DC100%2/2
defined $a and defined $p
defined $a defined $p
Truth table100%3/3
A and B
A= defined $aB= defined $p
ABresult
0X0
100
111
5292  
5293 9282 ▶ $r = ${"$class\::round_mode"} unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 9273 9 9282
5294 9282 ▶ if ($r !~ /^(even|odd|[+-]inf|zero|trunc|common)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r =~ /^(even|odd|[+-]inf|zero|trunc|common)$/) 3 9279 9282
5295 3 croak("Unknown round mode '$r'");
5296 }
5297  
5298 9279 ▶ $a = int($a) if defined $a;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a 9218 61 9279
5299 9279 ▶ $p = int($p) if defined $p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p 61 9218 9279
5300  
5301 9279 ($self, $a, $p, $r);
5302 }
5303  
5304 # Return true if the input is numeric and false if it is a string.
5305  
5306 sub _is_numeric {
5307 0 ▶ shift; # class name
Subroutine _is_numeric: not called CC=4 SCAR=25.0
5308 0 my $value = shift;
5309 51 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
5310 # detect numbers
5311 # string & "" -> ""
5312 # number & "" -> 0 (with warning)
5313 # nan and inf can detect as numbers, so check with * 0
5314 0 ▶ return unless CORE::length((my $dummy = "") & $value);
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length((my $dummy = "") & $value) 0 0 0
5315 0 ▶ return unless 0 + $value eq $value;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $value eq $value 0 0 0
5316 0 ▶ return 1 if $value * 0 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value * 0 == 0 0 0 0
5317 0 return -1; # Inf/NaN
5318 }
5319  
5320 # Trims the sign of the significand, the (absolute value of the) significand,
5321 # the sign of the exponent, and the (absolute value of the) exponent. The
5322 # returned values have no underscores ("_") or unnecessary leading or trailing
5323 # zeros.
5324  
5325 sub _trim_split_parts {
5326 10064 ▶ shift; # class name
Subroutine _trim_split_parts: called CC=10 SCAR=23.0
5327  
5328 10064 ▶ my $sig_sgn = shift() || '+';
Condition100%2/2
shift() || '+'
l!l
24807584
MC/DC100%1/1
shift() || '+'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
5329 10064 ▶ my $sig_str = shift() || '0';
Condition100%2/2
shift() || '0'
l!l
54664598
MC/DC100%1/1
shift() || '0'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
5330 10064 ▶ my $exp_sgn = shift() || '+';
Condition100%2/2
shift() || '+'
l!l
4139651
MC/DC100%1/1
shift() || '+'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
5331 10064 ▶ my $exp_str = shift() || '0';
Condition100%2/2
shift() || '0'
l!l
5859479
MC/DC100%1/1
shift() || '0'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
5332  
5333 10064 $sig_str =~ tr/_//d; # "1.0_0_0" -> "1.000"
5334 10064 $sig_str =~ s/^0+//; # "01.000" -> "1.000"
5335 10064 ▶ $sig_str =~ s/\.0*$// # "1.000" -> "1"
Branch: 1 branch
BranchTrueFalseTotal
$sig_str =~ s/\.0*$// or $sig_str =~ s/(\..*[^0])0+$/$1/ 10044 20 10064
5336 || $sig_str =~ s/(\..*[^0])0+$/$1/; # "1.010" -> "1.01"
5337 10064 ▶ $sig_str = '0' unless CORE::length($sig_str);
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $sig_str 4756 5308 10064
5338  
5339 10064 ▶ return '+', '0', '+', '0' if $sig_str eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $sig_str eq 0 4756 5308 10064
5340  
5341 5308 $exp_str =~ tr/_//d; # "01_234" -> "01234"
5342 5308 $exp_str =~ s/^0+//; # "01234" -> "1234"
5343 5308 ▶ $exp_str = '0' unless CORE::length($exp_str);
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $exp_str 4863 445 5308
5344 5308 ▶ $exp_sgn = '+' if $exp_str eq '0'; # "+3e-0" -> "+3e+0"
Branch: 1 branch
BranchTrueFalseTotal
if $exp_str eq 0 4863 445 5308
5345  
5346 5308 return $sig_sgn, $sig_str, $exp_sgn, $exp_str;
5347 }
5348  
5349 # Takes any string representing a valid decimal number and splits it into four
5350 # strings: the sign of the significand, the absolute value of the significand,
5351 # the sign of the exponent, and the absolute value of the exponent. Both the
5352 # significand and the exponent are in base 10.
5353 #
5354 # Perl accepts literals like the following. The value is 100.1.
5355 #
5356 # 1__0__.__0__1__e+0__1__ (prints "Misplaced _ in number")
5357 # 1_0.0_1e+0_1
5358 #
5359 # Strings representing decimal numbers do not allow underscores, so only the
5360 # following is valid
5361 #
5362 # "10.01e+01"
5363  
5364 sub _dec_str_to_dec_str_parts {
5365 9785 ▶ my $class = shift;
Subroutine _dec_str_to_dec_str_parts: called CC=2 SCAR=6.9
5366 9785 my $str = shift;
5367  
5368 9785 ▶ if ($str =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ / ^ # optional leading whitespace \s* # optional sign ( [+-]? ) # significand ( # integer part and optional fraction part ... \d+ (?: _+ \d+ )* _* (?: \. (?: _* \d+ (?: _+ \d+ )* _* )? )? | # ... or mandatory fraction part \. \d+ (?: _+ \d+ )* _* ) # optional exponent (?: [Ee] ( [+-]? ) ( \d+ (?: _+ \d+ )* _* ) )? # optional trailing whitespace \s* $ /x) 8695 1090 9785
5369 ^
5370  
5371 # optional leading whitespace
5372 \s*
5373  
5374 # optional sign
5375 ( [+-]? )
5376  
5377 # significand
5378 (
5379 # integer part and optional fraction part ...
5380 \d+ (?: _+ \d+ )* _*
5381 (?:
5382 \.
5383 (?: _* \d+ (?: _+ \d+ )* _* )?
5384 )?
5385 |
5386 # ... or mandatory fraction part
5387 \.
5388 \d+ (?: _+ \d+ )* _*
5389 )
5390  
5391 # optional exponent
5392 (?:
5393 [Ee]
5394 ( [+-]? )
5395 ( \d+ (?: _+ \d+ )* _* )
5396 )?
5397  
5398 # optional trailing whitespace
5399 \s*
5400  
5401 $
5402 /x)
5403 {
5404 8695 return $class -> _trim_split_parts($1, $2, $3, $4);
5405 }
5406  
5407 1090 return;
5408 }
5409  
5410 # Takes any string representing a valid hexadecimal number and splits it into
5411 # four strings: the sign of the significand, the absolute value of the
5412 # significand, the sign of the exponent, and the absolute value of the exponent.
5413 # The significand is in base 16, and the exponent is in base 2.
5414 #
5415 # Perl accepts literals like the following. The "x" might be a capital "X". The
5416 # value is 32.0078125.
5417 #
5418 # 0x__1__0__.0__1__p+0__1__ (prints "Misplaced _ in number")
5419 # 0x1_0.0_1p+0_1
5420 #
5421 # The CORE::hex() function does not accept floating point accepts
5422 #
5423 # "0x_1_0"
5424 # "x_1_0"
5425 # "_1_0"
5426  
5427 sub _hex_str_to_hex_str_parts {
5428 1118 ▶ my $class = shift;
Subroutine _hex_str_to_hex_str_parts: called CC=2 SCAR=6.9
5429 1118 my $str = shift;
5430  
5431 1118 ▶ if ($str =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ / ^ # optional leading whitespace \s* # optional sign ( [+-]? ) # optional hex prefix (?: 0? [Xx] _* )? # significand using the hex digits 0..9 and a..f ( # integer part and optional fraction part ... [0-9a-fA-F]+ (?: _+ [0-9a-fA-F]+ )* _* (?: \. (?: _* [0-9a-fA-F]+ (?: _+ [0-9a-fA-F]+ )* _* )? )? | # ... or mandatory fraction part \. [0-9a-fA-F]+ (?: _+ [0-9a-fA-F]+ )* _* ) # optional exponent (power of 2) using decimal digits (?: [Pp] ( [+-]? ) ( \d+ (?: _+ \d+ )* _* ) )? # optional trailing whitespace \s* $ /x) 1114 4 1118
5432 ^
5433  
5434 # optional leading whitespace
5435 \s*
5436  
5437 # optional sign
5438 ( [+-]? )
5439  
5440 # optional hex prefix
5441 (?: 0? [Xx] _* )?
5442  
5443 # significand using the hex digits 0..9 and a..f
5444 (
5445 # integer part and optional fraction part ...
5446 [0-9a-fA-F]+ (?: _+ [0-9a-fA-F]+ )* _*
5447 (?:
5448 \.
5449 (?: _* [0-9a-fA-F]+ (?: _+ [0-9a-fA-F]+ )* _* )?
5450 )?
5451 |
5452 # ... or mandatory fraction part
5453 \.
5454 [0-9a-fA-F]+ (?: _+ [0-9a-fA-F]+ )* _*
5455 )
5456  
5457 # optional exponent (power of 2) using decimal digits
5458 (?:
5459 [Pp]
5460 ( [+-]? )
5461 ( \d+ (?: _+ \d+ )* _* )
5462 )?
5463  
5464 # optional trailing whitespace
5465 \s*
5466  
5467 $
5468 /x)
5469 {
5470 1114 return $class -> _trim_split_parts($1, $2, $3, $4);
5471 }
5472  
5473 4 return;
5474 }
5475  
5476 # Takes any string representing a valid octal number and splits it into four
5477 # strings: the sign of the significand, the absolute value of the significand,
5478 # the sign of the exponent, and the absolute value of the exponent. The
5479 # significand is in base 8, and the exponent is in base 2.
5480  
5481 sub _oct_str_to_oct_str_parts {
5482 3 ▶ my $class = shift;
Subroutine _oct_str_to_oct_str_parts: called CC=2 SCAR=7.4
5483 3 my $str = shift;
5484  
5485 3 ▶ if ($str =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ / ^ # optional leading whitespace \s* # optional sign ( [+-]? ) # optional octal prefix (?: 0? [Oo] _* )? # significand using the octal digits 0..7 ( # integer part and optional fraction part ... [0-7]+ (?: _+ [0-7]+ )* _* (?: \. (?: _* [0-7]+ (?: _+ [0-7]+ )* _* )? )? | # ... or mandatory fraction part \. [0-7]+ (?: _+ [0-7]+ )* _* ) # optional exponent (power of 2) using decimal digits (?: [Pp] ( [+-]? ) ( \d+ (?: _+ \d+ )* _* ) )? # optional trailing whitespace \s* $ /x) 3 0 3
5486 ^
5487  
5488 # optional leading whitespace
5489 \s*
5490  
5491 # optional sign
5492 ( [+-]? )
5493  
5494 # optional octal prefix
5495 (?: 0? [Oo] _* )?
5496  
5497 # significand using the octal digits 0..7
5498 (
5499 # integer part and optional fraction part ...
5500 [0-7]+ (?: _+ [0-7]+ )* _*
5501 (?:
5502 \.
5503 (?: _* [0-7]+ (?: _+ [0-7]+ )* _* )?
5504 )?
5505 |
5506 # ... or mandatory fraction part
5507 \.
5508 [0-7]+ (?: _+ [0-7]+ )* _*
5509 )
5510  
5511 # optional exponent (power of 2) using decimal digits
5512 (?:
5513 [Pp]
5514 ( [+-]? )
5515 ( \d+ (?: _+ \d+ )* _* )
5516 )?
5517  
5518 # optional trailing whitespace
5519 \s*
5520  
5521 $
5522 /x)
5523 {
5524 3 return $class -> _trim_split_parts($1, $2, $3, $4);
5525 }
5526  
5527 0 return;
5528 }
5529  
5530 # Takes any string representing a valid binary number and splits it into four
5531 # strings: the sign of the significand, the absolute value of the significand,
5532 # the sign of the exponent, and the absolute value of the exponent. The
5533 # significand is in base 2, and the exponent is in base 2.
5534  
5535 sub _bin_str_to_bin_str_parts {
5536 275 ▶ my $class = shift;
Subroutine _bin_str_to_bin_str_parts: called CC=2 SCAR=6.9
5537 275 my $str = shift;
5538  
5539 275 ▶ if ($str =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ / ^ # optional leading whitespace \s* # optional sign ( [+-]? ) # optional binary prefix (?: 0? [Bb] _* )? # significand using the binary digits 0 and 1 ( # integer part and optional fraction part ... [01]+ (?: _+ [01]+ )* _* (?: \. (?: _* [01]+ (?: _+ [01]+ )* _* )? )? | # ... or mandatory fraction part \. [01]+ (?: _+ [01]+ )* _* ) # optional exponent (power of 2) using decimal digits (?: [Pp] ( [+-]? ) ( \d+ (?: _+ \d+ )* _* ) )? # optional trailing whitespace \s* $ /x) 252 23 275
5540 ^
5541  
5542 # optional leading whitespace
5543 \s*
5544  
5545 # optional sign
5546 ( [+-]? )
5547  
5548 # optional binary prefix
5549 (?: 0? [Bb] _* )?
5550  
5551 # significand using the binary digits 0 and 1
5552 (
5553 # integer part and optional fraction part ...
5554 [01]+ (?: _+ [01]+ )* _*
5555 (?:
5556 \.
5557 (?: _* [01]+ (?: _+ [01]+ )* _* )?
5558 )?
5559 |
5560 # ... or mandatory fraction part
5561 \.
5562 [01]+ (?: _+ [01]+ )* _*
5563 )
5564  
5565 # optional exponent (power of 2) using decimal digits
5566 (?:
5567 [Pp]
5568 ( [+-]? )
5569 ( \d+ (?: _+ \d+ )* _* )
5570 )?
5571  
5572 # optional trailing whitespace
5573 \s*
5574  
5575 $
5576 /x)
5577 {
5578 252 return $class -> _trim_split_parts($1, $2, $3, $4);
5579 }
5580  
5581 23 return;
5582 }
5583  
5584 # Takes any string representing a valid decimal number and splits it into four
5585 # parts: the sign of the significand, the absolute value of the significand as a
5586 # libray thingy, the sign of the exponent, and the absolute value of the
5587 # exponent as a library thingy.
5588  
5589 sub _dec_str_parts_to_flt_lib_parts {
5590 8695 ▶ shift; # class name
Subroutine _dec_str_parts_to_flt_lib_parts: called CC=5 SCAR=16.1
5591  
5592 8695 my ($sig_sgn, $sig_str, $exp_sgn, $exp_str) = @_;
5593  
5594 # Handle zero.
5595  
5596 8695 ▶ if ($sig_str eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_str eq 0) 4744 3951 8695
5597 4744 return '+', $LIB -> _zero(), '+', $LIB -> _zero();
5598 }
5599  
5600 # Absolute value of exponent as library "object".
5601  
5602 3951 my $exp_lib = $LIB -> _new($exp_str);
5603  
5604 # If there is a dot in the significand, remove it so the significand
5605 # becomes an integer and adjust the exponent accordingly. Also remove
5606 # leading zeros which might now appear in the significand. E.g.,
5607 #
5608 # 12.345e-2 -> 12345e-5
5609 # 12.345e+2 -> 12345e-1
5610 # 0.0123e+5 -> 00123e+1 -> 123e+1
5611  
5612 3951 my $idx = index $sig_str, '.';
5613 3951 ▶ if ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= 0) 2324 1627 3951
5614 2324 substr($sig_str, $idx, 1) = '';
5615  
5616 # delta = length - index
5617 2324 my $delta = $LIB -> _new(CORE::length($sig_str));
5618 2324 $delta = $LIB -> _sub($delta, $LIB -> _new($idx));
5619  
5620 # exponent - delta
5621 2324 ($exp_lib, $exp_sgn) = $LIB -> _ssub($exp_lib, $exp_sgn, $delta, '+');
5622  
5623 2324 $sig_str =~ s/^0+//;
5624 }
5625  
5626 # If there are trailing zeros in the significand, remove them and
5627 # adjust the exponent. E.g.,
5628 #
5629 # 12340e-5 -> 1234e-4
5630 # 12340e-1 -> 1234e0
5631 # 12340e+3 -> 1234e4
5632  
5633 3951 ▶ if ($sig_str =~ s/(0+)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_str =~ s/(0+)\z//) 863 3088 3951
5634 863 my $len = CORE::length($1);
5635 863 ($exp_lib, $exp_sgn) =
5636 $LIB -> _sadd($exp_lib, $exp_sgn, $LIB -> _new($len), '+');
5637 }
5638  
5639 # At this point, the significand is empty or an integer with no trailing
5640 # zeros. The exponent is in base 10.
5641  
5642 3951 ▶ unless (CORE::length $sig_str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::length $sig_str) 0 3951 3951
5643 0 return '+', $LIB -> _zero(), '+', $LIB -> _zero();
5644 }
5645  
5646 # Absolute value of significand as library "object".
5647  
5648 3951 my $sig_lib = $LIB -> _new($sig_str);
5649  
5650 3951 return $sig_sgn, $sig_lib, $exp_sgn, $exp_lib;
5651 }
5652  
5653 # Takes any string representing a valid binary number and splits it into four
5654 # parts: the sign of the significand, the absolute value of the significand as a
5655 # libray thingy, the sign of the exponent, and the absolute value of the
5656 # exponent as a library thingy.
5657  
5658 sub _bin_str_parts_to_flt_lib_parts {
5659 1369 ▶ shift; # class name
Subroutine _bin_str_parts_to_flt_lib_parts: called CC=13 SCAR=25.7
5660  
5661 1369 my ($sig_sgn, $sig_str, $exp_sgn, $exp_str, $bpc) = @_;
5662 1369 my $bpc_lib = $LIB -> _new($bpc);
5663  
5664 # Handle zero.
5665  
5666 1369 ▶ if ($sig_str eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_str eq 0) 12 1357 1369
5667 12 return '+', $LIB -> _zero(), '+', $LIB -> _zero();
5668 }
5669  
5670 # Absolute value of exponent as library "object".
5671  
5672 1357 my $exp_lib = $LIB -> _new($exp_str);
5673  
5674 # If there is a dot in the significand, remove it so the significand
5675 # becomes an integer and adjust the exponent accordingly. Also remove
5676 # leading zeros which might now appear in the significand. E.g., with
5677 # hexadecimal numbers
5678 #
5679 # 12.345p-2 -> 12345p-14
5680 # 12.345p+2 -> 12345p-10
5681 # 0.0123p+5 -> 00123p-11 -> 123p-11
5682  
5683 1357 my $idx = index $sig_str, '.';
5684 1357 ▶ if ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= 0) 3 1354 1357
5685 3 substr($sig_str, $idx, 1) = '';
5686  
5687 # delta = (length - index) * bpc
5688 3 my $delta = $LIB -> _new(CORE::length($sig_str));
5689 3 $delta = $LIB -> _sub($delta, $LIB -> _new($idx));
5690 3 ▶ $delta = $LIB -> _mul($delta, $bpc_lib) if $bpc != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bpc != 1 2 1 3
5691  
5692 # exponent - delta
5693 3 ($exp_lib, $exp_sgn) = $LIB -> _ssub($exp_lib, $exp_sgn, $delta, '+');
5694  
5695 3 $sig_str =~ s/^0+//;
5696 }
5697  
5698 # If there are trailing zeros in the significand, remove them and
5699 # adjust the exponent accordingly. E.g., with hexadecimal numbers
5700 #
5701 # 12340p-5 -> 1234p-1
5702 # 12340p-1 -> 1234p+3
5703 # 12340p+3 -> 1234p+7
5704  
5705 1357 ▶ if ($sig_str =~ s/(0+)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_str =~ s/(0+)\z//) 241 1116 1357
5706  
5707 # delta = length * bpc
5708 241 my $delta = $LIB -> _new(CORE::length($1));
5709 241 ▶ $delta = $LIB -> _mul($delta, $bpc_lib) if $bpc != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bpc != 1 216 25 241
5710  
5711 # exponent + delta
5712 241 ($exp_lib, $exp_sgn) = $LIB -> _sadd($exp_lib, $exp_sgn, $delta, '+');
5713 }
5714  
5715 # At this point, the significand is empty or an integer with no leading
5716 # or trailing zeros. The exponent is in base 2.
5717  
5718 1357 ▶ unless (CORE::length $sig_str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::length $sig_str) 0 1357 1357
5719 0 return '+', $LIB -> _zero(), '+', $LIB -> _zero();
5720 }
5721  
5722 # Absolute value of significand as library "object".
5723  
5724 1357 ▶ my $sig_lib = $bpc == 1 ? $LIB -> _from_bin('0b' . $sig_str)
Branch: 3 branches
BranchTrueFalseTotal
$bpc == 1 ? : 244 1113 1357
$bpc == 3 ? : 3 1110 1113
$bpc == 4 ? : 1110 0 1110
5725 : $bpc == 3 ? $LIB -> _from_oct('0' . $sig_str)
5726 : $bpc == 4 ? $LIB -> _from_hex('0x' . $sig_str)
5727 : die "internal error: invalid exponent multiplier";
5728  
5729 # If the exponent (in base 2) is positive or zero ...
5730  
5731 1357 ▶ if ($exp_sgn eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp_sgn eq '+') { } 1356 1 1357
5732  
5733 1356 ▶ if (!$LIB -> _is_zero($exp_lib)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($exp_lib)) 242 1114 1356
5734  
5735 # Multiply significand by 2 raised to the exponent.
5736  
5737 242 my $p = $LIB -> _pow($LIB -> _two(), $exp_lib);
5738 242 $sig_lib = $LIB -> _mul($sig_lib, $p);
5739 242 $exp_lib = $LIB -> _zero();
5740 }
5741 }
5742  
5743 # ... else if the exponent is negative ...
5744  
5745 else {
5746  
5747 # Rather than dividing the significand by 2 raised to the absolute
5748 # value of the exponent, multiply the significand by 5 raised to the
5749 # absolute value of the exponent and let the exponent be in base 10:
5750 #
5751 # a * 2^(-b) = a * 5^b * 10^(-b) = c * 10^(-b), where c = a * 5^b
5752  
5753 1 my $p = $LIB -> _pow($LIB -> _new("5"), $exp_lib);
5754 1 $sig_lib = $LIB -> _mul($sig_lib, $p);
5755 }
5756  
5757 # Adjust for the case when the conversion to decimal introduced trailing
5758 # zeros in the significand.
5759  
5760 1357 my $n = $LIB -> _zeros($sig_lib);
5761 1357 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 213 1144 1357
5762 213 $n = $LIB -> _new($n);
5763 213 $sig_lib = $LIB -> _rsft($sig_lib, $n, 10);
5764 213 ($exp_lib, $exp_sgn) = $LIB -> _sadd($exp_lib, $exp_sgn, $n, '+');
5765 }
5766  
5767 1357 return $sig_sgn, $sig_lib, $exp_sgn, $exp_lib;
5768 }
5769  
5770 # Takes any string representing a valid hexadecimal number and splits it into
5771 # four parts: the sign of the significand, the absolute value of the significand
5772 # as a libray thingy, the sign of the exponent, and the absolute value of the
5773 # exponent as a library thingy.
5774  
5775 sub _hex_str_to_flt_lib_parts {
5776 1118 ▶ my $class = shift;
Subroutine _hex_str_to_flt_lib_parts: called CC=2 SCAR=6.9
5777 1118 my $str = shift;
5778 1118 ▶ if (my @parts = $class -> _hex_str_to_hex_str_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_hex_str_to_hex_str_parts($str)) 1114 4 1118
5779 1114 return $class -> _bin_str_parts_to_flt_lib_parts(@parts, 4); # 4 bits pr. chr
5780 }
5781 4 return;
5782 }
5783  
5784 # Takes any string representing a valid octal number and splits it into four
5785 # parts: the sign of the significand, the absolute value of the significand as a
5786 # libray thingy, the sign of the exponent, and the absolute value of the
5787 # exponent as a library thingy.
5788  
5789 sub _oct_str_to_flt_lib_parts {
5790 3 ▶ my $class = shift;
Subroutine _oct_str_to_flt_lib_parts: called CC=2 SCAR=7.4
5791 3 my $str = shift;
5792 3 ▶ if (my @parts = $class -> _oct_str_to_oct_str_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_oct_str_to_oct_str_parts($str)) 3 0 3
5793 3 return $class -> _bin_str_parts_to_flt_lib_parts(@parts, 3); # 3 bits pr. chr
5794 }
5795 0 return;
5796 }
5797  
5798 # Takes any string representing a valid binary number and splits it into four
5799 # parts: the sign of the significand, the absolute value of the significand as a
5800 # libray thingy, the sign of the exponent, and the absolute value of the
5801 # exponent as a library thingy.
5802  
5803 sub _bin_str_to_flt_lib_parts {
5804 275 ▶ my $class = shift;
Subroutine _bin_str_to_flt_lib_parts: called CC=2 SCAR=6.9
5805 275 my $str = shift;
5806 275 ▶ if (my @parts = $class -> _bin_str_to_bin_str_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_bin_str_to_bin_str_parts($str)) 252 23 275
5807 252 return $class -> _bin_str_parts_to_flt_lib_parts(@parts, 1); # 1 bit pr. chr
5808 }
5809 23 return;
5810 }
5811  
5812 # Decimal string is split into the sign of the signficant, the absolute value of
5813 # the significand as library thingy, the sign of the exponent, and the absolute
5814 # value of the exponent as a a library thingy.
5815  
5816 sub _dec_str_to_flt_lib_parts {
5817 9785 ▶ my $class = shift;
Subroutine _dec_str_to_flt_lib_parts: called CC=2 SCAR=6.9
5818 9785 my $str = shift;
5819 9785 ▶ if (my @parts = $class -> _dec_str_to_dec_str_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_dec_str_to_dec_str_parts($str)) 8695 1090 9785
5820 8695 return $class -> _dec_str_parts_to_flt_lib_parts(@parts);
5821 }
5822 1090 return;
5823 }
5824  
5825 # Hexdecimal string to a string using decimal floating point notation.
5826  
5827 sub hex_str_to_dec_flt_str {
5828 0 ▶ my $class = shift;
Subroutine hex_str_to_dec_flt_str: not called CC=2 SCAR=17.9
Pod documented
5829 0 my $str = shift;
5830 0 ▶ if (my @parts = $class -> _hex_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_hex_str_to_flt_lib_parts($str)) 0 0 0
5831 0 return $class -> _flt_lib_parts_to_flt_str(@parts);
5832 }
5833 0 return;
5834 }
5835  
5836 # Octal string to a string using decimal floating point notation.
5837  
5838 sub oct_str_to_dec_flt_str {
5839 0 ▶ my $class = shift;
Subroutine oct_str_to_dec_flt_str: not called CC=2 SCAR=17.9
Pod documented
5840 0 my $str = shift;
5841 0 ▶ if (my @parts = $class -> _oct_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_oct_str_to_flt_lib_parts($str)) 0 0 0
5842 0 return $class -> _flt_lib_parts_to_flt_str(@parts);
5843 }
5844 0 return;
5845 }
5846  
5847 # Binary string to a string decimal floating point notation.
5848  
5849 sub bin_str_to_dec_flt_str {
5850 0 ▶ my $class = shift;
Subroutine bin_str_to_dec_flt_str: not called CC=2 SCAR=17.9
Pod documented
5851 0 my $str = shift;
5852 0 ▶ if (my @parts = $class -> _bin_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_bin_str_to_flt_lib_parts($str)) 0 0 0
5853 0 return $class -> _flt_lib_parts_to_flt_str(@parts);
5854 }
5855 0 return;
5856 }
5857  
5858 # Decimal string to a string using decimal floating point notation.
5859  
5860 sub dec_str_to_dec_flt_str {
5861 0 ▶ my $class = shift;
Subroutine dec_str_to_dec_flt_str: not called CC=2 SCAR=17.9
Pod documented
5862 0 my $str = shift;
5863 0 ▶ if (my @parts = $class -> _dec_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_dec_str_to_flt_lib_parts($str)) 0 0 0
5864 0 return $class -> _flt_lib_parts_to_flt_str(@parts);
5865 }
5866 0 return;
5867 }
5868  
5869 # Hexdecimal string to decimal notation (no exponent).
5870  
5871 sub hex_str_to_dec_str {
5872 0 ▶ my $class = shift;
Subroutine hex_str_to_dec_str: not called CC=2 SCAR=17.9
Pod documented
5873 0 my $str = shift;
5874 0 ▶ if (my @parts = $class -> _dec_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_dec_str_to_flt_lib_parts($str)) 0 0 0
5875 0 return $class -> _flt_lib_parts_to_dec_str(@parts);
5876 }
5877 0 return;
5878 }
5879  
5880 # Octal string to decimal notation (no exponent).
5881  
5882 sub oct_str_to_dec_str {
5883 0 ▶ my $class = shift;
Subroutine oct_str_to_dec_str: not called CC=2 SCAR=17.9
Pod documented
5884 0 my $str = shift;
5885 0 ▶ if (my @parts = $class -> _oct_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_oct_str_to_flt_lib_parts($str)) 0 0 0
5886 0 return $class -> _flt_lib_parts_to_dec_str(@parts);
5887 }
5888 0 return;
5889 }
5890  
5891 # Binary string to decimal notation (no exponent).
5892  
5893 sub bin_str_to_dec_str {
5894 0 ▶ my $class = shift;
Subroutine bin_str_to_dec_str: not called CC=2 SCAR=17.9
Pod documented
5895 0 my $str = shift;
5896 0 ▶ if (my @parts = $class -> _bin_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_bin_str_to_flt_lib_parts($str)) 0 0 0
5897 0 return $class -> _flt_lib_parts_to_dec_str(@parts);
5898 }
5899 0 return;
5900 }
5901  
5902 # Decimal string to decimal notation (no exponent).
5903  
5904 sub dec_str_to_dec_str {
5905 0 ▶ my $class = shift;
Subroutine dec_str_to_dec_str: not called CC=2 SCAR=17.9
Pod documented
5906 0 my $str = shift;
5907 0 ▶ if (my @parts = $class -> _dec_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_dec_str_to_flt_lib_parts($str)) 0 0 0
5908 0 return $class -> _flt_lib_parts_to_dec_str(@parts);
5909 }
5910 0 return;
5911 }
5912  
5913 sub _flt_lib_parts_to_flt_str {
5914 0 ▶ my $class = shift;
Subroutine _flt_lib_parts_to_flt_str: not called CC=1 SCAR=6.9
5915 0 my @parts = @_;
5916 0 return $parts[0] . $LIB -> _str($parts[1])
5917 . 'e' . $parts[2] . $LIB -> _str($parts[3]);
5918 }
5919  
5920 sub _flt_lib_parts_to_dec_str {
5921 0 ▶ my $class = shift;
Subroutine _flt_lib_parts_to_dec_str: not called CC=3 SCAR=24.8
5922 0 my @parts = @_;
5923  
5924 # The number is an integer iff the exponent is non-negative.
5925  
5926 0 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq '+') { } 0 0 0
5927 0 my $str = $parts[0]
5928 . $LIB -> _str($LIB -> _lsft($parts[1], $parts[3], 10));
5929 0 return $str;
5930 }
5931  
5932 # If it is not an integer, add a decimal point.
5933  
5934 else {
5935 0 my $mant = $LIB -> _str($parts[1]);
5936 0 my $mant_len = CORE::length($mant);
5937 0 my $expo = $LIB -> _num($parts[3]);
5938 0 my $len_cmp = $mant_len <=> $expo;
5939 0 ▶ if ($len_cmp <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_cmp <= 0) { } 0 0 0
5940 0 return $parts[0] . '0.' . '0' x ($expo - $mant_len) . $mant;
5941 } else {
5942 0 substr $mant, $mant_len - $expo, 0, '.';
5943 0 return $parts[0] . $mant;
5944 }
5945 }
5946 }
5947  
5948 # Takes four arguments, the sign of the significand, the absolute value of the
5949 # significand as a libray thingy, the sign of the exponent, and the absolute
5950 # value of the exponent as a library thingy, and returns three parts: the sign
5951 # of the rational number, the absolute value of the numerator as a libray
5952 # thingy, and the absolute value of the denominator as a library thingy.
5953 #
5954 # For example, to convert data representing the value "+12e-2", then
5955 #
5956 # $sm = "+";
5957 # $m = $LIB -> _new("12");
5958 # $se = "-";
5959 # $e = $LIB -> _new("2");
5960 # ($sr, $n, $d) = $class -> _flt_lib_parts_to_rat_lib_parts($sm, $m, $se, $e);
5961 #
5962 # returns data representing the same value written as the fraction "+3/25"
5963 #
5964 # $sr = "+"
5965 # $n = $LIB -> _new("3");
5966 # $d = $LIB -> _new("12");
5967  
5968 sub _flt_lib_parts_to_rat_lib_parts {
5969 0 ▶ my $self = shift;
Subroutine _flt_lib_parts_to_rat_lib_parts: not called CC=3 SCAR=24.8
5970 0 my ($msgn, $mabs, $esgn, $eabs) = @_;
5971  
5972 0 ▶ if ($esgn eq '-') { # "12e-2" -> "12/100" -> "3/25"
Branch: 2 branches
BranchTrueFalseTotal
if ($esgn eq '-') { } 0 0 0
elsif (not $LIB->_is_zero($eabs)) { } 0 0 0
5973 0 my $num_lib = $LIB -> _copy($mabs);
5974 0 my $den_lib = $LIB -> _1ex($LIB -> _num($eabs));
5975 0 my $gcd_lib = $LIB -> _gcd($LIB -> _copy($num_lib), $den_lib);
5976 0 $num_lib = $LIB -> _div($LIB -> _copy($num_lib), $gcd_lib);
5977 0 $den_lib = $LIB -> _div($den_lib, $gcd_lib);
5978 0 return $msgn, $num_lib, $den_lib;
5979 }
5980  
5981 elsif (!$LIB -> _is_zero($eabs)) { # "12e+2" -> "1200" -> "1200/1"
5982 0 return $msgn, $LIB -> _lsft($LIB -> _copy($mabs), $eabs, 10),
5983 $LIB -> _one();
5984 }
5985  
5986 else { # "12e+0" -> "12" -> "12/1"
5987 0 return $msgn, $mabs, $LIB -> _one();
5988 }
5989 }
5990  
5991 # Add the function _register_callback() to Math::BigInt. It is provided for
5992 # backwards compabibility so that old version of Math::BigRat etc. don't
5993 # complain about missing it.
5994  
5995 ▶sub _register_callback { }
Subroutine _register_callback: not called CC=1 SCAR=0.0
5996  
5997 ###############################################################################
5998 # this method returns 0 if the object can be modified, or 1 if not.
5999 # We use a fast constant sub() here, to avoid costly calls. Subclasses
6000 # may override it with special code (f.i. Math::BigInt::Constant does so)
6001  
6002 sub modify () { 0; }
6003  
6004 51 1;
6005  
6006 __END__
↑ Summary BigFloat.pm »