BigInt.pm

filter: Statementstmt 64.4% 1760 / 2731 Branchbran 53.6% 1373 / 2560 Conditioncond 58.4% 664 / 1136 MC/DCmcdc 39.3% 259 / 659 Subroutinesub 75.2% 191 / 254 Podpod 98.6% 149 / 151 totaltotal 58.6% 4396 / 7491 CC 1687 SCAR 47.3
CC
1687
Cov
59%
hyperop
1056.0
blsft
889.4
brsft
663.2
CRAP 112.8
« BigFloat.pm ↑ Summary BigInt/Lib.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 #
12 # sign : "+", "-", "+inf", "-inf", or "NaN"
13 # value : unsigned int with actual value ($LIB thingy)
14 # accuracy : accuracy (scalar)
15 # precision : precision (scalar)
16  
17 # Remember not to take shortcuts ala $xs = $x->{value}; $LIB->foo($xs); since
18 # underlying lib might change the reference!
19  
20 50 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 50 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 50 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 50 ▶use Carp qw< carp croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 50 ▶use Scalar::Util qw< blessed refaddr >;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 50 our $VERSION = '2.005003';
28 50 $VERSION =~ tr/_//d;
29  
30 50 require Exporter;
31 50 our @ISA = qw< Exporter >;
32 50 our @EXPORT_OK = qw< objectify bgcd blcm >;
33  
34 # Inside overload, the first arg is always an object. If the original code had
35 # it reversed (like $x = 2 * $y), then the third parameter is true.
36 # In some cases (like add, $x = $x + 2 is the same as $x = 2 + $x) this makes
37 # no difference, but in some cases it does.
38  
39 # For overloaded ops with only one argument we simple use $_[0]->copy() to
40 # preserve the argument.
41  
42 # Thus inheritance of overload operators becomes possible and transparent for
43 # our subclasses without the need to repeat the entire overload section there.
44  
45 use overload
46  
47 # overload key: with_assign
48  
49 309 ▶ '+' => sub { $_[0] -> copy() -> badd($_[1]); },
Subroutine BEGIN: called CC=1 SCAR=0.1
Subroutine __ANON__: called CC=1 SCAR=0.0
50  
51 356 ▶ '-' => sub { my $c = $_[0] -> copy();
Subroutine __ANON__: called CC=2 SCAR=6.9
52 356 ▶ $_[2] ? $c -> bneg() -> badd($_[1])
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 56 300 356
53 : $c -> bsub($_[1]); },
54  
55 838 ▶ '*' => sub { $_[0] -> copy() -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
56  
57 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
58 : $_[0] -> copy() -> bdiv($_[1]); },
59  
60 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
61 : $_[0] -> copy() -> bmod($_[1]); },
62  
63 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
64 : $_[0] -> copy() -> bpow($_[1]); },
65  
66 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
67 : $_[0] -> copy() -> bblsft($_[1]); },
68  
69 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
70 : $_[0] -> copy() -> bbrsft($_[1]); },
71  
72 # overload key: assign
73  
74 27 ▶ '+=' => sub { $_[0] -> badd($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
75  
76 29 ▶ '-=' => sub { $_[0] -> bsub($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
77  
78 17 ▶ '*=' => sub { $_[0] -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
79  
80 14 ▶ '/=' => sub { scalar $_[0] -> bdiv($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
81  
82 17 ▶ '%=' => sub { $_[0] -> bmod($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
83  
84 6 ▶ '**=' => sub { $_[0] -> bpow($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
85  
86 3 ▶ '<<=' => sub { $_[0] -> bblsft($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
87  
88 3 ▶ '>>=' => sub { $_[0] -> bbrsft($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
89  
90 # 'x=' => sub { },
91  
92 # '.=' => sub { },
93  
94 # overload key: num_comparison
95  
96 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
97 : $_[0] -> blt($_[1]); },
98  
99 618 ▶ '<=' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> ble($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 4 614 618
100 : $_[0] -> ble($_[1]); },
101  
102 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
103 : $_[0] -> bgt($_[1]); },
104  
105 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
106 : $_[0] -> bge($_[1]); },
107  
108 294 ▶ '==' => sub { $_[0] -> beq($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
109  
110 9 ▶ '!=' => sub { $_[0] -> bne($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
111  
112 # overload key: 3way_comparison
113  
114 0 ▶ '<=>' => sub { my $cmp = $_[0] -> bcmp($_[1]);
Subroutine __ANON__: not called CC=3 SCAR=24.8
115 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
116  
117 8102 ▶ 'cmp' => sub { $_[2] ? "$_[1]" cmp $_[0] -> bstr()
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 8102 8102
118 : $_[0] -> bstr() cmp "$_[1]"; },
119  
120 # overload key: str_comparison
121  
122 # 'lt' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrlt($_[0])
123 # : $_[0] -> bstrlt($_[1]); },
124 #
125 # 'le' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrle($_[0])
126 # : $_[0] -> bstrle($_[1]); },
127 #
128 # 'gt' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrgt($_[0])
129 # : $_[0] -> bstrgt($_[1]); },
130 #
131 # 'ge' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrge($_[0])
132 # : $_[0] -> bstrge($_[1]); },
133 #
134 # 'eq' => sub { $_[0] -> bstreq($_[1]); },
135 #
136 # 'ne' => sub { $_[0] -> bstrne($_[1]); },
137  
138 # overload key: binary
139  
140 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
141 : $_[0] -> copy() -> band($_[1]); },
142  
143 4 ▶ '&=' => sub { $_[0] -> band($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
144  
145 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
146 : $_[0] -> copy() -> bior($_[1]); },
147  
148 4 ▶ '|=' => sub { $_[0] -> bior($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
149  
150 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
151 : $_[0] -> copy() -> bxor($_[1]); },
152  
153 4 ▶ '^=' => sub { $_[0] -> bxor($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
154  
155 # '&.' => sub { },
156  
157 # '&.=' => sub { },
158  
159 # '|.' => sub { },
160  
161 # '|.=' => sub { },
162  
163 # '^.' => sub { },
164  
165 # '^.=' => sub { },
166  
167 # overload key: unary
168  
169 285 ▶ 'neg' => sub { $_[0] -> copy() -> bneg(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
170  
171 # '!' => sub { },
172  
173 0 ▶ '~' => sub { $_[0] -> copy() -> bnot(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
174  
175 # '~.' => sub { },
176  
177 # overload key: mutators
178  
179 22 ▶ '++' => sub { $_[0] -> binc() },
Subroutine __ANON__: called CC=1 SCAR=0.0
180  
181 3 ▶ '--' => sub { $_[0] -> bdec() },
Subroutine __ANON__: called CC=1 SCAR=0.0
182  
183 # overload key: func
184  
185 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
186 : $_[0] -> copy() -> batan2($_[1]); },
187  
188 0 ▶ 'cos' => sub { $_[0] -> copy() -> bcos(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
189  
190 0 ▶ 'sin' => sub { $_[0] -> copy() -> bsin(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
191  
192 0 ▶ 'exp' => sub { $_[0] -> copy() -> bexp($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
193  
194 3 ▶ 'abs' => sub { $_[0] -> copy() -> babs(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
195  
196 30 ▶ 'log' => sub { $_[0] -> copy() -> blog(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
197  
198 1 ▶ 'sqrt' => sub { $_[0] -> copy() -> bsqrt(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
199  
200 6 ▶ 'int' => sub { $_[0] -> copy() -> bint(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
201  
202 # overload key: conversion
203  
204 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
205  
206 1898 ▶ '""' => sub { $_[0] -> bstr(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
207  
208 51 ▶ '0+' => sub { $_[0] -> numify(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
209  
210 0 ▶ '=' => sub { $_[0] -> copy(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
211  
212 50 ;
213  
214 ##############################################################################
215 # global constants, flags and accessory
216  
217 # These vars are public, but their direct usage is not recommended, use the
218 # accessor methods instead
219  
220 50 our $accuracy = undef;
221 50 our $precision = undef;
222  
223 50 our $round_mode = 'even'; # even, odd, +inf, -inf, zero, trunc, common
224 50 our $div_scale = 40;
225  
226 50 our $upgrade = undef;
227 50 our $downgrade = undef;
228  
229 50 our $_trap_nan = 0; # croak on NaNs?
230 50 our $_trap_inf = 0; # croak on Infs?
231  
232 50 my $nan = 'NaN'; # constant for easier life
233  
234 # Module to do the low level math.
235  
236 50 my $DEFAULT_LIB = 'Math::BigInt::Calc';
237 50 my $LIB;
238  
239 # Has import() been called yet? This variable is needed to make "require" work.
240  
241 50 my $IMPORT = 0;
242  
243 ##############################################################################
244 # the old code had $rnd_mode, so we need to support it, too
245  
246 50 our $rnd_mode = 'even';
247  
248 50 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=1 SCAR=0.0
249 50 my ($class) = @_;
250 50 bless \$round_mode, $class;
251 }
252  
253 3 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
254 3 return $round_mode;
255 }
256  
257 51 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
258 51 $rnd_mode = (ref $_[0]) -> round_mode($_[1]);
259 }
260  
261 50 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
262 # tie to enable $rnd_mode to work transparently
263 50 tie $rnd_mode, 'Math::BigInt';
264  
265 # set up some handy alias names
266 50 *is_pos = \&is_positive;
267 50 *is_neg = \&is_negative;
268 50 *as_number = \&as_int;
269 }
270  
271 ###############################################################################
272 # Configuration methods
273 ###############################################################################
274  
275 77446 ▶sub accuracy {
Subroutine accuracy: called CC=12 SCAR=25.2
Pod documented
276 77446 my $x = shift;
277 77446 ▶ my $class = ref($x) || $x || __PACKAGE__;
Condition60%3/5
ref $x || $x || __PACKAGE__
l!l
774460
ref $x or $x
l!l&&r!l&&!r
631768150
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
278  
279 # setter/mutator
280  
281 77446 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1205 76241 77446
282 1205 my $a = shift;
283  
284 1205 ▶ if (defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a) 503 702 1205
285 503 ▶ $a = $a -> can('numify') ? $a -> numify() : 0 + "$a" if ref($a);
Branch: 2 branches
BranchTrueFalseTotal
if ref $a 0 503 503
$a->can('numify') ? : 0 0 0
286 503 ▶ croak "accuracy must be a number, not '$a'"
Branch: 1 branch
BranchTrueFalseTotal
unless $a =~ /^\s*[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\s*\z/ 0 503 503
287 if $a !~ /^\s*[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\s*\z/;
288 503 ▶ croak "accuracy must be an integer, not '$a'"
Branch: 1 branch
BranchTrueFalseTotal
if $a != int $a 0 503 503
289 if $a != int $a;
290 }
291  
292 1205 ▶ if (ref($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x) { } 518 687 1205
293 518 ▶ $x -> bround($a) if defined $a;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a 450 68 518
294 518 $x -> {precision} = undef; # clear instance P
295 518 $x -> {accuracy} = $a; # set instance A
296 } else {
297 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
298 687 ${"${class}::precision"} = undef; # clear class P
299 687 ${"${class}::accuracy"} = $a; # set class A
300 }
301 }
302  
303 # getter/accessor
304  
305 else {
306 76241 ▶ if (ref($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x) { } 113 76128 76241
307 113 return $x -> {accuracy};
308 } else {
309 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
310 76128 return ${"${class}::accuracy"};
311 }
312 }
313 }
314  
315 87310 ▶sub precision {
Subroutine precision: called CC=12 SCAR=25.2
Pod documented
316 87310 my $x = shift;
317 87310 ▶ my $class = ref($x) || $x || __PACKAGE__;
Condition60%3/5
ref $x || $x || __PACKAGE__
l!l
873100
ref $x or $x
l!l&&r!l&&!r
333869770
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
318  
319 # setter/mutator
320  
321 87310 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1285 86025 87310
322 1285 my $p = shift;
323  
324 1285 ▶ if (defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p) 230 1055 1285
325 230 ▶ $p = $p -> can('numify') ? $p -> numify() : 0 + "$p" if ref($p);
Branch: 2 branches
BranchTrueFalseTotal
if ref $p 0 230 230
$p->can('numify') ? : 0 0 0
326 230 ▶ croak "precision must be a number, not '$p'"
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /^\s*[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\s*\z/ 0 230 230
327 if $p !~ /^\s*[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\s*\z/;
328 230 ▶ croak "precision must be an integer, not '$p'"
Branch: 1 branch
BranchTrueFalseTotal
if $p != int $p 0 230 230
329 if $p != int $p;
330 }
331  
332 1285 ▶ if (ref($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x) { } 219 1066 1285
333 219 ▶ $x -> bfround($p) if defined $p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p 170 49 219
334 219 $x -> {accuracy} = undef; # clear instance A
335 219 $x -> {precision} = $p; # set instance P
336 } else {
337 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
338 1066 ${"${class}::accuracy"} = undef; # clear class A
339 1066 ${"${class}::precision"} = $p; # set class P
340 }
341 }
342  
343 # getter/accessor
344  
345 else {
346 86025 ▶ if (ref($x)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x) { } 114 85911 86025
347 114 return $x -> {precision};
348 } else {
349 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
350 85911 return ${"${class}::precision"};
351 }
352 }
353 }
354  
355 50622 ▶sub round_mode {
Subroutine round_mode: called CC=11 SCAR=25.6
Pod documented
356 50622 my $self = shift;
357 50622 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition60%3/5
ref $self || $self || __PACKAGE__
l!l
506220
ref $self or $self
l!l&&r!l&&!r
14506080
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
358  
359 # setter/mutator
360  
361 50622 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 382 50240 50622
362 382 my $m = shift;
363 382 ▶ croak("The value for 'round_mode' must be defined")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m 0 382 382
364 unless defined $m;
365 382 ▶ croak("Unknown round mode '$m'")
Branch: 1 branch
BranchTrueFalseTotal
unless $m =~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/ 4 378 382
366 unless $m =~ /^(even|odd|\+inf|\-inf|zero|trunc|common)$/;
367  
368 378 ▶ if (ref($self) && exists $self -> {round_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'round_mode'}) { } 0 378 378
Condition33%1/3
ref $self and exists $self->{"round_mode"}
!ll&&!rl&&r
37800
MC/DC0%0/2
ref $self and exists $self->{"round_mode"}
ref $self exists $self->{"round_mode"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"round_mode"}
ABresult
0X0
100
111
369 0 $self->{round_mode} = $m;
370 } else {
371 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
372 378 ${"${class}::round_mode"} = $m;
373 }
374 }
375  
376 # getter/accessor
377  
378 else {
379 50240 ▶ if (ref($self) && exists $self -> {round_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'round_mode'}) { } 0 50240 50240
Condition66%2/3
ref $self and exists $self->{"round_mode"}
!ll&&!rl&&r
50226140
MC/DC0%0/2
ref $self and exists $self->{"round_mode"}
ref $self exists $self->{"round_mode"}
Truth table66%2/3
A and B
A= ref $selfB= exists $self->{"round_mode"}
ABresult
0X0
100
111
380 0 return $self->{round_mode};
381 } else {
382 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
383 50240 my $m = ${"${class}::round_mode"};
384 50240 ▶ return defined($m) ? $m : $round_mode;
Branch: 1 branch
BranchTrueFalseTotal
defined $m ? : 50240 0 50240
385 }
386 }
387 }
388  
389 1237 ▶sub div_scale {
Subroutine div_scale: called CC=15 SCAR=30.7
Pod documented
390 1237 my $self = shift;
391 1237 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition60%3/5
ref $self || $self || __PACKAGE__
l!l
12370
ref $self or $self
l!l&&r!l&&!r
1412230
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
392  
393 # setter/mutator
394  
395 1237 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 31 1206 1237
396 31 my $f = shift;
397 31 ▶ croak("The value for 'div_scale' must be defined") unless defined $f;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 31 31
398 31 ▶ $f = $f -> can('numify') ? $f -> numify() : 0 + "$f" if ref($f);
Branch: 2 branches
BranchTrueFalseTotal
if ref $f 0 31 31
$f->can('numify') ? : 0 0 0
399 # also croak on non-numerical
400 31 ▶ croak "div_scale must be a number, not '$f'"
Branch: 1 branch
BranchTrueFalseTotal
unless $f =~ /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/ 0 31 31
401 unless $f =~/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/;
402 31 ▶ croak "div_scale must be an integer, not '$f'"
Branch: 1 branch
BranchTrueFalseTotal
if $f != int $f 0 31 31
403 if $f != int $f;
404 # It is not documented what div_scale <= 0 means, but Astro::Units sets
405 # div_scale to 0 and fails its tests if this is not supported. So we
406 # silently support div_scale = 0.
407 31 ▶ croak "div_scale must be positive, not '$f'" if $f < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $f < 0 0 31 31
408  
409 31 ▶ if (ref($self) && exists $self -> {div_scale}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'div_scale'}) { } 0 31 31
Condition33%1/3
ref $self and exists $self->{"div_scale"}
!ll&&!rl&&r
3100
MC/DC0%0/2
ref $self and exists $self->{"div_scale"}
ref $self exists $self->{"div_scale"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"div_scale"}
ABresult
0X0
100
111
410 0 $self -> {div_scale} = $f;
411 } else {
412 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
413 31 ${"${class}::div_scale"} = $f;
414 }
415 }
416  
417 # getter/accessor
418  
419 else {
420 1206 ▶ if (ref($self) && exists $self -> {div_scale}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'div_scale'}) { } 0 1206 1206
Condition66%2/3
ref $self and exists $self->{"div_scale"}
!ll&&!rl&&r
1192140
MC/DC0%0/2
ref $self and exists $self->{"div_scale"}
ref $self exists $self->{"div_scale"}
Truth table66%2/3
A and B
A= ref $selfB= exists $self->{"div_scale"}
ABresult
0X0
100
111
421 0 return $self -> {div_scale};
422 } else {
423 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
424 1206 my $f = ${"${class}::div_scale"};
425 1206 ▶ return defined($f) ? $f : $div_scale;
Branch: 1 branch
BranchTrueFalseTotal
defined $f ? : 1206 0 1206
426 }
427 }
428 }
429  
430 207 ▶sub trap_inf {
Subroutine trap_inf: called CC=9 SCAR=24.1
Pod documented
431 207 my $self = shift;
432 207 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition40%2/5
ref $self || $self || __PACKAGE__
l!l
2070
ref $self or $self
l!l&&r!l&&!r
02070
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
433  
434 # setter/mutator
435  
436 207 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 22 185 207
437 22 ▶ my $b = shift() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 13 9 22
438 22 ▶ if (ref($self) && exists $self -> {trap_inf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'trap_inf'}) { } 0 22 22
Condition33%1/3
ref $self and exists $self->{"trap_inf"}
!ll&&!rl&&r
2200
MC/DC0%0/2
ref $self and exists $self->{"trap_inf"}
ref $self exists $self->{"trap_inf"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"trap_inf"}
ABresult
0X0
100
111
439 0 $self -> {trap_inf} = $b;
440 } else {
441 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
442 22 ${"${class}::_trap_inf"} = $b;
443 }
444 }
445  
446 # getter/accessor
447  
448 else {
449 185 ▶ if (ref($self) && exists $self -> {trap_inf}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'trap_inf'}) { } 0 185 185
Condition33%1/3
ref $self and exists $self->{"trap_inf"}
!ll&&!rl&&r
18500
MC/DC0%0/2
ref $self and exists $self->{"trap_inf"}
ref $self exists $self->{"trap_inf"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"trap_inf"}
ABresult
0X0
100
111
450 0 return $self -> {trap_inf};
451 } else {
452 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
453 185 return ${"${class}::_trap_inf"};
454 }
455 }
456 }
457  
458 228 ▶sub trap_nan {
Subroutine trap_nan: called CC=9 SCAR=24.1
Pod documented
459 228 my $self = shift;
460 228 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition40%2/5
ref $self || $self || __PACKAGE__
l!l
2280
ref $self or $self
l!l&&r!l&&!r
02280
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
461  
462 # setter/mutator
463  
464 228 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 34 194 228
465 34 ▶ my $b = shift() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 19 15 34
466 34 ▶ if (ref($self) && exists $self -> {trap_nan}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'trap_nan'}) { } 0 34 34
Condition33%1/3
ref $self and exists $self->{"trap_nan"}
!ll&&!rl&&r
3400
MC/DC0%0/2
ref $self and exists $self->{"trap_nan"}
ref $self exists $self->{"trap_nan"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"trap_nan"}
ABresult
0X0
100
111
467 0 $self -> {trap_nan} = $b;
468 } else {
469 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
470 34 ${"${class}::_trap_nan"} = $b;
471 }
472 }
473  
474 # getter/accessor
475  
476 else {
477 194 ▶ if (ref($self) && exists $self -> {trap_nan}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'trap_nan'}) { } 0 194 194
Condition33%1/3
ref $self and exists $self->{"trap_nan"}
!ll&&!rl&&r
19400
MC/DC0%0/2
ref $self and exists $self->{"trap_nan"}
ref $self exists $self->{"trap_nan"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"trap_nan"}
ABresult
0X0
100
111
478 0 return $self -> {trap_nan};
479 } else {
480 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
481 194 return ${"${class}::_trap_nan"};
482 }
483 }
484 }
485  
486 80719 ▶sub upgrade {
Subroutine upgrade: called CC=8 SCAR=23.0
Pod documented
487 80719 my $self = shift;
488 80719 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition40%2/5
ref $self || $self || __PACKAGE__
l!l
807190
ref $self or $self
l!l&&r!l&&!r
0807190
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
489  
490 # setter/mutator
491  
492 80719 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 15126 65593 80719
493 15126 my $u = shift;
494 15126 ▶ if (ref($self) && exists $self -> {upgrade}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'upgrade'}) { } 0 15126 15126
Condition33%1/3
ref $self and exists $self->{"upgrade"}
!ll&&!rl&&r
1512600
MC/DC0%0/2
ref $self and exists $self->{"upgrade"}
ref $self exists $self->{"upgrade"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"upgrade"}
ABresult
0X0
100
111
495 0 $self -> {upgrade} = $u;
496 } else {
497 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
498 15126 ${"${class}::upgrade"} = $u;
499 }
500 }
501  
502 # getter/accessor
503  
504 else {
505 65593 ▶ if (ref($self) && exists $self -> {upgrade}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'upgrade'}) { } 0 65593 65593
Condition33%1/3
ref $self and exists $self->{"upgrade"}
!ll&&!rl&&r
6559300
MC/DC0%0/2
ref $self and exists $self->{"upgrade"}
ref $self exists $self->{"upgrade"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"upgrade"}
ABresult
0X0
100
111
506 0 return $self -> {upgrade};
507 } else {
508 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
509 65593 return ${"${class}::upgrade"};
510 }
511 }
512 }
513  
514 148273 ▶sub downgrade {
Subroutine downgrade: called CC=8 SCAR=23.0
Pod documented
515 148273 my $self = shift;
516 148273 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition40%2/5
ref $self || $self || __PACKAGE__
l!l
1482730
ref $self or $self
l!l&&r!l&&!r
01482730
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
517  
518 # setter/mutator
519  
520 148273 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 45877 102396 148273
521 45877 my $d = shift;
522 45877 ▶ if (ref($self) && exists $self -> {downgrade}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'downgrade'}) { } 0 45877 45877
Condition33%1/3
ref $self and exists $self->{"downgrade"}
!ll&&!rl&&r
4587700
MC/DC0%0/2
ref $self and exists $self->{"downgrade"}
ref $self exists $self->{"downgrade"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"downgrade"}
ABresult
0X0
100
111
523 0 $self -> {downgrade} = $d;
524 } else {
525 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
526 45877 ${"${class}::downgrade"} = $d;
527 }
528 }
529  
530 # getter/accessor
531  
532 else {
533 102396 ▶ if (ref($self) && exists $self -> {downgrade}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self and exists $self->{'downgrade'}) { } 0 102396 102396
Condition33%1/3
ref $self and exists $self->{"downgrade"}
!ll&&!rl&&r
10239600
MC/DC0%0/2
ref $self and exists $self->{"downgrade"}
ref $self exists $self->{"downgrade"}
Truth table33%1/3
A and B
A= ref $selfB= exists $self->{"downgrade"}
ABresult
0X0
100
111
534 0 return $self -> {downgrade};
535 } else {
536 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
537 102396 return ${"${class}::downgrade"};
538 }
539 }
540 }
541  
542 sub modify () {
543 # This method returns 0 if the object can be modified, or 1 if not. We use
544 # a fast constant sub() here, to avoid costly calls. Subclasses may
545 # override it with special code (f.i. Math::BigInt::Constant does so).
546  
547 0;
548 }
549  
550 920 ▶sub config {
Subroutine config: called CC=23 SCAR=31.4
Pod documented
551 # return (or set) configuration data.
552 920 my $self = shift;
553 920 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition60%3/5
ref $self || $self || __PACKAGE__
l!l
9200
ref $self or $self
l!l&&r!l&&!r
938270
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
554  
555 # setter/mutator
556 #
557 # $self -> config(param => value, ...)
558 # $self -> config({ param => value, ... })
559  
560 920 ▶ if (@_ > 1 || (@_ == 1 && (ref($_[0]) eq 'HASH'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1 or @_ == 1 and ref $_[0] eq "HASH") 166 754 920
Condition100%6/6
@_ > 1 or @_ == 1 and ref $_[0] eq "HASH"
l!l&&r!l&&!r
11551754
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
1074451
MC/DC100%3/3
@_ > 1 or @_ == 1 and ref $_[0] eq "HASH"
@_ > 1 @_ == 1 ref $_[0] eq "HASH"
Truth table100%4/4
A or B and C
A= @_ > 1B= @_ == 1C= ref $_[0] eq "HASH"
ABCresult
1XX1
0111
00X0
0100
561 # try to set given options as arguments from hash
562  
563 # If the argument is a hash ref, make a copy of it, since hash keys
564 # will be deleted below, and we don't want to modify the input hash.
565  
566 166 ▶ my $args = ref($_[0]) eq 'HASH' ? { %{ $_[0] } }: { @_ };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 51 115 166
567  
568 # We use this special handling of accuracy and precision because
569 # accuracy() always sets precision to undef and precision() always sets
570 # accuracy to undef. With out this special treatment, the following
571 # would result in both accuracy and precision being undef.
572 #
573 # $x -> config(accuracy => 3, precision => undef)
574  
575 croak "config(): both accuracy and precision are defined"
576 166 ▶ if defined($args -> {accuracy}) && defined ($args -> {precision});
Branch: 1 branch
BranchTrueFalseTotal
if defined $args->{"accuracy"} and defined $args->{"precision"} 0 166 166
Condition66%2/3
defined $args->{"accuracy"} and defined $args->{"precision"}
!ll&&!rl&&r
148180
MC/DC0%0/2
defined $args->{"accuracy"} and defined $args->{"precision"}
defined $args->{"accuracy"} defined $args->{"precision"}
Truth table66%2/3
A and B
A= defined $args->{"accuracy"}B= defined $args->{"precision"}
ABresult
0X0
100
111
577  
578 166 ▶ if (defined $args -> {accuracy}) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args->{'accuracy'}) { } 18 148 166
elsif (defined $args->{'precision'}) { } 24 124 148
579 18 $self -> accuracy($args -> {accuracy});
580 } elsif (defined $args -> {precision}) {
581 24 $self -> precision($args -> {precision});
582 } else {
583 124 $self -> accuracy(undef); # also sets precision to undef
584 }
585  
586 166 delete $args->{accuracy};
587 166 delete $args->{precision};
588  
589 # Set any remaining hash keys.
590  
591 166 foreach my $key (qw/
592 round_mode div_scale
593 upgrade downgrade
594 trap_inf trap_nan
595 /)
596 {
597 # use a method call to check argument
598 996 ▶ $self -> $key($args->{$key}) if exists $args->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args->{$key} 128 868 996
599 996 delete $args->{$key};
600 }
601  
602 # If there are any keys left, they are invalid.
603  
604 166 ▶ if (keys %$args) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$args) 6 160 166
605 6 croak("Illegal key(s) '", join("', '", keys %$args),
606 "' passed to ${class}->config()");
607 }
608 }
609  
610 # getter/accessor
611  
612 914 my $cfg = {};
613  
614 # When only a single parameter is wanted, it is not necessary to build the
615 # whole configuration first.
616  
617 914 ▶ if (@_ == 1 && (ref($_[0]) ne 'HASH')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] ne 'HASH') { } 744 170 914
Condition100%3/3
@_ == 1 and ref $_[0] ne "HASH"
!ll&&!rl&&r
11951744
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
618 744 my $param = shift;
619  
620 744 ▶ return $LIB if $param eq 'lib';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "lib" 290 454 744
621 454 ▶ return $LIB -> VERSION() if $param eq 'lib_version';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "lib_version" 3 451 454
622 451 ▶ return $class if $param eq 'class';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "class" 1 450 451
623 450 ▶ return $class -> VERSION() if $param eq 'version';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "version" 3 447 450
624  
625 # $x -> config("param") or $class -> config("param")
626  
627 447 return $self -> $param();
628 }
629  
630 else {
631  
632 170 ▶ if (ref($self)) { # $x -> config()
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 36 134 170
633  
634 # Currently, only 'accuracy' and 'precision' are supported, but
635 # more parameters will be added as the global variables are moved
636 # into the OO interface.
637  
638 36 my @param = ('accuracy', 'precision');
639  
640 36 for my $param (@param) {
641 72 $cfg -> {$param} = $self -> {$param};
642 }
643  
644 } else { # $class -> config()
645  
646 134 my @param = ('accuracy', 'precision', 'round_mode', 'div_scale',
647 'upgrade', 'downgrade', 'trap_inf', 'trap_nan');
648  
649 134 for my $param (@param) {
650 1072 $cfg -> {$param} = $self -> $param();
651 }
652  
653 # Additional read-only parameters.
654  
655 134 $cfg -> {lib} = $LIB;
656 134 $cfg -> {lib_version} = $LIB -> VERSION();
657 134 $cfg -> {class} = $class;
658 134 $cfg -> {version} = $class -> VERSION();
659 }
660  
661 170 return $cfg;
662 }
663 }
664  
665 77210 ▶sub _scale_a {
Subroutine _scale_a: called CC=6 SCAR=17.9
666 # select accuracy parameter based on precedence,
667 # used by bround() and bfround(), may return undef for scale (means no op)
668 77210 my ($x, $scale, $mode) = @_;
669  
670 77210 ▶ $scale = $x->{accuracy} unless defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 4574 72636 77210
671  
672 77210 my $class = ref($x);
673  
674 77210 ▶ $mode = $class -> round_mode() unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 13407 63803 77210
675  
676 77210 ▶ if (defined $scale) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scale) 72639 4571 77210
677 72639 ▶ $scale = $scale -> can('numify') ? $scale -> numify()
Branch: 2 branches
BranchTrueFalseTotal
if ref $scale 462 72177 72639
$scale->can('numify') ? : 462 0 462
678 : "$scale" if ref($scale);
679 72639 $scale = int($scale);
680 }
681  
682 77210 ($scale, $mode);
683 }
684  
685 975 ▶sub _scale_p {
Subroutine _scale_p: called CC=7 SCAR=19.7
686 # select precision parameter based on precedence,
687 # used by bround() and bfround(), may return undef for scale (means no op)
688 975 my ($x, $scale, $mode) = @_;
689  
690 975 ▶ $scale = $x->{precision} unless defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 4 971 975
691  
692 975 my $class = ref($x);
693  
694 975 ▶ $scale = $class -> precision() unless defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 4 971 975
695 975 ▶ $mode = $class -> round_mode() unless defined $mode;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mode 724 251 975
696  
697 975 ▶ if (defined $scale) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $scale) 971 4 975
698 971 ▶ $scale = $scale -> can('numify') ? $scale -> numify()
Branch: 2 branches
BranchTrueFalseTotal
if ref $scale 0 971 971
$scale->can('numify') ? : 0 0 0
699 : "$scale" if ref($scale);
700 971 $scale = int($scale);
701 }
702  
703 975 ($scale, $mode);
704 }
705  
706 # An undocumented method which downgrades an instance to its downgrade class.
707  
708 59936 ▶sub _dng {
Subroutine _dng: called CC=5 SCAR=16.1
709 59936 my $self = shift;
710 59936 my $class = ref($self);
711  
712 59936 my $downgrade = $class -> downgrade();
713 59936 ▶ return $self unless $downgrade; # bail out if no downgrading
Branch: 1 branch
BranchTrueFalseTotal
unless $downgrade 59795 141 59936
714 141 ▶ return $self if ref($self) eq $downgrade; # bail out if already downgraded
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq $downgrade 0 141 141
715  
716 # new() might perform upgrading or downgrading, so temporarily disable
717 # upgrading and downgrading in the downgrade class while calling new(). It
718 # should be possible to give new() extra arguments that disable
719 # downgrading. XXX
720  
721 141 my $upg = $downgrade -> upgrade();
722 141 my $dng = $downgrade -> downgrade();
723  
724 141 $downgrade -> upgrade(undef);
725 141 $downgrade -> downgrade(undef);
726  
727 141 my $tmp = $downgrade -> new($self); # new instance
728  
729 141 $downgrade -> upgrade($upg);
730 141 $downgrade -> downgrade($dng);
731  
732 141 for my $param ('accuracy', 'precision') { # copy instance variables
733 282 ▶ $tmp -> {$param} = $self -> {$param} if exists $self -> {$param};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$param} 92 190 282
734 }
735  
736 141 %$self = %$tmp; # replace
737 141 bless $self, $downgrade; # bless into downgrade class
738  
739 141 return $self;
740 }
741  
742 # An undocumented method which upgrades an instance to its upgrade class.
743  
744 74 ▶sub _upg {
Subroutine _upg: called CC=5 SCAR=16.1
745 74 my $self = shift;
746 74 my $class = ref($self);
747  
748 74 my $upgrade = $class -> upgrade();
749 74 ▶ return $self unless $upgrade; # bail out if no upgrading
Branch: 1 branch
BranchTrueFalseTotal
unless $upgrade 0 74 74
750 74 ▶ return $self if ref($self) eq $upgrade; # bail out if already upgraded
Branch: 1 branch
BranchTrueFalseTotal
if ref $self eq $upgrade 0 74 74
751  
752 # new() might perform upgrading or downgrading, so temporarily disable
753 # upgrading and downgrading in the upgrade class while calling new(). It
754 # should be possible to give new() extra arguments that disable
755 # upgrading. XXX
756  
757 74 my $upg = $upgrade -> upgrade();
758 74 my $dng = $upgrade -> downgrade();
759  
760 74 $upgrade -> upgrade(undef);
761 74 $upgrade -> downgrade(undef);
762  
763 74 my $tmp = $upgrade -> new($self); # new instance
764  
765 74 $upgrade -> upgrade($upg);
766 74 $upgrade -> downgrade($dng);
767  
768 74 for my $param ('accuracy', 'precision') { # copy instance variables
769 148 ▶ $tmp -> {$param} = $self -> {$param} if exists $self -> {$param};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$param} 4 144 148
770 }
771  
772 74 %$self = %$tmp; # replace
773 74 bless $self, $upgrade; # bless into upgrade class
774  
775 74 return $self;
776 }
777  
778 ###############################################################################
779 # Constructor methods
780 ###############################################################################
781  
782 9 ▶sub _init {
Subroutine _init: called CC=2 SCAR=7.0
783 9 my $self = shift;
784 9 my $class = ref($self);
785  
786 9 ▶ $self -> SUPER::_init() if SUPER -> can('_init');
Branch: 1 branch
BranchTrueFalseTotal
if "SUPER"->can("_init") 0 9 9
787  
788 9 $self -> {accuracy} = $class -> accuracy();
789 9 $self -> {precision} = $class -> precision();
790  
791 #$self -> {round_mode} = $round_mode;
792 #$self -> {div_scale} = $div_scale;
793  
794 #$self -> {trap_inf} = $_trap_inf;
795 #$self -> {trap_nan} = $_trap_nan;
796  
797 #$self -> {upgrade} = $upgrade;
798 #$self -> {downgrade} = $downgrade;
799  
800 9 return $self;
801 }
802  
803 22382 ▶sub new {
Subroutine new: called CC=34 SCAR=36.6
Pod documented
804 # Create a new Math::BigInt object from a string or another Math::BigInt,
805 # Math::BigFloat, or Math::BigRat object. See hash keys documented at top.
806  
807 22382 my $self = shift;
808 22382 my $selfref = ref $self;
809 22382 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
0223820
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
810  
811 # Make "require" work.
812  
813 22382 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 1 22381 22382
814  
815 # Calling new() with no input arguments has been discouraged for more than
816 # 10 years, but people apparently still use it, so we still support it.
817  
818 22382 ▶ return $class -> bzero() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 8 22374 22382
819  
820 22374 my ($wanted, @r) = @_;
821  
822 22374 ▶ if (!defined($wanted)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wanted) 0 22374 22374
823 #carp("Use of uninitialized value in new()")
824 # if warnings::enabled("uninitialized");
825 0 return $class -> bzero(@r);
826 }
827  
828 22374 ▶ if (!ref($wanted) && $wanted eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $wanted and $wanted eq "") 4 22370 22374
Condition100%3/3
not ref $wanted and $wanted eq ""
!ll&&!rl&&r
479218914
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
829 #carp(q|Argument "" isn't numeric in new()|)
830 # if warnings::enabled("numeric");
831 #return $class -> bzero(@r);
832 4 return $class -> bnan(@r);
833 }
834  
835 # Initialize a new object.
836  
837 22370 $self = bless {}, $class;
838 #$self -> _init(); # <-- this causes problems because if the global
839 # accuracy is 2, new(3, 5) will not set the accuracy
840 # to 5 because it is currently not possible to
841 # increase the accuracy. Ditto for precision. XXX
842  
843 # See if $wanted is an object that is a Math::BigInt. We could check if the
844 # object supports the as_int() method. However, as_int() truncates a finite
845 # non-integer whereas new() is supposed to return a NaN for finite
846 # non-integers. This inconsistency should be sorted out. XXX
847  
848 22370 ▶ if (defined(blessed($wanted)) && $wanted -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($wanted) and $wanted->isa(__PACKAGE__)) 1 22369 22370
Condition100%3/3
defined builtin::blessed($wanted) and $wanted->isa(__PACKAGE__)
!ll&&!rl&&r
218914781
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
849  
850 # Don't copy the accuracy and precision, because a new object should
851 # get them from the global configuration.
852  
853 1 $self -> {sign} = $wanted -> {sign};
854 1 $self -> {value} = $LIB -> _copy($wanted -> {value});
855 1 ▶ $self -> round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 1 0 1
Condition33%2/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
100
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
100
MC/DC0%0/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table25%1/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
856 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
857 1 return $self;
858 }
859  
860 # From now on we only work on the stringified version of $wanted, so
861 # stringify it once and for all.
862  
863 22369 $wanted = "$wanted";
864  
865 # Shortcut for non-zero scalar integers with no non-zero exponent.
866  
867 22369 ▶ if ($wanted =~
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ # optional leading whitespace \s* # optional sign ( [+-]? ) # integer mantissa with optional leading zeros 0* ( [1-9] \d* (?: _ \d+ )* | 0 ) # ... with optional zero fraction part (?: \.0* )? # optional non-negative exponent (?: [eE] \+? ( \d+ (?: _ \d+ )* ) )? # optional trailing whitespace \s* $ /x) 18009 4360 22369
868 / ^
869  
870 # optional leading whitespace
871 \s*
872  
873 # optional sign
874 ( [+-]? )
875  
876 # integer mantissa with optional leading zeros
877 0* ( [1-9] \d* (?: _ \d+ )* | 0 )
878  
879 # ... with optional zero fraction part
880 (?: \.0* )?
881  
882 # optional non-negative exponent
883 (?: [eE] \+? ( \d+ (?: _ \d+ )* ) )?
884  
885 # optional trailing whitespace
886 \s*
887  
888 $
889 /x)
890 {
891 18009 my $sign = $1;
892 18009 (my $mant = $2) =~ tr/_//d;
893 18009 my $expo = $3;
894 18009 ▶ $mant .= "0" x $expo if defined($expo) && $mant ne "0";
Branch: 1 branch
BranchTrueFalseTotal
if defined $expo and $mant ne 0 74 17935 18009
Condition100%3/3
defined $expo and $mant ne 0
!ll&&!rl&&r
178815474
MC/DC100%2/2
defined $expo and $mant ne 0
defined $expo $mant ne 0
Truth table100%3/3
A and B
A= defined $expoB= $mant ne 0
ABresult
0X0
100
111
895  
896 18009 ▶ $self->{sign} = $sign eq "-" && $mant ne "0" ? "-" : "+";
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' && $mant ne '0' ? : 2991 15018 18009
Condition100%3/3
$sign eq "-" and $mant ne 0
!ll&&!rl&&r
148831352991
MC/DC100%2/2
$sign eq "-" and $mant ne 0
$sign eq "-" $mant ne 0
Truth table100%3/3
A and B
A= $sign eq "-"B= $mant ne 0
ABresult
0X0
100
111
897 18009 $self->{value} = $LIB->_new($mant);
898 18009 $self -> round(@r);
899 18009 return $self;
900 }
901  
902 # Handle Infs.
903  
904 4360 ▶ if ($wanted =~ / ^
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ \s* ( [+-]? ) inf (?: inity )? \s* \z /xi) 1838 2522 4360
905 \s*
906 ( [+-]? )
907 inf (?: inity )?
908 \s*
909 \z
910 /ix)
911 {
912 1838 ▶ my $sgn = $1 || '+';
Condition100%2/2
$1 || '+'
l!l
1219619
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
913 1838 return $class -> binf($sgn, @r);
914 }
915  
916 # Handle explicit NaNs (not the ones returned due to invalid input).
917  
918 2522 ▶ if ($wanted =~ / ^
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ \s* ( [+-]? ) nan \s* \z /xi) 494 2028 2522
919 \s*
920 ( [+-]? )
921 nan
922 \s*
923 \z
924 /ix)
925 {
926 494 return $class -> bnan(@r);
927 }
928  
929 2028 my @parts;
930  
931 2028 ▶ 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)) 1385 643 2028
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
13850643
$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
1267118643
$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
1087180761
$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
10870941
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
93741087
$wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
94100
$wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
73823180
$wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
64300
MC/DC55%5/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 table15%6/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
932 # Handle hexadecimal numbers. We auto-detect hexadecimal numbers if
933 # they have a "0x", "0X", "x", or "X" prefix, cf. CORE::oct().
934  
935 $wanted =~ /^\s*[+-]?0?[Xx]/ and
936 @parts = $class -> _hex_str_to_flt_lib_parts($wanted)
937  
938 or
939  
940 # Handle octal numbers. We auto-detect octal numbers if they have a
941 # "0o", "0O", "o", "O" prefix, cf. CORE::oct().
942  
943 $wanted =~ /^\s*[+-]?0?[Oo]/ and
944 @parts = $class -> _oct_str_to_flt_lib_parts($wanted)
945  
946 or
947  
948 # Handle binary numbers. We auto-detect binary numbers if they have a
949 # "0b", "0B", "b", or "B" prefix, cf. CORE::oct().
950  
951 $wanted =~ /^\s*[+-]?0?[Bb]/ and
952 @parts = $class -> _bin_str_to_flt_lib_parts($wanted)
953  
954 or
955  
956 # At this point, what is left are decimal numbers that aren't handled
957 # above and octal floating point numbers that don't have any of the
958 # "0o", "0O", "o", or "O" prefixes. First see if it is a decimal
959 # number.
960  
961 @parts = $class -> _dec_str_to_flt_lib_parts($wanted)
962 or
963  
964 # See if it is an octal floating point number. The extra check is
965 # included because _oct_str_to_flt_lib_parts() accepts octal numbers
966 # that don't have a prefix (this is needed to make it work with, e.g.,
967 # from_oct() that don't require a prefix). However, Perl requires a
968 # prefix for octal floating point literals. For example, "1p+0" is not
969 # valid, but "01p+0" and "0__1p+0" are.
970  
971 $wanted =~ /^\s*[+-]?0_*\d/ and
972 @parts = $class -> _oct_str_to_flt_lib_parts($wanted))
973 {
974 # The value is an integer iff the exponent is non-negative.
975  
976 1385 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 1340 45 1385
977 1340 $self -> {sign} = $parts[0];
978 1340 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
979 1340 ▶ $self -> round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 1340 0 1340
Condition33%2/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
134000
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
134000
MC/DC0%0/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table25%1/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
980 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
981 1340 return $self;
982 }
983  
984 # The value is not an integer, so upgrade if upgrading is enabled.
985  
986 45 my $upg = $class -> upgrade();
987 45 ▶ return $upg -> new($wanted, @r) if $upg;
Branch: 1 branch
BranchTrueFalseTotal
if $upg 10 35 45
988 }
989  
990 # If we get here, the value is neither a valid decimal, binary, octal, or
991 # hexadecimal number. It is not explicit an Inf or a NaN either.
992  
993 678 return $class -> bnan(@r);
994 }
995  
996 # Create a Math::BigInt from a decimal string. This is an equivalent to
997 # from_hex(), from_oct(), and from_bin(). It is like new() except that it does
998 # not accept anything but a string representing a finite decimal number.
999  
1000 0 ▶sub from_dec {
Subroutine from_dec: not called CC=11 SCAR=48.8
Pod documented
1001 0 my $self = shift;
1002 0 my $selfref = ref $self;
1003 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
1004  
1005 # Make "require" work.
1006  
1007 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
1008  
1009 # Don't modify constant (read-only) objects.
1010  
1011 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
1012  
1013 0 my $str = shift;
1014 0 my @r = @_;
1015  
1016 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
1017  
1018 # If called as a class method, initialize a new object.
1019  
1020 0 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 0 0 0
1021 0 $self = bless {}, $class;
1022 #$self -> _init(); # see comment on _init() in new()
1023 }
1024  
1025 # The value is an integer iff the exponent is non-negative.
1026  
1027 0 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 0 0 0
1028 0 $self -> {sign} = $parts[0];
1029 0 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
1030 0 return $self -> round(@r);
1031 }
1032  
1033 # The value is not an integer, so upgrade if upgrading is enabled.
1034  
1035 0 my $upg = $class -> upgrade();
1036 0 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 0 0 0
1037 0 ▶ return $self -> _upg() -> from_dec($str, @r) # instance method
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $selfref ne $upg 0 0 0
Condition0%0/3
$selfref and $selfref ne $upg
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $selfref ne $upg
$selfref $selfref ne $upg
Truth table0%0/3
A and B
A= $selfrefB= $selfref ne $upg
ABresult
0X0
100
111
1038 if $selfref && $selfref ne $upg;
1039 0 return $upg -> from_dec($str, @r); # class method
1040 }
1041 }
1042  
1043 0 return $self -> bnan(@r);
1044 }
1045  
1046 # Create a Math::BigInt from a hexadecimal string.
1047  
1048 2 ▶sub from_hex {
Subroutine from_hex: called CC=11 SCAR=32.6
Pod documented
1049 2 my $self = shift;
1050 2 my $selfref = ref $self;
1051 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
1052  
1053 # Make "require" work.
1054  
1055 2 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 2 2
1056  
1057 # Don't modify constant (read-only) objects.
1058  
1059 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
1060  
1061 2 my $str = shift;
1062 2 my @r = @_;
1063  
1064 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
1065  
1066 # If called as a class method, initialize a new object.
1067  
1068 2 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 2 0 2
1069 2 $self = bless {}, $class;
1070 #$self -> _init(); # see comment on _init() in new()
1071 }
1072  
1073 # The value is an integer iff the exponent is non-negative.
1074  
1075 2 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 2 0 2
1076 2 $self -> {sign} = $parts[0];
1077 2 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
1078 2 return $self -> round(@r);
1079 }
1080  
1081 # The value is not an integer, so upgrade if upgrading is enabled.
1082  
1083 0 my $upg = $class -> upgrade();
1084 0 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 0 0 0
1085 0 ▶ return $self -> _upg() -> from_hex($str, @r) # instance method
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $selfref ne $upg 0 0 0
Condition0%0/3
$selfref and $selfref ne $upg
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $selfref ne $upg
$selfref $selfref ne $upg
Truth table0%0/3
A and B
A= $selfrefB= $selfref ne $upg
ABresult
0X0
100
111
1086 if $selfref && $selfref ne $upg;
1087 0 return $upg -> from_hex($str, @r); # class method
1088 }
1089 }
1090  
1091 0 return $self -> bnan(@r);
1092 }
1093  
1094 # Create a Math::BigInt from an octal string.
1095  
1096 2 ▶sub from_oct {
Subroutine from_oct: called CC=11 SCAR=32.6
Pod documented
1097 2 my $self = shift;
1098 2 my $selfref = ref $self;
1099 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
1100  
1101 # Make "require" work.
1102  
1103 2 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 2 2
1104  
1105 # Don't modify constant (read-only) objects.
1106  
1107 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
1108  
1109 2 my $str = shift;
1110 2 my @r = @_;
1111  
1112 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
1113  
1114 # If called as a class method, initialize a new object.
1115  
1116 2 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 2 0 2
1117 2 $self = bless {}, $class;
1118 #$self -> _init(); # see comment on _init() in new()
1119 }
1120  
1121 # The value is an integer iff the exponent is non-negative.
1122  
1123 2 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 2 0 2
1124 2 $self -> {sign} = $parts[0];
1125 2 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
1126 2 return $self -> round(@r);
1127 }
1128  
1129 # The value is not an integer, so upgrade if upgrading is enabled.
1130  
1131 0 my $upg = $class -> upgrade();
1132 0 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 0 0 0
1133 0 ▶ return $self -> _upg() -> from_oct($str, @r) # instance method
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $selfref ne $upg 0 0 0
Condition0%0/3
$selfref and $selfref ne $upg
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $selfref ne $upg
$selfref $selfref ne $upg
Truth table0%0/3
A and B
A= $selfrefB= $selfref ne $upg
ABresult
0X0
100
111
1134 if $selfref && $selfref ne $upg;
1135 0 return $upg -> from_oct($str, @r); # class method
1136 }
1137 }
1138  
1139 0 return $self -> bnan(@r);
1140 }
1141  
1142 # Create a Math::BigInt from a binary string.
1143  
1144 53 ▶sub from_bin {
Subroutine from_bin: called CC=11 SCAR=32.6
Pod documented
1145 53 my $self = shift;
1146 53 my $selfref = ref $self;
1147 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
1148  
1149 # Make "require" work.
1150  
1151 53 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 53 53
1152  
1153 # Don't modify constant (read-only) objects.
1154  
1155 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
1156  
1157 53 my $str = shift;
1158 53 my @r = @_;
1159  
1160 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
1161  
1162 # If called as a class method, initialize a new object.
1163  
1164 53 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 53 0 53
1165 53 $self = bless {}, $class;
1166 #$self -> _init(); # see comment on _init() in new()
1167 }
1168  
1169 # The value is an integer iff the exponent is non-negative.
1170  
1171 53 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq "+") 53 0 53
1172 53 $self -> {sign} = $parts[0];
1173 53 $self -> {value} = $LIB -> _lsft($parts[1], $parts[3], 10);
1174 53 return $self -> round(@r);
1175 }
1176  
1177 # The value is not an integer, so upgrade if upgrading is enabled.
1178  
1179 0 my $upg = $class -> upgrade();
1180 0 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 0 0 0
1181 0 ▶ return $self -> _upg() -> from_bin($str, @r) # instance method
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $selfref ne $upg 0 0 0
Condition0%0/3
$selfref and $selfref ne $upg
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $selfref ne $upg
$selfref $selfref ne $upg
Truth table0%0/3
A and B
A= $selfrefB= $selfref ne $upg
ABresult
0X0
100
111
1182 if $selfref && $selfref ne $upg;
1183 0 return $upg -> from_bin($str, @r); # class method
1184 }
1185 }
1186  
1187 0 return $self -> bnan(@r);
1188 }
1189  
1190 # Create a Math::BigInt from a byte string.
1191  
1192 0 ▶sub from_bytes {
Subroutine from_bytes: not called CC=7 SCAR=40.3
Pod documented
1193 0 my $self = shift;
1194 0 my $selfref = ref $self;
1195 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
1196  
1197 # Make "require" work.
1198  
1199 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
1200  
1201 # Don't modify constant (read-only) objects.
1202  
1203 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
1204  
1205 0 ▶ croak("from_bytes() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_from_bytes") 0 0 0
1206 unless $LIB -> can('_from_bytes');
1207  
1208 0 my $str = shift;
1209 0 my @r = @_;
1210  
1211 # If called as a class method, initialize a new object.
1212  
1213 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
1214 0 $self -> {sign} = '+';
1215 0 $self -> {value} = $LIB -> _from_bytes($str);
1216 0 return $self -> round(@r);
1217 }
1218  
1219 0 ▶sub from_ieee754 {
Subroutine from_ieee754: not called CC=8 SCAR=42.8
Pod documented
1220 0 my $self = shift;
1221 0 my $selfref = ref $self;
1222 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
1223  
1224 # Make "require" work.
1225  
1226 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
1227  
1228 # Don't modify constant (read-only) objects.
1229  
1230 0 ▶ return $self if $selfref && $self -> modify('from_ieee754');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_ieee754") 0 0 0
Condition0%0/3
$selfref and $self->modify("from_ieee754")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("from_ieee754")
$selfref $self->modify("from_ieee754")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("from_ieee754")
ABresult
0X0
100
111
1231  
1232 0 my $in = shift;
1233 0 my $format = shift;
1234 0 my @r = @_;
1235  
1236 0 require Math::BigFloat;
1237 0 my $tmp = Math::BigFloat -> from_ieee754($in, $format, @r);
1238 0 ▶ return $self -> bnan(@r) unless $tmp -> is_inf() || $tmp -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp->is_inf or $tmp->is_int 0 0 0
Condition0%0/3
$tmp->is_inf or $tmp->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$tmp->is_inf or $tmp->is_int
$tmp->is_inf $tmp->is_int
Truth table0%0/3
A or B
A= $tmp->is_infB= $tmp->is_int
ABresult
1X1
011
000
1239 0 $tmp = $tmp -> as_int();
1240  
1241 # If called as a class method, initialize a new object.
1242  
1243 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
1244 0 $self -> {sign} = $tmp -> {sign};
1245 0 $self -> {value} = $tmp -> {value};
1246  
1247 0 return $self;
1248 }
1249  
1250 0 ▶sub from_fp80 {
Subroutine from_fp80: not called CC=8 SCAR=42.8
Pod documented
1251 0 my $self = shift;
1252 0 my $selfref = ref $self;
1253 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
1254  
1255 # Make "require" work.
1256  
1257 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
1258  
1259 # Don't modify constant (read-only) objects.
1260  
1261 0 ▶ return $self if $selfref && $self -> modify('from_fp80');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_fp80") 0 0 0
Condition0%0/3
$selfref and $self->modify("from_fp80")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("from_fp80")
$selfref $self->modify("from_fp80")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("from_fp80")
ABresult
0X0
100
111
1262  
1263 0 my $in = shift;
1264 0 my @r = @_;
1265  
1266 0 require Math::BigFloat;
1267 0 my $tmp = Math::BigFloat -> from_fp80($in, @r);
1268 0 ▶ return $self -> bnan(@r) unless $tmp -> is_inf() || $tmp -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $tmp->is_inf or $tmp->is_int 0 0 0
Condition0%0/3
$tmp->is_inf or $tmp->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$tmp->is_inf or $tmp->is_int
$tmp->is_inf $tmp->is_int
Truth table0%0/3
A or B
A= $tmp->is_infB= $tmp->is_int
ABresult
1X1
011
000
1269 0 $tmp = $tmp -> as_int();
1270  
1271 # If called as a class method, initialize a new object.
1272  
1273 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
1274 0 $self -> {sign} = $tmp -> {sign};
1275 0 $self -> {value} = $tmp -> {value};
1276  
1277 0 return $self;
1278 }
1279  
1280 0 ▶sub from_base {
Subroutine from_base: not called CC=16 SCAR=56.1
Pod documented
1281 0 my $self = shift;
1282 0 my $selfref = ref $self;
1283 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
1284  
1285 # Make "require" work.
1286  
1287 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
1288  
1289 # Don't modify constant (read-only) objects.
1290  
1291 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
1292  
1293 0 my ($str, $base, $cs, @r) = @_; # $cs is the collation sequence
1294  
1295 0 ▶ $base = $class -> new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
1296  
1297 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
1298 if $base < 2 || ! $base -> is_int();
1299  
1300 # If called as a class method, initialize a new object.
1301  
1302 0 ▶ $self = $class -> bzero() unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
1303  
1304 # If no collating sequence is given, pass some of the conversions to
1305 # methods optimized for those cases.
1306  
1307 0 ▶ unless (defined $cs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cs) 0 0 0
1308 0 ▶ return $self -> from_bin($str, @r) if $base == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 2 0 0 0
1309 0 ▶ return $self -> from_oct($str, @r) if $base == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 8 0 0 0
1310 0 ▶ return $self -> from_hex($str, @r) if $base == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 16 0 0 0
1311 0 ▶ return $self -> from_dec($str, @r) if $base == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 10 0 0 0
1312 }
1313  
1314 0 ▶ croak("from_base() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_from_base") 0 0 0
1315 unless $LIB -> can('_from_base');
1316  
1317 0 $self -> {sign} = '+';
1318 $self -> {value} = $LIB->_from_base($str, $base -> {value},
1319 0 ▶ defined($cs) ? $cs : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $cs ? : 0 0 0
1320 0 return $self -> bround(@r);
1321 }
1322  
1323 0 ▶sub from_base_num {
Subroutine from_base_num: not called CC=14 SCAR=53.5
Pod documented
1324 0 my $self = shift;
1325 0 my $selfref = ref $self;
1326 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
1327  
1328 # Make "require" work.
1329  
1330 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
1331  
1332 # Don't modify constant (read-only) objects.
1333  
1334 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
1335  
1336 # Make sure we have an array of non-negative, finite, numerical objects.
1337  
1338 0 my $nums = shift;
1339 0 $nums = [ @$nums ]; # create new reference
1340  
1341 0 for my $i (0 .. $#$nums) {
1342 # Make sure we have an object.
1343 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
1344 unless defined(blessed($nums -> [$i]))
1345 && $nums -> [$i] -> isa(__PACKAGE__);
1346 # Make sure we have a finite, non-negative integer.
1347 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
1348 if $nums -> [$i] -> is_neg() || ! $nums -> [$i] -> is_int();
1349 }
1350  
1351 0 my $base = shift;
1352 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
1353 unless defined(blessed($base)) && $base -> isa(__PACKAGE__);
1354  
1355 0 my @r = @_;
1356  
1357 # If called as a class method, initialize a new object.
1358  
1359 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
1360  
1361 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
1362 unless $LIB -> can('_from_base_num');
1363  
1364 0 $self -> {sign} = '+';
1365 0 $self -> {value} = $LIB -> _from_base_num([ map { $_ -> {value} } @$nums ],
1366 0 $base -> {value});
1367  
1368 0 return $self -> round(@r);
1369 }
1370  
1371 1921 ▶sub bzero {
Subroutine bzero: called CC=15 SCAR=29.2
Pod documented
1372 # create/assign '+0'
1373  
1374 # Class::method(...) -> Class->method(...)
1375 1921 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 1921 1921
Condition55%5/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
001921
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
44314780
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
14780443
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
1376 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1377 {
1378 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1379 # " use is as a method instead";
1380 0 unshift @_, __PACKAGE__;
1381 }
1382  
1383 1921 my $self = shift;
1384 1921 my $selfref = ref $self;
1385 1921 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
44314780
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1386  
1387 # Make "require" work.
1388  
1389 1921 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 1921 1921
1390  
1391 # Don't modify constant (read-only) objects.
1392  
1393 1921 ▶ return $self if $selfref && $self -> modify('bzero');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bzero") 0 1921 1921
Condition66%2/3
$selfref and $self->modify("bzero")
!ll&&!rl&&r
14784430
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
1394  
1395 # Get the rounding parameters, if any.
1396  
1397 1921 my @r = @_;
1398  
1399 # If called as a class method, initialize a new object.
1400  
1401 1921 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1478 443 1921
1402 1478 $self = bless {}, $class;
1403 #$self -> _init(); # see comment on _init() in new()
1404 }
1405  
1406 1921 $self->{sign} = '+';
1407 1921 $self->{value} = $LIB->_zero();
1408  
1409 # If rounding parameters are given as arguments, use them. If no rounding
1410 # parameters are given, and if called as a class method, initialize the new
1411 # instance with the class variables.
1412  
1413 1921 ▶ if (@r) {
Branch: 2 branches
BranchTrueFalseTotal
if (@r) { } 18 1903 1921
elsif (not $selfref) { } 1472 431 1903
1414 18 ▶ if (@r >= 2 && defined($r[0]) && defined($r[1])) {
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
1530
@r >= 2 and defined $r[0]
!ll&&!rl&&r
3123
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
1415 0 carp "can't specify both accuracy and precision";
1416 0 return $self -> bnan();
1417 }
1418 18 $self->{accuracy} = $_[0];
1419 18 $self->{precision} = $_[1];
1420 } elsif (!$selfref) {
1421 1472 $self->{accuracy} = $class -> accuracy();
1422 1472 $self->{precision} = $class -> precision();
1423 }
1424  
1425 1921 return $self;
1426 }
1427  
1428 499 ▶sub bone {
Subroutine bone: called CC=17 SCAR=29.9
Pod documented
1429 # Create or assign '+1' (or -1 if given sign '-').
1430  
1431 # Class::method(...) -> Class->method(...)
1432 499 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 499 499
Condition55%5/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
00499
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
2122870
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
2870212
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
1433 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1434 {
1435 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1436 # " use is as a method instead";
1437 0 unshift @_, __PACKAGE__;
1438 }
1439  
1440 499 my $self = shift;
1441 499 my $selfref = ref $self;
1442 499 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
2122870
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1443  
1444 # Make "require" work.
1445  
1446 499 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 499 499
1447  
1448 # Don't modify constant (read-only) objects.
1449  
1450 499 ▶ return $self if $selfref && $self -> modify('bone');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bone") 0 499 499
Condition66%2/3
$selfref and $self->modify("bone")
!ll&&!rl&&r
2872120
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
1451  
1452 499 my ($sign, @r) = @_;
1453  
1454 # Get the sign.
1455  
1456 499 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/) { } 104 395 499
Condition100%3/3
defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/
!ll&&!rl&&r
3905104
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
1457 104 $sign = $1;
1458 104 shift;
1459 } else {
1460 395 $sign = '+';
1461 }
1462  
1463 # If called as a class method, initialize a new object.
1464  
1465 499 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 287 212 499
1466 287 $self = bless {}, $class;
1467 #$self -> _init(); # see comment on _init() in new()
1468 }
1469  
1470 499 $self->{sign} = $sign;
1471 499 $self->{value} = $LIB->_one();
1472  
1473 # If rounding parameters are given as arguments, use them. If no rounding
1474 # parameters are given, and if called as a class method, initialize the new
1475 # instance with the class variables.
1476  
1477 499 ▶ if (@r) {
Branch: 2 branches
BranchTrueFalseTotal
if (@r) { } 18 481 499
elsif (not $selfref) { } 281 200 481
1478 18 ▶ if (@r >= 2 && defined($r[0]) && defined($r[1])) {
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
1479 0 carp "can't specify both accuracy and precision";
1480 0 return $self -> bnan();
1481 }
1482 18 $self->{accuracy} = $_[0];
1483 18 $self->{precision} = $_[1];
1484 } elsif (!$selfref) {
1485 281 $self->{accuracy} = $class -> accuracy();
1486 281 $self->{precision} = $class -> precision();
1487 }
1488  
1489 499 return $self;
1490 }
1491  
1492 2353 ▶sub binf {
Subroutine binf: called CC=18 SCAR=31.2
Pod documented
1493 # create/assign a '+inf' or '-inf'
1494  
1495 # Class::method(...) -> Class->method(...)
1496 2353 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 2353 2353
Condition55%5/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
002353
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
43819150
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
19150438
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
1497 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1498 {
1499 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1500 # " use is as a method instead";
1501 0 unshift @_, __PACKAGE__;
1502 }
1503  
1504 2353 my $self = shift;
1505 2353 my $selfref = ref $self;
1506 2353 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
43819150
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1507  
1508 {
1509 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1510 2353 ▶ if (${"${class}::_trap_inf"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_inf";}) 5 2348 2353
1511 5 croak("Tried to create +-inf in $class->binf()");
1512 }
1513 }
1514  
1515 # Make "require" work.
1516  
1517 2348 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 2348 2348
1518  
1519 # Don't modify constant (read-only) objects.
1520  
1521 2348 ▶ return $self if $selfref && $self -> modify('binf');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("binf") 0 2348 2348
Condition66%2/3
$selfref and $self->modify("binf")
!ll&&!rl&&r
19134350
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
1522  
1523 # Get the sign.
1524  
1525 2348 my $sign = '+'; # default is to return positive infinity
1526 2348 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])(inf|$)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i) 2289 59 2348
Condition66%2/3
defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i
!ll&&!rl&&r
5902289
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
1527 2289 $sign = $1;
1528 2289 shift;
1529 }
1530  
1531 # Get the rounding parameters, if any.
1532  
1533 2348 my @r = @_;
1534  
1535 # If called as a class method, initialize a new object.
1536  
1537 2348 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1913 435 2348
1538 1913 $self = bless {}, $class;
1539 #$self -> _init(); # see comment on _init() in new()
1540 }
1541  
1542 2348 $self -> {sign} = $sign . 'inf';
1543 2348 $self -> {value} = $LIB -> _zero();
1544  
1545 # If rounding parameters are given as arguments, use them. If no rounding
1546 # parameters are given, and if called as a class method, initialize the new
1547 # instance with the class variables.
1548  
1549 2348 ▶ if (@r) {
Branch: 2 branches
BranchTrueFalseTotal
if (@r) { } 143 2205 2348
elsif (not $selfref) { } 1897 308 2205
1550 143 ▶ if (@r >= 2 && defined($r[0]) && defined($r[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r >= 2 and defined $r[0] and defined $r[1]) 0 143 143
Condition33%2/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
14300
@r >= 2 and defined $r[0]
!ll&&!rl&&r
01430
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
1551 0 carp "can't specify both accuracy and precision";
1552 0 return $self -> bnan();
1553 }
1554 143 $self->{accuracy} = $_[0];
1555 143 $self->{precision} = $_[1];
1556 } elsif (!$selfref) {
1557 1897 $self->{accuracy} = $class -> accuracy();
1558 1897 $self->{precision} = $class -> precision();
1559 }
1560  
1561 2348 return $self;
1562 }
1563  
1564 2454 ▶sub bnan {
Subroutine bnan: called CC=16 SCAR=29.9
Pod documented
1565 # create/assign a 'NaN'
1566  
1567 # Class::method(...) -> Class->method(...)
1568 2454 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 2454 2454
Condition55%5/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
002454
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
110213520
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
135201102
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
1569 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1570 {
1571 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1572 # " use is as a method instead";
1573 0 unshift @_, __PACKAGE__;
1574 }
1575  
1576 2454 my $self = shift;
1577 2454 my $selfref = ref($self);
1578 2454 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
110213520
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1579  
1580 {
1581 50 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1582 2454 ▶ if (${"${class}::_trap_nan"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_nan";}) 5 2449 2454
1583 5 croak("Tried to create NaN in $class->bnan()");
1584 }
1585 }
1586  
1587 # Make "require" work.
1588  
1589 2449 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 2449 2449
1590  
1591 # Don't modify constant (read-only) objects.
1592  
1593 2449 ▶ return $self if $selfref && $self -> modify('bnan');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bnan") 0 2449 2449
Condition66%2/3
$selfref and $self->modify("bnan")
!ll&&!rl&&r
135010990
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
1594  
1595 # Get the rounding parameters, if any.
1596  
1597 2449 my @r = @_;
1598  
1599 # If called as a class method, initialize a new object.
1600  
1601 2449 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1350 1099 2449
1602 1350 $self = bless {}, $class;
1603 #$self -> _init(); # see comment on _init() in new()
1604 }
1605  
1606 2449 $self -> {sign} = $nan;
1607 2449 $self -> {value} = $LIB -> _zero();
1608  
1609 # If rounding parameters are given as arguments, use them. If no rounding
1610 # parameters are given, and if called as a class method, initialize the new
1611 # instance with the class variables.
1612  
1613 2449 ▶ if (@r) {
Branch: 2 branches
BranchTrueFalseTotal
if (@r) { } 227 2222 2449
elsif (not $selfref) { } 1348 874 2222
1614 227 ▶ if (@r >= 2 && defined($r[0]) && defined($r[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r >= 2 and defined $r[0] and defined $r[1]) 0 227 227
Condition50%3/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
22700
@r >= 2 and defined $r[0]
!ll&&!rl&&r
22250
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
1615 0 carp "can't specify both accuracy and precision";
1616 0 return $self -> bnan();
1617 }
1618 227 $self->{accuracy} = $_[0];
1619 227 $self->{precision} = $_[1];
1620 } elsif (!$selfref) {
1621 1348 $self->{accuracy} = $class -> accuracy();
1622 1348 $self->{precision} = $class -> precision();
1623 }
1624  
1625 2449 return $self;
1626 }
1627  
1628 9 ▶sub bpi {
Subroutine bpi: called CC=12 SCAR=34.4
Pod documented
1629  
1630 # Class::method(...) -> Class->method(...)
1631 9 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
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
1632 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1633 {
1634 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1635 # " use is as a method instead";
1636 0 unshift @_, __PACKAGE__;
1637 }
1638  
1639 # Called as Argument list
1640 # --------- -------------
1641 # Math::BigFloat->bpi() ("Math::BigFloat")
1642 # Math::BigFloat->bpi(10) ("Math::BigFloat", 10)
1643 # $x->bpi() ($x)
1644 # $x->bpi(10) ($x, 10)
1645 # Math::BigFloat::bpi() ()
1646 # Math::BigFloat::bpi(10) (10)
1647 #
1648 # In ambiguous cases, we favour the OO-style, so the following case
1649 #
1650 # $n = Math::BigFloat->new("10");
1651 # $x = Math::BigFloat->bpi($n);
1652 #
1653 # which gives an argument list with the single element $n, is resolved as
1654 #
1655 # $n->bpi();
1656  
1657 9 my $self = shift;
1658 9 my $selfref = ref $self;
1659 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
1660 9 my @r = @_; # rounding paramters
1661  
1662 # Make "require" work.
1663  
1664 9 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 9 9
1665  
1666 9 ▶ if ($selfref) { # bpi() called as an instance method
Branch: 1 branch
BranchTrueFalseTotal
if ($selfref) { } 0 9 9
1667 0 ▶ return $self if $self -> modify('bpi');
Branch: 1 branch
BranchTrueFalseTotal
if $self->modify("bpi") 0 0 0
1668 } else { # bpi() called as a class method
1669 9 $self = bless {}, $class; # initialize new instance
1670 #$self -> _init(); # see comment on _init() in new()
1671 }
1672  
1673 9 my $upg = $class -> upgrade();
1674 9 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 0 9 9
1675 0 ▶ return $self -> _upg() -> bpi(@r) # instance method
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $selfref ne $upg 0 0 0
Condition0%0/3
$selfref and $selfref ne $upg
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $selfref ne $upg
$selfref $selfref ne $upg
Truth table0%0/3
A and B
A= $selfrefB= $selfref ne $upg
ABresult
0X0
100
111
1676 if $selfref && $selfref ne $upg;
1677 0 return $upg -> bpi(@r); # class method
1678 }
1679  
1680 # hard-wired to "3"
1681 9 $self -> {sign} = '+';
1682 9 $self -> {value} = $LIB -> _new("3");
1683 9 $self -> round(@r);
1684 9 return $self;
1685 }
1686  
1687 4223 ▶sub copy {
Subroutine copy: called CC=5 SCAR=16.4
Pod documented
1688 4223 my ($x, $class);
1689 4223 ▶ if (ref($_[0])) { # $y = $x -> copy()
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 4223 0 4223
1690 4223 $x = shift;
1691 4223 $class = ref($x);
1692 } else { # $y = Math::BigInt -> copy($x)
1693 0 $class = shift;
1694 0 $x = shift;
1695 }
1696  
1697 4223 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 4223 4223
1698  
1699 4223 my $copy = bless {}, $class;
1700  
1701 4223 $copy->{sign} = $x->{sign};
1702 4223 $copy->{value} = $LIB->_copy($x->{value});
1703 4223 ▶ $copy->{accuracy} = $x->{accuracy} if exists $x->{accuracy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{"accuracy"} 300 3923 4223
1704 4223 ▶ $copy->{precision} = $x->{precision} if exists $x->{precision};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{"precision"} 279 3944 4223
1705  
1706 4223 return $copy;
1707 }
1708  
1709 5 ▶sub as_int {
Subroutine as_int: called CC=5 SCAR=19.6
Pod documented
1710 5 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 0 5
1711  
1712 # Temporarily disable upgrading and downgrading.
1713  
1714 5 my $upg = Math::BigInt -> upgrade();
1715 5 my $dng = Math::BigInt -> downgrade();
1716 5 Math::BigInt -> upgrade(undef);
1717 5 Math::BigInt -> downgrade(undef);
1718  
1719 5 my $y;
1720 5 ▶ if ($x -> isa("Math::BigInt")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigInt')) { } 5 0 5
1721 5 $y = $x -> copy();
1722 } else {
1723 0 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 0 0 0
elsif ($x->is_nan) { } 0 0 0
1724 0 $y = Math::BigInt -> binf($x -> sign());
1725 } elsif ($x -> is_nan()) {
1726 0 $y = Math::BigInt -> bnan();
1727 } else {
1728 0 $y = Math::BigInt -> new($x -> copy() -> bint() -> bdstr());
1729 }
1730  
1731 # Copy the remaining instance variables.
1732  
1733 0 ($y->{accuracy}, $y->{precision}) = ($x->{accuracy}, $x->{precision});
1734 }
1735  
1736 5 $y -> round(@r);
1737  
1738 # Restore upgrading and downgrading.
1739  
1740 5 Math::BigInt -> upgrade($upg);
1741 5 Math::BigInt -> downgrade($dng);
1742  
1743 5 return $y;
1744 }
1745  
1746 889 ▶sub as_rat {
Subroutine as_rat: called CC=5 SCAR=17.0
Pod documented
1747 889 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 889 0 889
1748  
1749 # Temporarily disable upgrading and downgrading.
1750  
1751 889 require Math::BigRat;
1752 889 my $upg = Math::BigRat -> upgrade();
1753 889 my $dng = Math::BigRat -> downgrade();
1754 889 Math::BigRat -> upgrade(undef);
1755 889 Math::BigRat -> downgrade(undef);
1756  
1757 889 my $y;
1758 889 ▶ if ($x -> isa("Math::BigRat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigRat')) { } 0 889 889
1759 0 $y = $x -> copy();
1760 } else {
1761  
1762 889 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 0 889 889
elsif ($x->is_nan) { } 0 889 889
1763 0 $y = Math::BigRat -> binf($x -> sign());
1764 } elsif ($x -> is_nan()) {
1765 0 $y = Math::BigRat -> bnan();
1766 } else {
1767 889 $y = Math::BigRat -> new($x -> bfstr());
1768 }
1769  
1770 # Copy the remaining instance variables.
1771  
1772 889 ($y->{accuracy}, $y->{precision}) = ($x->{accuracy}, $x->{precision});
1773 }
1774  
1775 889 $y -> round(@r);
1776  
1777 # Restore upgrading and downgrading.
1778  
1779 889 Math::BigRat -> upgrade($upg);
1780 889 Math::BigRat -> downgrade($dng);
1781  
1782 889 return $y;
1783 }
1784  
1785 470 ▶sub as_float {
Subroutine as_float: called CC=7 SCAR=22.3
Pod documented
1786 470 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 470 0 470
1787  
1788 # Temporarily disable upgrading and downgrading.
1789  
1790 470 require Math::BigFloat;
1791 470 my $upg = Math::BigFloat -> upgrade();
1792 470 my $dng = Math::BigFloat -> downgrade();
1793 470 Math::BigFloat -> upgrade(undef);
1794 470 Math::BigFloat -> downgrade(undef);
1795  
1796 470 my $y;
1797 470 ▶ if ($x -> isa("Math::BigFloat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigFloat')) { } 0 470 470
1798 0 $y = $x -> copy();
1799 } else {
1800 470 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 4 466 470
elsif ($x->is_nan) { } 0 466 466
1801 4 $y = Math::BigFloat -> binf($x -> sign());
1802 } elsif ($x -> is_nan()) {
1803 0 $y = Math::BigFloat -> bnan();
1804 } else {
1805 466 ▶ if ($x -> isa("Math::BigRat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigRat')) { } 0 466 466
1806 0 ▶ if ($x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_int) { } 0 0 0
1807 0 $y = Math::BigFloat -> new($x -> bdstr());
1808 } else {
1809 0 my ($num, $den) = $x -> fparts();
1810 0 my $str = $num -> as_float() -> bdiv($den, @r) -> bdstr();
1811 0 $y = Math::BigFloat -> new($str);
1812 }
1813 } else {
1814 466 $y = Math::BigFloat -> new($x -> bdstr());
1815 }
1816 }
1817  
1818 # Copy the remaining instance variables.
1819  
1820 470 ($y->{accuracy}, $y->{precision}) = ($x->{accuracy}, $x->{precision});
1821 }
1822  
1823 470 $y -> round(@r);
1824  
1825 # Restore upgrading and downgrading..
1826  
1827 470 Math::BigFloat -> upgrade($upg);
1828 470 Math::BigFloat -> downgrade($dng);
1829  
1830 470 return $y;
1831 }
1832  
1833 ###############################################################################
1834 # Boolean methods
1835 ###############################################################################
1836  
1837 36061 ▶sub is_zero {
Subroutine is_zero: called CC=4 SCAR=13.9
Pod documented
1838 # return true if arg (BINT or num_str) is zero (array '+', '0')
1839 36061 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36057 4 36061
1840  
1841 36061 ▶ return 0 if $x->{sign} ne '+';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne "+" 3522 32539 36061
1842 32539 ▶ return 1 if $LIB->_is_zero($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 502 32037 32539
1843 32037 return 0;
1844 }
1845  
1846 2426 ▶sub is_one {
Subroutine is_one: called CC=7 SCAR=19.5
Pod documented
1847 # return true if arg (BINT or num_str) is +1, or -1 if sign is given
1848 2426 ▶ my (undef, $x, $sign) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2422 4 2426
1849  
1850 2426 ▶ if (defined($sign)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sign) { } 1832 594 2426
1851 1832 ▶ croak 'is_one(): sign argument must be "+" or "-"'
Branch: 1 branch
BranchTrueFalseTotal
unless $sign eq "+" or $sign eq "-" 0 1832 1832
Condition66%2/3
$sign eq "+" or $sign eq "-"
l!l&&r!l&&!r
8979350
MC/DC0%0/2
$sign eq "+" or $sign eq "-"
$sign eq "+" $sign eq "-"
Truth table66%2/3
A or B
A= $sign eq "+"B= $sign eq "-"
ABresult
1X1
011
000
1852 unless $sign eq '+' || $sign eq '-';
1853 } else {
1854 594 $sign = '+';
1855 }
1856  
1857 2426 ▶ return 0 if $x->{sign} ne $sign;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $sign 957 1469 2426
1858 1469 ▶ $LIB->_is_one($x->{value}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$LIB->_is_one($x->{'value'}) ? : 186 1283 1469
1859 }
1860  
1861 262819 ▶sub is_finite {
Subroutine is_finite: called CC=4 SCAR=13.9
Pod documented
1862 262819 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 262819 0 262819
1863 262819 ▶ $x->{sign} eq '+' || $x->{sign} eq '-' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '+' || $x->{'sign'} eq '-' ? : 256743 6076 262819
Condition100%3/3
$x->{"sign"} eq "+" or $x->{"sign"} eq "-"
l!l&&r!l&&!r
234174225696076
MC/DC100%2/2
$x->{"sign"} eq "+" or $x->{"sign"} eq "-"
$x->{"sign"} eq "+" $x->{"sign"} eq "-"
Truth table100%3/3
A or B
A= $x->{"sign"} eq "+"B= $x->{"sign"} eq "-"
ABresult
1X1
011
000
1864 }
1865  
1866 121302 ▶sub is_inf {
Subroutine is_inf: called CC=7 SCAR=19.5
Pod documented
1867 # return true if arg (BINT or num_str) is +-inf
1868 121302 ▶ my (undef, $x, $sign) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 121294 8 121302
1869  
1870 121302 ▶ if (defined $sign) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sign) 44340 76962 121302
1871 44340 ▶ $sign = '[+-]inf' if $sign eq ''; # +- doesn't matter, only that's inf
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "" 64 44276 44340
1872 44340 ▶ $sign = "[$1]inf" if $sign =~ /^([+-])(inf)?$/; # extract '+' or '-'
Branch: 1 branch
BranchTrueFalseTotal
if $sign =~ /^([+-])(inf)?$/ 44276 64 44340
1873 44340 ▶ return $x->{sign} =~ /^$sign$/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^$sign$/ ? : 3110 41230 44340
1874 }
1875 76962 ▶ $x->{sign} =~ /^[+-]inf$/ ? 1 : 0; # only +-inf is infinity
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^[+-]inf$/ ? : 3946 73016 76962
1876 }
1877  
1878 152654 ▶sub is_nan {
Subroutine is_nan: called CC=3 SCAR=11.0
Pod documented
1879 # return true if arg (BINT or num_str) is NaN
1880 152654 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 152646 8 152654
1881  
1882 152654 ▶ $x->{sign} eq $nan ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $nan ? : 3708 148946 152654
1883 }
1884  
1885 828 ▶sub is_positive {
Subroutine is_positive: called CC=5 SCAR=16.1
Pod documented
1886 # return true when arg (BINT or num_str) is positive (> 0)
1887 828 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 820 8 828
1888  
1889 828 ▶ return 1 if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 60 768 828
1890  
1891 # 0+ is neither positive nor negative
1892 768 ▶ ($x->{sign} eq '+' && !$x -> is_zero()) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '+' && !$x->is_zero ? : 620 148 768
Condition100%3/3
$x->{"sign"} eq "+" and not $x->is_zero
!ll&&!rl&&r
12523620
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
1893 }
1894  
1895 4449 ▶sub is_negative {
Subroutine is_negative: called CC=3 SCAR=11.0
Pod documented
1896 # return true when arg (BINT or num_str) is negative (< 0)
1897 4449 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4441 8 4449
1898  
1899 4449 ▶ $x->{sign} =~ /^-/ ? 1 : 0; # -inf is negative, but NaN is not
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^-/ ? : 1163 3286 4449
1900 }
1901  
1902 64 ▶sub is_non_positive {
Subroutine is_non_positive: called CC=4 SCAR=13.9
Pod documented
1903 # Return true if argument is non-positive (<= 0).
1904 64 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 56 8 64
1905  
1906 64 ▶ return 1 if $x->{sign} =~ /^\-/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^\-/ 24 40 64
1907 40 ▶ return 1 if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 32 40
1908 32 return 0;
1909 }
1910  
1911 64 ▶sub is_non_negative {
Subroutine is_non_negative: called CC=4 SCAR=13.9
Pod documented
1912 # Return true if argument is non-negative (>= 0).
1913 64 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 56 8 64
1914  
1915 64 ▶ return 1 if $x->{sign} =~ /^\+/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^\+/ 32 32 64
1916 32 ▶ return 1 if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 32 32
1917 32 return 0;
1918 }
1919  
1920 185 ▶sub is_odd {
Subroutine is_odd: called CC=4 SCAR=13.9
Pod documented
1921 # return true when arg (BINT or num_str) is odd, false for even
1922 185 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 185 0 185
1923  
1924 185 ▶ return 0 unless $x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 7 178 185
1925 178 ▶ $LIB->_is_odd($x->{value}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$LIB->_is_odd($x->{'value'}) ? : 93 85 178
1926 }
1927  
1928 44 ▶sub is_even {
Subroutine is_even: called CC=4 SCAR=13.9
Pod documented
1929 # return true when arg (BINT or num_str) is even, false for odd
1930 44 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 44 0 44
1931  
1932 44 ▶ return 0 unless $x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 4 40 44
1933 40 ▶ $LIB->_is_even($x->{value}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$LIB->_is_even($x->{'value'}) ? : 16 24 40
1934 }
1935  
1936 862 ▶sub is_int {
Subroutine is_int: called CC=3 SCAR=11.1
Pod documented
1937 # return true when arg (BINT or num_str) is an integer
1938 862 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 862 0 862
1939  
1940 862 ▶ $x -> is_finite() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->is_finite ? : 767 95 862
1941 }
1942  
1943 ###############################################################################
1944 # Comparison methods
1945 ###############################################################################
1946  
1947 2501 ▶sub bcmp {
Subroutine bcmp: called CC=21 SCAR=30.7
Pod documented
1948 # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
1949 # (BINT or num_str, BINT or num_str) return cond_code
1950  
1951 # set up parameters
1952 2501 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 2051 450 2501
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
04502051
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
1953 ? (ref($_[0]), @_)
1954 : objectify(2, @_);
1955  
1956 2501 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 2501 2501
1957  
1958 ###########################################################################
1959 # Code for all classes that share the common interface.
1960 ###########################################################################
1961  
1962 # Unless both $x and $y are finite ...
1963  
1964 2501 ▶ unless ($x -> is_finite() && $y -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 333 2168 2501
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
293402168
MC/DC100%2/2
$x->is_finite and $y->is_finite
$x->is_finite $y->is_finite
Truth table100%3/3
A and B
A= $x->is_finiteB= $y->is_finite
ABresult
0X0
100
111
1965 # handle +-inf and NaN
1966 333 ▶ return if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 64 269 333
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
3529269
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
1967 269 ▶ 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$/ 43 226 269
Condition66%2/3
$x->{"sign"} eq $y->{"sign"} and $x->{"sign"} =~ /^[+-]inf$/
!ll&&!rl&&r
226043
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
1968 226 ▶ return +1 if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 96 130 226
1969 130 ▶ return -1 if $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("-") 111 19 130
1970 19 ▶ return -1 if $y -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_inf("+") 8 11 19
1971 11 return +1;
1972 }
1973  
1974 # check sign for speed first
1975 2168 ▶ 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 1906 2168
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "-"
!ll&&!rl&&r
2991607262
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
1976 1906 ▶ 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 "+" 206 1700 1906
Condition100%3/3
$x->{"sign"} eq "-" and $y->{"sign"} eq "+"
!ll&&!rl&&r
160793206
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
1977  
1978 ###########################################################################
1979 # Code for things that aren't Math::BigInt
1980 ###########################################################################
1981  
1982 # If called with "foreign" arguments.
1983  
1984 1700 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 1 1699 1700
1985 1 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 1 1
1986 0 $y = $y -> as_int();
1987 } else {
1988 1 ▶ return $x -> _upg() -> bcmp($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 1 0 1
1989 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
1990 }
1991 }
1992  
1993 ###########################################################################
1994 # Code for Math::BigInt objects
1995 ###########################################################################
1996  
1997 # post-normalized compare for internal use (honors signs)
1998 1699 ▶ if ($x->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} eq "+") 1606 93 1699
1999 # $x and $y both > 0
2000 1606 return $LIB->_acmp($x->{value}, $y->{value});
2001 }
2002  
2003 # $x && $y both < 0; use swapped acmp (lib returns 0, 1, -1)
2004 93 $LIB->_acmp($y->{value}, $x->{value});
2005 }
2006  
2007 116 ▶sub bacmp {
Subroutine bacmp: called CC=15 SCAR=30.6
Pod documented
2008 # Compares 2 values, ignoring their signs.
2009 # Returns one of undef, <0, =0, >0. (suitable for sort)
2010 # (BINT, BINT) return cond_code
2011  
2012 # set up parameters
2013 116 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 116 0 116
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00116
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
2014 ? (ref($_[0]), @_)
2015 : objectify(2, @_);
2016  
2017 116 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 116 116
2018  
2019 ###########################################################################
2020 # Code for all classes that share the common interface.
2021 ###########################################################################
2022  
2023 116 ▶ if ((!$x -> is_finite()) || (!$y -> is_finite())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 72 44 116
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
561644
MC/DC100%2/2
$x->is_finite and $y->is_finite
$x->is_finite $y->is_finite
Truth table100%3/3
A and B
A= $x->is_finiteB= $y->is_finite
ABresult
0X0
100
111
2024 # handle +-inf and NaN
2025 72 ▶ return if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 28 44 72
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
161244
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
2026 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
2027 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
2028 12 return -1;
2029 }
2030  
2031 ###########################################################################
2032 # Code for things that aren't Math::BigInt
2033 ###########################################################################
2034  
2035 # If called with "foreign" arguments.
2036  
2037 44 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 0 44 44
2038 0 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 0 0
2039 0 $y = $y -> as_int();
2040 } else {
2041 0 ▶ return $x -> _upg() -> bacmp($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
2042 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
2043 }
2044 }
2045  
2046 ###########################################################################
2047 # Code for Math::BigInt objects
2048 ###########################################################################
2049  
2050 44 $LIB->_acmp($x->{value}, $y->{value}); # lib does only 0, 1, -1
2051 }
2052  
2053 529 ▶sub beq {
Subroutine beq: called CC=5 SCAR=16.2
Pod documented
2054 529 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 200 329 529
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0329200
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
2055 ? (undef, @_)
2056 : objectify(2, @_);
2057  
2058 529 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 529 529
2059  
2060 529 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
2061 529 ▶ return defined($cmp) && !$cmp;
Condition100%3/3
defined $cmp && !$cmp
!ll&&!rl&&r
18324187
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
2062 }
2063  
2064 18 ▶sub bne {
Subroutine bne: called CC=6 SCAR=22.0
Pod documented
2065 18 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
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
2066 ? (undef, @_)
2067 : objectify(2, @_);
2068  
2069 18 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 18 18
2070  
2071 18 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
2072 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
2073 }
2074  
2075 1053 ▶sub blt {
Subroutine blt: called CC=5 SCAR=16.2
Pod documented
2076 1053 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 9 1044 1053
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
010449
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
2077 ? (undef, @_)
2078 : objectify(2, @_);
2079  
2080 1053 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 1053 1053
2081  
2082 1053 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
2083 1053 ▶ return defined($cmp) && $cmp < 0;
Condition100%3/3
defined $cmp && $cmp < 0
!ll&&!rl&&r
4939110
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
2084 }
2085  
2086 1463 ▶sub ble {
Subroutine ble: called CC=5 SCAR=16.2
Pod documented
2087 1463 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1195 268 1463
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
02681195
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
2088 ? (undef, @_)
2089 : objectify(2, @_);
2090  
2091 1463 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 1463 1463
2092  
2093 1463 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
2094 1463 ▶ return defined($cmp) && $cmp <= 0;
Condition100%3/3
defined $cmp && $cmp <= 0
!ll&&!rl&&r
131193257
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
2095 }
2096  
2097 1465 ▶sub bgt {
Subroutine bgt: called CC=5 SCAR=16.6
Pod documented
2098 1465 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1072 393 1465
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
03931072
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
2099 ? (undef, @_)
2100 : objectify(2, @_);
2101  
2102 1465 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 1465 1465
2103  
2104 1465 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
2105 1465 ▶ return defined($cmp) && $cmp > 0;
Condition66%2/3
defined $cmp && $cmp > 0
!ll&&!rl&&r
0607858
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
2106 }
2107  
2108 356 ▶sub bge {
Subroutine bge: called CC=5 SCAR=16.2
Pod documented
2109 356 ▶ my (undef, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 169 187 356
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0187169
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
2110 ? (undef, @_)
2111 : objectify(2, @_);
2112  
2113 356 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 356 356
2114  
2115 356 my $cmp = $x -> bcmp($y); # bcmp() upgrades if necessary
2116 356 ▶ return defined($cmp) && $cmp >= 0;
Condition100%3/3
defined $cmp && $cmp >= 0
!ll&&!rl&&r
1383260
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
2117 }
2118  
2119 ###############################################################################
2120 # Arithmetic methods
2121 ###############################################################################
2122  
2123 490 ▶sub bneg {
Subroutine bneg: called CC=7 SCAR=20.0
Pod documented
2124 # negate number or make a negated number from string
2125 490 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 490 0 490
2126  
2127 ###########################################################################
2128 # Code for all classes that share the common interface.
2129 ###########################################################################
2130  
2131 # Don't modify constant (read-only) objects.
2132  
2133 490 ▶ return $x if $x -> modify('bneg');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bneg") 0 490 490
2134  
2135 490 ▶ $x->{sign} =~ tr/+-/-+/ unless $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_zero 482 8 490
2136 490 $x -> round(@r);
2137 490 ▶ $x -> _dng() if ($x -> is_int() || $x -> is_inf() || $x -> is_nan());
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 490 0 490
Condition83%5/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
478120
$x->is_int or $x->is_inf
l!l&&r!l&&!r
4572112
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table75%3/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
2138 490 return $x;
2139 }
2140  
2141 891 ▶sub babs {
Subroutine babs: called CC=6 SCAR=17.9
Pod documented
2142 # (BINT or num_str) return BINT
2143 # make number absolute, or return absolute BINT from string
2144 891 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 879 12 891
2145  
2146 # Don't modify constant (read-only) objects.
2147  
2148 891 ▶ return $x if $x -> modify('babs');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("babs") 0 891 891
2149  
2150 891 $x->{sign} =~ s/^-/+/;
2151  
2152 891 $x -> round(@r);
2153 891 ▶ $x -> _dng() if ($x -> is_int() || $x -> is_inf() || $x -> is_nan());
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 327 564 891
Condition100%6/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
31512564
$x->is_int or $x->is_inf
l!l&&r!l&&!r
29124576
MC/DC100%3/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table100%4/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
2154 891 return $x;
2155 }
2156  
2157 22 ▶sub bsgn {
Subroutine bsgn: called CC=6 SCAR=17.9
Pod documented
2158 # Signum function.
2159 22 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 18 4 22
2160  
2161 # Don't modify constant (read-only) objects.
2162  
2163 22 ▶ return $x if $x -> modify('bsgn');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsgn") 0 22 22
2164  
2165 # bone() downgrades, if necessary
2166  
2167 22 ▶ return $x -> bone("+", @r) if $x -> is_pos();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_pos 10 12 22
2168 12 ▶ return $x -> bone("-", @r) if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 6 6 12
2169  
2170 6 $x -> round(@r);
2171 6 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 3 3 6
2172 6 return $x;
2173 }
2174  
2175 800 ▶sub bnorm {
Subroutine bnorm: called CC=3 SCAR=11.1
Pod documented
2176 # (numstr or BINT) return BINT
2177 # Normalize number -- no-op here
2178 800 ▶ my ($class, $x, @r) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 8 792 800
2179  
2180 # This method is called from the rounding methods, so if this method
2181 # supports rounding by calling the rounding methods, we get an infinite
2182 # recursion.
2183  
2184 800 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 800 800
2185  
2186 800 $x;
2187 }
2188  
2189 190 ▶sub binc {
Subroutine binc: called CC=8 SCAR=21.0
Pod documented
2190 # increment arg by one
2191 190 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 190 0 190
2192  
2193 ###########################################################################
2194 # Code for all classes that share the common interface.
2195 ###########################################################################
2196  
2197 # Don't modify constant (read-only) objects.
2198  
2199 190 ▶ return $x if $x -> modify('binc');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("binc") 0 190 190
2200  
2201 190 ▶ return $x -> round(@r) if $x -> is_inf() || $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf or $x->is_nan 16 174 190
Condition100%3/3
$x->is_inf or $x->is_nan
l!l&&r!l&&!r
88174
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
2202  
2203 ###########################################################################
2204 # Code for Math::BigInt objects
2205 ###########################################################################
2206  
2207 174 ▶ if ($x->{sign} eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '+') { } 100 74 174
elsif ($x->{'sign'} eq '-') { } 74 0 74
2208 100 $x->{value} = $LIB->_inc($x->{value});
2209 } elsif ($x->{sign} eq '-') {
2210 74 $x->{value} = $LIB->_dec($x->{value});
2211 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
2212 }
2213  
2214 174 return $x -> round(@r);
2215 }
2216  
2217 31 ▶sub bdec {
Subroutine bdec: called CC=8 SCAR=20.9
Pod documented
2218 # decrement arg by one
2219 31 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 31 0 31
2220  
2221 ###########################################################################
2222 # Code for all classes that share the common interface.
2223 ###########################################################################
2224  
2225 # Don't modify constant (read-only) objects.
2226  
2227 31 ▶ return $x if $x -> modify('bdec');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdec") 0 31 31
2228  
2229 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
2230  
2231 ###########################################################################
2232 # Code for Math::BigInt objects
2233 ###########################################################################
2234  
2235 19 ▶ if ($x->{sign} eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '-') { } 4 15 19
elsif ($x->{'sign'} eq '+') { } 15 0 15
2236 4 $x->{value} = $LIB->_inc($x->{value});
2237 } elsif ($x->{sign} eq '+') {
2238 15 ▶ if ($LIB->_is_zero($x->{value})) { # +1 - 1 => +0
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{'value'})) { } 4 11 15
2239 4 $x->{value} = $LIB->_one();
2240 4 $x->{sign} = '-';
2241 } else {
2242 11 $x->{value} = $LIB->_dec($x->{value});
2243 }
2244 }
2245  
2246 19 return $x -> round(@r);
2247 }
2248  
2249 #sub bstrcmp {
2250 # my $self = shift;
2251 # my $selfref = ref $self;
2252 # my $class = $selfref || $self;
2253 #
2254 # croak 'bstrcmp() is an instance method, not a class method'
2255 # unless $selfref;
2256 # croak 'Wrong number of arguments for bstrcmp()' unless @_ == 1;
2257 #
2258 # return $self -> bstr() CORE::cmp shift;
2259 #}
2260 #
2261 #sub bstreq {
2262 # my $self = shift;
2263 # my $selfref = ref $self;
2264 # my $class = $selfref || $self;
2265 #
2266 # croak 'bstreq() is an instance method, not a class method'
2267 # unless $selfref;
2268 # croak 'Wrong number of arguments for bstreq()' unless @_ == 1;
2269 #
2270 # my $cmp = $self -> bstrcmp(shift);
2271 # return defined($cmp) && ! $cmp;
2272 #}
2273 #
2274 #sub bstrne {
2275 # my $self = shift;
2276 # my $selfref = ref $self;
2277 # my $class = $selfref || $self;
2278 #
2279 # croak 'bstrne() is an instance method, not a class method'
2280 # unless $selfref;
2281 # croak 'Wrong number of arguments for bstrne()' unless @_ == 1;
2282 #
2283 # my $cmp = $self -> bstrcmp(shift);
2284 # return defined($cmp) && ! $cmp ? '' : 1;
2285 #}
2286 #
2287 #sub bstrlt {
2288 # my $self = shift;
2289 # my $selfref = ref $self;
2290 # my $class = $selfref || $self;
2291 #
2292 # croak 'bstrlt() is an instance method, not a class method'
2293 # unless $selfref;
2294 # croak 'Wrong number of arguments for bstrlt()' unless @_ == 1;
2295 #
2296 # my $cmp = $self -> bstrcmp(shift);
2297 # return defined($cmp) && $cmp < 0;
2298 #}
2299 #
2300 #sub bstrle {
2301 # my $self = shift;
2302 # my $selfref = ref $self;
2303 # my $class = $selfref || $self;
2304 #
2305 # croak 'bstrle() is an instance method, not a class method'
2306 # unless $selfref;
2307 # croak 'Wrong number of arguments for bstrle()' unless @_ == 1;
2308 #
2309 # my $cmp = $self -> bstrcmp(shift);
2310 # return defined($cmp) && $cmp <= 0;
2311 #}
2312 #
2313 #sub bstrgt {
2314 # my $self = shift;
2315 # my $selfref = ref $self;
2316 # my $class = $selfref || $self;
2317 #
2318 # croak 'bstrgt() is an instance method, not a class method'
2319 # unless $selfref;
2320 # croak 'Wrong number of arguments for bstrgt()' unless @_ == 1;
2321 #
2322 # my $cmp = $self -> bstrcmp(shift);
2323 # return defined($cmp) && $cmp > 0;
2324 #}
2325 #
2326 #sub bstrge {
2327 # my $self = shift;
2328 # my $selfref = ref $self;
2329 # my $class = $selfref || $self;
2330 #
2331 # croak 'bstrge() is an instance method, not a class method'
2332 # unless $selfref;
2333 # croak 'Wrong number of arguments for bstrge()' unless @_ == 1;
2334 #
2335 # my $cmp = $self -> bstrcmp(shift);
2336 # return defined($cmp) && $cmp >= 0;
2337 #}
2338  
2339 735 ▶sub badd {
Subroutine badd: called CC=17 SCAR=28.6
Pod documented
2340 # add second arg (BINT or string) to first (BINT) (modifies first)
2341 # return result as BINT
2342  
2343 # set up parameters
2344 735 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 646 89 735
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
1574646
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
2345 ? (ref($_[0]), @_)
2346 : objectify(2, @_);
2347  
2348 735 ▶ return $x -> badd($y, @r) unless $x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $x->isa(__PACKAGE__) 0 735 735
2349  
2350 ###########################################################################
2351 # Code for all classes that share the common interface.
2352 ###########################################################################
2353  
2354 # Don't modify constant (read-only) objects.
2355  
2356 735 ▶ return $x if $x -> modify('badd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("badd") 0 735 735
2357  
2358 735 $r[3] = $y; # no push!
2359  
2360 735 ▶ unless ($x -> is_finite() && $y -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 125 610 735
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
9134610
MC/DC100%2/2
$x->is_finite and $y->is_finite
$x->is_finite $y->is_finite
Truth table100%3/3
A and B
A= $x->is_finiteB= $y->is_finite
ABresult
0X0
100
111
2361  
2362 125 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 55 70 125
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
312470
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
2363  
2364 70 ▶ return $x -> is_inf("+") ? ($y -> is_inf("-") ? $x -> bnan(@r)
Branch: 5 branches
BranchTrueFalseTotal
$x->is_inf('+') ? : 24 46 70
$y->is_inf('-') ? : 6 18 24
$x->is_inf('-') ? : 24 22 46
$y->is_inf('+') ? : 6 18 24
$y->is_inf('+') ? : 11 11 22
2365 : $x -> binf("+", @r))
2366 : $x -> is_inf("-") ? ($y -> is_inf("+") ? $x -> bnan(@r)
2367 : $x -> binf("-", @r))
2368 : ($y -> is_inf("+") ? $x -> binf("+", @r)
2369 : $x -> binf("-", @r));
2370 }
2371  
2372 ###########################################################################
2373 # Code for things that aren't Math::BigInt
2374 ###########################################################################
2375  
2376 # If called with "foreign" arguments.
2377  
2378 610 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 2 608 610
2379 2 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 2 2
2380 0 $y = $y -> as_int();
2381 } else {
2382 2 ▶ return $x -> _upg() -> badd($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 2 0 2
2383 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
2384 }
2385 }
2386  
2387 ###########################################################################
2388 # Code for Math::BigInt objects
2389 ###########################################################################
2390  
2391 ($x->{value}, $x->{sign})
2392 608 = $LIB -> _sadd($x->{value}, $x->{sign}, $y->{value}, $y->{sign});
2393  
2394 608 $x -> round(@r);
2395 }
2396  
2397 1077 ▶sub bsub {
Subroutine bsub: called CC=18 SCAR=30.5
Pod documented
2398 # (BINT or num_str, BINT or num_str) return BINT
2399 # subtract second arg from first, modify first
2400  
2401 # set up parameters
2402 1077 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1050 27 1077
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
12151050
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
2403 ? (ref($_[0]), @_)
2404 : objectify(2, @_);
2405  
2406 1077 ▶ return $x -> bsub($y, @r) unless $x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $x->isa(__PACKAGE__) 0 1077 1077
2407  
2408 ###########################################################################
2409 # Code for all classes that share the common interface.
2410 ###########################################################################
2411  
2412 # Don't modify constant (read-only) objects.
2413  
2414 1077 ▶ return $x if $x -> modify('bsub');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsub") 0 1077 1077
2415  
2416 1077 $r[3] = $y; # no push!
2417  
2418 1077 ▶ unless ($x -> is_finite() && $y -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 112 965 1077
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
7537965
MC/DC100%2/2
$x->is_finite and $y->is_finite
$x->is_finite $y->is_finite
Truth table100%3/3
A and B
A= $x->is_finiteB= $y->is_finite
ABresult
0X0
100
111
2419  
2420 112 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 39 73 112
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
231673
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
2421  
2422 73 ▶ return $x -> is_inf("+") ? ($y -> is_inf("+") ? $x -> bnan(@r)
Branch: 5 branches
BranchTrueFalseTotal
$x->is_inf('+') ? : 24 49 73
$y->is_inf('+') ? : 9 15 24
$x->is_inf('-') ? : 24 25 49
$y->is_inf('-') ? : 9 15 24
$y->is_inf('+') ? : 14 11 25
2423 : $x -> binf("+", @r))
2424 : $x -> is_inf("-") ? ($y -> is_inf("-") ? $x -> bnan(@r)
2425 : $x -> binf("-", @r))
2426 : ($y -> is_inf("+") ? $x -> binf("-", @r)
2427 : $x -> binf("+", @r));
2428 }
2429  
2430 965 ▶ return $x -> bzero(@r) if refaddr($x) eq refaddr($y); # $x -> bsub($x)
Branch: 1 branch
BranchTrueFalseTotal
if builtin::refaddr($x) eq builtin::refaddr($y) 6 959 965
2431  
2432 ###########################################################################
2433 # Code for things that aren't Math::BigInt
2434 ###########################################################################
2435  
2436 # If called with "foreign" arguments.
2437  
2438 959 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 0 959 959
2439 0 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 0 0
2440 0 $y = $y -> as_int();
2441 } else {
2442 0 ▶ return $x -> _upg() -> bsub($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
2443 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
2444 }
2445 }
2446  
2447 ###########################################################################
2448 # Code for Math::BigInt objects
2449 ###########################################################################
2450  
2451 ($x->{value}, $x->{sign})
2452 959 = $LIB -> _ssub($x->{value}, $x->{sign}, $y->{value}, $y->{sign});
2453  
2454 959 $x -> round(@r);
2455 }
2456  
2457 1464 ▶sub bmul {
Subroutine bmul: called CC=20 SCAR=30.1
Pod documented
2458 # multiply the first number by the second number
2459 # (BINT or num_str, BINT or num_str) return BINT
2460  
2461 # set up parameters
2462 1464 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1435 29 1464
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
12171435
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
2463 ? (ref($_[0]), @_)
2464 : objectify(2, @_);
2465  
2466 ###########################################################################
2467 # Code for all classes that share the common interface.
2468 ###########################################################################
2469  
2470 # Don't modify constant (read-only) objects.
2471  
2472 1464 ▶ return $x if $x -> modify('bmul');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmul") 0 1464 1464
2473  
2474 1464 ▶ return $x -> bmul($y, @r) unless $x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $x->isa(__PACKAGE__) 0 1464 1464
2475  
2476 1464 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 52 1412 1464
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
28241412
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
2477  
2478 # inf handling
2479 1412 ▶ if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/) 52 1360 1412
Condition100%3/3
$x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/
l!l&&r!l&&!r
36161360
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
2480 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
2481 # result will always be +-inf:
2482 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
2483 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
2484 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
2485 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
2486 20 return $x -> binf('-', @r);
2487 }
2488  
2489 ###########################################################################
2490 # Code for things that aren't Math::BigInt
2491 ###########################################################################
2492  
2493 # If called with "foreign" arguments.
2494  
2495 1360 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 1 1359 1360
2496 1 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 1 1
2497 0 $y = $y -> as_int();
2498 } else {
2499 1 ▶ return $x -> _upg() -> bmul($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 1 0 1
2500 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
2501 }
2502 }
2503  
2504 ###########################################################################
2505 # Code for Math::BigInt objects
2506 ###########################################################################
2507  
2508 1359 $r[3] = $y; # no push here
2509  
2510 1359 ▶ $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 1309 50 1359
2511  
2512 1359 $x->{value} = $LIB->_mul($x->{value}, $y->{value}); # do actual math
2513 1359 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{value}); # no -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 312 1047 1359
2514  
2515 1359 $x -> round(@r);
2516 }
2517  
2518 50 *bdiv = \&bfdiv;
2519 50 *bmod = \&bfmod;
2520  
2521 1468 ▶sub bfdiv {
Subroutine bfdiv: called CC=37 SCAR=36.2
Pod documented
2522 # This does floored division, where the quotient is floored, i.e., rounded
2523 # towards negative infinity. As a consequence, the remainder has the same
2524 # sign as the divisor.
2525 #
2526 # ($q, $r) = $x -> btdiv($y) returns $q and $r so that $q is floor($x / $y)
2527 # and $q * $y + $r = $x.
2528  
2529 # Set up parameters.
2530 1468 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1446 22 1468
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
6161446
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
2531 ? (ref($_[0]), @_)
2532 : objectify(2, @_);
2533  
2534 ###########################################################################
2535 # Code for all classes that share the common interface.
2536 ###########################################################################
2537  
2538 # Don't modify constant (read-only) objects.
2539  
2540 1468 ▶ return $x if $x -> modify('bfdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfdiv") 0 1468 1468
2541  
2542 1468 my $wantarray = wantarray; # call only once
2543  
2544 # At least one argument is NaN. Return NaN for both quotient and the
2545 # modulo/remainder.
2546  
2547 1468 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 51 1417 1468
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
26251417
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
2548 51 ▶ return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 24 27 51
2549 : $x -> bnan(@r);
2550 }
2551  
2552 # Divide by zero and modulo zero.
2553 #
2554 # Division: Use the common convention that x / 0 is inf with the same sign
2555 # as x, except when x = 0, where we return NaN. This is also what earlier
2556 # versions did.
2557 #
2558 # Modulo: In modular arithmetic, the congruence relation z = x (mod y)
2559 # means that there is some integer k such that z - x = k y. If y = 0, we
2560 # get z - x = 0 or z = x. This is also what earlier versions did, except
2561 # that 0 % 0 returned NaN.
2562 #
2563 # inf / 0 = inf inf % 0 = inf
2564 # 5 / 0 = inf 5 % 0 = 5
2565 # 0 / 0 = NaN 0 % 0 = 0
2566 # -5 / 0 = -inf -5 % 0 = -5
2567 # -inf / 0 = -inf -inf % 0 = -inf
2568  
2569 1417 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 68 1349 1417
2570 68 my $rem;
2571 68 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 32 36 68
2572 32 $rem = $x -> copy() -> round(@r);
2573 }
2574 68 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 18 50 68
2575 18 $x -> bnan(@r);
2576 } else {
2577 50 $x -> binf($x -> {sign}, @r);
2578 }
2579 64 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 32 32 64
2580 }
2581  
2582 # Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
2583 # The divide by zero cases are covered above. In all of the cases listed
2584 # below we return the same as core Perl.
2585 #
2586 # inf / -inf = NaN inf % -inf = NaN
2587 # inf / -5 = -inf inf % -5 = NaN
2588 # inf / 5 = inf inf % 5 = NaN
2589 # inf / inf = NaN inf % inf = NaN
2590 #
2591 # -inf / -inf = NaN -inf % -inf = NaN
2592 # -inf / -5 = inf -inf % -5 = NaN
2593 # -inf / 5 = -inf -inf % 5 = NaN
2594 # -inf / inf = NaN -inf % inf = NaN
2595  
2596 1349 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 96 1253 1349
2597 96 my $rem;
2598 96 ▶ $rem = $class -> bnan(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 48 48 96
2599 96 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 48 48 96
2600 48 $x -> bnan(@r);
2601 } else {
2602 48 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 24 24 48
2603 48 $x -> binf($sign, @r);
2604 }
2605 96 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 48 48 96
2606 }
2607  
2608 # Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
2609 # are covered above. In the modulo cases (in the right column) we return
2610 # the same as core Perl, which does floored division, so for consistency we
2611 # also do floored division in the division cases (in the left column).
2612 #
2613 # -5 / inf = -1 -5 % inf = inf
2614 # 0 / inf = 0 0 % inf = 0
2615 # 5 / inf = 0 5 % inf = 5
2616 #
2617 # -5 / -inf = 0 -5 % -inf = -5
2618 # 0 / -inf = 0 0 % -inf = 0
2619 # 5 / -inf = -1 5 % -inf = -inf
2620  
2621 1253 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 80 1173 1253
2622 80 my $rem;
2623 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
2624 56 ▶ $rem = $x -> copy() -> round(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 28 28 56
2625 56 $x -> bzero(@r);
2626 } else {
2627 24 ▶ $rem = $class -> binf($y -> {sign}, @r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 12 12 24
2628 24 $x -> bone('-', @r);
2629 }
2630 80 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 40 40 80
2631 }
2632  
2633 ###########################################################################
2634 # Code for things that aren't Math::BigInt
2635 ###########################################################################
2636  
2637 # At this point, both the numerator and denominator are finite, non-zero
2638 # numbers.
2639  
2640 1173 ▶ unless ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wantarray) 667 506 1173
2641 667 my $upg = $class -> upgrade();
2642 667 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 55 612 667
2643 55 my $tmp = $upg -> bfdiv($x, $y, @r);
2644 55 ▶ if ($tmp -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp->is_int) { } 21 34 55
2645 21 $tmp = $tmp -> as_int();
2646 21 %$x = %$tmp;
2647 } else {
2648 34 %$x = %$tmp;
2649 34 bless $x, $upg;
2650 }
2651 55 return $x;
2652 }
2653 }
2654  
2655 # If called with "foreign" arguments.
2656  
2657 1118 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 0 1118 1118
2658 0 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 0 0
2659 0 $y = $y -> as_int();
2660 } else {
2661 0 ▶ return $x -> _upg() -> bfdiv($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
2662 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
2663 }
2664 }
2665  
2666 ###########################################################################
2667 # Code for Math::BigInt objects
2668 ###########################################################################
2669  
2670 1118 $r[3] = $y; # no push!
2671  
2672 # Initialize remainder.
2673  
2674 1118 my $rem = $class -> bzero();
2675  
2676 # Are both operands the same object, i.e., like $x -> bfdiv($x)? If so,
2677 # flipping the sign of $y also flips the sign of $x.
2678  
2679 1118 my $xsign = $x -> {sign};
2680 1118 my $ysign = $y -> {sign};
2681  
2682 1118 $y -> {sign} =~ tr/+-/-+/; # Flip the sign of $y, and see ...
2683 1118 my $same = $xsign ne $x -> {sign}; # ... if that changed the sign of $x.
2684 1118 $y -> {sign} = $ysign; # Restore the original sign.
2685  
2686 1118 ▶ if ($same) { # $x -> bfdiv($x)
Branch: 1 branch
BranchTrueFalseTotal
if ($same) { } 6 1112 1118
2687 6 $x -> bone();
2688 } else {
2689  
2690 # Compute quotient and remainder, ignoring signs.
2691  
2692 ($x -> {value}, $rem -> {value}) =
2693 1112 $LIB -> _div($x -> {value}, $y -> {value});
2694  
2695 # x y q r
2696 # 23 / 7 => 3 2
2697 # -23 / 7 => -4 5
2698 # 23 / -7 => -4 -5
2699 # -23 / -7 => 3 -2
2700  
2701 # We are doing floored division, so adjust quotient and remainder as
2702 # necessary.
2703  
2704 1112 ▶ if ($xsign ne $ysign && !$LIB -> _is_zero($rem -> {value})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsign ne $ysign and not $LIB->_is_zero($rem->{"value"})) 53 1059 1112
Condition100%3/3
$xsign ne $ysign and not $LIB->_is_zero($rem->{"value"})
!ll&&!rl&&r
9847553
MC/DC100%2/2
$xsign ne $ysign and not $LIB->_is_zero($rem->{"value"})
$xsign ne $ysign not $LIB->_is_zero($rem->{"value"})
Truth table100%3/3
A and not(B)
A= $xsign ne $ysignB= not $LIB->_is_zero($rem->{"value"})
ABresult
0X0
100
111
2705 53 $x -> {value} = $LIB -> _inc($x -> {value});
2706 $rem -> {value} = $LIB -> _sub($LIB -> _copy($y -> {value}),
2707 53 $rem -> {value});
2708 }
2709  
2710 # Now do the signs.
2711  
2712 $x -> {sign} = $xsign eq $ysign || $LIB -> _is_zero($x -> {value})
2713 1112 ▶ ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$xsign eq $ysign || $LIB->_is_zero($x->{'value'}) ? : 1004 108 1112
Condition100%3/3
$xsign eq $ysign or $LIB->_is_zero($x->{"value"})
l!l&&r!l&&!r
98420108
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
2714 $rem -> {sign} = $ysign eq '+' || $LIB -> _is_zero($rem -> {value})
2715 1112 ▶ ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$ysign eq '+' || $LIB->_is_zero($rem->{'value'}) ? : 1063 49 1112
Condition100%3/3
$ysign eq "+" or $LIB->_is_zero($rem->{"value"})
l!l&&r!l&&!r
9877649
MC/DC100%2/2
$ysign eq "+" or $LIB->_is_zero($rem->{"value"})
$ysign eq "+" $LIB->_is_zero($rem->{"value"})
Truth table100%3/3
A or B
A= $ysign eq "+"B= $LIB->_is_zero($rem->{"value"})
ABresult
1X1
011
000
2716 }
2717  
2718 # List context.
2719  
2720 1118 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 506 612 1118
2721 506 $rem -> {accuracy} = $x -> {accuracy};
2722 506 $rem -> {precision} = $x -> {precision};
2723 506 $x -> round(@r);
2724 506 $rem -> round(@r);
2725 506 return $x, $rem;
2726 }
2727  
2728 # Scalar context.
2729  
2730 612 ▶ return $x -> round(@r) if $LIB -> _is_zero($rem -> {value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($rem->{"value"}) 416 196 612
2731  
2732 # We could use this instead of the upgrade code above, but this code gives
2733 # more decimals when the integer part is non-zero. This is because the
2734 # fraction part is divided separately and the rounding is done on that part
2735 # separeately before the integer part is added.
2736 #
2737 #if ($class -> upgrade()) {
2738 # $rem -> _upg() -> bfdiv($y);
2739 # $x -> _upg() -> badd($rem, @r);
2740 # return $x;
2741 #}
2742  
2743 196 $x -> round(@r);
2744 196 return $x;
2745 }
2746  
2747 764 ▶sub bfmod {
Subroutine bfmod: called CC=16 SCAR=28.6
Pod documented
2748 # This is the remainder after floored division.
2749  
2750 # Set up parameters.
2751 764 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 743 21 764
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
129743
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
2752 ? (ref($_[0]), @_)
2753 : objectify(2, @_);
2754  
2755 ###########################################################################
2756 # Code for all classes that share the common interface.
2757 ###########################################################################
2758  
2759 # Don't modify constant (read-only) objects.
2760  
2761 764 ▶ return $x if $x -> modify('bfmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfmod") 0 764 764
2762  
2763 764 $r[3] = $y; # no push!
2764  
2765 # At least one argument is NaN.
2766  
2767 764 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 27 737 764
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1413737
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
2768 27 return $x -> bnan(@r);
2769 }
2770  
2771 # Modulo zero. See documentation for bfdiv().
2772  
2773 737 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 34 703 737
2774 34 return $x -> round(@r);
2775 }
2776  
2777 # Numerator (dividend) is +/-inf.
2778  
2779 703 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 48 655 703
2780 48 return $x -> bnan(@r);
2781 }
2782  
2783 # Denominator (divisor) is +/-inf.
2784  
2785 655 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 40 615 655
2786 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
2787 28 return $x -> round(@r);
2788 } else {
2789 12 return $x -> binf($y -> sign(), @r);
2790 }
2791 }
2792  
2793 ###########################################################################
2794 # Code for things that aren't Math::BigInt
2795 ###########################################################################
2796  
2797 # If called with "foreign" arguments.
2798  
2799 615 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 0 615 615
2800 0 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 0 0
2801 0 $y = $y -> as_int();
2802 } else {
2803 0 ▶ return $x -> _upg() -> bfmod($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
2804 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
2805 }
2806 }
2807  
2808 ###########################################################################
2809 # Code for Math::BigInt objects
2810 ###########################################################################
2811  
2812 # Calc new sign and in case $y == +/- 1, return $x.
2813  
2814 615 $x -> {value} = $LIB -> _mod($x -> {value}, $y -> {value});
2815 615 ▶ if ($LIB -> _is_zero($x -> {value})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{'value'})) { } 154 461 615
2816 154 $x -> {sign} = '+'; # do not leave -0
2817 } else {
2818 $x -> {value} = $LIB -> _sub($y -> {value}, $x -> {value}, 1) # $y-$x
2819 461 ▶ if ($x -> {sign} ne $y -> {sign});
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $y->{"sign"} 172 289 461
2820 461 $x -> {sign} = $y -> {sign};
2821 }
2822  
2823 615 $x -> round(@r);
2824 }
2825  
2826 366 ▶sub btdiv {
Subroutine btdiv: called CC=30 SCAR=38.0
Pod documented
2827 # This does truncated division, where the quotient is truncted, i.e.,
2828 # rounded towards zero.
2829 #
2830 # ($q, $r) = $x -> btdiv($y) returns $q and $r so that $q is int($x / $y)
2831 # and $q * $y + $r = $x.
2832  
2833 # Set up parameters
2834 366 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
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
2835 ? (ref($_[0]), @_)
2836 : objectify(2, @_);
2837  
2838 ###########################################################################
2839 # Code for all classes that share the common interface.
2840 ###########################################################################
2841  
2842 # Don't modify constant (read-only) objects.
2843  
2844 366 ▶ return $x if $x -> modify('btdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btdiv") 0 366 366
2845  
2846 366 my $wantarray = wantarray; # call only once
2847  
2848 # At least one argument is NaN. Return NaN for both quotient and the
2849 # modulo/remainder.
2850  
2851 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
2852 0 ▶ return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
2853 : $x -> bnan(@r);
2854 }
2855  
2856 # Divide by zero and modulo zero.
2857 #
2858 # Division: Use the common convention that x / 0 is inf with the same sign
2859 # as x, except when x = 0, where we return NaN. This is also what earlier
2860 # versions did.
2861 #
2862 # Modulo: In modular arithmetic, the congruence relation z = x (mod y)
2863 # means that there is some integer k such that z - x = k y. If y = 0, we
2864 # get z - x = 0 or z = x. This is also what earlier versions did, except
2865 # that 0 % 0 returned NaN.
2866 #
2867 # inf / 0 = inf inf % 0 = inf
2868 # 5 / 0 = inf 5 % 0 = 5
2869 # 0 / 0 = NaN 0 % 0 = 0
2870 # -5 / 0 = -inf -5 % 0 = -5
2871 # -inf / 0 = -inf -inf % 0 = -inf
2872  
2873 366 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 30 336 366
2874 30 my $rem;
2875 30 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 15 15 30
2876 15 $rem = $x -> copy(@r);
2877 }
2878 30 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 6 24 30
2879 6 $x -> bnan(@r);
2880 } else {
2881 24 $x -> binf($x -> {sign}, @r);
2882 }
2883 30 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 15 15 30
2884 }
2885  
2886 # Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
2887 # The divide by zero cases are covered above. In all of the cases listed
2888 # below we return the same as core Perl.
2889 #
2890 # inf / -inf = NaN inf % -inf = NaN
2891 # inf / -5 = -inf inf % -5 = NaN
2892 # inf / 5 = inf inf % 5 = NaN
2893 # inf / inf = NaN inf % inf = NaN
2894 #
2895 # -inf / -inf = NaN -inf % -inf = NaN
2896 # -inf / -5 = inf -inf % -5 = NaN
2897 # -inf / 5 = -inf -inf % 5 = NaN
2898 # -inf / inf = NaN -inf % inf = NaN
2899  
2900 336 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 48 288 336
2901 48 my $rem;
2902 48 ▶ $rem = $class -> bnan(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 24 24 48
2903 48 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 24 24 48
2904 24 $x -> bnan(@r);
2905 } else {
2906 24 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 12 12 24
2907 24 $x -> binf($sign,@r );
2908 }
2909 48 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 24 24 48
2910 }
2911  
2912 # Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
2913 # are covered above. In the modulo cases (in the right column) we return
2914 # the same as core Perl, which does floored division, so for consistency we
2915 # also do floored division in the division cases (in the left column).
2916 #
2917 # -5 / inf = 0 -5 % inf = -5
2918 # 0 / inf = 0 0 % inf = 0
2919 # 5 / inf = 0 5 % inf = 5
2920 #
2921 # -5 / -inf = 0 -5 % -inf = -5
2922 # 0 / -inf = 0 0 % -inf = 0
2923 # 5 / -inf = 0 5 % -inf = 5
2924  
2925 288 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 36 252 288
2926 36 my $rem;
2927 36 ▶ $rem = $x -> copy() -> round(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 18 18 36
2928 36 $x -> bzero(@r);
2929 36 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 18 18 36
2930 }
2931  
2932 ###########################################################################
2933 # Code for things that aren't Math::BigInt
2934 ###########################################################################
2935  
2936 # Division might return a non-integer result, so upgrade, if upgrading is
2937 # enabled.
2938  
2939 252 ▶ unless ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wantarray) 126 126 252
2940 126 my $upg = $class -> upgrade();
2941 126 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 0 126 126
2942 0 my $tmp = $upg -> btdiv($x, $y, @r);
2943 0 ▶ if ($tmp -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmp->is_int) { } 0 0 0
2944 0 $tmp = $tmp -> as_int();
2945 0 %$x = %$tmp;
2946 } else {
2947 0 %$x = %$tmp;
2948 0 bless $x, $upg;
2949 }
2950 0 return $x;
2951 }
2952 }
2953  
2954 # If called with "foreign" arguments.
2955  
2956 252 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 0 252 252
2957 0 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 0 0
2958 0 $y = $y -> as_int();
2959 } else {
2960 0 ▶ return $x -> _upg() -> btdiv($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
2961 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
2962 }
2963 }
2964  
2965 ###########################################################################
2966 # Code for Math::BigInt objects objects
2967 ###########################################################################
2968  
2969 252 $r[3] = $y; # no push!
2970  
2971 # Initialize remainder.
2972  
2973 252 my $rem = $class -> bzero();
2974  
2975 # Are both operands the same object, i.e., like $x -> btdiv($x)? If so,
2976 # flipping the sign of $y also flips the sign of $x.
2977  
2978 252 my $xsign = $x -> {sign};
2979 252 my $ysign = $y -> {sign};
2980  
2981 252 $y -> {sign} =~ tr/+-/-+/; # Flip the sign of $y, and see ...
2982 252 my $same = $xsign ne $x -> {sign}; # ... if that changed the sign of $x.
2983 252 $y -> {sign} = $ysign; # Re-insert the original sign.
2984  
2985 252 ▶ if ($same) {
Branch: 1 branch
BranchTrueFalseTotal
if ($same) { } 0 252 252
2986 0 $x -> bone(@r);
2987 } else {
2988 ($x -> {value}, $rem -> {value}) =
2989 252 $LIB -> _div($x -> {value}, $y -> {value});
2990  
2991 252 ▶ $x -> {sign} = $xsign eq $ysign ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$xsign eq $ysign ? : 126 126 252
2992 252 ▶ $x -> {sign} = '+' if $LIB -> _is_zero($x -> {value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 108 144 252
2993 252 $x -> round(@r);
2994 }
2995  
2996 252 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 126 126 252
2997 126 $rem -> {sign} = $xsign;
2998 126 ▶ $rem -> {sign} = '+' if $LIB -> _is_zero($rem -> {value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($rem->{"value"}) 78 48 126
2999 126 $rem -> {accuracy} = $x -> {accuracy};
3000 126 $rem -> {precision} = $x -> {precision};
3001 126 $rem -> round(@r);
3002 126 return $x, $rem;
3003 }
3004  
3005 126 return $x;
3006 }
3007  
3008 0 ▶sub btmod {
Subroutine btmod: not called CC=13 SCAR=52.0
Pod documented
3009 # Remainder after truncated division.
3010  
3011 # set up parameters
3012 0 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
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
3013 ? (ref($_[0]), @_)
3014 : objectify(2, @_);
3015  
3016 ###########################################################################
3017 # Code for all classes that share the common interface.
3018 ###########################################################################
3019  
3020 # Don't modify constant (read-only) objects.
3021  
3022 0 ▶ return $x if $x -> modify('btmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btmod") 0 0 0
3023  
3024 0 $r[3] = $y; # no push!
3025  
3026 # At least one argument is NaN.
3027  
3028 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
3029 0 return $x -> bnan(@r);
3030 }
3031  
3032 # Modulo zero. See documentation for btdiv().
3033  
3034 0 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 0 0 0
3035 0 return $x -> round(@r);
3036 }
3037  
3038 # Numerator (dividend) is +/-inf.
3039  
3040 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
3041 0 return $x -> bnan(@r);
3042 }
3043  
3044 # Denominator (divisor) is +/-inf.
3045  
3046 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 0 0 0
3047 0 return $x -> round(@r);
3048 }
3049  
3050 ###########################################################################
3051 # Code for things that aren't Math::BigInt
3052 ###########################################################################
3053  
3054 # If called with "foreign" arguments.
3055  
3056 0 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 0 0 0
3057 0 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 0 0
3058 0 $y = $y -> as_int();
3059 } else {
3060 0 ▶ return $x -> _upg() -> btmod($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3061 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
3062 }
3063 }
3064  
3065 ###########################################################################
3066 # Code for Math::BigInt objects
3067 ###########################################################################
3068  
3069 0 my $xsign = $x -> {sign};
3070  
3071 0 $x -> {value} = $LIB -> _mod($x -> {value}, $y -> {value});
3072  
3073 0 $x -> {sign} = $xsign;
3074 0 ▶ $x -> {sign} = '+' if $LIB -> _is_zero($x -> {value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 0 0 0
3075 0 $x -> round(@r);
3076 }
3077  
3078 0 ▶sub binv {
Subroutine binv: not called CC=10 SCAR=47.0
Pod documented
3079 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3080  
3081 ###########################################################################
3082 # Code for all classes that share the common interface.
3083 ###########################################################################
3084  
3085 # Don't modify constant (read-only) objects.
3086  
3087 0 ▶ return $x if $x -> modify('binv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("binv") 0 0 0
3088  
3089 0 ▶ return $x -> binf("+", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3090 0 ▶ return $x -> bzero(@r) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
3091 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
3092 0 ▶ return $x -> round(@r) if $x -> is_one("+") || $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") or $x->is_one("-") 0 0 0
Condition0%0/3
$x->is_one("+") or $x->is_one("-")
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_one("+") or $x->is_one("-")
$x->is_one("+") $x->is_one("-")
Truth table0%0/3
A or B
A= $x->is_one("+")B= $x->is_one("-")
ABresult
1X1
011
000
3093  
3094 ###########################################################################
3095 # Output might be finite, non-integer, so upgrade.
3096 ###########################################################################
3097  
3098 0 ▶ return $x -> _upg() -> binv(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3099  
3100 ###########################################################################
3101 # Code for things that aren't Math::BigInt
3102 ###########################################################################
3103  
3104 0 ▶ unless ($x -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->isa(__PACKAGE__)) 0 0 0
3105 0 croak "Can't handle a ", ref($x), " in ", (caller(0))[3], "()";
3106 }
3107  
3108 ###########################################################################
3109 # Code for Math::BigInt objects
3110 ###########################################################################
3111  
3112 0 $x -> bzero(@r);
3113 }
3114  
3115 122 ▶sub bsqrt {
Subroutine bsqrt: called CC=10 SCAR=23.1
Pod documented
3116 # calculate square root of $x
3117 122 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 119 3 122
3118  
3119 ###########################################################################
3120 # Code for all classes that share the common interface.
3121 ###########################################################################
3122  
3123 # Don't modify constant (read-only) objects.
3124  
3125 122 ▶ return $x if $x -> modify('bsqrt');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsqrt") 0 122 122
3126  
3127 122 ▶ return $x -> round(@r) if ($x -> is_zero() || $x -> is_one("+") ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one("+") or $x->is_nan or $x->is_inf("+") 16 106 122
Condition100%9/9
$x->is_zero or $x->is_one("+") or $x->is_nan or $x->is_inf("+")
l!l&&r!l&&!r
124106
$x->is_zero or $x->is_one("+") or $x->is_nan
l!l&&r!l&&!r
84110
$x->is_zero or $x->is_one("+")
l!l&&r!l&&!r
44114
MC/DC100%4/4
$x->is_zero or $x->is_one("+") or $x->is_nan or $x->is_inf("+")
$x->is_zero $x->is_one("+") $x->is_nan $x->is_inf("+")
Truth table100%5/5
A or B or C or D
A= $x->is_zeroB= $x->is_one("+")C= $x->is_nanD= $x->is_inf("+")
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3128 $x -> is_nan() || $x -> is_inf("+"));
3129 106 ▶ return $x -> bnan(@r) if $x -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_negative 12 94 106
3130  
3131 ###########################################################################
3132 # Output might be finite, non-integer, so upgrade.
3133 ###########################################################################
3134  
3135 94 ▶ return $x -> _upg() -> bsqrt(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 19 75 94
3136  
3137 ###########################################################################
3138 # Code for things that aren't Math::BigInt
3139 ###########################################################################
3140  
3141 75 ▶ unless ($x -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->isa(__PACKAGE__)) 0 75 75
3142 0 croak "Can't handle a ", ref($x), " in ", (caller(0))[3], "()";
3143 }
3144  
3145 ###########################################################################
3146 # Code for Math::BigInt objects
3147 ###########################################################################
3148  
3149 75 $x->{value} = $LIB -> _sqrt($x->{value});
3150 75 return $x -> round(@r);
3151 }
3152  
3153 520 ▶sub bpow {
Subroutine bpow: called CC=32 SCAR=34.7
Pod documented
3154 # (BINT or num_str, BINT or num_str) return BINT
3155 # compute power of two numbers -- stolen from Knuth Vol 2 pg 233
3156 # modifies first argument
3157  
3158 # set up parameters
3159 520 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 469 51 520
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
1239469
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
3160 ? (ref($_[0]), @_)
3161 : objectify(2, @_);
3162  
3163 ###########################################################################
3164 # Code for all classes that share the common interface.
3165 ###########################################################################
3166  
3167 # Don't modify constant (read-only) objects.
3168  
3169 520 ▶ return $x if $x -> modify('bpow');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bpow") 0 520 520
3170  
3171 # $x and/or $y is a NaN
3172 520 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 65 455 520
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
3431455
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
3173  
3174 # $x and/or $y is a +/-Inf
3175 455 ▶ if ($x -> is_inf("-")) {
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 39 416 455
elsif ($x->is_inf('+')) { } 35 381 416
elsif ($y->is_inf('-')) { } 21 360 381
elsif ($y->is_inf('+')) { } 21 339 360
3176 39 ▶ return $x -> bzero(@r) if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 16 23 39
3177 23 ▶ return $x -> bnan(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 3 20 23
3178 20 ▶ return $x -> round(@r) if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 10 10 20
3179 10 return $x -> bneg(@r);
3180 } elsif ($x -> is_inf("+")) {
3181 35 ▶ return $x -> bzero(@r) if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 16 19 35
3182 19 ▶ return $x -> bnan(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 3 16 19
3183 16 return $x -> round(@r);
3184 } elsif ($y -> is_inf("-")) {
3185 21 ▶ return $x -> bnan(@r) if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 3 18 21
3186 18 ▶ return $x -> binf("+", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 3 15 18
3187 15 ▶ return $x -> bone(@r) if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 3 12 15
3188 12 return $x -> bzero(@r);
3189 } elsif ($y -> is_inf("+")) {
3190 21 ▶ return $x -> bnan(@r) if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 3 18 21
3191 18 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 3 15 18
3192 15 ▶ return $x -> bone(@r) if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 3 12 15
3193 12 return $x -> binf("+", @r);
3194 }
3195  
3196 339 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 26 313 339
3197 26 ▶ return $x -> bone(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 22 26
3198 22 ▶ return $x -> binf(@r) if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 11 11 22
3199 11 return $x -> round(@r);
3200 }
3201  
3202 313 ▶ if ($x -> is_one("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("+")) 28 285 313
3203 28 return $x -> round(@r);
3204 }
3205  
3206 285 ▶ if ($x -> is_one("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("-")) 31 254 285
3207 31 ▶ return $x -> round(@r) if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 17 14 31
3208 14 return $x -> bneg(@r);
3209 }
3210  
3211 ###########################################################################
3212 # Code for things that aren't Math::BigInt
3213 ###########################################################################
3214  
3215 254 ▶ return $x -> _upg() -> bpow($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 31 223 254
3216  
3217 # We don't support finite non-integers, so return zero. The reason for
3218 # returning zero, not NaN, is that all output is in the open interval
3219 # (0,1), and truncating that to integer gives zero.
3220  
3221 223 ▶ if ($y->{sign} eq '-' || !$y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{"sign"} eq "-" or not $y->isa(__PACKAGE__)) 36 187 223
Condition66%2/3
$y->{"sign"} eq "-" or not $y->isa(__PACKAGE__)
l!l&&r!l&&!r
360187
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
3222 36 return $x -> bzero(@r);
3223 }
3224  
3225 187 $r[3] = $y; # no push!
3226  
3227 187 $x->{value} = $LIB -> _pow($x->{value}, $y->{value});
3228 187 ▶ $x->{sign} = $x -> is_negative() && $y -> is_odd() ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative && $y->is_odd ? : 21 166 187
Condition100%3/3
$x->is_negative and $y->is_odd
!ll&&!rl&&r
1481821
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
3229 187 $x -> round(@r);
3230 }
3231  
3232 177 ▶sub broot {
Subroutine broot: called CC=16 SCAR=29.8
Pod documented
3233 # calculate $y'th root of $x
3234  
3235 # set up parameters
3236  
3237 177 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 169 8 177
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
08169
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
3238 ? (ref($_[0]), @_) : objectify(2, @_);
3239  
3240 177 ▶ $y = $class -> new("2") unless defined $y; # default base
Branch: 1 branch
BranchTrueFalseTotal
unless defined $y 0 177 177
3241  
3242 # Don't modify constant (read-only) objects.
3243  
3244 177 ▶ return $x if $x -> modify('broot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("broot") 0 177 177
3245  
3246 # If called with "foreign" argument.
3247  
3248 177 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 0 177 177
3249 0 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 0 0
3250 0 $y = $y -> as_int();
3251 } else {
3252 0 ▶ return $x -> _upg() -> broot($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3253 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
3254 }
3255 }
3256  
3257 # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0
3258 return $x -> bnan(@r) if ($x->{sign} !~ /^\+/ || $y -> is_zero() ||
3259 177 ▶ $y->{sign} !~ /^\+$/);
Branch: 1 branch
BranchTrueFalseTotal
if not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/ 75 102 177
Condition100%6/6
not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/
l!l&&r!l&&!r
5718102
not $x->{"sign"} =~ /^\+/ or $y->is_zero
l!l&&r!l&&!r
489120
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
3260  
3261 # Quick exit for trivial cases.
3262 102 ▶ return $x -> round(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one or $x->is_inf or $y->is_one 12 90 102
Condition100%9/9
$x->is_zero or $x->is_one or $x->is_inf or $y->is_one
l!l&&r!l&&!r
9390
$x->is_zero or $x->is_one or $x->is_inf
l!l&&r!l&&!r
6393
$x->is_zero or $x->is_one
l!l&&r!l&&!r
3396
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
3263 if $x -> is_zero() || $x -> is_one() || $x -> is_inf() || $y -> is_one();
3264  
3265 90 ▶ return $x -> _upg() -> broot($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 2 88 90
3266  
3267 88 $x->{value} = $LIB->_root($x->{value}, $y->{value});
3268 88 $x -> round(@r);
3269 }
3270  
3271 177 ▶sub bmuladd {
Subroutine bmuladd: called CC=47 SCAR=57.8
Pod documented
3272 # multiply two numbers and then add the third to the result
3273 # (BINT or num_str, BINT or num_str, BINT or num_str) return BINT
3274  
3275 # set up parameters
3276 177 ▶ my ($class, $x, $y, $z, @r)
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
3277 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
3278 ? (ref($_[0]), @_)
3279 : objectify(3, @_);
3280  
3281 # Don't modify constant (read-only) objects.
3282  
3283 177 ▶ return $x if $x -> modify('bmuladd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmuladd") 0 177 177
3284  
3285 # At least one of x, y, and z is a NaN
3286  
3287 177 ▶ return $x -> bnan(@r) if ($x -> is_nan() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan or $z->is_nan 24 153 177
Condition100%6/6
$x->is_nan or $y->is_nan or $z->is_nan
l!l&&r!l&&!r
213153
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
129156
MC/DC100%3/3
$x->is_nan or $y->is_nan or $z->is_nan
$x->is_nan $y->is_nan $z->is_nan
Truth table100%4/4
A or B or C
A= $x->is_nanB= $y->is_nanC= $z->is_nan
ABCresult
1XX1
01X1
0011
0000
3288 $y -> is_nan() ||
3289 $z -> is_nan());
3290  
3291 # At least one of x, y, and z is an Inf
3292  
3293 153 ▶ if ($x -> is_inf("-")) {
Branch: 5 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 6 147 153
elsif ($x->{'sign'} eq '+inf') { } 6 141 147
elsif ($x->is_neg) { } 27 114 141
elsif ($x->is_zero) { } 12 102 114
elsif ($x->is_pos) { } 102 0 102
3294  
3295 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
3296 3 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 3 3
3297 0 return $x -> bnan(@r);
3298 } else {
3299 3 return $x -> binf("+", @r);
3300 }
3301 } elsif ($y -> is_zero()) { # x = -inf, y = 0
3302 0 return $x -> bnan(@r);
3303 } else { # x = -inf, y > 0
3304 3 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 3 3
3305 0 return $x -> bnan(@r);
3306 } else {
3307 3 return $x -> binf("-", @r);
3308 }
3309 }
3310  
3311 } elsif ($x->{sign} eq "+inf") {
3312  
3313 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
3314 3 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 3 3
3315 0 return $x -> bnan(@r);
3316 } else {
3317 3 return $x -> binf("-", @r);
3318 }
3319 } elsif ($y -> is_zero()) { # x = +inf, y = 0
3320 0 return $x -> bnan(@r);
3321 } else { # x = +inf, y > 0
3322 3 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 3 3
3323 0 return $x -> bnan(@r);
3324 } else {
3325 3 return $x -> binf("+", @r);
3326 }
3327 }
3328  
3329 } elsif ($x -> is_neg()) {
3330  
3331 27 ▶ if ($y -> is_inf("-")) { # -inf < x < 0, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('-')) { } 0 27 27
elsif ($y->{'sign'} eq '+inf') { } 0 27 27
3332 0 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
3333 0 return $x -> bnan(@r);
3334 } else {
3335 0 return $x -> binf("+", @r);
3336 }
3337 } elsif ($y->{sign} eq "+inf") { # -inf < x < 0, y = +inf
3338 0 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 0 0
3339 0 return $x -> bnan(@r);
3340 } else {
3341 0 return $x -> binf("-", @r);
3342 }
3343 } else { # -inf < x < 0, -inf < y < +inf
3344 27 ▶ if ($z -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 27 27
elsif ($z->{'sign'} eq '+inf') { } 0 27 27
3345 0 return $x -> binf("-", @r);
3346 } elsif ($z->{sign} eq "+inf") {
3347 0 return $x -> binf("+", @r);
3348 }
3349 }
3350  
3351 } elsif ($x -> is_zero()) {
3352  
3353 12 ▶ if ($y -> is_inf("-")) { # x = 0, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('-')) { } 0 12 12
elsif ($y->{'sign'} eq '+inf') { } 0 12 12
3354 0 return $x -> bnan(@r);
3355 } elsif ($y->{sign} eq "+inf") { # x = 0, y = +inf
3356 0 return $x -> bnan(@r);
3357 } else { # x = 0, -inf < y < +inf
3358 12 ▶ if ($z -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 12 12
elsif ($z->{'sign'} eq '+inf') { } 0 12 12
3359 0 return $x -> binf("-", @r);
3360 } elsif ($z->{sign} eq "+inf") {
3361 0 return $x -> binf("+", @r);
3362 }
3363 }
3364  
3365 } elsif ($x -> is_pos()) {
3366  
3367 102 ▶ if ($y -> is_inf("-")) { # 0 < x < +inf, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('-')) { } 0 102 102
elsif ($y->{'sign'} eq '+inf') { } 0 102 102
3368 0 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 0 0
3369 0 return $x -> bnan(@r);
3370 } else {
3371 0 return $x -> binf("-", @r);
3372 }
3373 } elsif ($y->{sign} eq "+inf") { # 0 < x < +inf, y = +inf
3374 0 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
3375 0 return $x -> bnan(@r);
3376 } else {
3377 0 return $x -> binf("+", @r);
3378 }
3379 } else { # 0 < x < +inf, -inf < y < +inf
3380 102 ▶ if ($z -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 102 102
elsif ($z->{'sign'} eq '+inf') { } 0 102 102
3381 0 return $x -> binf("-", @r);
3382 } elsif ($z->{sign} eq "+inf") {
3383 0 return $x -> binf("+", @r);
3384 }
3385 }
3386 }
3387  
3388 # If called with "foreign" arguments.
3389  
3390 141 ▶ unless ($y -> isa(__PACKAGE__) && $z -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__) and $z->isa(__PACKAGE__)) 0 141 141
Condition33%1/3
$y->isa(__PACKAGE__) and $z->isa(__PACKAGE__)
!ll&&!rl&&r
00141
MC/DC0%0/2
$y->isa(__PACKAGE__) and $z->isa(__PACKAGE__)
$y->isa(__PACKAGE__) $z->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= $y->isa(__PACKAGE__)B= $z->isa(__PACKAGE__)
ABresult
0X0
100
111
3391 0 ▶ if ($y -> is_int() && $z -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int and $z->is_int) { } 0 0 0
Condition0%0/3
$y->is_int and $z->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
$y->is_int and $z->is_int
$y->is_int $z->is_int
Truth table0%0/3
A and B
A= $y->is_intB= $z->is_int
ABresult
0X0
100
111
3392 0 $y = $y -> as_int();
3393 0 $z = $z -> as_int();
3394 } else {
3395 0 ▶ return $x -> _upg() -> bmuladd($y, $z, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3396 0 ▶ croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()"
Branch: 1 branch
BranchTrueFalseTotal
unless $y->isa(__PACKAGE__) 0 0 0
3397 unless $y -> isa(__PACKAGE__);
3398 0 ▶ croak "Can't handle a ", ref($z), " in ", (caller(0))[3], "()"
Branch: 1 branch
BranchTrueFalseTotal
unless $z->isa(__PACKAGE__) 0 0 0
3399 unless $z -> isa(__PACKAGE__);
3400 }
3401 }
3402  
3403 # At this point, we know that x, y, and z are finite numbers
3404  
3405 # TODO: what if $y and $z have A or P set?
3406 141 $r[3] = $z; # no push here
3407  
3408 141 my $zs = $z->{sign};
3409 141 my $zv = $z->{value};
3410 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
3411  
3412 141 ▶ $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 111 30 141
3413 141 $x->{value} = $LIB->_mul($x->{value}, $y->{value}); # do actual math
3414 141 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{value}); # no -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"value"}) 21 120 141
3415  
3416 ($x->{value}, $x->{sign})
3417 141 = $LIB -> _sadd($x->{value}, $x->{sign}, $zv, $zs);
3418 141 return $x -> round(@r);
3419 }
3420  
3421 571 ▶sub bmodpow {
Subroutine bmodpow: called CC=24 SCAR=38.8
Pod documented
3422 # Modular exponentiation. Raises a very large number to a very large
3423 # exponent in a given very large modulus quickly, thanks to binary
3424 # exponentiation. Supports negative exponents.
3425 571 ▶ my ($class, $num, $exp, $mod, @r)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 571 0 571
Condition33%2/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
00571
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00571
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
3426 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
3427 ? (ref($_[0]), @_)
3428 : objectify(3, @_);
3429  
3430 ###########################################################################
3431 # Code for all classes that share the common interface.
3432 ###########################################################################
3433  
3434 # Don't modify constant (read-only) objects.
3435  
3436 571 ▶ return $num if $num -> modify('bmodpow');
Branch: 1 branch
BranchTrueFalseTotal
if $num->modify("bmodpow") 0 571 571
3437  
3438 # Check for valid input. All operands must be finite, and the modulus must
3439 # be non-zero.
3440  
3441 571 ▶ return $num -> bnan(@r) if (!$num -> is_finite() || # NaN, -inf, +inf
Branch: 1 branch
BranchTrueFalseTotal
unless $num->is_finite and $exp->is_finite and $mod->is_finite 63 508 571
Condition100%6/6
$num->is_finite and $exp->is_finite and $mod->is_finite
!ll&&!rl&&r
567508
$num->is_finite and $exp->is_finite
!ll&&!rl&&r
3521515
MC/DC100%3/3
$num->is_finite and $exp->is_finite and $mod->is_finite
$num->is_finite $exp->is_finite $mod->is_finite
Truth table100%4/4
A and B and C
A= $num->is_finiteB= $exp->is_finiteC= $mod->is_finite
ABCresult
0XX0
10X0
1100
1111
3442 !$exp -> is_finite() || # NaN, -inf, +inf
3443 !$mod -> is_finite()); # NaN, -inf, +inf
3444  
3445 ###########################################################################
3446 # Code for things that aren't Math::BigInt
3447 ###########################################################################
3448  
3449 # If called with "foreign" arguments.
3450  
3451 508 ▶ unless ($exp -> isa(__PACKAGE__) && $mod -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exp->isa(__PACKAGE__) and $mod->isa(__PACKAGE__)) 0 508 508
Condition33%1/3
$exp->isa(__PACKAGE__) and $mod->isa(__PACKAGE__)
!ll&&!rl&&r
00508
MC/DC0%0/2
$exp->isa(__PACKAGE__) and $mod->isa(__PACKAGE__)
$exp->isa(__PACKAGE__) $mod->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= $exp->isa(__PACKAGE__)B= $mod->isa(__PACKAGE__)
ABresult
0X0
100
111
3452 0 ▶ if ($exp -> is_int() && $mod -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp->is_int and $mod->is_int) { } 0 0 0
Condition0%0/3
$exp->is_int and $mod->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
$exp->is_int and $mod->is_int
$exp->is_int $mod->is_int
Truth table0%0/3
A and B
A= $exp->is_intB= $mod->is_int
ABresult
0X0
100
111
3453 0 $exp = $exp -> as_int();
3454 0 $mod = $mod -> as_int();
3455 } else {
3456 0 ▶ return $num -> _upg() -> bmodpow($exp, $mod, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3457 0 ▶ croak "Can't handle a ", ref($exp), " in ", (caller(0))[3], "()"
Branch: 1 branch
BranchTrueFalseTotal
unless $exp->isa(__PACKAGE__) 0 0 0
3458 unless $exp -> isa(__PACKAGE__);
3459 0 ▶ croak "Can't handle a ", ref($mod), " in ", (caller(0))[3], "()"
Branch: 1 branch
BranchTrueFalseTotal
unless $mod->isa(__PACKAGE__) 0 0 0
3460 unless $mod -> isa(__PACKAGE__);
3461 }
3462 }
3463  
3464 # When the exponent 'e' is negative, use the following relation, which is
3465 # based on finding the multiplicative inverse 'd' of 'b' modulo 'm':
3466 #
3467 # b^(-e) (mod m) = d^e (mod m) where b*d = 1 (mod m)
3468 #
3469 # Return NaN if no modular multiplicative inverse exists.
3470  
3471 508 ▶ if ($exp->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp->{"sign"} eq "-") 143 365 508
3472 143 $num -> bmodinv($mod);
3473 143 ▶ return $num -> bnan(@r) if $num -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $num->is_nan 43 100 143
3474 }
3475  
3476 # Modulo zero. See documentation for Math::BigInt's bmod() method.
3477  
3478 465 ▶ if ($mod -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->is_zero) 3 462 465
3479 3 ▶ if ($num -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num->is_zero) { } 0 3 3
3480 0 return $num -> bnan(@r);
3481 } else {
3482 3 return $num -> round(@r);
3483 }
3484 }
3485  
3486 ###########################################################################
3487 # Code for Math::BigInt objects
3488 ###########################################################################
3489  
3490 # Compute 'a (mod m)', ignoring the signs on 'a' and 'm'. If the resulting
3491 # value is zero, the output is also zero, regardless of the signs on 'a'
3492 # and 'm'.
3493  
3494 462 my $value = $LIB->_modpow($num->{value}, $exp->{value}, $mod->{value});
3495 462 my $sign = '+';
3496  
3497 # If the resulting value is non-zero, we have four special cases, depending
3498 # on the signs on 'a' and 'm'.
3499  
3500 462 ▶ unless ($LIB->_is_zero($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($value)) 239 223 462
3501  
3502 # There is a negative sign on 'a' (= $num**$exp) only if the number we
3503 # are exponentiating ($num) is negative and the exponent ($exp) is odd.
3504  
3505 239 ▶ if ($num->{sign} eq '-' && $exp -> is_odd()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num->{'sign'} eq '-' and $exp->is_odd) { } 21 218 239
Condition100%3/3
$num->{"sign"} eq "-" and $exp->is_odd
!ll&&!rl&&r
1883021
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
3506  
3507 # When both the number 'a' and the modulus 'm' have a negative
3508 # sign, use this relation:
3509 #
3510 # -a (mod -m) = -(a (mod m))
3511  
3512 21 ▶ if ($mod->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->{'sign'} eq '-') { } 0 21 21
3513 0 $sign = '-';
3514 }
3515  
3516 # When only the number 'a' has a negative sign, use this relation:
3517 #
3518 # -a (mod m) = m - (a (mod m))
3519  
3520 else {
3521 # Use copy of $mod since _sub() modifies the first argument.
3522 21 my $mod = $LIB->_copy($mod->{value});
3523 21 $value = $LIB->_sub($mod, $value);
3524 21 $sign = '+';
3525 }
3526  
3527 } else {
3528  
3529 # When only the modulus 'm' has a negative sign, use this relation:
3530 #
3531 # a (mod -m) = (a (mod m)) - m
3532 # = -(m - (a (mod m)))
3533  
3534 218 ▶ if ($mod->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($mod->{"sign"} eq "-") 7 211 218
3535 # Use copy of $mod since _sub() modifies the first argument.
3536 7 my $mod = $LIB->_copy($mod->{value});
3537 7 $value = $LIB->_sub($mod, $value);
3538 7 $sign = '-';
3539 }
3540  
3541 # When neither the number 'a' nor the modulus 'm' have a negative
3542 # sign, directly return the already computed value.
3543 #
3544 # (a (mod m))
3545  
3546 }
3547  
3548 }
3549  
3550 462 $num->{value} = $value;
3551 462 $num->{sign} = $sign;
3552  
3553 462 return $num -> round(@r);
3554 }
3555  
3556 313 ▶sub bmodinv {
Subroutine bmodinv: called CC=17 SCAR=32.2
Pod documented
3557 # Return modular multiplicative inverse:
3558 #
3559 # z is the modular inverse of x (mod y) if and only if
3560 #
3561 # x*z ≡ 1 (mod y)
3562 #
3563 # If the modulus y is larger than one, x and z are relative primes (i.e.,
3564 # their greatest common divisor is one).
3565 #
3566 # If no modular multiplicative inverse exists, NaN is returned.
3567  
3568 # set up parameters
3569 313 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 313 0 313
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00313
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
3570 ? (ref($_[0]), @_)
3571 : objectify(2, @_);
3572  
3573 ###########################################################################
3574 # Code for all classes that share the common interface.
3575 ###########################################################################
3576  
3577 # Don't modify constant (read-only) objects.
3578  
3579 313 ▶ return $x if $x -> modify('bmodinv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmodinv") 0 313 313
3580  
3581 # Return NaN if one or both arguments is +inf, -inf, or nan.
3582  
3583 313 ▶ return $x -> bnan(@r) if !$y -> is_finite() || !$x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $y->is_finite and $x->is_finite 49 264 313
Condition100%3/3
$y->is_finite and $x->is_finite
!ll&&!rl&&r
2821264
MC/DC100%2/2
$y->is_finite and $x->is_finite
$y->is_finite $x->is_finite
Truth table100%3/3
A and B
A= $y->is_finiteB= $x->is_finite
ABresult
0X0
100
111
3584  
3585 # Return NaN if $y is zero; 1 % 0 makes no sense.
3586  
3587 264 ▶ return $x -> bnan(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 0 264 264
3588  
3589 ###########################################################################
3590 # Code for things that aren't Math::BigInt
3591 ###########################################################################
3592  
3593 # If called with "foreign" arguments.
3594  
3595 264 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 0 264 264
3596 0 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 0 0
3597 0 $y = $y -> as_int();
3598 } else {
3599 0 ▶ return $x -> _upg() -> bmodinv($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3600 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
3601 }
3602 }
3603  
3604 ###########################################################################
3605 # Code for Math::BigInt objects
3606 ###########################################################################
3607  
3608 # Return 0 in the trivial case. $x % 1 or $x % -1 is zero for all finite
3609 # integers $x.
3610  
3611 264 ▶ return $x -> bzero(@r) if $y -> is_one('+') || $y -> is_one('-');
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_one("+") or $y->is_one("-") 63 201 264
Condition66%2/3
$y->is_one("+") or $y->is_one("-")
l!l&&r!l&&!r
630201
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
3612  
3613 # Return NaN if $x = 0, or $x modulo $y is zero. The only valid case when
3614 # $x = 0 is when $y = 1 or $y = -1, but that was covered above.
3615 #
3616 # Note that computing $x modulo $y here affects the value we'll feed to
3617 # $LIB->_modinv() below when $x and $y have opposite signs. E.g., if $x =
3618 # 5 and $y = 7, those two values are fed to _modinv(), but if $x = -5 and
3619 # $y = 7, the values fed to _modinv() are $x = 2 (= -5 % 7) and $y = 7.
3620 # The value if $x is affected only when $x and $y have opposite signs.
3621  
3622 201 $x -> bfmod($y);
3623 201 ▶ return $x -> bnan(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 36 165 201
3624  
3625 # Compute the modular multiplicative inverse of the absolute values. We'll
3626 # correct for the signs of $x and $y later. Return NaN if no GCD is found.
3627  
3628 165 ($x->{value}, $x->{sign}) = $LIB->_modinv($x->{value}, $y->{value});
3629 165 ▶ return $x -> bnan(@r) if !defined($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x->{"value"} 25 140 165
3630  
3631 # Library inconsistency workaround: _modinv() in Math::BigInt::GMP versions
3632 # <= 1.32 return undef rather than a "+" for the sign.
3633  
3634 140 ▶ $x->{sign} = '+' unless defined $x->{sign};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $x->{"sign"} 0 140 140
3635  
3636 # When one or both arguments are negative, we have the following
3637 # relations. If x and y are positive:
3638 #
3639 # modinv(-x, -y) = -modinv(x, y)
3640 # modinv(-x, y) = y - modinv(x, y) = -modinv(x, y) (mod y)
3641 # modinv( x, -y) = modinv(x, y) - y = modinv(x, y) (mod -y)
3642  
3643 # We must swap the sign of the result if the original $x is negative.
3644 # However, we must compensate for ignoring the signs when computing the
3645 # inverse modulo. The net effect is that we must swap the sign of the
3646 # result if $y is negative.
3647  
3648 140 ▶ $x -> bneg() if $y->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} eq "-" 7 133 140
3649  
3650 # Compute $x modulo $y again after correcting the sign.
3651  
3652 140 ▶ $x -> bmod($y) if $x->{sign} ne $y->{sign};
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $y->{"sign"} 76 64 140
3653  
3654 140 $x -> round(@r);
3655 }
3656  
3657 199 ▶sub blog {
Subroutine blog: called CC=29 SCAR=34.4
Pod documented
3658 # Return the logarithm of the operand. If a second operand is defined, that
3659 # value is used as the base, otherwise the base is assumed to be Euler's
3660 # constant.
3661  
3662 199 my ($class, $x, $base, @r);
3663  
3664 # Only objectify the base if it is defined, since an undefined base, as in
3665 # $x->blog() or $x->blog(undef) signals that the base is Euler's number =
3666 # 2.718281828...
3667  
3668 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
3669 # E.g., Math::BigInt->blog(256, 2)
3670 0 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
3671 defined $_[2] ? objectify(2, @_) : objectify(1, @_);
3672 } else {
3673 # E.g., $x->blog(2) or the deprecated Math::BigInt::blog(256, 2)
3674 199 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 166 33 199
3675 defined $_[1] ? objectify(2, @_) : objectify(1, @_);
3676 }
3677  
3678 # Don't modify constant (read-only) objects.
3679  
3680 199 ▶ return $x if $x->modify('blog');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blog") 0 199 199
3681  
3682 # Handle all exception cases and all trivial cases. I have used Wolfram
3683 # Alpha (http://www.wolframalpha.com) as the reference for these cases.
3684  
3685 199 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 9 190 199
3686  
3687 190 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 160 30 190
3688 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
3689 unless defined(blessed($base)) && $base -> isa(__PACKAGE__);
3690 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
3691 12 return $x -> bnan(@r);
3692 ▶ } 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
3693 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
3694 15 return $x -> bzero(@r);
3695 } elsif ($base -> is_negative()) { # -inf < base < 0
3696 12 ▶ return $x -> bzero(@r) if $x -> is_one(); # x = 1
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 3 9 12
3697 9 ▶ return $x -> bone('+', @r) if $x == $base; # x = base
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 0 9 9
3698 # we can't handle these cases, so upgrade, if we can
3699 9 ▶ return $x -> _upg() -> blog($base, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 9 9
3700 9 return $x -> bnan(@r);
3701 }
3702 100 ▶ return $x -> bone(@r) if $x == $base; # 0 < base && 0 < x < inf
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 3 97 100
3703 }
3704  
3705 # We now know that the base is either undefined or >= 2 and finite.
3706  
3707 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
3708 15 return $x -> binf('+', @r);
3709 } elsif ($x -> is_neg()) { # -inf < x < 0
3710 6 ▶ return $x -> _upg() -> blog($base, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 6 6
3711 6 return $x -> bnan(@r);
3712 } elsif ($x -> is_one()) { # x = 1
3713 9 return $x -> bzero(@r);
3714 } elsif ($x -> is_zero()) { # x = 0
3715 6 return $x -> binf('-', @r);
3716 }
3717  
3718 # At this point we are done handling all exception cases and trivial cases.
3719  
3720 91 ▶ return $x -> _upg() -> blog($base, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 2 89 91
3721  
3722 # fix for bug #24969:
3723 # the default base is e (Euler's number) which is not an integer
3724 89 ▶ if (!defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $base) 15 74 89
3725 15 require Math::BigFloat;
3726  
3727 # disable upgrading and downgrading
3728  
3729 15 my $upg = Math::BigFloat -> upgrade();
3730 15 my $dng = Math::BigFloat -> downgrade();
3731 15 Math::BigFloat -> upgrade(undef);
3732 15 Math::BigFloat -> downgrade(undef);
3733  
3734 15 my $u = Math::BigFloat -> new($x) -> blog() -> as_int();
3735  
3736 # reset upgrading and downgrading
3737  
3738 15 Math::BigFloat -> upgrade($upg);
3739 15 Math::BigFloat -> downgrade($dng);
3740  
3741 # modify $x in place
3742  
3743 15 $x->{value} = $u->{value};
3744 15 $x->{sign} = $u->{sign};
3745  
3746 15 return $x -> round(@r);
3747 }
3748  
3749 74 my ($rc) = $LIB -> _log_int($x->{value}, $base->{value});
3750 74 ▶ return $x -> bnan(@r) unless defined $rc; # not possible to take log?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 0 74 74
3751 74 $x->{value} = $rc;
3752 74 $x -> round(@r);
3753 }
3754  
3755 15 ▶sub bexp {
Subroutine bexp: called CC=9 SCAR=23.2
Pod documented
3756 # Calculate e ** $x (Euler's number to the power of X), truncated to
3757 # an integer value.
3758 15 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 15 0 15
3759  
3760 ###########################################################################
3761 # Code for all classes that share the common interface.
3762 ###########################################################################
3763  
3764 # Don't modify constant (read-only) objects.
3765  
3766 15 ▶ return $x if $x -> modify('bexp');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bexp") 0 15 15
3767  
3768 # inf, -inf, NaN, <0 => NaN
3769 15 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 3 12 15
3770 12 ▶ return $x -> bone(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 12 12
3771 12 ▶ return $x -> round(@r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 3 9 12
3772 9 ▶ return $x -> bzero(@r) if $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("-") 0 9 9
3773  
3774 ###########################################################################
3775 # Output might be finite, non-integer, so upgrade.
3776 ###########################################################################
3777  
3778 9 ▶ return $x -> _upg() -> bexp(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 9 9
3779  
3780 ###########################################################################
3781 # Code for things that aren't Math::BigInt
3782 ###########################################################################
3783  
3784 9 ▶ unless ($x -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->isa(__PACKAGE__)) 0 9 9
3785 0 croak "Can't handle a ", ref($x), " in ", (caller(0))[3], "()";
3786 }
3787  
3788 ###########################################################################
3789 # Code for Math::BigInt objects
3790 ###########################################################################
3791  
3792 9 require Math::BigFloat;
3793 9 my $tmp = Math::BigFloat -> bexp($x) -> bint() -> round(@r) -> as_int();
3794 9 $x->{value} = $tmp->{value};
3795 9 return $x -> round(@r);
3796 }
3797  
3798 0 ▶sub bilog2 {
Subroutine bilog2: not called CC=8 SCAR=42.8
Pod documented
3799 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3800  
3801 # Don't modify constant (read-only) objects.
3802  
3803 0 ▶ return $x if $x -> modify('bilog2');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bilog2") 0 0 0
3804  
3805 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
3806 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
3807 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3808  
3809 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
3810 0 ▶ return $x -> _upg() -> bilog2(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3811 0 return $x -> bnan(@r);
3812 }
3813  
3814 0 $x -> {value} = $LIB -> _ilog2($x -> {value});
3815 0 return $x -> round(@r);
3816 }
3817  
3818 0 ▶sub bilog10 {
Subroutine bilog10: not called CC=8 SCAR=42.8
Pod documented
3819 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3820  
3821 # Don't modify constant (read-only) objects.
3822  
3823 0 ▶ return $x if $x -> modify('bilog10');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bilog10") 0 0 0
3824  
3825 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
3826 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
3827 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3828  
3829 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
3830 0 ▶ return $x -> _upg() -> bilog10(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3831 0 return $x -> bnan(@r);
3832 }
3833  
3834 0 $x -> {value} = $LIB -> _ilog10($x -> {value});
3835 0 return $x -> round(@r);
3836 }
3837  
3838 0 ▶sub bclog2 {
Subroutine bclog2: not called CC=8 SCAR=42.8
Pod documented
3839 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3840  
3841 # Don't modify constant (read-only) objects.
3842  
3843 0 ▶ return $x if $x -> modify('bclog2');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bclog2") 0 0 0
3844  
3845 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
3846 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
3847 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3848  
3849 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
3850 0 ▶ return $x -> _upg() -> bclog2(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3851 0 return $x -> bnan(@r);
3852 }
3853  
3854 0 $x -> {value} = $LIB -> _clog2($x -> {value});
3855 0 return $x -> round(@r);
3856 }
3857  
3858 0 ▶sub bclog10 {
Subroutine bclog10: not called CC=8 SCAR=42.8
Pod documented
3859 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3860  
3861 # Don't modify constant (read-only) objects.
3862  
3863 0 ▶ return $x if $x -> modify('bclog10');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bclog10") 0 0 0
3864  
3865 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
3866 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
3867 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
3868  
3869 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
3870 0 ▶ return $x -> _upg() -> bclog10(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3871 0 return $x -> bnan(@r);
3872 }
3873  
3874 0 $x -> {value} = $LIB -> _clog10($x -> {value});
3875 0 return $x -> round(@r);
3876 }
3877  
3878 93 ▶sub bnok {
Subroutine bnok: called CC=24 SCAR=44.6
Pod documented
3879 # Calculate n over k (binomial coefficient or "choose" function) as
3880 # integer.
3881  
3882 # Set up parameters.
3883 93 ▶ my ($class, $n, $k, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
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
3884 ? (ref($_[0]), @_)
3885 : objectify(2, @_);
3886  
3887 93 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 93 93
3888  
3889 # Don't modify constant (read-only) objects.
3890  
3891 93 ▶ return $n if $n -> modify('bnok');
Branch: 1 branch
BranchTrueFalseTotal
if $n->modify("bnok") 0 93 93
3892  
3893 # If called with "foreign" arguments.
3894  
3895 93 ▶ unless ($k -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k->isa(__PACKAGE__)) 0 93 93
3896 0 ▶ if ($k -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k->is_int) { } 0 0 0
3897 0 $k = $k -> as_int();
3898 } else {
3899 0 ▶ return $n -> _upg() -> bnok($k, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
3900 0 croak "Can't handle a ", ref($k), " in ", (caller(0))[3], "()";
3901 }
3902 }
3903  
3904 # All cases where at least one argument is NaN.
3905  
3906 93 ▶ return $n -> bnan(@r) if $n -> is_nan() || $k -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $n->is_nan or $k->is_nan 9 84 93
Condition100%3/3
$n->is_nan or $k->is_nan
l!l&&r!l&&!r
6384
MC/DC100%2/2
$n->is_nan or $k->is_nan
$n->is_nan $k->is_nan
Truth table100%3/3
A or B
A= $n->is_nanB= $k->is_nan
ABresult
1X1
011
000
3907  
3908 # All cases where at least one argument is +/-inf.
3909  
3910 84 ▶ if ($n -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($n->is_inf) { } 7 77 84
elsif ($k->is_inf) { } 0 77 77
3911 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
3912 0 return $n -> bnan(@r);
3913 } elsif ($k -> is_neg()) { # bnok(+/-inf,k), k < 0
3914 0 return $n -> bzero(@r);
3915 } elsif ($k -> is_zero()) { # bnok(+/-inf,k), k = 0
3916 0 return $n -> bone(@r);
3917 } else {
3918 7 ▶ if ($n -> is_inf("+", @r)) { # bnok(+inf,k), 0 < k < +inf
Branch: 1 branch
BranchTrueFalseTotal
if ($n->is_inf('+', @r)) { } 7 0 7
3919 7 return $n -> binf("+");
3920 } else { # bnok(-inf,k), k > 0
3921 0 ▶ my $sign = $k -> is_even() ? "+" : "-";
Branch: 1 branch
BranchTrueFalseTotal
$k->is_even ? : 0 0 0
3922 0 return $n -> binf($sign, @r);
3923 }
3924 }
3925 }
3926  
3927 elsif ($k -> is_inf()) { # bnok(n,+/-inf), -inf <= n <= inf
3928 0 return $n -> bnan(@r);
3929 }
3930  
3931 # At this point, both n and k are real numbers.
3932  
3933 77 my $sign = 1;
3934  
3935 77 ▶ if ($n >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n >= 0) { } 77 0 77
3936 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/DC100%2/2
$k < 0 or $k > $n
$k < 0 $k > $n
Truth table100%3/3
A or B
A= $k < 0B= $k > $n
ABresult
1X1
011
000
3937 21 return $n -> bzero(@r);
3938 }
3939 } else {
3940  
3941 0 ▶ if ($k >= 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k >= 0) { } 0 0 0
elsif ($k <= $n) { } 0 0 0
3942  
3943 # n < 0 and k >= 0: bnok(n,k) = (-1)^k * bnok(-n+k-1,k)
3944  
3945 0 $sign = (-1) ** $k;
3946 0 $n -> bneg() -> badd($k) -> bdec();
3947  
3948 } elsif ($k <= $n) {
3949  
3950 # n < 0 and k <= n: bnok(n,k) = (-1)^(n-k) * bnok(-k-1,n-k)
3951  
3952 0 $sign = (-1) ** ($n - $k);
3953 0 my $x0 = $n -> copy();
3954 0 $n -> bone() -> badd($k) -> bneg();
3955 0 $k = $k -> copy();
3956 0 $k -> bneg() -> badd($x0);
3957  
3958 } else {
3959  
3960 # n < 0 and n < k < 0:
3961  
3962 0 return $n -> bzero(@r);
3963 }
3964 }
3965  
3966 # Some backends, e.g., Math::BigInt::GMP, can't handle the case when k is
3967 # very large, so if k > n/2, or, equivalently, 2*k > n, perform range
3968 # reduction by computing nok(n, k) as nok(n, n-k).
3969  
3970 56 my $k_val = $k->{value};
3971 56 my $two_k = $LIB -> _mul($LIB -> _two(), $k_val);
3972 56 ▶ if ($LIB -> _acmp($two_k, $n->{value}) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($two_k, $n->{"value"}) > 0) 28 28 56
3973 28 $k_val = $LIB -> _sub($LIB -> _copy($n->{value}), $k_val);
3974 }
3975  
3976 56 $n->{value} = $LIB -> _nok($n->{value}, $k_val);
3977 56 ▶ $n -> bneg() if $sign == -1;
Branch: 1 branch
BranchTrueFalseTotal
if $sign == -1 0 56 56
3978 56 $n -> round(@r);
3979 }
3980  
3981 0 ▶sub bperm {
Subroutine bperm: not called CC=16 SCAR=56.1
Pod documented
3982 # Calculate permutations: n! / (n - k)!
3983  
3984 # Set up parameters.
3985 0 ▶ my ($class, $n, $k, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
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
3986 ? (ref($_[0]), @_)
3987 : objectify(2, @_);
3988  
3989 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
3990  
3991 # Don't modify constant (read-only) objects.
3992  
3993 0 ▶ return $n if $n -> modify('bnok');
Branch: 1 branch
BranchTrueFalseTotal
if $n->modify("bnok") 0 0 0
3994  
3995 # If called with "foreign" arguments.
3996  
3997 0 ▶ unless ($k -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k->isa(__PACKAGE__)) 0 0 0
3998 0 ▶ if ($k -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k->is_int) { } 0 0 0
3999 0 $k = $k -> as_int();
4000 } else {
4001 0 ▶ return $n -> _upg() -> bperm($k, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4002 0 croak "Can't handle a ", ref($k), " in ", (caller(0))[3], "()";
4003 }
4004 }
4005  
4006 # Special cases.
4007  
4008 0 ▶ return $n -> bnan(@r) if $n -> is_nan() || $k -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $n->is_nan or $k->is_nan 0 0 0
Condition0%0/3
$n->is_nan or $k->is_nan
l!l&&r!l&&!r
000
MC/DC0%0/2
$n->is_nan or $k->is_nan
$n->is_nan $k->is_nan
Truth table0%0/3
A or B
A= $n->is_nanB= $k->is_nan
ABresult
1X1
011
000
4009 0 ▶ return $n -> bnan(@r) unless $n >= $k && $k >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $n >= $k and $k >= 0 0 0 0
Condition0%0/3
$n >= $k and $k >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
$n >= $k and $k >= 0
$n >= $k $k >= 0
Truth table0%0/3
A and B
A= $n >= $kB= $k >= 0
ABresult
0X0
100
111
4010 0 ▶ return $n -> bone("+", @r) if $k -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $k->is_zero 0 0 0
4011  
4012 0 ▶ if ($n -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->is_inf) 0 0 0
4013 0 ▶ if ($k -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k->is_inf) { } 0 0 0
4014 0 return $n -> bnan(@r);
4015 } else {
4016 0 return $n -> binf("+", @r);
4017 }
4018 }
4019  
4020 # Should this code be moved into the backend library? XXX
4021  
4022 # $factor is $n
4023 0 my $factor = $LIB -> _copy($n->{value});
4024  
4025 # $limit is $n - $k + 1
4026 0 my $limit = $LIB -> _copy($n->{value});
4027 0 $limit = $LIB -> _sub($limit, $k->{value});
4028 0 $limit = $LIB -> _inc($limit);
4029  
4030 0 ▶ while ($LIB -> _acmp($factor, $limit) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($factor, $limit) > 0) 0 0 0
4031 0 $LIB -> _dec($factor);
4032 0 $LIB -> _mul($n->{value}, $factor);
4033 }
4034  
4035 0 $n -> round(@r);
4036 }
4037  
4038 0 ▶sub bhyperop {
Subroutine bhyperop: not called CC=2 SCAR=17.9
Pod documented
4039 0 my ($class, $a, $n, $b, @r) = objectify(3, @_);
4040  
4041 # Don't modify constant (read-only) objects.
4042  
4043 0 ▶ return $a if $a -> modify('bhyperop');
Branch: 1 branch
BranchTrueFalseTotal
if $a->modify("bhyperop") 0 0 0
4044  
4045 0 my $tmp = $a -> hyperop($n, $b);
4046 0 $a -> {value} = $tmp -> {value};
4047 0 return $a -> round(@r);
4048 }
4049  
4050 0 ▶sub hyperop {
Subroutine hyperop: not called CC=32 SCAR=69.6
Pod documented
4051 0 my ($class, $a, $n, $b, @r) = objectify(3, @_);
4052  
4053 0 ▶ croak("a must be non-negative") if $a < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $a < 0 0 0 0
4054 0 ▶ croak("n must be non-negative") if $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 0 0 0
4055 0 ▶ croak("b must be non-negative") if $b < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $b < 0 0 0 0
4056  
4057 # The following is a non-recursive implementation of the hyperoperator,
4058 # with special cases handled for speed.
4059  
4060 0 my @stack = ($a, $n, $b);
4061 0 ▶ while (@stack > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack > 1) 0 0 0
4062 0 my ($a, $n, $b) = splice @stack, -3;
4063  
4064 # Special cases for $b
4065  
4066 0 ▶ if ($b == 2 && $a == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 2 and $a == 2) 0 0 0
Condition0%0/3
$b == 2 and $a == 2
!ll&&!rl&&r
000
MC/DC0%0/2
$b == 2 and $a == 2
$b == 2 $a == 2
Truth table0%0/3
A and B
A= $b == 2B= $a == 2
ABresult
0X0
100
111
4067 0 ▶ push @stack, $n == 0 ? Math::BigInt -> new("3")
Branch: 1 branch
BranchTrueFalseTotal
$n == 0 ? : 0 0 0
4068 : Math::BigInt -> new("4");
4069 0 next;
4070 }
4071  
4072 0 ▶ if ($b == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 1) 0 0 0
4073 0 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 0 0
4074 0 push @stack, Math::BigInt -> new("2");
4075 0 next;
4076 }
4077 0 ▶ if ($n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) 0 0 0
4078 0 push @stack, $a + 1;
4079 0 next;
4080 }
4081 0 push @stack, $a;
4082 0 next;
4083 }
4084  
4085 0 ▶ if ($b == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 0) 0 0 0
4086 0 ▶ if ($n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) 0 0 0
4087 0 push @stack, $a;
4088 0 next;
4089 }
4090 0 ▶ if ($n == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 2) 0 0 0
4091 0 push @stack, Math::BigInt -> bzero();
4092 0 next;
4093 }
4094 0 push @stack, Math::BigInt -> bone();
4095 0 next;
4096 }
4097  
4098 # Special cases for $a
4099  
4100 0 ▶ if ($a == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a == 0) 0 0 0
4101 0 ▶ if ($n == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 0 0
4102 0 push @stack, $b + 1;
4103 0 next;
4104 }
4105 0 ▶ if ($n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) 0 0 0
4106 0 push @stack, $b;
4107 0 next;
4108 }
4109 0 ▶ if ($n == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 2) 0 0 0
4110 0 push @stack, Math::BigInt -> bzero();
4111 0 next;
4112 }
4113 0 ▶ if ($n == 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 3) 0 0 0
4114 0 ▶ push @stack, $b == 0 ? Math::BigInt -> bone()
Branch: 1 branch
BranchTrueFalseTotal
$b == 0 ? : 0 0 0
4115 : Math::BigInt -> bzero();
4116 0 next;
4117 }
4118 0 ▶ push @stack, $b -> is_odd() ? Math::BigInt -> bzero()
Branch: 1 branch
BranchTrueFalseTotal
$b->is_odd ? : 0 0 0
4119 : Math::BigInt -> bone();
4120 0 next;
4121 }
4122  
4123 0 ▶ if ($a == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a == 1) 0 0 0
4124 0 ▶ if ($n == 0 || $n == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0 or $n == 1) 0 0 0
Condition0%0/3
$n == 0 or $n == 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$n == 0 or $n == 1
$n == 0 $n == 1
Truth table0%0/3
A or B
A= $n == 0B= $n == 1
ABresult
1X1
011
000
4125 0 push @stack, $b + 1;
4126 0 next;
4127 }
4128 0 ▶ if ($n == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 2) 0 0 0
4129 0 push @stack, $b;
4130 0 next;
4131 }
4132 0 push @stack, Math::BigInt -> bone();
4133 0 next;
4134 }
4135  
4136 # Special cases for $n
4137  
4138 0 ▶ if ($n == 4) { # tetration
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 4) 0 0 0
4139 0 ▶ if ($b == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 0) 0 0 0
4140 0 push @stack, Math::BigInt -> bone();
4141 0 next;
4142 }
4143 0 my $y = $a;
4144 0 $y = $a ** $y for 2 .. $b;
4145 0 push @stack, $y;
4146 0 next;
4147 }
4148  
4149 0 ▶ if ($n == 3) { # exponentiation
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 3) 0 0 0
4150 0 push @stack, $a ** $b;
4151 0 next;
4152 }
4153  
4154 0 ▶ if ($n == 2) { # multiplication
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 2) 0 0 0
4155 0 push @stack, $a * $b;
4156 0 next;
4157 }
4158  
4159 0 ▶ if ($n == 1) { # addition
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 1) 0 0 0
4160 0 push @stack, $a + $b;
4161 0 next;
4162 }
4163  
4164 0 ▶ if ($n == 0) { # succession
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 0 0
4165 0 push @stack, $b + 1;
4166 0 next;
4167 }
4168  
4169 0 push @stack, $a, $n - 1, $a, $n, $b - 1;
4170 }
4171  
4172 0 $a = pop @stack;
4173 0 return $a -> round(@r);
4174 }
4175  
4176 0 ▶sub buparrow {
Subroutine buparrow: not called CC=2 SCAR=17.9
Pod documented
4177 0 my ($class, $a, $n, $b, @r) = objectify(3, @_);
4178  
4179 # Don't modify constant (read-only) objects.
4180  
4181 0 ▶ return $a if $a -> modify('buparrow');
Branch: 1 branch
BranchTrueFalseTotal
if $a->modify("buparrow") 0 0 0
4182  
4183 0 $a -> bhyperop($n + 2, $b, @r);
4184 }
4185  
4186 0 ▶sub uparrow {
Subroutine uparrow: not called CC=1 SCAR=6.9
Pod documented
4187 0 my ($class, $a, $n, $b, @r) = objectify(3, @_);
4188 0 $a -> hyperop($n + 2, $b, @r);
4189 }
4190  
4191 0 ▶sub backermann {
Subroutine backermann: not called CC=2 SCAR=17.9
Pod documented
4192 0 my $m = shift;
4193  
4194 # Don't modify constant (read-only) objects.
4195  
4196 0 ▶ return $m if $m -> modify('backermann');
Branch: 1 branch
BranchTrueFalseTotal
if $m->modify("backermann") 0 0 0
4197  
4198 0 my $y = $m -> ackermann(@_);
4199 0 $m -> {value} = $y -> {value};
4200 0 return $m;
4201 }
4202  
4203 0 ▶sub ackermann {
Subroutine ackermann: not called CC=10 SCAR=47.0
Pod documented
4204 # Ackermann's function ackermann(m, n)
4205 #
4206 # The following is a simple, recursive implementation of the ackermann
4207 # function, just to show the idea. Such implementations cause "Deep
4208 # recursion on subroutine ..." warnings, so we use a faster, non-recursive
4209 # algorithm below with @_ as a stack.
4210 #
4211 # sub ackermann {
4212 # my ($m, $n) = @_;
4213 # return $n + 1 if $m == 0;
4214 # return ackermann($m - 1, 1) if $m > 0 && $n == 0;
4215 # return ackermann($m - 1, ackermann($m, $n - 1) if $m > 0 && $n > 0;
4216 # }
4217  
4218 0 my ($m, $n) = @_;
4219 0 my $class = ref $m;
4220 0 ▶ croak("m must be non-negative") if $m < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $m < 0 0 0 0
4221 0 ▶ croak("n must be non-negative") if $n < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n < 0 0 0 0
4222  
4223 0 my $two = $class -> new("2");
4224 0 my $three = $class -> new("3");
4225 0 my $thirteen = $class -> new("13");
4226  
4227 0 $n = pop;
4228 0 ▶ $n = $class -> new($n) unless ref($n);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $n 0 0 0
4229 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
4230 0 my $m = pop;
4231 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
4232 0 push @_, (--$m) x $n;
4233 0 ▶ while (--$m >= $three) {
Branch: 1 branch
BranchTrueFalseTotal
if (--$m >= $three) 0 0 0
4234 0 push @_, $m;
4235 }
4236 0 $n = $thirteen;
4237 } elsif ($m == $three) {
4238 0 $n = $class -> bone() -> blsft($n + $three) -> bsub($three);
4239 } elsif ($m == $two) {
4240 0 $n -> bmul($two) -> badd($three);
4241 } elsif ($m >= 0) {
4242 0 $n -> badd($m) -> binc();
4243 } else {
4244 0 die "negative m!";
4245 }
4246 }
4247 0 $n;
4248 }
4249  
4250 0 ▶sub bsin {
Subroutine bsin: not called CC=8 SCAR=42.8
Pod documented
4251 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4252  
4253 # Don't modify constant (read-only) objects.
4254  
4255 0 ▶ return $x if $x -> modify('bsin');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsin") 0 0 0
4256  
4257 # Trivial cases.
4258  
4259 0 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
4260 0 ▶ return $x -> bnan(@r) if $x -> is_inf() || $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf or $x->is_nan 0 0 0
Condition0%0/3
$x->is_inf or $x->is_nan
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_inf or $x->is_nan
$x->is_inf $x->is_nan
Truth table0%0/3
A or B
A= $x->is_infB= $x->is_nan
ABresult
1X1
011
000
4261  
4262 0 my $upg = $class -> upgrade();
4263 0 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 0 0 0
4264 0 my $xtmp = $upg -> bsin($x, @r);
4265 0 ▶ if ($xtmp -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xtmp->is_int) { } 0 0 0
4266 0 $xtmp = $xtmp -> as_int();
4267 0 %$x = %$xtmp;
4268 } else {
4269 0 %$x = %$xtmp;
4270 0 bless $x, $upg;
4271 }
4272 0 return $x;
4273 }
4274  
4275 # When x is an integer ≠ 0, sin(x) truncated to an integer is always zero.
4276  
4277 0 $x -> bzero(@r);
4278 }
4279  
4280 0 ▶sub bcos {
Subroutine bcos: not called CC=8 SCAR=42.8
Pod documented
4281 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4282  
4283 # Don't modify constant (read-only) objects.
4284  
4285 0 ▶ return $x if $x -> modify('bcos');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bcos") 0 0 0
4286  
4287 # Trivial cases.
4288  
4289 0 ▶ return $x -> bone(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
4290 0 ▶ return $x -> bnan(@r) if $x -> is_inf() || $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf or $x->is_nan 0 0 0
Condition0%0/3
$x->is_inf or $x->is_nan
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_inf or $x->is_nan
$x->is_inf $x->is_nan
Truth table0%0/3
A or B
A= $x->is_infB= $x->is_nan
ABresult
1X1
011
000
4291  
4292 0 my $upg = $class -> upgrade();
4293 0 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 0 0 0
4294 0 my $xtmp = $upg -> bcos($x, @r);
4295 0 ▶ if ($xtmp -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xtmp->is_int) { } 0 0 0
4296 0 $xtmp = $xtmp -> as_int();
4297 0 %$x = %$xtmp;
4298 } else {
4299 0 %$x = %$xtmp;
4300 0 bless $x, $upg;
4301 }
4302 0 return $x;
4303 }
4304  
4305 # When x is a non-zero integer, cos(x) truncated to an integer is always
4306 # zero.
4307  
4308 0 $x -> bzero(@r);
4309 }
4310  
4311 0 ▶sub batan {
Subroutine batan: not called CC=8 SCAR=42.8
Pod documented
4312 # Calculate arctan(x) to N digits. Unless upgrading is in effect, returns
4313 # the result truncated to an integer.
4314 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4315  
4316 # Don't modify constant (read-only) objects.
4317  
4318 0 ▶ return $x if $x -> modify('batan');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("batan") 0 0 0
4319  
4320 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4321 0 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
4322  
4323 0 ▶ return $x -> _upg() -> batan(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4324  
4325 0 ▶ return $x -> bone("+", @r) if $x -> bgt("1");
Branch: 1 branch
BranchTrueFalseTotal
if $x->bgt(1) 0 0 0
4326 0 ▶ return $x -> bone("-", @r) if $x -> blt("-1");
Branch: 1 branch
BranchTrueFalseTotal
if $x->blt(-1) 0 0 0
4327  
4328 0 $x -> bzero(@r);
4329 }
4330  
4331 84 ▶sub batan2 {
Subroutine batan2: called CC=15 SCAR=28.5
Pod documented
4332 # calculate arcus tangens of ($y/$x)
4333  
4334 84 ▶ my ($class, $y, $x, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
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
4335 ? (ref($_[0]), @_) : objectify(2, @_);
4336  
4337 # Don't modify constant (read-only) objects.
4338  
4339 84 ▶ return $y if $y -> modify('batan2');
Branch: 1 branch
BranchTrueFalseTotal
if $y->modify("batan2") 0 84 84
4340  
4341 # If called with "foreign" arguments.
4342  
4343 84 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 0 84 84
4344 0 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 0 0
4345 0 $y = $y -> as_int();
4346 } else {
4347 0 ▶ return $y -> _upg() -> batan2($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4348 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
4349 }
4350 }
4351  
4352 84 ▶ return $y -> bnan() if $y -> is_nan() || $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_nan or $x->is_nan 9 75 84
Condition100%3/3
$y->is_nan or $x->is_nan
l!l&&r!l&&!r
6375
MC/DC100%2/2
$y->is_nan or $x->is_nan
$y->is_nan $x->is_nan
Truth table100%3/3
A or B
A= $y->is_nanB= $x->is_nan
ABresult
1X1
011
000
4353  
4354 # Y X
4355 # != 0 -inf result is +- pi
4356 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
4357 30 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 18 12 30
4358 18 ▶ if ($x -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 6 12 18
elsif ($x->is_inf('+')) { } 6 6 12
4359 # calculate 3 pi/4 => 2.3.. => 2
4360 6 $y -> bone(substr($y->{sign}, 0, 1));
4361 6 $y -> bmul($class -> new(2));
4362 } elsif ($x -> is_inf("+")) {
4363 # calculate pi/4 => 0.7 => 0
4364 6 $y -> bzero();
4365 } else {
4366 # calculate pi/2 => 1.5 => 1
4367 6 $y -> bone(substr($y->{sign}, 0, 1));
4368 }
4369 } else {
4370 12 ▶ if ($x -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf('+')) { } 3 9 12
4371 # calculate pi/4 => 0.7 => 0
4372 3 $y -> bzero();
4373 } else {
4374 # PI => 3.1415.. => 3
4375 9 $y -> bone(substr($y->{sign}, 0, 1));
4376 9 $y -> bmul($class -> new(3));
4377 }
4378 }
4379 30 return $y;
4380 }
4381  
4382 # Temporarily disable downgrading in Math::BigFloat.
4383  
4384 45 my $dng = Math::BigFloat -> downgrade();
4385 45 Math::BigFloat -> downgrade(undef);
4386  
4387 45 my $yflt = $y -> as_float();
4388 45 my $xflt = $x -> as_float();
4389 45 my $yint = $yflt -> batan2($xflt, @r) -> as_int();
4390  
4391 45 $y->{value} = $yint->{value};
4392 45 $y->{sign} = $yint->{sign};
4393  
4394 # Restore downgrading.
4395  
4396 45 Math::BigFloat -> downgrade($dng);
4397 45 $y -> round(@r);
4398 }
4399  
4400 89 ▶sub bfac {
Subroutine bfac: called CC=9 SCAR=22.0
Pod documented
4401 # (BINT or num_str, BINT or num_str) return BINT
4402 # compute factorial number from $x, modify $x in place
4403 89 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 89 0 89
4404  
4405 # Don't modify constant (read-only) objects.
4406  
4407 89 ▶ return $x if $x -> modify('bfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfac") 0 89 89
4408  
4409 89 ▶ return $x -> bnan(@r) if $x -> is_nan() || $x -> is_inf("-");;
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 6 83 89
Condition100%3/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
3383
MC/DC100%2/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table100%3/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
4410 83 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 3 80 83
4411 80 ▶ return $x -> bnan(@r) if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 3 77 80
4412 77 ▶ return $x -> bone(@r) if $x -> is_zero() || $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one 8 69 77
Condition100%3/3
$x->is_zero or $x->is_one
l!l&&r!l&&!r
4469
MC/DC100%2/2
$x->is_zero or $x->is_one
$x->is_zero $x->is_one
Truth table100%3/3
A or B
A= $x->is_zeroB= $x->is_one
ABresult
1X1
011
000
4413  
4414 69 $x->{value} = $LIB->_fac($x->{value});
4415 69 $x -> round(@r);
4416 }
4417  
4418 54 ▶sub bdfac {
Subroutine bdfac: called CC=9 SCAR=22.1
Pod documented
4419 # compute double factorial, modify $x in place
4420 54 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 54 0 54
4421  
4422 # Don't modify constant (read-only) objects.
4423  
4424 54 ▶ return $x if $x -> modify('bdfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdfac") 0 54 54
4425  
4426 54 ▶ return $x -> bnan(@r) if $x -> is_nan() || $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 6 48 54
Condition100%3/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
3348
MC/DC100%2/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table100%3/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
4427 48 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 3 45 48
4428 45 ▶ return $x -> bnan(@r) if $x <= -2;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= -2 3 42 45
4429 42 ▶ return $x -> bone(@r) if $x <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= 1 9 33 42
4430  
4431 33 ▶ croak("bdfac() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_dfac") 0 33 33
4432 unless $LIB -> can('_dfac');
4433  
4434 33 $x->{value} = $LIB->_dfac($x->{value});
4435 33 $x -> round(@r);
4436 }
4437  
4438 57 ▶sub btfac {
Subroutine btfac: called CC=8 SCAR=20.8
Pod documented
4439 # compute triple factorial, modify $x in place
4440 57 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 57 0 57
4441  
4442 # Don't modify constant (read-only) objects.
4443  
4444 57 ▶ return $x if $x -> modify('btfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btfac") 0 57 57
4445  
4446 57 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 3 54 57
4447 54 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 3 51 54
4448  
4449 51 my $k = $class -> new("3");
4450 51 ▶ return $x -> bnan(@r) if $x <= -$k;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= -$k 6 45 51
4451  
4452 45 my $one = $class -> bone();
4453 45 ▶ return $x -> bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 12 33 45
4454  
4455 33 my $f = $x -> copy();
4456 33 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 45 33 78
4457 45 $x -> bmul($f);
4458 }
4459 33 $x -> round(@r);
4460 }
4461  
4462 270 ▶sub bmfac {
Subroutine bmfac: called CC=14 SCAR=29.3
Pod documented
4463 # compute multi-factorial
4464  
4465 270 ▶ my ($class, $x, $k, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
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
4466 ? (ref($_[0]), @_) : objectify(2, @_);
4467  
4468 # Don't modify constant (read-only) objects.
4469  
4470 270 ▶ return $x if $x -> modify('bmfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmfac") 0 270 270
4471  
4472 270 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 15 255 270
4473 255 ▶ return $x -> bnan(@r) if $k -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $k->is_nan 3 252 255
4474 252 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 15 237 252
4475  
4476 # If called with "foreign" arguments.
4477  
4478 237 ▶ unless ($k -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k->isa(__PACKAGE__)) 0 237 237
4479 0 ▶ if ($k -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k->is_int) { } 0 0 0
4480 0 $k = $k -> as_int();
4481 } else {
4482 0 ▶ return $x -> _upg() -> bmfac($k, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4483 0 croak "Can't handle a ", ref($k), " in ", (caller(0))[3], "()";
4484 }
4485 }
4486  
4487 237 ▶ return $x -> bnan(@r) if $k < 1 || $x <= -$k;
Branch: 1 branch
BranchTrueFalseTotal
if $k < 1 or $x <= -$k 39 198 237
Condition100%3/3
$k < 1 or $x <= -$k
l!l&&r!l&&!r
930198
MC/DC100%2/2
$k < 1 or $x <= -$k
$k < 1 $x <= -$k
Truth table100%3/3
A or B
A= $k < 1B= $x <= -$k
ABresult
1X1
011
000
4488  
4489 198 my $one = $class -> bone();
4490 198 ▶ return $x -> bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 60 138 198
4491  
4492 138 my $f = $x -> copy();
4493 138 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 213 138 351
4494 213 $x -> bmul($f);
4495 }
4496 138 $x -> round(@r);
4497 }
4498  
4499 0 ▶sub bfib {
Subroutine bfib: not called CC=18 SCAR=58.3
Pod documented
4500 # compute Fibonacci number(s)
4501 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4502  
4503 0 ▶ croak("bfib() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_fib") 0 0 0
4504 unless $LIB -> can('_fib');
4505  
4506 # Don't modify constant (read-only) objects.
4507  
4508 0 ▶ return $x if $x -> modify('bfib');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfib") 0 0 0
4509  
4510 # List context.
4511  
4512 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
4513 0 ▶ croak("bfib() can't return an infinitely long list of numbers")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4514 if $x -> is_inf();
4515  
4516 0 ▶ return if $x -> is_nan() || !$x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or not $x->is_int 0 0 0
Condition0%0/3
$x->is_nan or not $x->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or not $x->is_int
$x->is_nan not $x->is_int
Truth table0%0/3
A or not(B)
A= $x->is_nanB= not $x->is_int
ABresult
1X1
011
000
4517  
4518 # The following places a limit on how large $x can be. Should this
4519 # limit be removed? XXX
4520  
4521 0 my $n = $x -> numify();
4522  
4523 0 my @y;
4524 {
4525 0 $y[0] = $x -> copy() -> babs();
4526 0 $y[0]{value} = $LIB -> _zero();
4527 0 ▶ last if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
4528  
4529 0 $y[1] = $y[0] -> copy();
4530 0 $y[1]{value} = $LIB -> _one();
4531 0 ▶ last if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
4532  
4533 0 ▶ for (my $i = 2 ; $i <= abs($n) ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= abs $n) 0 0 0
4534 0 $y[$i] = $y[$i - 1] -> copy();
4535 $y[$i]{value} = $LIB -> _add($LIB -> _copy($y[$i - 1]{value}),
4536 0 $y[$i - 2]{value});
4537 }
4538  
4539 # If negative, insert sign as appropriate.
4540  
4541 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
4542 0 ▶ for (my $i = 2 ; $i <= $#y ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#y) 0 0 0
4543 0 $y[$i]{sign} = '-';
4544 }
4545 }
4546  
4547 # The last element in the array is the invocand.
4548  
4549 0 $x->{value} = $y[-1]{value};
4550 0 $x->{sign} = $y[-1]{sign};
4551 0 $y[-1] = $x;
4552 }
4553  
4554 0 @y = map { $_ -> round(@r) } @y;
4555 0 return @y;
4556 }
4557  
4558 # Scalar context.
4559  
4560 else {
4561 0 ▶ return $x if $x -> is_inf('+');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
4562 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
4563  
4564 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
4565 0 $x->{value} = $LIB -> _fib($x->{value});
4566 0 return $x -> round(@r);
4567 }
4568 }
4569  
4570 0 ▶sub blucas {
Subroutine blucas: not called CC=17 SCAR=57.2
Pod documented
4571 # compute Lucas number(s)
4572 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4573  
4574 0 ▶ croak("blucas() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_lucas") 0 0 0
4575 unless $LIB -> can('_lucas');
4576  
4577 # Don't modify constant (read-only) objects.
4578  
4579 0 ▶ return $x if $x -> modify('blucas');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blucas") 0 0 0
4580  
4581 # List context.
4582  
4583 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
4584 0 ▶ return if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4585 0 ▶ croak("blucas() can't return an infinitely long list of numbers")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4586 if $x -> is_inf();
4587  
4588 # The following places a limit on how large $x can be. Should this
4589 # limit be removed? XXX
4590  
4591 0 my $n = $x -> numify();
4592  
4593 0 my @y;
4594 {
4595 0 $y[0] = $x -> copy() -> babs();
4596 0 $y[0]{value} = $LIB -> _two();
4597 0 ▶ last if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
4598  
4599 0 $y[1] = $y[0] -> copy();
4600 0 $y[1]{value} = $LIB -> _one();
4601 0 ▶ last if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
4602  
4603 0 ▶ for (my $i = 2 ; $i <= abs($n) ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= abs $n) 0 0 0
4604 0 $y[$i] = $y[$i - 1] -> copy();
4605 $y[$i]{value} = $LIB -> _add($LIB -> _copy($y[$i - 1]{value}),
4606 0 $y[$i - 2]{value});
4607 }
4608  
4609 # If negative, insert sign as appropriate.
4610  
4611 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
4612 0 ▶ for (my $i = 2 ; $i <= $#y ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#y) 0 0 0
4613 0 $y[$i]{sign} = '-';
4614 }
4615 }
4616  
4617 # The last element in the array is the invocand.
4618  
4619 0 $x->{value} = $y[-1]{value};
4620 0 $x->{sign} = $y[-1]{sign};
4621 0 $y[-1] = $x;
4622 }
4623  
4624 0 @y = map { $_ -> round(@r) } @y;
4625 0 return @y;
4626 }
4627  
4628 # Scalar context.
4629  
4630 else {
4631 0 ▶ return $x if $x -> is_inf('+');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
4632 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
4633  
4634 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
4635 0 $x->{value} = $LIB -> _lucas($x->{value});
4636 0 return $x -> round(@r);
4637 }
4638 }
4639  
4640 62 ▶sub blsft {
Subroutine blsft: called CC=54 SCAR=67.9
Pod documented
4641 # (BINT or num_str, BINT or num_str) return BINT
4642 # compute $x << $y, base $n
4643  
4644 62 my ($class, $x, $y, $b, @r);
4645  
4646 # Objectify the base only when it is defined, since an undefined base, as
4647 # in $x->blsft(3) or $x->blog(3, undef) means use the default base 2.
4648  
4649 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
4650 # E.g., Math::BigInt->blog(256, 5, 2)
4651 12 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 12 12
4652 defined $_[3] ? objectify(3, @_) : objectify(2, @_);
4653 } else {
4654 # E.g., Math::BigInt::blog(256, 5, 2) or $x->blog(5, 2)
4655 50 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 44 6 50
4656 defined $_[2] ? objectify(3, @_) : objectify(2, @_);
4657 }
4658  
4659 # Don't modify constant (read-only) objects.
4660  
4661 62 ▶ return $x if $x -> modify('blsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blsft") 0 62 62
4662  
4663 # The default base is 2.
4664  
4665 62 ▶ $b = 2 unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 18 44 62
4666 62 ▶ $b = $class -> new($b) unless defined(blessed($b));
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($b) 18 44 62
4667  
4668 # If called with "foreign" arguments.
4669  
4670 62 ▶ unless ($y -> isa(__PACKAGE__) && $b -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__) and $b->isa(__PACKAGE__)) 0 62 62
Condition33%1/3
$y->isa(__PACKAGE__) and $b->isa(__PACKAGE__)
!ll&&!rl&&r
0062
MC/DC0%0/2
$y->isa(__PACKAGE__) and $b->isa(__PACKAGE__)
$y->isa(__PACKAGE__) $b->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= $y->isa(__PACKAGE__)B= $b->isa(__PACKAGE__)
ABresult
0X0
100
111
4671 0 ▶ if ($y -> is_int() && $b -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int and $b->is_int) { } 0 0 0
Condition0%0/3
$y->is_int and $b->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
$y->is_int and $b->is_int
$y->is_int $b->is_int
Truth table0%0/3
A and B
A= $y->is_intB= $b->is_int
ABresult
0X0
100
111
4672 0 $y = $y -> as_int();
4673 0 $b = $b -> as_int();
4674 } else {
4675 0 ▶ return $x -> _upg() -> blsft($y, $b, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4676 0 ▶ croak "Can't handle a ", ref($x), " in ", (caller(0))[3], "()"
Branch: 1 branch
BranchTrueFalseTotal
unless $y->isa(__PACKAGE__) 0 0 0
4677 unless $y -> isa(__PACKAGE__);
4678 0 ▶ croak "Can't handle a ", ref($b), " in ", (caller(0))[3], "()"
Branch: 1 branch
BranchTrueFalseTotal
unless $b->isa(__PACKAGE__) 0 0 0
4679 unless $b -> isa(__PACKAGE__);
4680 }
4681 }
4682  
4683 # Handle NaN cases.
4684  
4685 62 ▶ return $x -> bnan(@r)
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
4686 if $x -> is_nan() || $y -> is_nan() || $b -> is_nan();
4687  
4688 # blsft($x, -$y, $b) = brsft($x, $y, $b)
4689  
4690 62 ▶ return $x -> brsft($y -> copy() -> bneg(), $b, @r) if $y -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 4 58 62
4691  
4692 # Now handle all cases where at least one operand is ±Inf or the result
4693 # will be ±Inf or NaN.
4694  
4695 58 ▶ if ($y -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf("+")) 0 58 58
4696 0 ▶ if ($b -> is_one("-")) {
Branch: 3 branches
BranchTrueFalseTotal
if ($b->is_one('-')) { } 0 0 0
elsif ($b->is_one('+')) { } 0 0 0
elsif ($b->is_zero) { } 0 0 0
4697 0 return $x -> bnan(@r);
4698 } elsif ($b -> is_one("+")) {
4699 0 return $x -> round(@r);
4700 } elsif ($b -> is_zero()) {
4701 0 ▶ return $x -> bnan(@r) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4702 0 return $x -> bzero(@r);
4703 } else {
4704 0 ▶ return $x -> binf("-", @r) if $x -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_negative 0 0 0
4705 0 ▶ return $x -> binf("+", @r) if $x -> is_positive();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_positive 0 0 0
4706 0 return $x -> bnan(@r);
4707 }
4708 }
4709  
4710 58 ▶ if ($b -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_inf) 0 58 58
4711 0 ▶ return $x -> bnan(@r) if $x -> is_zero() || $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $y->is_zero 0 0 0
Condition0%0/3
$x->is_zero or $y->is_zero
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_zero or $y->is_zero
$x->is_zero $y->is_zero
Truth table0%0/3
A or B
A= $x->is_zeroB= $y->is_zero
ABresult
1X1
011
000
4712 0 ▶ if ($b -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_inf('-')) { } 0 0 0
4713 0 ▶ return $x -> binf("+", @r)
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_negative and $y->is_odd or $x->is_positive and $y->is_even 0 0 0
Condition0%0/9
$x->is_negative and $y->is_odd or $x->is_positive and $y->is_even
l!l&&r!l&&!r
000
$x->is_negative and $y->is_odd
!ll&&!rl&&r
000
$x->is_positive and $y->is_even
!ll&&!rl&&r
000
MC/DC0%0/4
$x->is_negative and $y->is_odd or $x->is_positive and $y->is_even
$x->is_negative $y->is_odd $x->is_positive $y->is_even
Truth table0%0/7
A and B or C and D
A= $x->is_negativeB= $y->is_oddC= $x->is_positiveD= $y->is_even
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
4714 if ($x -> is_negative() && $y -> is_odd() ||
4715 $x -> is_positive() && $y -> is_even());
4716 0 return $x -> binf("-", @r);
4717 } else {
4718 0 ▶ return $x -> binf("-", @r) if $x -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_negative 0 0 0
4719 0 return $x -> binf("+", @r);
4720 }
4721 }
4722  
4723 58 ▶ if ($b -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_zero) 0 58 58
4724 0 ▶ return $x -> round(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 0 0 0
4725 0 ▶ return $x -> bnan(@r) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4726 0 return $x -> bzero(@r);
4727 }
4728  
4729 58 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 58 58
4730 0 ▶ if ($b -> is_negative()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_negative) { } 0 0 0
4731 0 ▶ if ($x -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 0 0 0
4732 0 ▶ if ($y -> is_even()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_even) { } 0 0 0
4733 0 return $x -> round(@r);
4734 } else {
4735 0 return $x -> binf("+", @r);
4736 }
4737 } else {
4738 0 ▶ if ($y -> is_even()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_even) { } 0 0 0
4739 0 return $x -> round(@r);
4740 } else {
4741 0 return $x -> binf("-", @r);
4742 }
4743 }
4744 } else {
4745 0 return $x -> round(@r);
4746 }
4747 }
4748  
4749 # At this point, we know that both the input and the output is finite.
4750 # Handle some trivial cases.
4751  
4752 58 ▶ return $x -> round(@r) if $x -> is_zero() || $y -> is_zero()
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $y->is_zero or $b->is_one("+") or $b->is_one("-") and $y->is_even 8 50 58
Condition58%7/12
$x->is_zero or $y->is_zero or $b->is_one("+") or $b->is_one("-") and $y->is_even
l!l&&r!l&&!r
8050
$x->is_zero or $y->is_zero or $b->is_one("+")
l!l&&r!l&&!r
8050
$x->is_zero or $y->is_zero
l!l&&r!l&&!r
0850
$b->is_one("-") and $y->is_even
!ll&&!rl&&r
5000
MC/DC20%1/5
$x->is_zero or $y->is_zero or $b->is_one("+") or $b->is_one("-") and $y->is_even
$x->is_zero $y->is_zero $b->is_one("+") $b->is_one("-") $y->is_even
Truth table33%2/6
A or B or C or D and E
A= $x->is_zeroB= $y->is_zeroC= $b->is_one("+")D= $b->is_one("-")E= $y->is_even
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
4753 || $b -> is_one("+")
4754 || $b -> is_one("-") && $y -> is_even();
4755  
4756 50 ▶ return $x -> bneg(@r) if $b -> is_one("-") && $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $b->is_one("-") and $y->is_odd 0 50 50
Condition33%1/3
$b->is_one("-") and $y->is_odd
!ll&&!rl&&r
5000
MC/DC0%0/2
$b->is_one("-") and $y->is_odd
$b->is_one("-") $y->is_odd
Truth table33%1/3
A and B
A= $b->is_one("-")B= $y->is_odd
ABresult
0X0
100
111
4757  
4758 # While some of the libraries support an arbitrarily large base, not all of
4759 # them do, so rather than returning an incorrect result in those cases,
4760 # disallow bases that don't work with all libraries.
4761  
4762 50 my $uintmax = ~0;
4763 50 ▶ if ($x -> bcmp($uintmax) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bcmp($uintmax) > 0) { } 0 50 50
4764 0 $x -> bmul($b -> bpow($y));
4765 } else {
4766 50 my $neg = 0;
4767 50 ▶ if ($b -> is_negative()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_negative) 0 50 50
4768 0 ▶ $neg = 1 if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 0 0 0
4769 0 $b -> babs();
4770 }
4771 50 $b = $b -> numify();
4772 50 $x -> {value} = $LIB -> _lsft($x -> {value}, $y -> {value}, $b);
4773 50 ▶ $x -> {sign} =~ tr/+-/-+/ if $neg;
Branch: 1 branch
BranchTrueFalseTotal
if $neg 0 50 50
4774 }
4775 50 $x -> round(@r);
4776 }
4777  
4778 134 ▶sub brsft {
Subroutine brsft: called CC=57 SCAR=65.0
Pod documented
4779 # (BINT or num_str, BINT or num_str) return BINT
4780 # compute $x >> $y, base $n
4781  
4782 134 my ($class, $x, $y, $b, @r);
4783  
4784 # Objectify the base only when it is defined, since an undefined base, as
4785 # in $x->blsft(3) or $x->blog(3, undef) means use the default base 2.
4786  
4787 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
4788 # E.g., Math::BigInt->blog(256, 5, 2)
4789 12 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 12 12
4790 defined $_[3] ? objectify(3, @_) : objectify(2, @_);
4791 } else {
4792 # E.g., Math::BigInt::blog(256, 5, 2) or $x -> blog(5, 2)
4793 122 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 116 6 122
4794 defined $_[2] ? objectify(3, @_) : objectify(2, @_);
4795 }
4796  
4797 # Don't modify constant (read-only) objects.
4798  
4799 134 ▶ return $x if $x -> modify('brsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("brsft") 0 134 134
4800  
4801 # The default base is 2.
4802  
4803 134 ▶ $b = 2 unless defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 18 116 134
4804 134 ▶ $b = $class -> new($b) unless defined(blessed($b));
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($b) 18 116 134
4805  
4806 # If called with "foreign" arguments.
4807  
4808 134 ▶ unless ($y -> isa(__PACKAGE__) && $b -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__) and $b->isa(__PACKAGE__)) 0 134 134
Condition33%1/3
$y->isa(__PACKAGE__) and $b->isa(__PACKAGE__)
!ll&&!rl&&r
00134
MC/DC0%0/2
$y->isa(__PACKAGE__) and $b->isa(__PACKAGE__)
$y->isa(__PACKAGE__) $b->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= $y->isa(__PACKAGE__)B= $b->isa(__PACKAGE__)
ABresult
0X0
100
111
4809 0 ▶ if ($y -> is_int() && $b -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int and $b->is_int) { } 0 0 0
Condition0%0/3
$y->is_int and $b->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
$y->is_int and $b->is_int
$y->is_int $b->is_int
Truth table0%0/3
A and B
A= $y->is_intB= $b->is_int
ABresult
0X0
100
111
4810 0 $y = $y -> as_int();
4811 0 $b = $b -> as_int();
4812 } else {
4813 0 ▶ return $x -> _upg() -> brsft($y, $b, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4814 0 ▶ croak "Can't handle a ", ref($x), " in ", (caller(0))[3], "()"
Branch: 1 branch
BranchTrueFalseTotal
unless $y->isa(__PACKAGE__) 0 0 0
4815 unless $y -> isa(__PACKAGE__);
4816 0 ▶ croak "Can't handle a ", ref($b), " in ", (caller(0))[3], "()"
Branch: 1 branch
BranchTrueFalseTotal
unless $b->isa(__PACKAGE__) 0 0 0
4817 unless $b -> isa(__PACKAGE__);
4818 }
4819 }
4820  
4821 # Handle NaN cases.
4822  
4823 134 ▶ return $x -> bnan(@r)
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
4824 if $x -> is_nan() || $y -> is_nan() || $b -> is_nan();
4825  
4826 # brsft($x, -$y, $b) = blsft($x, $y, $b)
4827  
4828 134 ▶ return $x -> blsft($y -> copy() -> bneg(), $b, @r) if $y -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 4 130 134
4829  
4830 # Now handle all cases where at least one operand is ±Inf or the result
4831 # will be ±Inf or NaN.
4832  
4833 130 ▶ if ($b -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_inf) 0 130 130
4834 0 ▶ return $x -> bnan(@r) if $x -> is_inf() || $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf or $y->is_zero 0 0 0
Condition0%0/3
$x->is_inf or $y->is_zero
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_inf or $y->is_zero
$x->is_inf $y->is_zero
Truth table0%0/3
A or B
A= $x->is_infB= $y->is_zero
ABresult
1X1
011
000
4835 0 ▶ if ($b -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_inf('+')) { } 0 0 0
4836 0 ▶ if ($x -> is_negative()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_negative) { } 0 0 0
4837 0 return $x -> bone("-", @r);
4838 } else {
4839 0 return $x -> bzero(@r);
4840 }
4841 } else {
4842 0 ▶ if ($x -> is_negative()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_negative) { } 0 0 0
elsif ($x->is_positive) { } 0 0 0
4843 0 ▶ return $y -> is_odd() ? $x -> bzero(@r)
Branch: 1 branch
BranchTrueFalseTotal
$y->is_odd ? : 0 0 0
4844 : $x -> bone("-", @r);
4845 } elsif ($x -> is_positive()) {
4846 0 ▶ return $y -> is_odd() ? $x -> bone("-", @r)
Branch: 1 branch
BranchTrueFalseTotal
$y->is_odd ? : 0 0 0
4847 : $x -> bzero(@r);
4848 } else {
4849 0 return $x -> bzero(@r);
4850 }
4851 }
4852 }
4853  
4854 130 ▶ if ($b -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_zero) 0 130 130
4855 0 ▶ return $x -> round(@r) if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 0 0 0
4856 0 ▶ return $x -> bnan(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
4857 0 ▶ return $x -> is_negative() ? $x -> binf("-", @r)
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative ? : 0 0 0
4858 : $x -> binf("+", @r);
4859 }
4860  
4861 130 ▶ if ($y -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf("+")) 0 130 130
4862 0 ▶ if ($b -> is_one("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($b->is_one('-')) { } 0 0 0
elsif ($b->is_one('+')) { } 0 0 0
4863 0 return $x -> bnan(@r);
4864 } elsif ($b -> is_one("+")) {
4865 0 return $x -> round(@r);
4866 } else {
4867 0 ▶ return $x -> bnan(@r) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4868 0 ▶ return $x -> is_negative() ? $x -> bone("-", @r)
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative ? : 0 0 0
4869 : $x -> bzero(@r);
4870 }
4871 }
4872  
4873 130 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 130 130
4874 0 ▶ if ($b -> is_negative()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b->is_negative) { } 0 0 0
4875 0 ▶ if ($x -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 0 0 0
4876 0 ▶ if ($y -> is_even()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_even) { } 0 0 0
4877 0 return $x -> round(@r);
4878 } else {
4879 0 return $x -> binf("+", @r);
4880 }
4881 } else {
4882 0 ▶ if ($y -> is_even()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_even) { } 0 0 0
4883 0 return $x -> round(@r);
4884 } else {
4885 0 return $x -> binf("-", @r);
4886 }
4887 }
4888 } else {
4889 0 return $x -> round(@r);
4890 }
4891 }
4892  
4893 # At this point, we know that both the input and the output is finite.
4894 # Handle some trivial cases.
4895  
4896 130 ▶ return $x -> round(@r) if $x -> is_zero() || $y -> is_zero()
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $y->is_zero or $b->is_one("+") or $b->is_one("-") and $y->is_even 8 122 130
Condition58%7/12
$x->is_zero or $y->is_zero or $b->is_one("+") or $b->is_one("-") and $y->is_even
l!l&&r!l&&!r
80122
$x->is_zero or $y->is_zero or $b->is_one("+")
l!l&&r!l&&!r
80122
$x->is_zero or $y->is_zero
l!l&&r!l&&!r
08122
$b->is_one("-") and $y->is_even
!ll&&!rl&&r
12200
MC/DC20%1/5
$x->is_zero or $y->is_zero or $b->is_one("+") or $b->is_one("-") and $y->is_even
$x->is_zero $y->is_zero $b->is_one("+") $b->is_one("-") $y->is_even
Truth table33%2/6
A or B or C or D and E
A= $x->is_zeroB= $y->is_zeroC= $b->is_one("+")D= $b->is_one("-")E= $y->is_even
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
4897 || $b -> is_one("+")
4898 || $b -> is_one("-") && $y -> is_even();
4899  
4900 122 ▶ return $x -> bneg(@r) if $b -> is_one("-") && $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $b->is_one("-") and $y->is_odd 0 122 122
Condition33%1/3
$b->is_one("-") and $y->is_odd
!ll&&!rl&&r
12200
MC/DC0%0/2
$b->is_one("-") and $y->is_odd
$b->is_one("-") $y->is_odd
Truth table33%1/3
A and B
A= $b->is_one("-")B= $y->is_odd
ABresult
0X0
100
111
4901  
4902 # We know that $y is positive. Shifting right by a positive amount might
4903 # lead to a non-integer result.
4904  
4905 122 ▶ return $x -> _upg() -> brsft($y, $b, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 11 111 122
4906  
4907 # This only works for negative numbers when shifting in base 2.
4908 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
4909 57 ▶ return $x -> round(@r) if $x -> is_one('-'); # -1 => -1
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 3 54 57
4910 # Although this is O(N*N) in Math::BigInt::Calc->_as_bin(), it is O(N)
4911 # in Pari et al., but perhaps there is a better emulation for two's
4912 # complement shift ... if $y != 1, we must simulate it by doing:
4913 # convert to bin, flip all bits, shift, and be done
4914 54 $x -> binc(); # -3 => -2
4915 54 my $bin = $x -> to_bin(); # convert to string
4916 54 $bin =~ s/^-//; # strip leading minus
4917 54 $bin =~ tr/10/01/; # flip bits
4918 54 my $nbits = CORE::length($bin);
4919 54 ▶ return $x -> bone("-", @r) if $y >= $nbits;
Branch: 1 branch
BranchTrueFalseTotal
if $y >= $nbits 3 51 54
4920 51 $bin = substr $bin, 0, $nbits - $y; # keep most significant bits
4921 51 $bin = '1' . $bin; # prepend one dummy '1'
4922 51 $bin =~ tr/10/01/; # flip bits back
4923 51 my $res = $class -> from_bin($bin); # convert back from string
4924 51 $res -> binc(); # remember to increment
4925 51 $x -> {value} = $res -> {value}; # take over value
4926 51 return $x -> round(@r);
4927 }
4928  
4929 # While some of the libraries support an arbitrarily large base, not all of
4930 # them do, so rather than returning an incorrect result in those cases, use
4931 # division.
4932  
4933 54 my $uintmax = ~0;
4934 54 ▶ if ($x -> bcmp($uintmax) > 0 || $x -> is_neg() || $b -> is_negative()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bcmp($uintmax) > 0 or $x->is_neg or $b->is_negative) { } 0 54 54
Condition33%2/6
$x->bcmp($uintmax) > 0 or $x->is_neg or $b->is_negative
l!l&&r!l&&!r
0054
$x->bcmp($uintmax) > 0 or $x->is_neg
l!l&&r!l&&!r
0054
MC/DC0%0/3
$x->bcmp($uintmax) > 0 or $x->is_neg or $b->is_negative
$x->bcmp($uintmax) > 0 $x->is_neg $b->is_negative
Truth table25%1/4
A or B or C
A= $x->bcmp($uintmax) > 0B= $x->is_negC= $b->is_negative
ABCresult
1XX1
01X1
0011
0000
4935 0 $x -> bdiv($b -> bpow($y));
4936 } else {
4937 54 $b = $b -> numify();
4938 54 $x -> {value} = $LIB -> _rsft($x -> {value}, $y -> {value}, $b);
4939 }
4940  
4941 54 return $x -> round(@r);
4942 }
4943  
4944 ###############################################################################
4945 # Bitwise methods
4946 ###############################################################################
4947  
4948 # Bitwise left shift.
4949  
4950 35 ▶sub bblsft {
Subroutine bblsft: called CC=21 SCAR=39.9
Pod documented
4951 # We don't call objectify(), because the bitwise methods should not
4952 # upgrade, even when upgrading is enabled.
4953  
4954 35 my ($class, $x, $y, @r);
4955  
4956 # $x -> bblsft($y)
4957  
4958 35 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 27 8 35
4959 27 ($class, $x, $y, @r) = (ref($_[0]), @_);
4960 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
4961 if ref($y) && !$y -> isa(__PACKAGE__) && $y -> can('as_int');
4962 27 ▶ $y = $class -> new(int($y)) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 27 27
4963 }
4964  
4965 # $class -> bblsft($x, $y)
4966  
4967 else {
4968 8 ($class, $x, $y, @r) = @_;
4969 8 for ($x, $y) {
4970 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
4971 if ref($_) && !$_ -> isa(__PACKAGE__) && $_ -> can('as_int');
4972 16 ▶ $_ = $class -> new(int($_)) unless ref($_);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 16 16
4973 }
4974 }
4975  
4976 # Don't modify constant (read-only) objects.
4977  
4978 35 ▶ return $x if $x -> modify('bblsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bblsft") 0 35 35
4979  
4980 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
4981  
4982 # bblsft($x, -$y) = bbrsft($x, $y)
4983  
4984 31 ▶ return $x -> bbrsft($y -> copy() -> bneg()) if $y -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 4 27 31
4985  
4986 # Shifting infinitely far to the left.
4987  
4988 27 ▶ if ($y -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf("+")) 0 27 27
4989 0 ▶ return $x -> binf("+", @r) if $x -> is_pos();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_pos 0 0 0
4990 0 ▶ return $x -> binf("-", @r) if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
4991 0 return $x -> bnan(@r);
4992 }
4993  
4994 # These cases change nothing.
4995  
4996 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
4997 $y -> is_zero();
4998  
4999 27 $x -> {value} = $LIB -> _lsft($x -> {value}, $y -> {value}, 2);
5000 27 $x -> round(@r);
5001 }
5002  
5003 # Bitwise right shift.
5004  
5005 35 ▶sub bbrsft {
Subroutine bbrsft: called CC=23 SCAR=43.7
Pod documented
5006 # We don't call objectify(), because the bitwise methods should not
5007 # upgrade, even when upgrading is enabled.
5008  
5009 35 my ($class, $x, $y, @r);
5010  
5011 # $x -> bblsft($y)
5012  
5013 35 ▶ if (ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 27 8 35
5014 27 ($class, $x, $y, @r) = (ref($_[0]), @_);
5015 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
5016 if ref($y) && !$y -> isa(__PACKAGE__) && $y -> can('as_int');
5017 27 ▶ $y = $class -> new(int($y)) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 27 27
5018 }
5019  
5020 # $class -> bblsft($x, $y)
5021  
5022 else {
5023 8 ($class, $x, $y, @r) = @_;
5024 8 for ($x, $y) {
5025 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
5026 if ref($_) && !$_ -> isa(__PACKAGE__) && $_ -> can('as_int');
5027 16 ▶ $_ = $class -> new(int($_)) unless ref($_);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ 0 16 16
5028 }
5029 }
5030  
5031 # Don't modify constant (read-only) objects.
5032  
5033 35 ▶ return $x if $x -> modify('bbrsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bbrsft") 0 35 35
5034  
5035 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
5036  
5037 # bbrsft($x, -$y) = bblsft($x, $y)
5038  
5039 31 ▶ return $x -> bblsft($y -> copy() -> bneg()) if $y -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_neg 4 27 31
5040  
5041 # Shifting infinitely far to the right.
5042  
5043 27 ▶ if ($y -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf("+")) 0 27 27
5044 0 ▶ return $x -> bnan(@r) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
5045 0 ▶ return $x -> bone("-", @r) if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
5046 0 return $x -> bzero(@r);
5047 }
5048  
5049 # These cases change nothing.
5050  
5051 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
5052 $y -> is_zero();
5053  
5054 # At this point, $x is either positive or negative, not zero.
5055  
5056 27 ▶ if ($x -> is_pos()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_pos) { } 27 0 27
5057 27 $x -> {value} = $LIB -> _rsft($x -> {value}, $y -> {value}, 2);
5058 } else {
5059 0 my $n = $x -> {value};
5060 0 my $d = $LIB -> _pow($LIB -> _new("2"), $y -> {value});
5061 0 my ($p, $q) = $LIB -> _div($n, $d);
5062 0 ▶ $p = $LIB -> _inc($p) unless $LIB -> _is_zero($q);
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_zero($q) 0 0 0
5063 0 $x -> {value} = $p;
5064 }
5065  
5066 27 $x -> round(@r);
5067 }
5068  
5069 464 ▶sub band {
Subroutine band: called CC=11 SCAR=24.5
Pod documented
5070 #(BINT or num_str, BINT or num_str) return BINT
5071 # compute x & y
5072  
5073 464 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 463 1 464
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
01463
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
5074 ? (ref($_[0]), @_) : objectify(2, @_);
5075  
5076 # Don't modify constant (read-only) objects.
5077  
5078 464 ▶ return $x if $x -> modify('band');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("band") 0 464 464
5079  
5080 # If called with "foreign" arguments.
5081  
5082 464 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 1 463 464
5083 1 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 1 1
5084 0 $y = $y -> as_int();
5085 } else {
5086 1 ▶ return $x -> _upg() -> band($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 1 0 1
5087 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
5088 }
5089 }
5090  
5091 463 $r[3] = $y; # no push!
5092  
5093 # If $x and/or $y is Inf or NaN, return NaN.
5094  
5095 463 ▶ return $x -> bnan(@r) if !$x -> is_finite() || !$y -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite and $y->is_finite 12 451 463
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
84451
MC/DC100%2/2
$x->is_finite and $y->is_finite
$x->is_finite $y->is_finite
Truth table100%3/3
A and B
A= $x->is_finiteB= $y->is_finite
ABresult
0X0
100
111
5096  
5097 451 ▶ if ($x->{sign} eq '+' && $y->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '+' and $y->{'sign'} eq '+') { } 418 33 451
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "+"
!ll&&!rl&&r
249418
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
5098 418 $x->{value} = $LIB->_and($x->{value}, $y->{value});
5099 } else {
5100 ($x->{value}, $x->{sign}) = $LIB->_sand($x->{value}, $x->{sign},
5101 33 $y->{value}, $y->{sign});
5102 }
5103  
5104 451 return $x -> round(@r);
5105 }
5106  
5107 525 ▶sub bior {
Subroutine bior: called CC=11 SCAR=24.5
Pod documented
5108 #(BINT or num_str, BINT or num_str) return BINT
5109 # compute x | y
5110  
5111 525 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 524 1 525
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
01524
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
5112 ? (ref($_[0]), @_) : objectify(2, @_);
5113  
5114 # Don't modify constant (read-only) objects.
5115  
5116 525 ▶ return $x if $x -> modify('bior');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bior") 0 525 525
5117  
5118 # If called with "foreign" arguments.
5119  
5120 525 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 1 524 525
5121 1 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 1 1
5122 0 $y = $y -> as_int();
5123 } else {
5124 1 ▶ return $x -> _upg() -> bior($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 1 0 1
5125 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
5126 }
5127 }
5128  
5129 524 $r[3] = $y; # no push!
5130  
5131 # If $x and/or $y is Inf or NaN, return NaN.
5132  
5133 524 ▶ return $x -> bnan() if (!$x -> is_finite() || !$y -> is_finite());
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite and $y->is_finite 12 512 524
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
84512
MC/DC100%2/2
$x->is_finite and $y->is_finite
$x->is_finite $y->is_finite
Truth table100%3/3
A and B
A= $x->is_finiteB= $y->is_finite
ABresult
0X0
100
111
5134  
5135 512 ▶ if ($x->{sign} eq '+' && $y->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '+' and $y->{'sign'} eq '+') { } 477 35 512
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "+"
!ll&&!rl&&r
2312477
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
5136 477 $x->{value} = $LIB->_or($x->{value}, $y->{value});
5137 } else {
5138 ($x->{value}, $x->{sign}) = $LIB->_sor($x->{value}, $x->{sign},
5139 35 $y->{value}, $y->{sign});
5140 }
5141 512 return $x -> round(@r);
5142 }
5143  
5144 535 ▶sub bxor {
Subroutine bxor: called CC=11 SCAR=24.5
Pod documented
5145 #(BINT or num_str, BINT or num_str) return BINT
5146 # compute x ^ y
5147  
5148 535 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 534 1 535
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
01534
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
5149 ? (ref($_[0]), @_) : objectify(2, @_);
5150  
5151 # Don't modify constant (read-only) objects.
5152  
5153 535 ▶ return $x if $x -> modify('bxor');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bxor") 0 535 535
5154  
5155 # If called with "foreign" arguments.
5156  
5157 535 ▶ unless ($y -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->isa(__PACKAGE__)) 1 534 535
5158 1 ▶ if ($y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int) { } 0 1 1
5159 0 $y = $y -> as_int();
5160 } else {
5161 1 ▶ return $x -> _upg() -> bxor($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 1 0 1
5162 0 croak "Can't handle a ", ref($y), " in ", (caller(0))[3], "()";
5163 }
5164 }
5165  
5166 534 $r[3] = $y; # no push!
5167  
5168 # If $x and/or $y is Inf or NaN, return NaN.
5169  
5170 534 ▶ return $x -> bnan(@r) if !$x -> is_finite() || !$y -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite and $y->is_finite 12 522 534
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
84522
MC/DC100%2/2
$x->is_finite and $y->is_finite
$x->is_finite $y->is_finite
Truth table100%3/3
A and B
A= $x->is_finiteB= $y->is_finite
ABresult
0X0
100
111
5171  
5172 522 ▶ if ($x->{sign} eq '+' && $y->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '+' and $y->{'sign'} eq '+') { } 482 40 522
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "+"
!ll&&!rl&&r
2713482
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
5173 482 $x->{value} = $LIB->_xor($x->{value}, $y->{value});
5174 } else {
5175 ($x->{value}, $x->{sign}) = $LIB->_sxor($x->{value}, $x->{sign},
5176 40 $y->{value}, $y->{sign});
5177 }
5178 522 return $x -> round(@r);
5179 }
5180  
5181 39 ▶sub bnot {
Subroutine bnot: called CC=3 SCAR=11.7
Pod documented
5182 # (num_str or BINT) return BINT
5183 # represent ~x as twos-complement number
5184 39 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 39 0 39
5185  
5186 # Don't modify constant (read-only) objects.
5187  
5188 39 ▶ return $x if $x -> modify('bnot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bnot") 0 39 39
5189  
5190 39 $x -> binc() -> bneg(@r);
5191 }
5192  
5193 ###############################################################################
5194 # Rounding methods
5195 ###############################################################################
5196  
5197 90021 ▶sub round {
Subroutine round: called CC=37 SCAR=36.2
Pod documented
5198 # Round $self according to given parameters, or given second argument's
5199 # parameters or global defaults
5200 90021 ▶ my ($class, $self, @args) = ref($_[0]) ? (ref($_[0]), @_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 90021 0 90021
5201 : objectify(1, @_);
5202  
5203 # These signal no rounding:
5204 #
5205 # $x->round(undef)
5206 # $x->round(undef, undef, ...)
5207 #
5208 # The "@args <= 3" is necessary because the final set of parameters that
5209 # will be used for rounding depend on the "extra arguments", if any.
5210  
5211 90021 ▶ if (@args == 1 && !defined($args[0]) ||
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and not defined $args[0] or @args >= 2 and @args <= 3 and not defined $args[0] and not defined $args[1]) 70 89951 90021
Condition100%15/15
@args == 1 and not defined $args[0] or @args >= 2 and @args <= 3 and not defined $args[0] and not defined $args[1]
l!l&&r!l&&!r
224889951
@args == 1 and not defined $args[0]
!ll&&!rl&&r
8942957022
@args >= 2 and @args <= 3 and not defined $args[0] and not defined $args[1]
!ll&&!rl&&r
898906148
@args >= 2 and @args <= 3 and not defined $args[0]
!ll&&!rl&&r
8980189109
@args >= 2 and @args <= 3
!ll&&!rl&&r
4681442987198
MC/DC100%6/6
@args == 1 and not defined $args[0] or @args >= 2 and @args <= 3 and not defined $args[0] and not defined $args[1]
@args == 1 not defined $args[0] @args >= 2 @args <= 3 not defined $args[0] not defined $args[1]
Truth table63%7/11
A and not(B) or C and D and not(E) and not(F)
A= @args == 1B= not defined $args[0]C= @args >= 2D= @args <= 3E= not defined $args[0]F= not defined $args[1]
ABCDEFresult
11XXXX1
0X11111
1011111
0X0XXX0
0X10XX0
0X110X0
0X11100
100XXX0
1010XX0
10110X0
1011100
5212 @args >= 2 && @args <= 3 && !defined($args[0]) && !defined($args[1]))
5213 {
5214 70 $self->{accuracy} = undef;
5215 70 $self->{precision} = undef;
5216 70 return $self;
5217 }
5218  
5219 89951 my ($a, $p, $r) = splice @args, 0, 3;
5220  
5221 # $a accuracy, if given by caller
5222 # $p precision, if given by caller
5223 # $r round_mode, if given by caller
5224 # @args all 'other' arguments (0 for unary, 1 for binary ops)
5225  
5226 89951 ▶ if (defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a) 1842 88109 89951
5227 1842 ▶ croak "accuracy must be a number, not '$a'"
Branch: 1 branch
BranchTrueFalseTotal
unless $a =~ /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/ 0 1842 1842
5228 unless $a =~/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/;
5229 }
5230  
5231 89951 ▶ if (defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p) 94 89857 89951
5232 94 ▶ croak "precision must be a number, not '$p'"
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/ 0 94 94
5233 unless $p =~/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[Ee][+-]?\d+)?\z/;
5234 }
5235  
5236 # now pick $a or $p, but only if we have got "arguments"
5237 89951 ▶ if (!defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a) 88109 1842 89951
5238 88109 foreach ($self, @args) {
5239 # take the defined one, or if both defined, the one that is smaller
5240 $a = $_->{accuracy}
5241 129913 ▶ if (defined $_->{accuracy}) && (!defined $a || $_->{accuracy} < $a);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"accuracy"} and !defined($a) || $_->{"accuracy"} < $a 24121 105792 129913
Condition100%6/6
defined $_->{"accuracy"} and !defined($a) || $_->{"accuracy"} < $a
!ll&&!rl&&r
908251496724121
not defined $a or $_->{"accuracy"} < $a
l!l&&r!l&&!r
241091214967
MC/DC100%3/3
defined $_->{"accuracy"} and !defined($a) || $_->{"accuracy"} < $a
defined $_->{"accuracy"} not defined $a $_->{"accuracy"} < $a
Truth table100%4/4
A and not(B) or C
A= defined $_->{"accuracy"}B= not defined $aC= $_->{"accuracy"} < $a
ABCresult
0XX0
1000
11X1
1011
5242 }
5243 }
5244 89951 ▶ if (!defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p) 89857 94 89951
5245 # even if $a is defined, take $p, to signal error for both defined
5246 89857 foreach ($self, @args) {
5247 # take the defined one, or if both defined, the one that is bigger
5248 # -2 > -3, and 3 > 2
5249 $p = $_->{precision}
5250 132829 ▶ if (defined $_->{precision}) && (!defined $p || $_->{precision} > $p);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"precision"} and !defined($p) || $_->{"precision"} > $p 136 132693 132829
Condition83%5/6
defined $_->{"precision"} and !defined($p) || $_->{"precision"} > $p
!ll&&!rl&&r
1326903136
not defined $p or $_->{"precision"} > $p
l!l&&r!l&&!r
13603
MC/DC66%2/3
defined $_->{"precision"} and !defined($p) || $_->{"precision"} > $p
defined $_->{"precision"} not defined $p $_->{"precision"} > $p
Truth table75%3/4
A and not(B) or C
A= defined $_->{"precision"}B= not defined $pC= $_->{"precision"} > $p
ABCresult
0XX0
1000
11X1
1011
5251 }
5252 }
5253  
5254 # if still none defined, use globals
5255 89951 ▶ unless (defined $a || defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a or defined $p) 63818 26133 89951
Condition100%3/3
defined $a or defined $p
l!l&&r!l&&!r
2595118263818
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
5256 63818 $a = $class -> accuracy();
5257 63818 $p = $class -> precision();
5258 }
5259  
5260 # A == 0 is useless, so undef it to signal no rounding
5261 89951 ▶ $a = undef if defined $a && $a == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and $a == 0 18 89933 89951
Condition100%3/3
defined $a and $a == 0
!ll&&!rl&&r
639642597215
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
5262  
5263 # no rounding today?
5264 89951 ▶ return $self unless defined $a || defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a or defined $p 63782 26169 89951
Condition100%3/3
defined $a or defined $p
l!l&&r!l&&!r
2596920063782
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
5265  
5266 # set A and set P is an fatal error
5267 26169 ▶ if (defined $a && defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a and defined $p) 58 26111 26169
Condition100%3/3
defined $a and defined $p
!ll&&!rl&&r
2002591158
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
5268 #carp "can't specify both accuracy and precision";
5269 58 return $self -> bnan();
5270 }
5271  
5272 # Infs and NaNs are not rounded, but assign rounding parameters to them.
5273 #
5274 #if ($self -> is_inf() || $self -> is_nan()) {
5275 # $self->{accuracy} = $a;
5276 # $self->{precision} = $p;
5277 # return $self;
5278 #}
5279  
5280 26111 ▶ $r = $class -> round_mode() unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 26027 84 26111
5281 26111 ▶ if ($r !~ /^(even|odd|[+-]inf|zero|trunc|common)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r =~ /^(even|odd|[+-]inf|zero|trunc|common)$/) 0 26111 26111
5282 0 croak("Unknown round mode '$r'");
5283 }
5284  
5285 # now round, by calling either bround or bfround:
5286 26111 ▶ if (defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $a) { } 25911 200 26111
5287 $self -> bround(int($a), $r)
5288 25911 ▶ if !defined $self->{accuracy} || $self->{accuracy} >= $a;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"accuracy"} or $self->{"accuracy"} >= $a 25853 58 25911
Condition100%3/3
not defined $self->{"accuracy"} or $self->{"accuracy"} >= $a
l!l&&r!l&&!r
5082534558
MC/DC100%2/2
not defined $self->{"accuracy"} or $self->{"accuracy"} >= $a
not defined $self->{"accuracy"} $self->{"accuracy"} >= $a
Truth table100%3/3
not(A) or B
A= not defined $self->{"accuracy"}B= $self->{"accuracy"} >= $a
ABresult
1X1
011
000
5289 } else { # both can't be undefined due to early out
5290 $self -> bfround(int($p), $r)
5291 200 ▶ if !defined $self->{precision} || $self->{precision} <= $p;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $self->{"precision"} or $self->{"precision"} <= $p 196 4 200
Condition100%3/3
not defined $self->{"precision"} or $self->{"precision"} <= $p
l!l&&r!l&&!r
123734
MC/DC100%2/2
not defined $self->{"precision"} or $self->{"precision"} <= $p
not defined $self->{"precision"} $self->{"precision"} <= $p
Truth table100%3/3
not(A) or B
A= not defined $self->{"precision"}B= $self->{"precision"} <= $p
ABresult
1X1
011
000
5292 }
5293  
5294 # bround() or bfround() already called bnorm() if nec.
5295 26111 $self;
5296 }
5297  
5298 29306 ▶sub bround {
Subroutine bround: called CC=45 SCAR=38.1
Pod documented
5299 # accuracy: +$n preserve $n digits from left,
5300 # -$n preserve $n digits from right (f.i. for 0.1234 style in MBF)
5301 # no-op for $n == 0
5302 # and overwrite the rest with 0's, return normalized number
5303 # do not return $x->bnorm(), but $x
5304  
5305 29306 ▶ my ($class, $x, @a) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 29306 0 29306
5306  
5307 # Don't modify constant (read-only) objects.
5308  
5309 29306 ▶ return $x if $x -> modify('bround');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bround") 0 29306 29306
5310  
5311 29306 my ($scale, $mode) = $x->_scale_a(@a);
5312 29306 ▶ return $x if !defined $scale; # no-op
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 2 29304 29306
5313  
5314 29304 ▶ if ($x -> is_zero() || $scale == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero or $scale == 0) 101 29203 29304
Condition100%3/3
$x->is_zero or $scale == 0
l!l&&r!l&&!r
821929203
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
5315 $x->{accuracy} = $scale
5316 101 ▶ if !defined $x->{accuracy} || $x->{accuracy} > $scale; # 3 > 2
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x->{"accuracy"} or $x->{"accuracy"} > $scale 89 12 101
Condition66%2/3
not defined $x->{"accuracy"} or $x->{"accuracy"} > $scale
l!l&&r!l&&!r
89012
MC/DC50%1/2
not defined $x->{"accuracy"} or $x->{"accuracy"} > $scale
not defined $x->{"accuracy"} $x->{"accuracy"} > $scale
Truth table66%2/3
not(A) or B
A= not defined $x->{"accuracy"}B= $x->{"accuracy"} > $scale
ABresult
1X1
011
000
5317 101 return $x;
5318 }
5319 29203 ▶ return $x if !$x -> is_finite(); # inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 12 29191 29203
5320  
5321 # we have fewer digits than we want to scale to
5322 29191 my $len = $x -> length();
5323 # convert $scale to a scalar in case it is an object (put's a limit on the
5324 # number length, but this would already limited by memory constraints),
5325 # makes it faster
5326 29191 ▶ $scale = $scale -> numify() if ref ($scale);
Branch: 1 branch
BranchTrueFalseTotal
if ref $scale 0 29191 29191
5327  
5328 # scale < 0, but > -len (not >=!)
5329 29191 ▶ if (($scale < 0 && $scale < -$len-1) || ($scale >= $len)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < 0 and $scale < -$len - 1 or $scale >= $len) 216 28975 29191
Condition100%6/6
$scale < 0 and $scale < -$len - 1 or $scale >= $len
l!l&&r!l&&!r
121528975
$scale < 0 and $scale < -$len - 1
!ll&&!rl&&r
290531371
MC/DC100%3/3
$scale < 0 and $scale < -$len - 1 or $scale >= $len
$scale < 0 $scale < -$len - 1 $scale >= $len
Truth table80%4/5
A and B or C
A= $scale < 0B= $scale < -$len - 1C= $scale >= $len
ABCresult
11X1
0X11
1011
0X00
1000
5330 $x->{accuracy} = $scale
5331 216 ▶ if !defined $x->{accuracy} || $x->{accuracy} > $scale; # 3 > 2
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x->{"accuracy"} or $x->{"accuracy"} > $scale 182 34 216
Condition100%3/3
not defined $x->{"accuracy"} or $x->{"accuracy"} > $scale
l!l&&r!l&&!r
181134
MC/DC100%2/2
not defined $x->{"accuracy"} or $x->{"accuracy"} > $scale
not defined $x->{"accuracy"} $x->{"accuracy"} > $scale
Truth table100%3/3
not(A) or B
A= not defined $x->{"accuracy"}B= $x->{"accuracy"} > $scale
ABresult
1X1
011
000
5332 216 return $x;
5333 }
5334  
5335 # count of 0's to pad, from left (+) or right (-): 9 - +6 => 3, or |-6| => 6
5336 28975 my ($pad, $digit_round, $digit_after);
5337 28975 $pad = $len - $scale;
5338 28975 ▶ $pad = abs($scale-1) if $scale < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $scale < 0 137 28838 28975
5339  
5340 # do not use digit(), it is very costly for binary => decimal
5341 # getting the entire string is also costly, but we need to do it only once
5342 28975 my $xs = $LIB->_str($x->{value});
5343 28975 my $pl = -$pad-1;
5344  
5345 # pad: 123: 0 => -1, at 1 => -2, at 2 => -3, at 3 => -4
5346 # pad+1: 123: 0 => 0, at 1 => -1, at 2 => -2, at 3 => -3
5347 28975 $digit_round = '0';
5348 28975 ▶ $digit_round = substr($xs, $pl, 1) if $pad <= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $pad <= $len 28851 124 28975
5349 28975 $pl++;
5350 28975 ▶ $pl ++ if $pad >= $len;
Branch: 1 branch
BranchTrueFalseTotal
if $pad >= $len 128 28847 28975
5351 28975 $digit_after = '0';
5352 28975 ▶ $digit_after = substr($xs, $pl, 1) if $pad > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pad > 0 28975 0 28975
5353  
5354 # in case of 01234 we round down, for 6789 up, and only in case 5 we look
5355 # closer at the remaining digits of the original $x, remember decision
5356 28975 my $round_up = 1; # default round up
5357 $round_up -- if
5358 ($mode eq 'trunc') || # trunc by round down
5359 ($digit_after =~ /[01234]/) || # round down anyway,
5360 # 6789 => round up
5361 ($digit_after eq '5') && # not 5000...0000
5362 ($x->_scan_for_nonzero($pad, $xs, $len) == 0) &&
5363 (
5364 ($mode eq 'even') && ($digit_round =~ /[24680]/) ||
5365 ($mode eq 'odd') && ($digit_round =~ /[13579]/) ||
5366 ($mode eq '+inf') && ($x->{sign} eq '-') ||
5367 28975 ▶ ($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" 15030 13945 28975
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
1460342713945
$mode eq "trunc" or $digit_after =~ /[01234]/
l!l&&r!l&&!r
1481445514372
$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
13554391427
$digit_after eq 5 and $x->_scan_for_nonzero($pad, $xs, $len) == 0
!ll&&!rl&&r
110342520818
$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
37651391
$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
34828442
$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
32424470
$mode eq "even" and $digit_round =~ /[24680]/ or $mode eq "odd" and $digit_round =~ /[13579]/
l!l&&r!l&&!r
27153494
$mode eq "even" and $digit_round =~ /[24680]/
!ll&&!rl&&r
266281271
$mode eq "odd" and $digit_round =~ /[13579]/
!ll&&!rl&&r
4454953
$mode eq "+inf" and $x->{"sign"} eq "-"
!ll&&!rl&&r
4432724
$mode eq "-inf" and $x->{"sign"} eq "+"
!ll&&!rl&&r
4222028
MC/DC100%13/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 table27%14/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
5368 ($mode eq 'zero') # round down if zero, sign adjusted below
5369 );
5370 28975 my $put_back = 0; # not yet modified
5371  
5372 28975 ▶ if (($pad > 0) && ($pad <= $len)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pad > 0 and $pad <= $len) { } 28851 124 28975
elsif ($pad > $len) { } 124 0 124
Condition66%2/3
$pad > 0 and $pad <= $len
!ll&&!rl&&r
012428851
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
5373 28851 substr($xs, -$pad, $pad) = '0' x $pad; # replace with '00...'
5374 28851 $xs =~ s/^0+(\d)/$1/; # "00000" -> "0"
5375 28851 $put_back = 1; # need to put back
5376 } elsif ($pad > $len) {
5377 124 $x -> bzero(); # round to '0'
5378 }
5379  
5380 28975 ▶ if ($round_up) { # what gave test above?
Branch: 1 branch
BranchTrueFalseTotal
if ($round_up) 13945 15030 28975
5381 13945 $put_back = 1; # need to put back
5382 13945 ▶ $pad = $len, $xs = '0' x $pad if $scale < 0; # tlr: whack 0.51=>1.0
Branch: 1 branch
BranchTrueFalseTotal
if $scale < 0 59 13886 13945
5383  
5384 # we modify directly the string variant instead of creating a number
5385 # and adding it, since that is faster (we already have the string)
5386 13945 my $c = 0;
5387 13945 $pad ++; # for $pad == $len case
5388 13945 ▶ while ($pad <= $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pad <= $len) 15624 61 15685
5389 15624 $c = substr($xs, -$pad, 1) + 1;
5390 15624 ▶ $c = '0' if $c eq '10';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq 10 1740 13884 15624
5391 15624 substr($xs, -$pad, 1) = $c;
5392 15624 $pad++;
5393 15624 ▶ last if $c != 0; # no overflow => early out
Branch: 1 branch
BranchTrueFalseTotal
if $c != 0 13884 1740 15624
5394 }
5395 13945 ▶ $xs = '1'.$xs if $c == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c == 0 61 13884 13945
5396 }
5397 28975 ▶ $x->{value} = $LIB->_new($xs) if $put_back == 1; # put back, if needed
Branch: 1 branch
BranchTrueFalseTotal
if $put_back == 1 28910 65 28975
5398  
5399 28975 ▶ $x->{accuracy} = $scale if $scale >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $scale >= 0 28838 137 28975
5400 28975 ▶ if ($scale < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < 0) 137 28838 28975
5401 137 $x->{accuracy} = $len+$scale;
5402 137 ▶ $x->{accuracy} = 0 if $scale < -$len;
Branch: 1 branch
BranchTrueFalseTotal
if $scale < -$len 5 132 137
5403 }
5404 28975 $x;
5405 }
5406  
5407 219 ▶sub bfround {
Subroutine bfround: called CC=5 SCAR=16.4
Pod documented
5408 # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.'
5409 # $n == 0 || $n == 1 => round to integer
5410  
5411 219 ▶ my ($class, $x, @p) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 219 0 219
5412  
5413 # Don't modify constant (read-only) objects.
5414  
5415 219 ▶ return $x if $x -> modify('bfround');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfround") 0 219 219
5416  
5417 219 my ($scale, $mode) = $x->_scale_p(@p);
5418  
5419 219 ▶ return $x if !defined $scale;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $scale 0 219 219
5420  
5421 # no-op for Math::BigInt objects if $n <= 0
5422 219 ▶ $x = $x -> bround($x -> length() - $scale, $mode) if $scale > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $scale > 0 108 111 219
5423  
5424 219 $x->{accuracy} = undef;
5425 219 $x->{precision} = $scale; # store new precision
5426 219 $x;
5427 }
5428  
5429 0 ▶sub fround {
Subroutine fround: not called CC=2 SCAR=17.9
Pod undocumented
5430 # Exists to make life easier for switch between MBF and MBI (should we
5431 # autoload fxxx() like MBF does for bxxx()?)
5432 0 my $x = shift;
5433 0 ▶ $x = __PACKAGE__ -> new($x) unless ref $x;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 0 0 0
5434 0 $x -> bround(@_);
5435 }
5436  
5437 36 ▶sub bfloor {
Subroutine bfloor: called CC=3 SCAR=11.7
Pod documented
5438 # round towards minus infinity; no-op since it's already integer
5439 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
5440  
5441 # Don't modify constant (read-only) objects.
5442  
5443 36 ▶ return $x if $x -> modify('bfloor');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfloor") 0 36 36
5444  
5445 36 $x -> round(@r);
5446 }
5447  
5448 36 ▶sub bceil {
Subroutine bceil: called CC=3 SCAR=11.7
Pod documented
5449 # round towards plus infinity; no-op since it's already int
5450 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
5451  
5452 # Don't modify constant (read-only) objects.
5453  
5454 36 ▶ return $x if $x -> modify('bceil');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bceil") 0 36 36
5455  
5456 36 $x -> round(@r);
5457 }
5458  
5459 38 ▶sub bint {
Subroutine bint: called CC=3 SCAR=11.7
Pod documented
5460 # round towards zero; no-op since it's already integer
5461 38 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 38 0 38
5462  
5463 # Don't modify constant (read-only) objects.
5464  
5465 38 ▶ return $x if $x -> modify('bint');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bint") 0 38 38
5466  
5467 38 $x->round(@r);
5468 }
5469  
5470 ###############################################################################
5471 # Other mathematical methods
5472 ###############################################################################
5473  
5474 97 ▶sub bgcd {
Subroutine bgcd: called CC=14 SCAR=27.5
Pod documented
5475 # GCD -- Euclid's algorithm, variant C (Knuth Vol 3, pg 341 ff)
5476  
5477 # Class::method(...) -> Class->method(...)
5478 97 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)) 1 96 97
Condition75%9/12
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
!ll&&!rl&&r
0196
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
l!l&&r!l&&!r
17791
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
80017
$_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
!ll&&!rl&&r
1079
MC/DC40%2/5
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i not $_[0] =~ /^(inf|nan)/i
Truth table37%3/8
A and B and C or D and not(E)
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/iE= not $_[0] =~ /^(inf|nan)/i
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
5479 ($_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i &&
5480 $_[0] !~ /^(inf|nan)/i)))
5481 {
5482 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
5483 # " use is as a method instead";
5484 1 unshift @_, __PACKAGE__;
5485 }
5486  
5487 97 my ($class, @args) = objectify(0, @_);
5488  
5489 # Pre-process list of operands.
5490  
5491 97 for my $arg (@args) {
5492 181 ▶ return $class -> bnan() unless $arg -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->is_int 33 148 181
5493 }
5494  
5495 # Upgrade?
5496  
5497 64 my $upg = $class -> upgrade();
5498 64 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 12 52 64
5499 12 my $do_upgrade = 0;
5500 12 for my $arg (@args) {
5501 26 ▶ unless ($arg -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg->isa(__PACKAGE__)) 0 26 26
5502 0 $do_upgrade = 1;
5503 0 last;
5504 }
5505 }
5506 12 ▶ if ($do_upgrade) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_upgrade) 0 12 12
5507 0 my $x = shift @args;
5508 0 $x -> _upg();
5509 0 return $x -> bgcd(@args);
5510 }
5511 }
5512  
5513 64 my $x = shift @args;
5514 64 $x = $x -> copy(); # bgcd() and blcm() never modify any operands
5515 64 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 74 29 103
5516 74 my $y = shift @args;
5517 74 $x->{value} = $LIB->_gcd($x->{value}, $y->{value});
5518 74 ▶ last if $LIB->_is_one($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_one($x->{"value"}) 35 39 74
5519 }
5520  
5521 64 return $x -> babs();
5522 }
5523  
5524 35 ▶sub blcm {
Subroutine blcm: called CC=16 SCAR=29.1
Pod documented
5525 # Least Common Multiple
5526  
5527 # Class::method(...) -> Class->method(...)
5528 35 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)) 1 34 35
Condition75%9/12
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
!ll&&!rl&&r
0134
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
l!l&&r!l&&!r
9251
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
2609
$_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
!ll&&!rl&&r
1025
MC/DC40%2/5
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i not $_[0] =~ /^(inf|nan)/i
Truth table37%3/8
A and B and C or D and not(E)
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/iE= not $_[0] =~ /^(inf|nan)/i
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
5529 ($_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i &&
5530 $_[0] !~ /^(inf|nan)/i)))
5531 {
5532 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
5533 # " use is as a method instead";
5534 1 unshift @_, __PACKAGE__;
5535 }
5536  
5537 35 my ($class, @args) = objectify(0, @_);
5538  
5539 # Pre-process list of operands.
5540  
5541 35 for my $arg (@args) {
5542 65 ▶ return $class -> bnan() unless $arg -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->is_finite 12 53 65
5543 }
5544  
5545 23 for my $arg (@args) {
5546 41 ▶ return $class -> bzero() if $arg -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $arg->is_zero 12 29 41
5547 }
5548  
5549 # Upgrade?
5550  
5551 11 my $upg = $class -> upgrade();
5552 11 ▶ if ($upg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($upg) 2 9 11
5553 2 my $do_upgrade = 0;
5554 2 for my $arg (@args) {
5555 4 ▶ unless ($arg -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($arg->isa(__PACKAGE__)) 0 4 4
5556 0 $do_upgrade = 1;
5557 0 last;
5558 }
5559 }
5560 2 ▶ if ($do_upgrade) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_upgrade) 0 2 2
5561 0 my $x = shift @args;
5562 0 $x -> _upg();
5563 0 return $x -> bgcd(@args);
5564 }
5565 }
5566  
5567 11 my $x = shift @args;
5568 11 $x = $x -> copy(); # bgcd() and blcm() never modify any operands
5569  
5570 11 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 14 11 25
5571 14 my $y = shift @args;
5572 14 ▶ return $x -> bnan() if !$y -> is_int(); # is $y not integer?
Branch: 1 branch
BranchTrueFalseTotal
unless $y->is_int 0 14 14
5573 14 $x -> {value} = $LIB->_lcm($x -> {value}, $y -> {value});
5574 }
5575  
5576 11 return $x -> babs();
5577 }
5578  
5579 ###############################################################################
5580 # Object property methods
5581 ###############################################################################
5582  
5583 923 ▶sub sign {
Subroutine sign: called CC=3 SCAR=11.7
Pod documented
5584 # return the sign of the number: +/-/-inf/+inf/NaN
5585 923 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 923 0 923
5586  
5587 923 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 923 923
5588  
5589 923 $x->{sign};
5590 }
5591  
5592 87 ▶sub digit {
Subroutine digit: called CC=5 SCAR=16.1
Pod documented
5593 # return the nth decimal digit, negative values count backward, 0 is right
5594 87 ▶ my (undef, $x, $n, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 84 3 87
5595  
5596 87 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 87 87
5597  
5598 87 ▶ $n = $n -> numify() if ref($n);
Branch: 1 branch
BranchTrueFalseTotal
if ref $n 63 24 87
5599 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
5600 }
5601  
5602 0 ▶sub bdigitsum {
Subroutine bdigitsum: not called CC=6 SCAR=37.4
Pod documented
5603 # like digitsum(), but assigns the result to the invocand
5604 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5605  
5606 # Don't modify constant (read-only) objects.
5607  
5608 0 ▶ return $x if $x -> modify('bdigitsum');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdigitsum") 0 0 0
5609  
5610 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5611  
5612 0 ▶ return $x if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
5613 0 ▶ return $x -> bnan() if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
5614  
5615 0 $x -> {value} = $LIB -> _digitsum($x -> {value});
5616 0 $x -> {sign} = '+';
5617 0 return $x;
5618 }
5619  
5620 0 ▶sub digitsum {
Subroutine digitsum: not called CC=5 SCAR=34.0
Pod documented
5621 # compute sum of decimal digits and return it
5622 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5623  
5624 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5625  
5626 0 ▶ return $class -> bnan() if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
5627 0 ▶ return $class -> bnan() if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
5628  
5629 0 my $y = $class -> bzero();
5630 0 $y -> {value} = $LIB -> _digitsum($x -> {value});
5631 0 $y -> round(@r);
5632 }
5633  
5634 29343 ▶sub length {
Subroutine length: called CC=4 SCAR=14.2
Pod documented
5635 29343 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 29343 0 29343
5636  
5637 29343 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 29343 29343
5638  
5639 29343 my $e = $LIB->_len($x->{value});
5640 29343 ▶ wantarray ? ($e, 0) : $e;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 29337 29343
5641 }
5642  
5643 68 ▶sub mantissa {
Subroutine mantissa: called CC=5 SCAR=16.2
Pod documented
5644 # return the mantissa (compatible to Math::BigFloat, e.g. reduced)
5645 68 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 68 0 68
5646  
5647 68 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 68 68
5648  
5649 68 ▶ if (!$x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 24 44 68
5650 # for NaN, +inf, -inf: keep the sign
5651 24 return $class -> new($x->{sign}, @r);
5652 }
5653 44 my $m = $x -> copy();
5654 44 $m -> precision(undef);
5655 44 $m -> accuracy(undef);
5656  
5657 # that's a bit inefficient:
5658 44 my $zeros = $LIB->_zeros($m->{value});
5659 44 ▶ $m = $m -> brsft($zeros, 10) if $zeros != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $zeros != 0 8 36 44
5660 44 $m -> round(@r);
5661 }
5662  
5663 72 ▶sub exponent {
Subroutine exponent: called CC=5 SCAR=16.2
Pod documented
5664 # return a copy of the exponent (here always 0, NaN or 1 for $m == 0)
5665 72 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 72 0 72
5666  
5667 72 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 72 72
5668  
5669 72 ▶ if (!$x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 24 48 72
5670 24 my $s = $x->{sign};
5671 24 $s =~ s/^[+-]//; # NaN, -inf, +inf => NaN or inf
5672 24 return $class -> new($s, @r);
5673 }
5674 48 ▶ return $class -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 40 48
5675  
5676 # 12300 => 2 trailing zeros => exponent is 2
5677 40 $class -> new($LIB->_zeros($x->{value}), @r);
5678 }
5679  
5680 36 ▶sub parts {
Subroutine parts: called CC=3 SCAR=11.7
Pod documented
5681 # return a copy of both the exponent and the mantissa
5682 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
5683  
5684 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
5685  
5686 36 ($x -> mantissa(@r), $x -> exponent(@r));
5687 }
5688  
5689 # Parts used for scientific notation with significand/mantissa and exponent as
5690 # integers. E.g., "12345.6789" is returned as "123456789" (mantissa) and "-4"
5691 # (exponent).
5692  
5693 0 ▶sub sparts {
Subroutine sparts: not called CC=9 SCAR=45.0
Pod documented
5694 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5695  
5696 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5697  
5698 # Not-a-number.
5699  
5700 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
5701 0 my $mant = $class -> bnan(@r); # mantissa
5702 0 ▶ return $mant unless wantarray; # scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5703 0 my $expo = $class -> bnan(@r); # exponent
5704 0 return $mant, $expo; # list context
5705 }
5706  
5707 # Infinity.
5708  
5709 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
5710 0 my $mant = $class -> binf($x->{sign}, @r); # mantissa
5711 0 ▶ return $mant unless wantarray; # scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5712 0 my $expo = $class -> binf('+', @r); # exponent
5713 0 return $mant, $expo; # list context
5714 }
5715  
5716 # Finite number.
5717  
5718 0 my $mant = $x -> copy();
5719 0 my $nzeros = $LIB -> _zeros($mant -> {value});
5720  
5721 $mant -> {value}
5722 0 ▶ = $LIB -> _rsft($mant -> {value}, $LIB -> _new($nzeros), 10)
Branch: 1 branch
BranchTrueFalseTotal
if $nzeros != 0 0 0 0
5723 if $nzeros != 0;
5724 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5725  
5726 0 my $expo = $class -> new($nzeros, @r);
5727 0 return $mant, $expo;
5728 }
5729  
5730 # Parts used for normalized notation with significand/mantissa as either 0 or a
5731 # number in the semi-open interval [1,10). E.g., "12345.6789" is returned as
5732 # "1.23456789" and "4".
5733  
5734 0 ▶sub nparts {
Subroutine nparts: not called CC=10 SCAR=47.0
Pod documented
5735 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5736  
5737 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5738  
5739 # Not-a-Number and Infinity.
5740  
5741 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
5742  
5743 # Finite number.
5744  
5745 0 my ($mant, $expo) = $x -> sparts(@r);
5746 0 ▶ if ($mant -> bcmp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant->bcmp(0)) 0 0 0
5747 0 my ($ndigtot, $ndigfrac) = $mant -> length();
5748 0 my $expo10adj = $ndigtot - $ndigfrac - 1;
5749  
5750 0 ▶ if ($expo10adj > 0) { # if mantissa is not an integer
Branch: 1 branch
BranchTrueFalseTotal
if ($expo10adj > 0) 0 0 0
5751 0 ▶ return $x -> _upg() -> nparts(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
5752 0 $mant -> bnan(@r);
5753 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5754 0 $expo -> badd($expo10adj, @r);
5755 0 return $mant, $expo;
5756 }
5757 }
5758  
5759 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5760 0 return $mant, $expo;
5761 }
5762  
5763 # Parts used for engineering notation with significand/mantissa as either 0 or
5764 # a number in the semi-open interval [1,1000) and the exponent is a multiple of
5765 # 3. E.g., "12345.6789" is returned as "12.3456789" and "3".
5766  
5767 0 ▶sub eparts {
Subroutine eparts: not called CC=10 SCAR=47.0
Pod documented
5768 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5769  
5770 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5771  
5772 # Not-a-number and Infinity.
5773  
5774 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
5775  
5776 # Finite number.
5777  
5778 0 my ($mant, $expo) = $x -> sparts(@r);
5779  
5780 0 ▶ if ($mant -> bcmp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant->bcmp(0)) 0 0 0
5781 0 my $ndigmant = $mant -> length();
5782 0 $expo -> badd($ndigmant, @r);
5783  
5784 # $c is the number of digits that will be in the integer part of the
5785 # final mantissa.
5786  
5787 0 my $c = $expo -> copy() -> bdec() -> bmod(3) -> binc();
5788 0 $expo -> bsub($c);
5789  
5790 0 ▶ if ($ndigmant > $c) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ndigmant > $c) 0 0 0
5791 0 ▶ return $x -> _upg() -> eparts(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
5792 0 $mant -> bnan(@r);
5793 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5794 0 return $mant, $expo;
5795 }
5796  
5797 0 $mant -> blsft($c - $ndigmant, 10, @r);
5798 }
5799  
5800 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5801 0 return $mant, $expo;
5802 }
5803  
5804 # Parts used for decimal notation, e.g., "12345.6789" is returned as "12345"
5805 # (integer part) and "0.6789" (fraction part).
5806  
5807 0 ▶sub dparts {
Subroutine dparts: not called CC=8 SCAR=42.8
Pod documented
5808 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5809  
5810 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5811  
5812 # Not-a-number.
5813  
5814 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
5815 0 my $int = $class -> bnan(@r);
5816 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5817 0 my $frc = $class -> bzero(@r); # or NaN?
5818 0 return $int, $frc;
5819 }
5820  
5821 # Infinity.
5822  
5823 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
5824 0 my $int = $class -> binf($x->{sign}, @r);
5825 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5826 0 my $frc = $class -> bzero(@r);
5827 0 return $int, $frc;
5828 }
5829  
5830 # Finite number.
5831  
5832 0 my $int = $x -> copy() -> round(@r);
5833 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5834  
5835 0 my $frc = $class -> bzero(@r);
5836 0 return $int, $frc;
5837 }
5838  
5839 # Fractional parts with the numerator and denominator as integers. E.g.,
5840 # "123.4375" is returned as "1975" and "16".
5841  
5842 0 ▶sub fparts {
Subroutine fparts: not called CC=8 SCAR=42.8
Pod documented
5843 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5844  
5845 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5846  
5847 # NaN => NaN/NaN
5848  
5849 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
5850 0 ▶ return $class -> bnan(@r) unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5851 0 return $class -> bnan(@r), $class -> bnan(@r);
5852 }
5853  
5854 # ±Inf => ±Inf/1
5855  
5856 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
5857 0 my $numer = $class -> binf($x->{sign}, @r);
5858 0 ▶ return $numer unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5859 0 my $denom = $class -> bone(@r);
5860 0 return $numer, $denom;
5861 }
5862  
5863 # N => N/1
5864  
5865 0 my $numer = $x -> copy() -> round(@r);
5866 0 ▶ return $numer unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5867  
5868 0 my $denom = $x -> copy();
5869 0 $denom -> {sign} = "+";
5870 0 $denom -> {value} = $LIB -> _one();
5871 0 $denom -> round(@r);
5872 0 return $numer, $denom;
5873 }
5874  
5875 0 ▶sub numerator {
Subroutine numerator: not called CC=3 SCAR=24.8
Pod documented
5876 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5877  
5878 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5879  
5880 0 return $x -> copy() -> round(@r);
5881 }
5882  
5883 0 ▶sub denominator {
Subroutine denominator: not called CC=4 SCAR=30.0
Pod documented
5884 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5885  
5886 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5887  
5888 0 ▶ return $x -> is_nan() ? $class -> bnan(@r) : $class -> bone(@r);
Branch: 1 branch
BranchTrueFalseTotal
$x->is_nan ? : 0 0 0
5889 }
5890  
5891 ###############################################################################
5892 # String conversion methods
5893 ###############################################################################
5894  
5895 12382 ▶sub bstr {
Subroutine bstr: called CC=9 SCAR=22.1
Pod documented
5896 12382 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 12370 12 12382
5897  
5898 12382 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 12382 12382
5899  
5900 # Inf and NaN
5901  
5902 12382 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 2823 9559 12382
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
837511842823
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
5903 2823 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 2242 581 2823
5904 581 return 'inf'; # +inf
5905 }
5906  
5907 # Upgrade?
5908  
5909 9559 ▶ $x -> _upg() -> bstr(@r) if $class -> upgrade() && !$x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 9559 9559
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
88247350
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
5910  
5911 # Finite number
5912  
5913 9559 my $str = $LIB->_str($x->{value});
5914 9559 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 1184 8375 9559
5915 }
5916  
5917 # Scientific notation with significand/mantissa as an integer, e.g., "12345" is
5918 # written as "1.2345e+4".
5919  
5920 66 ▶sub bsstr {
Subroutine bsstr: called CC=10 SCAR=23.1
Pod documented
5921 66 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 50 16 66
5922  
5923 66 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 66 66
5924  
5925 # Inf and NaN
5926  
5927 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
5928 18 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 11 7 18
5929 7 return 'inf'; # +inf
5930 }
5931  
5932 # Upgrade?
5933  
5934 48 ▶ $x -> _upg() -> bsstr(@r) if $class -> upgrade() && !$x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 48 48
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
4530
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
5935  
5936 # Finite number
5937  
5938 48 my $expo = $LIB -> _zeros($x->{value});
5939 48 my $mant = $LIB -> _str($x->{value});
5940 48 ▶ $mant = substr($mant, 0, -$expo) if $expo; # strip trailing zeros
Branch: 1 branch
BranchTrueFalseTotal
if $expo 25 23 48
5941  
5942 48 ▶ ($x->{sign} eq '-' ? '-' : '') . $mant . 'e+' . $expo;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 14 34 48
5943 }
5944  
5945 # Normalized notation, e.g., "12345" is written as "1.2345e+4".
5946  
5947 0 ▶sub bnstr {
Subroutine bnstr: not called CC=11 SCAR=48.8
Pod documented
5948 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5949  
5950 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5951  
5952 # Inf and NaN
5953  
5954 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
5955 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
5956 0 return 'inf'; # +inf
5957 }
5958  
5959 # Upgrade?
5960  
5961 0 ▶ $x -> _upg() -> bnstr(@r) if $class -> upgrade() && !$x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
5962  
5963 # Finite number
5964  
5965 0 my $expo = $LIB -> _zeros($x->{value});
5966 0 my $mant = $LIB -> _str($x->{value});
5967 0 ▶ $mant = substr($mant, 0, -$expo) if $expo; # strip trailing zeros
Branch: 1 branch
BranchTrueFalseTotal
if $expo 0 0 0
5968  
5969 0 my $mantlen = CORE::length($mant);
5970 0 ▶ if ($mantlen > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mantlen > 1) 0 0 0
5971 0 $expo += $mantlen - 1; # adjust exponent
5972 0 substr $mant, 1, 0, "."; # insert decimal point
5973 }
5974  
5975 0 ▶ ($x->{sign} eq '-' ? '-' : '') . $mant . 'e+' . $expo;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
5976 }
5977  
5978 # Engineering notation, e.g., "12345" is written as "12.345e+3".
5979  
5980 0 ▶sub bestr {
Subroutine bestr: not called CC=12 SCAR=50.5
Pod documented
5981 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5982  
5983 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5984  
5985 # Inf and NaN
5986  
5987 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
5988 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
5989 0 return 'inf'; # +inf
5990 }
5991  
5992 # Upgrade?
5993  
5994 0 ▶ $x -> _upg() -> bestr(@r) if $class -> upgrade() && !$x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
5995  
5996 # Finite number
5997  
5998 0 my $expo = $LIB -> _zeros($x->{value}); # number of trailing zeros
5999 0 my $mant = $LIB -> _str($x->{value}); # mantissa as a string
6000 0 ▶ $mant = substr($mant, 0, -$expo) if $expo; # strip trailing zeros
Branch: 1 branch
BranchTrueFalseTotal
if $expo 0 0 0
6001 0 my $mantlen = CORE::length($mant); # length of mantissa
6002 0 $expo += $mantlen;
6003  
6004 0 my $dotpos = ($expo - 1) % 3 + 1; # offset of decimal point
6005 0 $expo -= $dotpos;
6006  
6007 0 ▶ if ($dotpos < $mantlen) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dotpos < $mantlen) { } 0 0 0
elsif ($dotpos > $mantlen) { } 0 0 0
6008 0 substr $mant, $dotpos, 0, "."; # insert decimal point
6009 } elsif ($dotpos > $mantlen) {
6010 0 $mant .= "0" x ($dotpos - $mantlen); # append zeros
6011 }
6012  
6013 0 ▶ ($x->{sign} eq '-' ? '-' : '') . $mant . 'e+' . $expo;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
6014 }
6015  
6016 # Decimal notation, e.g., "12345" (no exponent).
6017  
6018 469 ▶sub bdstr {
Subroutine bdstr: called CC=9 SCAR=26.5
Pod documented
6019 469 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 469 0 469
6020  
6021 469 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 469 469
6022  
6023 # Inf and NaN
6024  
6025 469 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 0 469 469
Condition66%2/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
408610
MC/DC0%0/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table66%2/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
6026 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
6027 0 return 'inf'; # +inf
6028 }
6029  
6030 # Upgrade?
6031  
6032 469 ▶ $x -> _upg() -> bdstr(@r) if $class -> upgrade() && !$x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 469 469
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2202490
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6033  
6034 # Finite number
6035  
6036 469 ▶ ($x->{sign} eq '-' ? '-' : '') . $LIB->_str($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 61 408 469
6037 }
6038  
6039 # Fraction notation, e.g., "123.4375" is written as "1975/16", but "123" is
6040 # written as "123", not "123/1".
6041  
6042 1021 ▶sub bfstr {
Subroutine bfstr: called CC=9 SCAR=23.1
Pod documented
6043 1021 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1021 0 1021
6044  
6045 1021 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 1021 1021
6046  
6047 # Inf and NaN
6048  
6049 1021 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 68 953 1021
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
9411268
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
6050 68 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 68 0 68
6051 0 return 'inf'; # +inf
6052 }
6053  
6054 # Upgrade?
6055  
6056 953 ▶ $x -> _upg() -> bfstr(@r) if $class -> upgrade() && !$x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 953 953
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
95120
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6057  
6058 # Finite number
6059  
6060 953 ▶ ($x->{sign} eq '-' ? '-' : '') . $LIB->_str($x->{value});
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 12 941 953
6061 }
6062  
6063 36 ▶sub to_hex {
Subroutine to_hex: called CC=9 SCAR=22.3
Pod documented
6064 # return as hex string with no prefix
6065  
6066 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
6067  
6068 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
6069  
6070 # Inf and NaN
6071  
6072 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
6073 12 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 8 4 12
6074 4 return 'inf'; # +inf
6075 }
6076  
6077 # Upgrade?
6078  
6079 24 ▶ return $x -> _upg() -> to_hex(@r) if $class -> upgrade() && !$x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 24 24
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
1860
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6080  
6081 # Finite number
6082  
6083 24 my $hex = $LIB->_to_hex($x->{value});
6084 24 ▶ return $x->{sign} eq '-' ? "-$hex" : $hex;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 20 24
6085 }
6086  
6087 40 ▶sub to_oct {
Subroutine to_oct: called CC=9 SCAR=22.3
Pod documented
6088 # return as octal string with no prefix
6089  
6090 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
6091  
6092 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
6093  
6094 # Inf and NaN
6095  
6096 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
6097 12 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 8 4 12
6098 4 return 'inf'; # +inf
6099 }
6100  
6101 # Upgrade?
6102  
6103 28 ▶ return $x -> _upg() -> to_oct(@r) if $class -> upgrade() && !$x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 28 28
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2170
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6104  
6105 # Finite number
6106  
6107 28 my $oct = $LIB->_to_oct($x->{value});
6108 28 ▶ return $x->{sign} eq '-' ? "-$oct" : $oct;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 24 28
6109 }
6110  
6111 93 ▶sub to_bin {
Subroutine to_bin: called CC=9 SCAR=22.3
Pod documented
6112 # return as binary string with no prefix
6113  
6114 93 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 93 0 93
6115  
6116 93 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 93 93
6117  
6118 # Inf and NaN
6119  
6120 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
6121 12 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 8 4 12
6122 4 return 'inf'; # +inf
6123 }
6124  
6125 # Upgrade?
6126  
6127 81 ▶ return $x -> _upg() -> to_bin(@r) if $class -> upgrade() && !$x -> isa(__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 81 81
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
7560
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6128  
6129 # Finite number
6130  
6131 81 my $bin = $LIB->_to_bin($x->{value});
6132 81 ▶ return $x->{sign} eq '-' ? "-$bin" : $bin;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 58 23 81
6133 }
6134  
6135 0 ▶sub to_bytes {
Subroutine to_bytes: not called CC=8 SCAR=42.8
Pod documented
6136 # return a byte string
6137  
6138 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
6139  
6140 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
6141  
6142 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
6143 if $x -> is_neg() || ! $x -> is_int();
6144  
6145 0 ▶ return $x -> _upg() -> to_bytes(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6146 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6147  
6148 0 ▶ croak("to_bytes() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_to_bytes") 0 0 0
6149 unless $LIB -> can('_to_bytes');
6150  
6151 0 return $LIB->_to_bytes($x->{value});
6152 }
6153  
6154 0 ▶sub to_ieee754 {
Subroutine to_ieee754: not called CC=8 SCAR=42.8
Pod documented
6155 0 ▶ my ($class, $x, $format, @r) = ref($_[0]) ? (ref($_[0]), @_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
6156 : objectify(1, @_);
6157  
6158 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
6159  
6160 0 ▶ return $x -> _upg() -> to_ieee754($format, @r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6161 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6162  
6163 0 ▶ croak("the value to convert must be an integer, +/-infinity, or NaN")
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int or $x->is_inf or $x->is_nan 0 0 0
Condition0%0/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
000
$x->is_int or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table0%0/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
6164 unless $x -> is_int() || $x -> is_inf() || $x -> is_nan();
6165  
6166 0 return $x -> as_float() -> to_ieee754($format);
6167 }
6168  
6169 0 ▶sub to_fp80 {
Subroutine to_fp80: not called CC=7 SCAR=40.3
Pod documented
6170 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
6171 : objectify(1, @_);
6172  
6173 0 ▶ return $x -> _upg() -> to_fp80(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6174 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6175  
6176 0 ▶ croak("the value to convert must be an integer, +/-infinity, or NaN")
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int or $x->is_inf or $x->is_nan 0 0 0
Condition0%0/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
000
$x->is_int or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table0%0/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
6177 unless $x -> is_int() || $x -> is_inf() || $x -> is_nan();
6178  
6179 0 return $x -> as_float(@r) -> to_fp80();
6180 }
6181  
6182 0 ▶sub to_base {
Subroutine to_base: not called CC=18 SCAR=58.3
Pod documented
6183 # return a base anything string
6184  
6185 # $cs is the collation sequence
6186 0 ▶ my ($class, $x, $base, $cs, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
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
6187 ? (ref($_[0]), @_) : objectify(2, @_);
6188  
6189 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
6190  
6191 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
6192 if $x -> is_neg() || !$x -> is_int();
6193  
6194 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
6195 if $base < 2 || ! $base -> is_int();
6196  
6197 # If no collating sequence is given, pass some of the conversions to
6198 # methods optimized for those cases.
6199  
6200 0 ▶ unless (defined $cs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cs) 0 0 0
6201 0 ▶ return $x -> to_bin() if $base == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 2 0 0 0
6202 0 ▶ return $x -> to_oct() if $base == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 8 0 0 0
6203 0 ▶ return uc $x -> to_hex() if $base == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 16 0 0 0
6204 0 ▶ return $x -> bstr() if $base == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 10 0 0 0
6205 }
6206  
6207 0 ▶ croak("to_base() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_to_base") 0 0 0
6208 unless $LIB -> can('_to_base');
6209  
6210 0 ▶ return $x -> _upg() -> to_basen($base, $cs, @r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__) 0 0 0
Condition0%0/6
$class->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
$class->upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__)
$class->upgrade $x->isa(__PACKAGE__) $base->isa(__PACKAGE__)
Truth table0%0/4
A and not(B && C)
A= $class->upgradeB= $x->isa(__PACKAGE__)C= $base->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
6211 if $class -> upgrade() && (!$x -> isa(__PACKAGE__) ||
6212 !$base -> isa(__PACKAGE__));
6213  
6214 return $LIB->_to_base($x->{value}, $base -> {value},
6215 0 ▶ defined($cs) ? $cs : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $cs ? : 0 0 0
6216 }
6217  
6218 0 ▶sub to_base_num {
Subroutine to_base_num: not called CC=13 SCAR=52.0
Pod documented
6219 # return a base anything array ref, e.g.,
6220 # Math::BigInt -> new(255) -> to_base_num(10) returns [2, 5, 5];
6221  
6222 # $cs is the collation sequence
6223 0 ▶ my ($class, $x, $base, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
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
6224 ? (ref($_[0]), @_) : objectify(2, @_);
6225  
6226 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
6227  
6228 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
6229 if $x -> is_neg() || !$x -> is_int();
6230  
6231 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
6232 if $base < 2 || ! $base -> is_int();
6233  
6234 0 ▶ croak("to_base() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_to_base") 0 0 0
6235 unless $LIB -> can('_to_base');
6236  
6237 0 ▶ return $x -> _upg() -> to_base_num($base, @r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__) 0 0 0
Condition0%0/6
$class->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
$class->upgrade and not $x->isa(__PACKAGE__) && $base->isa(__PACKAGE__)
$class->upgrade $x->isa(__PACKAGE__) $base->isa(__PACKAGE__)
Truth table0%0/4
A and not(B && C)
A= $class->upgradeB= $x->isa(__PACKAGE__)C= $base->isa(__PACKAGE__)
ABCresult
0XX0
1110
10X1
1101
6238 if $class -> upgrade() && (!$x -> isa(__PACKAGE__) ||
6239 !$base -> isa(__PACKAGE__));
6240  
6241 # Get a reference to an array of library thingies, and replace each element
6242 # with a Math::BigInt object using that thingy.
6243  
6244 0 my $vals = $LIB -> _to_base_num($x->{value}, $base -> {value});
6245  
6246 0 for my $i (0 .. $#$vals) {
6247 0 my $x = $class -> bzero();
6248 0 $x -> {value} = $vals -> [$i];
6249 0 $vals -> [$i] = $x;
6250 }
6251  
6252 0 return $vals;
6253 }
6254  
6255 36 ▶sub as_hex {
Subroutine as_hex: called CC=7 SCAR=20.1
Pod documented
6256 # return as hex string, with prefixed 0x
6257  
6258 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
6259  
6260 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
6261  
6262 36 ▶ return $x -> bstr() if !$x -> is_finite(); # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 12 24 36
6263  
6264 24 ▶ return $x -> _upg() -> as_hex(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 24 24
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
1860
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6265 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6266  
6267 24 my $hex = $LIB->_as_hex($x->{value});
6268 24 ▶ return $x->{sign} eq '-' ? "-$hex" : $hex;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 20 24
6269 }
6270  
6271 40 ▶sub as_oct {
Subroutine as_oct: called CC=7 SCAR=20.1
Pod documented
6272 # return as octal string, with prefixed 0
6273  
6274 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
6275  
6276 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
6277  
6278 40 ▶ return $x -> bstr() if !$x -> is_finite(); # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 12 28 40
6279  
6280 28 ▶ return $x -> _upg() -> as_oct(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 28 28
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2170
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6281 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6282  
6283 28 my $oct = $LIB->_as_oct($x->{value});
6284 28 ▶ return $x->{sign} eq '-' ? "-$oct" : $oct;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 24 28
6285 }
6286  
6287 39 ▶sub as_bin {
Subroutine as_bin: called CC=7 SCAR=20.1
Pod documented
6288 # return as binary string, with prefixed 0b
6289  
6290 39 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 39 0 39
6291  
6292 39 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 39 39
6293  
6294 39 ▶ return $x -> bstr() if !$x -> is_finite(); # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 12 27 39
6295  
6296 27 ▶ return $x -> _upg() -> as_bin(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 27 27
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2160
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6297 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6298  
6299 27 my $bin = $LIB->_as_bin($x->{value});
6300 27 ▶ return $x->{sign} eq '-' ? "-$bin" : $bin;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 23 27
6301 }
6302  
6303 50 *as_bytes = \&to_bytes;
6304  
6305 ###############################################################################
6306 # Other conversion methods
6307 ###############################################################################
6308  
6309 545 ▶sub numify {
Subroutine numify: called CC=9 SCAR=28.5
Pod documented
6310 # Make a Perl scalar number from a Math::BigInt object.
6311 545 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 545 0 545
6312  
6313 545 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 545 545
6314  
6315 545 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 545 545
6316 0 require Math::Complex;
6317 0 my $inf = $Math::Complex::Inf;
6318 0 return $inf - $inf;
6319 }
6320  
6321 545 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 545 545
6322 0 require Math::Complex;
6323 0 my $inf = $Math::Complex::Inf;
6324 0 ▶ return $x -> is_negative() ? -$inf : $inf;
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative ? : 0 0 0
6325 }
6326  
6327 545 ▶ return $x -> _upg() -> numify(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 545 545
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
483620
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6328 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6329  
6330 545 my $num = 0 + $LIB->_num($x->{value});
6331 545 ▶ return $x->{sign} eq '-' ? -$num : $num;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 39 506 545
6332 }
6333  
6334 ###############################################################################
6335 # Private methods and functions.
6336 ###############################################################################
6337  
6338 0 ▶sub _trailing_zeros {
Subroutine _trailing_zeros: not called CC=3 SCAR=24.8
6339 # return the amount of trailing zeros in $x (as scalar)
6340 0 my $x = shift;
6341 0 ▶ $x = __PACKAGE__ -> new($x) unless ref $x;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $x 0 0 0
6342  
6343 0 ▶ return 0 if !$x -> is_finite(); # NaN, inf, -inf etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 0 0 0
6344  
6345 0 $LIB->_zeros($x->{value}); # must handle odd values, 0 etc
6346 }
6347  
6348 3338 ▶sub _scan_for_nonzero {
Subroutine _scan_for_nonzero: called CC=5 SCAR=16.1
6349 # internal, used by bround() to scan for non-zeros after a '5'
6350 3338 my ($x, $pad, $xs, $len) = @_;
6351  
6352 3338 ▶ return 0 if $len == 1; # "5" is trailed by invisible zeros
Branch: 1 branch
BranchTrueFalseTotal
if $len == 1 23 3315 3338
6353 3315 my $follow = $pad - 1;
6354 3315 ▶ return 0 if $follow > $len || $follow < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $follow > $len or $follow < 1 709 2606 3315
Condition66%2/3
$follow > $len or $follow < 1
l!l&&r!l&&!r
07092606
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
6355  
6356 # use the string form to check whether only '0's follow or not
6357 2606 ▶ substr ($xs, -$follow) =~ /[^0]/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
substr($xs, -$follow) =~ /[^0]/ ? : 2520 86 2606
6358 }
6359  
6360 10901 ▶sub _find_round_parameters {
Subroutine _find_round_parameters: called CC=29 SCAR=34.3
6361 # After any operation or when calling round(), the result is rounded by
6362 # regarding the A & P from arguments, local parameters, or globals.
6363  
6364 # !!!!!!! If you change this, remember to change round(), too! !!!!!!!!!!
6365  
6366 # This procedure finds the round parameters, but it is for speed reasons
6367 # duplicated in round. Otherwise, it is tested by the testsuite and used
6368 # by bdiv().
6369  
6370 # returns ($self) or ($self, $a, $p, $r) - sets $self to NaN of both A and
6371 # P were requested/defined (locally or globally or both)
6372  
6373 10901 my ($self, $a, $p, $r, @args) = @_;
6374 # $a accuracy, if given by caller
6375 # $p precision, if given by caller
6376 # $r round_mode, if given by caller
6377 # @args all 'other' arguments (0 for unary, 1 for binary ops)
6378  
6379 10901 my $class = ref($self); # find out class of argument(s)
6380  
6381 # convert to normal scalar for speed and correctness in inner parts
6382 10901 ▶ $a = $a -> can('numify') ? $a -> numify() : "$a" if defined $a && ref($a);
Branch: 2 branches
BranchTrueFalseTotal
if defined $a and ref $a 316 10585 10901
$a->can('numify') ? : 316 0 316
Condition100%3/3
defined $a and ref $a
!ll&&!rl&&r
10989487316
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
6383 10901 ▶ $p = $p -> can('numify') ? $p -> numify() : "$p" if defined $p && ref($p);
Branch: 2 branches
BranchTrueFalseTotal
if defined $p and ref $p 0 10901 10901
$p->can('numify') ? : 0 0 0
Condition66%2/3
defined $p and ref $p
!ll&&!rl&&r
10849520
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
6384  
6385 # now pick $a or $p, but only if we have got "arguments"
6386 10901 ▶ if (!defined $a) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $a) 1098 9803 10901
6387 1098 foreach ($self, @args) {
6388 # take the defined one, or if both defined, the one that is smaller
6389 $a = $_->{accuracy}
6390 1760 ▶ if (defined $_->{accuracy}) && (!defined $a || $_->{accuracy} < $a);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"accuracy"} and !defined($a) || $_->{"accuracy"} < $a 32 1728 1760
Condition50%3/6
defined $_->{"accuracy"} and !defined($a) || $_->{"accuracy"} < $a
!ll&&!rl&&r
1728032
not defined $a or $_->{"accuracy"} < $a
l!l&&r!l&&!r
3200
MC/DC33%1/3
defined $_->{"accuracy"} and !defined($a) || $_->{"accuracy"} < $a
defined $_->{"accuracy"} not defined $a $_->{"accuracy"} < $a
Truth table50%2/4
A and not(B) or C
A= defined $_->{"accuracy"}B= not defined $aC= $_->{"accuracy"} < $a
ABCresult
0XX0
1000
11X1
1011
6391 }
6392 }
6393 10901 ▶ if (!defined $p) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $p) 10849 52 10901
6394 # even if $a is defined, take $p, to signal error for both defined
6395 10849 foreach ($self, @args) {
6396 # take the defined one, or if both defined, the one that is bigger
6397 # -2 > -3, and 3 > 2
6398 $p = $_->{precision}
6399 20306 ▶ if (defined $_->{precision}) && (!defined $p || $_->{precision} > $p);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_->{"precision"} and !defined($p) || $_->{"precision"} > $p 10 20296 20306
Condition50%3/6
defined $_->{"precision"} and !defined($p) || $_->{"precision"} > $p
!ll&&!rl&&r
20296010
not defined $p or $_->{"precision"} > $p
l!l&&r!l&&!r
1000
MC/DC33%1/3
defined $_->{"precision"} and !defined($p) || $_->{"precision"} > $p
defined $_->{"precision"} not defined $p $_->{"precision"} > $p
Truth table50%2/4
A and not(B) or C
A= defined $_->{"precision"}B= not defined $pC= $_->{"precision"} > $p
ABCresult
0XX0
1000
11X1
1011
6400 }
6401 }
6402  
6403 # if still none defined, use globals (#2)
6404 10901 ▶ $a = $class -> accuracy() unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 1066 9835 10901
6405 10901 ▶ $p = $class -> precision() unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 10839 62 10901
6406  
6407 # A == 0 is useless, so undef it to signal no rounding
6408 10901 ▶ $a = undef if defined $a && $a == 0;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and $a == 0 16 10885 10901
Condition100%3/3
defined $a and $a == 0
!ll&&!rl&&r
1063982216
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
6409  
6410 # no rounding today?
6411 10901 ▶ return ($self) unless defined $a || defined $p; # early out
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a or defined $p 1015 9886 10901
Condition100%3/3
defined $a or defined $p
l!l&&r!l&&!r
9822641015
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
6412  
6413 # set A and set P is an fatal error
6414 9886 ▶ return ($self -> bnan()) if defined $a && defined $p; # error
Branch: 1 branch
BranchTrueFalseTotal
if defined $a and defined $p 9 9877 9886
Condition100%3/3
defined $a and defined $p
!ll&&!rl&&r
6498139
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
6415  
6416 9877 ▶ $r = $class -> round_mode() unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 9868 9 9877
6417 9877 ▶ if ($r !~ /^(even|odd|[+-]inf|zero|trunc|common)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($r =~ /^(even|odd|[+-]inf|zero|trunc|common)$/) 3 9874 9877
6418 3 croak("Unknown round mode '$r'");
6419 }
6420  
6421 9874 ▶ $a = int($a) if defined $a;
Branch: 1 branch
BranchTrueFalseTotal
if defined $a 9813 61 9874
6422 9874 ▶ $p = int($p) if defined $p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $p 61 9813 9874
6423  
6424 9874 ($self, $a, $p, $r);
6425 }
6426  
6427 # Return true if the input is numeric and false if it is a string.
6428  
6429 0 ▶sub _is_numeric {
Subroutine _is_numeric: not called CC=4 SCAR=25.0
6430 0 shift; # class name
6431 0 my $value = shift;
6432 50 ▶ no warnings 'numeric';
Subroutine BEGIN: called CC=1 SCAR=0.0
6433 # detect numbers
6434 # string & "" -> ""
6435 # number & "" -> 0 (with warning)
6436 # nan and inf can detect as numbers, so check with * 0
6437 0 ▶ return unless CORE::length((my $dummy = "") & $value);
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length((my $dummy = "") & $value) 0 0 0
6438 0 ▶ return unless 0 + $value eq $value;
Branch: 1 branch
BranchTrueFalseTotal
unless 0 + $value eq $value 0 0 0
6439 0 ▶ return 1 if $value * 0 == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $value * 0 == 0 0 0 0
6440 0 return -1; # Inf/NaN
6441 }
6442  
6443 # Trims the sign of the significand, the (absolute value of the) significand,
6444 # the sign of the exponent, and the (absolute value of the) exponent. The
6445 # returned values have no underscores ("_") or unnecessary leading or trailing
6446 # zeros.
6447  
6448 8671 ▶sub _trim_split_parts {
Subroutine _trim_split_parts: called CC=10 SCAR=23.0
6449 8671 shift; # class name
6450  
6451 8671 ▶ my $sig_sgn = shift() || '+';
Condition100%2/2
shift() || '+'
l!l
20276644
MC/DC100%1/1
shift() || '+'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
6452 8671 ▶ my $sig_str = shift() || '0';
Condition100%2/2
shift() || '0'
l!l
61942477
MC/DC100%1/1
shift() || '0'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
6453 8671 ▶ my $exp_sgn = shift() || '+';
Condition100%2/2
shift() || '+'
l!l
4158256
MC/DC100%1/1
shift() || '+'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
6454 8671 ▶ my $exp_str = shift() || '0';
Condition100%2/2
shift() || '0'
l!l
5348137
MC/DC100%1/1
shift() || '0'
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
6455  
6456 8671 $sig_str =~ tr/_//d; # "1.0_0_0" -> "1.000"
6457 8671 $sig_str =~ s/^0+//; # "01.000" -> "1.000"
6458 8671 ▶ $sig_str =~ s/\.0*$// # "1.000" -> "1"
Branch: 1 branch
BranchTrueFalseTotal
$sig_str =~ s/\.0*$// or $sig_str =~ s/(\..*[^0])0+$/$1/ 25 8646 8671
6459 || $sig_str =~ s/(\..*[^0])0+$/$1/; # "1.010" -> "1.01"
6460 8671 ▶ $sig_str = '0' unless CORE::length($sig_str);
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $sig_str 2585 6086 8671
6461  
6462 8671 ▶ return '+', '0', '+', '0' if $sig_str eq '0';
Branch: 1 branch
BranchTrueFalseTotal
if $sig_str eq 0 2585 6086 8671
6463  
6464 6086 $exp_str =~ tr/_//d; # "01_234" -> "01234"
6465 6086 $exp_str =~ s/^0+//; # "01234" -> "1234"
6466 6086 ▶ $exp_str = '0' unless CORE::length($exp_str);
Branch: 1 branch
BranchTrueFalseTotal
unless CORE::length $exp_str 5650 436 6086
6467 6086 ▶ $exp_sgn = '+' if $exp_str eq '0'; # "+3e-0" -> "+3e+0"
Branch: 1 branch
BranchTrueFalseTotal
if $exp_str eq 0 5650 436 6086
6468  
6469 6086 return $sig_sgn, $sig_str, $exp_sgn, $exp_str;
6470 }
6471  
6472 # Takes any string representing a valid decimal number and splits it into four
6473 # strings: the sign of the significand, the absolute value of the significand,
6474 # the sign of the exponent, and the absolute value of the exponent. Both the
6475 # significand and the exponent are in base 10.
6476 #
6477 # Perl accepts literals like the following. The value is 100.1.
6478 #
6479 # 1__0__.__0__1__e+0__1__ (prints "Misplaced _ in number")
6480 # 1_0.0_1e+0_1
6481 #
6482 # Strings representing decimal numbers do not allow underscores, so only the
6483 # following is valid
6484 #
6485 # "10.01e+01"
6486  
6487 8722 ▶sub _dec_str_to_dec_str_parts {
Subroutine _dec_str_to_dec_str_parts: called CC=2 SCAR=6.9
6488 8722 my $class = shift;
6489 8722 my $str = shift;
6490  
6491 8722 ▶ 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) 7291 1431 8722
6492 ^
6493  
6494 # optional leading whitespace
6495 \s*
6496  
6497 # optional sign
6498 ( [+-]? )
6499  
6500 # significand
6501 (
6502 # integer part and optional fraction part ...
6503 \d+ (?: _+ \d+ )* _*
6504 (?:
6505 \.
6506 (?: _* \d+ (?: _+ \d+ )* _* )?
6507 )?
6508 |
6509 # ... or mandatory fraction part
6510 \.
6511 \d+ (?: _+ \d+ )* _*
6512 )
6513  
6514 # optional exponent
6515 (?:
6516 [Ee]
6517 ( [+-]? )
6518 ( \d+ (?: _+ \d+ )* _* )
6519 )?
6520  
6521 # optional trailing whitespace
6522 \s*
6523  
6524 $
6525 /x)
6526 {
6527 7291 return $class -> _trim_split_parts($1, $2, $3, $4);
6528 }
6529  
6530 1431 return;
6531 }
6532  
6533 # Takes any string representing a valid hexadecimal number and splits it into
6534 # four strings: the sign of the significand, the absolute value of the
6535 # significand, the sign of the exponent, and the absolute value of the
6536 # exponent. The significand is in base 16, and the exponent is in base 2.
6537 #
6538 # Perl accepts literals like the following. The "x" might be a capital "X". The
6539 # value is 32.0078125.
6540 #
6541 # 0x__1__0__.0__1__p+0__1__ (prints "Misplaced _ in number")
6542 # 0x1_0.0_1p+0_1
6543 #
6544 # The CORE::hex() function does not accept floating point accepts
6545 #
6546 # "0x_1_0"
6547 # "x_1_0"
6548 # "_1_0"
6549  
6550 1123 ▶sub _hex_str_to_hex_str_parts {
Subroutine _hex_str_to_hex_str_parts: called CC=2 SCAR=6.9
6551 1123 my $class = shift;
6552 1123 my $str = shift;
6553  
6554 1123 ▶ 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) 1119 4 1123
6555 ^
6556  
6557 # optional leading whitespace
6558 \s*
6559  
6560 # optional sign
6561 ( [+-]? )
6562  
6563 # optional hex prefix
6564 (?: 0? [Xx] _* )?
6565  
6566 # significand using the hex digits 0..9 and a..f
6567 (
6568 # integer part and optional fraction part ...
6569 [0-9a-fA-F]+ (?: _+ [0-9a-fA-F]+ )* _*
6570 (?:
6571 \.
6572 (?: _* [0-9a-fA-F]+ (?: _+ [0-9a-fA-F]+ )* _* )?
6573 )?
6574 |
6575 # ... or mandatory fraction part
6576 \.
6577 [0-9a-fA-F]+ (?: _+ [0-9a-fA-F]+ )* _*
6578 )
6579  
6580 # optional exponent (power of 2) using decimal digits
6581 (?:
6582 [Pp]
6583 ( [+-]? )
6584 ( \d+ (?: _+ \d+ )* _* )
6585 )?
6586  
6587 # optional trailing whitespace
6588 \s*
6589  
6590 $
6591 /x)
6592 {
6593 1119 return $class -> _trim_split_parts($1, $2, $3, $4);
6594 }
6595  
6596 4 return;
6597 }
6598  
6599 # Takes any string representing a valid octal number and splits it into four
6600 # strings: the sign of the significand, the absolute value of the significand,
6601 # the sign of the exponent, and the absolute value of the exponent. The
6602 # significand is in base 8, and the exponent is in base 2.
6603  
6604 6 ▶sub _oct_str_to_oct_str_parts {
Subroutine _oct_str_to_oct_str_parts: called CC=2 SCAR=7.4
6605 6 my $class = shift;
6606 6 my $str = shift;
6607  
6608 6 ▶ 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) 6 0 6
6609 ^
6610  
6611 # optional leading whitespace
6612 \s*
6613  
6614 # optional sign
6615 ( [+-]? )
6616  
6617 # optional octal prefix
6618 (?: 0? [Oo] _* )?
6619  
6620 # significand using the octal digits 0..7
6621 (
6622 # integer part and optional fraction part ...
6623 [0-7]+ (?: _+ [0-7]+ )* _*
6624 (?:
6625 \.
6626 (?: _* [0-7]+ (?: _+ [0-7]+ )* _* )?
6627 )?
6628 |
6629 # ... or mandatory fraction part
6630 \.
6631 [0-7]+ (?: _+ [0-7]+ )* _*
6632 )
6633  
6634 # optional exponent (power of 2) using decimal digits
6635 (?:
6636 [Pp]
6637 ( [+-]? )
6638 ( \d+ (?: _+ \d+ )* _* )
6639 )?
6640  
6641 # optional trailing whitespace
6642 \s*
6643  
6644 $
6645 /x)
6646 {
6647 6 return $class -> _trim_split_parts($1, $2, $3, $4);
6648 }
6649  
6650 0 return;
6651 }
6652  
6653 # Takes any string representing a valid binary number and splits it into four
6654 # strings: the sign of the significand, the absolute value of the significand,
6655 # the sign of the exponent, and the absolute value of the exponent. The
6656 # significand is in base 2, and the exponent is in base 2.
6657  
6658 314 ▶sub _bin_str_to_bin_str_parts {
Subroutine _bin_str_to_bin_str_parts: called CC=2 SCAR=6.9
6659 314 my $class = shift;
6660 314 my $str = shift;
6661  
6662 314 ▶ 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) 255 59 314
6663 ^
6664  
6665 # optional leading whitespace
6666 \s*
6667  
6668 # optional sign
6669 ( [+-]? )
6670  
6671 # optional binary prefix
6672 (?: 0? [Bb] _* )?
6673  
6674 # significand using the binary digits 0 and 1
6675 (
6676 # integer part and optional fraction part ...
6677 [01]+ (?: _+ [01]+ )* _*
6678 (?:
6679 \.
6680 (?: _* [01]+ (?: _+ [01]+ )* _* )?
6681 )?
6682 |
6683 # ... or mandatory fraction part
6684 \.
6685 [01]+ (?: _+ [01]+ )* _*
6686 )
6687  
6688 # optional exponent (power of 2) using decimal digits
6689 (?:
6690 [Pp]
6691 ( [+-]? )
6692 ( \d+ (?: _+ \d+ )* _* )
6693 )?
6694  
6695 # optional trailing whitespace
6696 \s*
6697  
6698 $
6699 /x)
6700 {
6701 255 return $class -> _trim_split_parts($1, $2, $3, $4);
6702 }
6703  
6704 59 return;
6705 }
6706  
6707 # Takes any string representing a valid decimal number and splits it into four
6708 # parts: the sign of the significand, the absolute value of the significand as
6709 # a libray thingy, the sign of the exponent, and the absolute value of the
6710 # exponent as a library thingy.
6711  
6712 7291 ▶sub _dec_str_parts_to_flt_lib_parts {
Subroutine _dec_str_parts_to_flt_lib_parts: called CC=5 SCAR=16.1
6713 7291 shift; # class name
6714  
6715 7291 my ($sig_sgn, $sig_str, $exp_sgn, $exp_str) = @_;
6716  
6717 # Handle zero.
6718  
6719 7291 ▶ if ($sig_str eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_str eq 0) 2573 4718 7291
6720 2573 return '+', $LIB -> _zero(), '+', $LIB -> _zero();
6721 }
6722  
6723 # Absolute value of exponent as library "object".
6724  
6725 4718 my $exp_lib = $LIB -> _new($exp_str);
6726  
6727 # If there is a dot in the significand, remove it so the significand
6728 # becomes an integer and adjust the exponent accordingly. Also remove
6729 # leading zeros which might now appear in the significand. E.g.,
6730 #
6731 # 12.345e-2 -> 12345e-5
6732 # 12.345e+2 -> 12345e-1
6733 # 0.0123e+5 -> 00123e+1 -> 123e+1
6734  
6735 4718 my $idx = index $sig_str, '.';
6736 4718 ▶ if ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= 0) 3437 1281 4718
6737 3437 substr($sig_str, $idx, 1) = '';
6738  
6739 # delta = length - index
6740 3437 my $delta = $LIB -> _new(CORE::length($sig_str));
6741 3437 $delta = $LIB -> _sub($delta, $LIB -> _new($idx));
6742  
6743 # exponent - delta
6744 3437 ($exp_lib, $exp_sgn) = $LIB -> _ssub($exp_lib, $exp_sgn, $delta, '+');
6745  
6746 3437 $sig_str =~ s/^0+//;
6747 }
6748  
6749 # If there are trailing zeros in the significand, remove them and
6750 # adjust the exponent. E.g.,
6751 #
6752 # 12340e-5 -> 1234e-4
6753 # 12340e-1 -> 1234e0
6754 # 12340e+3 -> 1234e4
6755  
6756 4718 ▶ if ($sig_str =~ s/(0+)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_str =~ s/(0+)\z//) 900 3818 4718
6757 900 my $len = CORE::length($1);
6758 900 ($exp_lib, $exp_sgn) =
6759 $LIB -> _sadd($exp_lib, $exp_sgn, $LIB -> _new($len), '+');
6760 }
6761  
6762 # At this point, the significand is empty or an integer with no trailing
6763 # zeros. The exponent is in base 10.
6764  
6765 4718 ▶ unless (CORE::length $sig_str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::length $sig_str) 0 4718 4718
6766 0 return '+', $LIB -> _zero(), '+', $LIB -> _zero();
6767 }
6768  
6769 # Absolute value of significand as library "object".
6770  
6771 4718 my $sig_lib = $LIB -> _new($sig_str);
6772  
6773 4718 return $sig_sgn, $sig_lib, $exp_sgn, $exp_lib;
6774 }
6775  
6776 # Takes any string representing a valid binary number and splits it into four
6777 # parts: the sign of the significand, the absolute value of the significand as
6778 # a libray thingy, the sign of the exponent, and the absolute value of the
6779 # exponent as a library thingy.
6780  
6781 1380 ▶sub _bin_str_parts_to_flt_lib_parts {
Subroutine _bin_str_parts_to_flt_lib_parts: called CC=13 SCAR=25.7
6782 1380 shift; # class name
6783  
6784 1380 my ($sig_sgn, $sig_str, $exp_sgn, $exp_str, $bpc) = @_;
6785 1380 my $bpc_lib = $LIB -> _new($bpc);
6786  
6787 # Handle zero.
6788  
6789 1380 ▶ if ($sig_str eq '0') {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_str eq 0) 12 1368 1380
6790 12 return '+', $LIB -> _zero(), '+', $LIB -> _zero();
6791 }
6792  
6793 # Absolute value of exponent as library "object".
6794  
6795 1368 my $exp_lib = $LIB -> _new($exp_str);
6796  
6797 # If there is a dot in the significand, remove it so the significand
6798 # becomes an integer and adjust the exponent accordingly. Also remove
6799 # leading zeros which might now appear in the significand. E.g., with
6800 # hexadecimal numbers
6801 #
6802 # 12.345p-2 -> 12345p-14
6803 # 12.345p+2 -> 12345p-10
6804 # 0.0123p+5 -> 00123p-11 -> 123p-11
6805  
6806 1368 my $idx = index $sig_str, '.';
6807 1368 ▶ if ($idx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx >= 0) 3 1365 1368
6808 3 substr($sig_str, $idx, 1) = '';
6809  
6810 # delta = (length - index) * bpc
6811 3 my $delta = $LIB -> _new(CORE::length($sig_str));
6812 3 $delta = $LIB -> _sub($delta, $LIB -> _new($idx));
6813 3 ▶ $delta = $LIB -> _mul($delta, $bpc_lib) if $bpc != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bpc != 1 2 1 3
6814  
6815 # exponent - delta
6816 3 ($exp_lib, $exp_sgn) = $LIB -> _ssub($exp_lib, $exp_sgn, $delta, '+');
6817  
6818 3 $sig_str =~ s/^0+//;
6819 }
6820  
6821 # If there are trailing zeros in the significand, remove them and
6822 # adjust the exponent accordingly. E.g., with hexadecimal numbers
6823 #
6824 # 12340p-5 -> 1234p-1
6825 # 12340p-1 -> 1234p+3
6826 # 12340p+3 -> 1234p+7
6827  
6828 1368 ▶ if ($sig_str =~ s/(0+)\z//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sig_str =~ s/(0+)\z//) 248 1120 1368
6829  
6830 # delta = length * bpc
6831 248 my $delta = $LIB -> _new(CORE::length($1));
6832 248 ▶ $delta = $LIB -> _mul($delta, $bpc_lib) if $bpc != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $bpc != 1 220 28 248
6833  
6834 # exponent + delta
6835 248 ($exp_lib, $exp_sgn) = $LIB -> _sadd($exp_lib, $exp_sgn, $delta, '+');
6836 }
6837  
6838 # At this point, the significand is empty or an integer with no leading
6839 # or trailing zeros. The exponent is in base 2.
6840  
6841 1368 ▶ unless (CORE::length $sig_str) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::length $sig_str) 0 1368 1368
6842 0 return '+', $LIB -> _zero(), '+', $LIB -> _zero();
6843 }
6844  
6845 # Absolute value of significand as library "object".
6846  
6847 1368 ▶ my $sig_lib = $bpc == 1 ? $LIB -> _from_bin('0b' . $sig_str)
Branch: 3 branches
BranchTrueFalseTotal
$bpc == 1 ? : 247 1121 1368
$bpc == 3 ? : 6 1115 1121
$bpc == 4 ? : 1115 0 1115
6848 : $bpc == 3 ? $LIB -> _from_oct('0' . $sig_str)
6849 : $bpc == 4 ? $LIB -> _from_hex('0x' . $sig_str)
6850 : die "internal error: invalid exponent multiplier";
6851  
6852 # If the exponent (in base 2) is positive or zero ...
6853  
6854 1368 ▶ if ($exp_sgn eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp_sgn eq '+') { } 1367 1 1368
6855  
6856 1367 ▶ if (!$LIB -> _is_zero($exp_lib)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($exp_lib)) 249 1118 1367
6857  
6858 # Multiply significand by 2 raised to the exponent.
6859  
6860 249 my $p = $LIB -> _pow($LIB -> _two(), $exp_lib);
6861 249 $sig_lib = $LIB -> _mul($sig_lib, $p);
6862 249 $exp_lib = $LIB -> _zero();
6863 }
6864 }
6865  
6866 # ... else if the exponent is negative ...
6867  
6868 else {
6869  
6870 # Rather than dividing the significand by 2 raised to the absolute
6871 # value of the exponent, multiply the significand by 5 raised to the
6872 # absolute value of the exponent and let the exponent be in base 10:
6873 #
6874 # a * 2^(-b) = a * 5^b * 10^(-b) = c * 10^(-b), where c = a * 5^b
6875  
6876 1 my $p = $LIB -> _pow($LIB -> _new("5"), $exp_lib);
6877 1 $sig_lib = $LIB -> _mul($sig_lib, $p);
6878 }
6879  
6880 # Adjust for the case when the conversion to decimal introduced trailing
6881 # zeros in the significand.
6882  
6883 1368 my $n = $LIB -> _zeros($sig_lib);
6884 1368 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 213 1155 1368
6885 213 $n = $LIB -> _new($n);
6886 213 $sig_lib = $LIB -> _rsft($sig_lib, $n, 10);
6887 213 ($exp_lib, $exp_sgn) = $LIB -> _sadd($exp_lib, $exp_sgn, $n, '+');
6888 }
6889  
6890 1368 return $sig_sgn, $sig_lib, $exp_sgn, $exp_lib;
6891 }
6892  
6893 # Takes any string representing a valid hexadecimal number and splits it into
6894 # four parts: the sign of the significand, the absolute value of the
6895 # significand as a libray thingy, the sign of the exponent, and the absolute
6896 # value of the exponent as a library thingy.
6897  
6898 1123 ▶sub _hex_str_to_flt_lib_parts {
Subroutine _hex_str_to_flt_lib_parts: called CC=2 SCAR=6.9
6899 1123 my $class = shift;
6900 1123 my $str = shift;
6901 1123 ▶ 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)) 1119 4 1123
6902 # 4 bits pr. chr
6903 1119 return $class -> _bin_str_parts_to_flt_lib_parts(@parts, 4);
6904 }
6905 4 return;
6906 }
6907  
6908 # Takes any string representing a valid octal number and splits it into four
6909 # parts: the sign of the significand, the absolute value of the significand as
6910 # a libray thingy, the sign of the exponent, and the absolute value of the
6911 # exponent as a library thingy.
6912  
6913 6 ▶sub _oct_str_to_flt_lib_parts {
Subroutine _oct_str_to_flt_lib_parts: called CC=2 SCAR=7.4
6914 6 my $class = shift;
6915 6 my $str = shift;
6916 6 ▶ 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)) 6 0 6
6917 # 3 bits pr. chr
6918 6 return $class -> _bin_str_parts_to_flt_lib_parts(@parts, 3);
6919 }
6920 0 return;
6921 }
6922  
6923 # Takes any string representing a valid binary number and splits it into four
6924 # parts: the sign of the significand, the absolute value of the significand as
6925 # a libray thingy, the sign of the exponent, and the absolute value of the
6926 # exponent as a library thingy.
6927  
6928 314 ▶sub _bin_str_to_flt_lib_parts {
Subroutine _bin_str_to_flt_lib_parts: called CC=2 SCAR=6.9
6929 314 my $class = shift;
6930 314 my $str = shift;
6931 314 ▶ 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)) 255 59 314
6932 # 1 bit pr. chr
6933 255 return $class -> _bin_str_parts_to_flt_lib_parts(@parts, 1);
6934 }
6935 59 return;
6936 }
6937  
6938 # Decimal string is split into the sign of the signficant, the absolute value
6939 # of the significand as library thingy, the sign of the exponent, and the
6940 # absolute value of the exponent as a a library thingy.
6941  
6942 8722 ▶sub _dec_str_to_flt_lib_parts {
Subroutine _dec_str_to_flt_lib_parts: called CC=2 SCAR=6.9
6943 8722 my $class = shift;
6944 8722 my $str = shift;
6945 8722 ▶ 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)) 7291 1431 8722
6946 7291 return $class -> _dec_str_parts_to_flt_lib_parts(@parts);
6947 }
6948 1431 return;
6949 }
6950  
6951 # Decimal string to a string using decimal floating point notation.
6952  
6953 0 ▶sub dec_str_to_dec_flt_str {
Subroutine dec_str_to_dec_flt_str: not called CC=2 SCAR=17.9
Pod documented
6954 0 my $class = shift;
6955 0 my $str = shift;
6956 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
6957 0 return $class -> _flt_lib_parts_to_flt_str(@parts);
6958 }
6959 0 return;
6960 }
6961  
6962 # Hexdecimal string to a string using decimal floating point notation.
6963  
6964 0 ▶sub hex_str_to_dec_flt_str {
Subroutine hex_str_to_dec_flt_str: not called CC=2 SCAR=17.9
Pod documented
6965 0 my $class = shift;
6966 0 my $str = shift;
6967 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
6968 0 return $class -> _flt_lib_parts_to_flt_str(@parts);
6969 }
6970 0 return;
6971 }
6972  
6973 # Octal string to a string using decimal floating point notation.
6974  
6975 0 ▶sub oct_str_to_dec_flt_str {
Subroutine oct_str_to_dec_flt_str: not called CC=2 SCAR=17.9
Pod documented
6976 0 my $class = shift;
6977 0 my $str = shift;
6978 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
6979 0 return $class -> _flt_lib_parts_to_flt_str(@parts);
6980 }
6981 0 return;
6982 }
6983  
6984 # Binary string to a string decimal floating point notation.
6985  
6986 0 ▶sub bin_str_to_dec_flt_str {
Subroutine bin_str_to_dec_flt_str: not called CC=2 SCAR=17.9
Pod documented
6987 0 my $class = shift;
6988 0 my $str = shift;
6989 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
6990 0 return $class -> _flt_lib_parts_to_flt_str(@parts);
6991 }
6992 0 return;
6993 }
6994  
6995 # Decimal string to decimal notation (no exponent).
6996  
6997 0 ▶sub dec_str_to_dec_str {
Subroutine dec_str_to_dec_str: not called CC=2 SCAR=17.9
Pod documented
6998 0 my $class = shift;
6999 0 my $str = shift;
7000 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
7001 0 return $class -> _flt_lib_parts_to_dec_str(@parts);
7002 }
7003 0 return;
7004 }
7005  
7006 # Hexdecimal string to decimal notation (no exponent).
7007  
7008 0 ▶sub hex_str_to_dec_str {
Subroutine hex_str_to_dec_str: not called CC=2 SCAR=17.9
Pod documented
7009 0 my $class = shift;
7010 0 my $str = shift;
7011 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
7012 0 return $class -> _flt_lib_parts_to_dec_str(@parts);
7013 }
7014 0 return;
7015 }
7016  
7017 # Octal string to decimal notation (no exponent).
7018  
7019 0 ▶sub oct_str_to_dec_str {
Subroutine oct_str_to_dec_str: not called CC=2 SCAR=17.9
Pod documented
7020 0 my $class = shift;
7021 0 my $str = shift;
7022 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
7023 0 return $class -> _flt_lib_parts_to_dec_str(@parts);
7024 }
7025 0 return;
7026 }
7027  
7028 # Binary string to decimal notation (no exponent).
7029  
7030 0 ▶sub bin_str_to_dec_str {
Subroutine bin_str_to_dec_str: not called CC=2 SCAR=17.9
Pod documented
7031 0 my $class = shift;
7032 0 my $str = shift;
7033 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
7034 0 return $class -> _flt_lib_parts_to_dec_str(@parts);
7035 }
7036 0 return;
7037 }
7038  
7039 0 ▶sub _flt_lib_parts_to_flt_str {
Subroutine _flt_lib_parts_to_flt_str: not called CC=1 SCAR=6.9
7040 0 my $class = shift;
7041 0 my @parts = @_;
7042 0 return $parts[0] . $LIB -> _str($parts[1])
7043 . 'e' . $parts[2] . $LIB -> _str($parts[3]);
7044 }
7045  
7046 0 ▶sub _flt_lib_parts_to_dec_str {
Subroutine _flt_lib_parts_to_dec_str: not called CC=3 SCAR=24.8
7047 0 my $class = shift;
7048 0 my @parts = @_;
7049  
7050 # The number is an integer iff the exponent is non-negative.
7051  
7052 0 ▶ if ($parts[2] eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parts[2] eq '+') { } 0 0 0
7053 0 my $str = $parts[0]
7054 . $LIB -> _str($LIB -> _lsft($parts[1], $parts[3], 10));
7055 0 return $str;
7056 }
7057  
7058 # If it is not an integer, add a decimal point.
7059  
7060 else {
7061 0 my $mant = $LIB -> _str($parts[1]);
7062 0 my $mant_len = CORE::length($mant);
7063 0 my $expo = $LIB -> _num($parts[3]);
7064 0 my $len_cmp = $mant_len <=> $expo;
7065 0 ▶ if ($len_cmp <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_cmp <= 0) { } 0 0 0
7066 0 return $parts[0] . '0.' . '0' x ($expo - $mant_len) . $mant;
7067 } else {
7068 0 substr $mant, $mant_len - $expo, 0, '.';
7069 0 return $parts[0] . $mant;
7070 }
7071 }
7072 }
7073  
7074 # Takes four arguments, the sign of the significand, the absolute value of the
7075 # significand as a libray thingy, the sign of the exponent, and the absolute
7076 # value of the exponent as a library thingy, and returns three parts: the sign
7077 # of the rational number, the absolute value of the numerator as a libray
7078 # thingy, and the absolute value of the denominator as a library thingy.
7079 #
7080 # For example, to convert data representing the value "+12e-2", then
7081 #
7082 # $sm = "+";
7083 # $m = $LIB -> _new("12");
7084 # $se = "-";
7085 # $e = $LIB -> _new("2");
7086 # ($sr, $n, $d) = $class -> _flt_lib_parts_to_rat_lib_parts($sm, $m, $se, $e);
7087 #
7088 # returns data representing the same value written as the fraction "+3/25"
7089 #
7090 # $sr = "+"
7091 # $n = $LIB -> _new("3");
7092 # $d = $LIB -> _new("12");
7093  
7094 1030 ▶sub _flt_lib_parts_to_rat_lib_parts {
Subroutine _flt_lib_parts_to_rat_lib_parts: called CC=3 SCAR=12.0
7095 1030 my $self = shift;
7096 1030 my ($msgn, $mabs, $esgn, $eabs) = @_;
7097  
7098 1030 ▶ if ($esgn eq '-') { # "12e-2" -> "12/100" -> "3/25"
Branch: 2 branches
BranchTrueFalseTotal
if ($esgn eq '-') { } 1030 0 1030
elsif (not $LIB->_is_zero($eabs)) { } 0 0 0
7099 1030 my $num_lib = $LIB -> _copy($mabs);
7100 1030 my $den_lib = $LIB -> _1ex($LIB -> _num($eabs));
7101 1030 my $gcd_lib = $LIB -> _gcd($LIB -> _copy($num_lib), $den_lib);
7102 1030 $num_lib = $LIB -> _div($LIB -> _copy($num_lib), $gcd_lib);
7103 1030 $den_lib = $LIB -> _div($den_lib, $gcd_lib);
7104 1030 return $msgn, $num_lib, $den_lib;
7105 }
7106  
7107 elsif (!$LIB -> _is_zero($eabs)) { # "12e+2" -> "1200" -> "1200/1"
7108 0 return $msgn, $LIB -> _lsft($LIB -> _copy($mabs), $eabs, 10),
7109 $LIB -> _one();
7110 }
7111  
7112 else { # "12e+0" -> "12" -> "12/1"
7113 0 return $msgn, $mabs, $LIB -> _one();
7114 }
7115 }
7116  
7117 # Add the function _register_callback() to Math::BigInt. It is provided for
7118 # backwards compabibility so that old version of Math::BigRat etc. don't
7119 # complain about missing it.
7120  
7121 0 ▶sub _register_callback { }
Subroutine _register_callback: not called CC=1 SCAR=0.0
7122  
7123 ###############################################################################
7124 # Other methods.
7125 ###############################################################################
7126  
7127 6024 ▶sub objectify {
Subroutine objectify: called CC=27 SCAR=34.2
Pod undocumented
7128 # Convert strings and "foreign objects" to the objects we want.
7129  
7130 # The first argument, $count, is the number of following arguments that
7131 # objectify() looks at and converts to objects. The first is a classname.
7132 # If the given count is 0, all arguments will be used.
7133  
7134 # After the count is read, objectify obtains the name of the class to which
7135 # the following arguments are converted. If the second argument is a
7136 # reference, use the reference type as the class name. Otherwise, if it is
7137 # a string that looks like a class name, use that. Otherwise, use $class.
7138  
7139 # Caller: Gives us:
7140 #
7141 # $x->badd(1); => ref x, scalar y
7142 # Class->badd(1, 2); => classname x (scalar), scalar x, scalar y
7143 # Class->badd(Class->(1), 2); => classname x (scalar), ref x, scalar y
7144 # Math::BigInt::badd(1, 2); => scalar x, scalar y
7145  
7146 # A shortcut for the common case $x->unary_op(), in which case the argument
7147 # list is (0, $x) or (1, $x).
7148  
7149 6024 ▶ return (ref($_[1]), $_[1]) if @_ == 2 && ($_[0] || 0) == 1 && ref($_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 and ($_[0] || 0) == 1 and ref $_[1] 205 5819 6024
Condition87%7/8
@_ == 2 and ($_[0] || 0) == 1 and ref $_[1]
!ll&&!rl&&r
58190205
@_ == 2 and ($_[0] || 0) == 1
!ll&&!rl&&r
58181205
$_[0] || 0
l!l
2051
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
7150  
7151 # Check the context.
7152  
7153 5819 ▶ unless (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
unless (wantarray) 0 5819 5819
7154 0 croak(__PACKAGE__ . "::objectify() needs list context");
7155 }
7156  
7157 # Get the number of arguments to objectify.
7158  
7159 5819 my $count = shift;
7160  
7161 # Initialize the output array.
7162  
7163 5819 my @a = @_;
7164  
7165 # If the first argument is a reference, use that reference type as our
7166 # class name. Otherwise, if the first argument looks like a class name,
7167 # then use that as our class name. Otherwise, use the default class name.
7168  
7169 5819 my $class;
7170 5819 ▶ if (ref($a[0])) { # reference?
Branch: 2 branches
BranchTrueFalseTotal
if (ref $a[0]) { } 4395 1424 5819
elsif ($a[0] =~ /^[A-Z].*::/) { } 1412 12 1424
7171 4395 $class = ref($a[0]);
7172 } elsif ($a[0] =~ /^[A-Z].*::/) { # string with class name?
7173 1412 $class = shift @a;
7174 } else {
7175 12 $class = __PACKAGE__; # default class name
7176 }
7177  
7178 5819 ▶ $count ||= @a;
Condition66%2/3
$count ||= @a
l!l&&r!l&&!r
55282910
MC/DC0%0/2
$count ||= @a
$count @a
Truth table66%2/3
A ||= B
A= $countB= @a
ABresult
1X1
011
000
7179 5819 unshift @a, $class;
7180  
7181 # What we upgrade to, if anything. Note that we need the whole upgrade
7182 # chain, since there might be multiple levels of upgrading. E.g., class A
7183 # upgrades to class B, which upgrades to class C. Delay getting the chain
7184 # until we actually need it.
7185  
7186 5819 my @upg = ();
7187 5819 my $have_upgrade_chain = 0;
7188  
7189 # Disable downgrading, because Math::BigFloat -> foo('1.0', '2.0') needs
7190 # floats.
7191  
7192 5819 my $dng = $class -> downgrade();
7193 5819 $class -> downgrade(undef);
7194  
7195 5819 ARG: for my $i (1 .. $count) {
7196  
7197 10772 my $ref = ref $a[$i];
7198  
7199 # Perl scalars are fed to the appropriate constructor.
7200  
7201 10772 ▶ unless ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 5103 5669 10772
7202 5103 $a[$i] = $class -> new($a[$i]);
7203 5103 next;
7204 }
7205  
7206 # If it is an object of the right class, all is fine.
7207  
7208 5669 ▶ next if $ref -> isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $ref->isa($class) 5433 236 5669
7209  
7210 # Upgrading is OK, so skip further tests if the argument is upgraded,
7211 # but first get the whole upgrade chain if we haven't got it yet.
7212  
7213 236 ▶ unless ($have_upgrade_chain) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($have_upgrade_chain) 181 55 236
7214 181 my $cls = $class;
7215 181 my $upg = $cls -> upgrade();
7216 181 ▶ while (defined $upg) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $upg) 9 181 190
7217 9 ▶ last if $upg eq $cls;
Branch: 1 branch
BranchTrueFalseTotal
if $upg eq $cls 0 9 9
7218 9 push @upg, $upg;
7219 9 $cls = $upg;
7220 9 $upg = $cls -> upgrade();
7221 }
7222 181 $have_upgrade_chain = 1;
7223 }
7224  
7225 236 for my $upg (@upg) {
7226 9 ▶ next ARG if $ref -> isa($upg);
Branch: 1 branch
BranchTrueFalseTotal
if $ref->isa($upg) 9 0 9
7227 }
7228  
7229 # See if we can call one of the as_xxx() methods. We don't know whether
7230 # the as_xxx() method returns an object or a scalar, so re-check
7231 # afterwards.
7232  
7233 227 my $recheck = 0;
7234  
7235 227 ▶ if ($class -> isa('Math::BigInt')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($class->isa('Math::BigInt')) { } 2 225 227
elsif ($class->isa('Math::BigRat')) { } 16 209 225
elsif ($class->isa('Math::BigFloat')) { } 209 0 209
7236 2 ▶ if ($a[$i] -> can('as_int')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($a[$i]->can('as_int')) { } 2 0 2
elsif ($a[$i]->can('as_number')) { } 0 0 0
7237 2 $a[$i] = $a[$i] -> as_int();
7238 2 $recheck = 1;
7239 } elsif ($a[$i] -> can('as_number')) {
7240 0 $a[$i] = $a[$i] -> as_number();
7241 0 $recheck = 1;
7242 }
7243 }
7244  
7245 elsif ($class -> isa('Math::BigRat')) {
7246 16 ▶ if ($a[$i] -> can('as_rat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a[$i]->can("as_rat")) 16 0 16
7247 16 $a[$i] = $a[$i] -> as_rat();
7248 16 $recheck = 1;
7249 }
7250 }
7251  
7252 elsif ($class -> isa('Math::BigFloat')) {
7253 209 ▶ if ($a[$i] -> can('as_float')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($a[$i]->can("as_float")) 209 0 209
7254 209 $a[$i] = $a[$i] -> as_float();
7255 209 $recheck = 1;
7256 }
7257 }
7258  
7259 # If we called one of the as_xxx() methods, recheck.
7260  
7261 227 ▶ if ($recheck) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recheck) 227 0 227
7262 227 $ref = ref($a[$i]);
7263  
7264 # Perl scalars are fed to the appropriate constructor.
7265  
7266 227 ▶ unless ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ref) 0 227 227
7267 0 $a[$i] = $class -> new($a[$i]);
7268 0 next;
7269 }
7270  
7271 # If it is an object of the right class, all is fine.
7272  
7273 227 ▶ next if $ref -> isa($class);
Branch: 1 branch
BranchTrueFalseTotal
if $ref->isa($class) 213 14 227
7274 }
7275  
7276 # Last resort.
7277  
7278 14 $a[$i] = $class -> new($a[$i]);
7279 }
7280  
7281 # Restore the downgrading.
7282  
7283 5819 $class -> downgrade($dng);
7284  
7285 5819 return @a;
7286 }
7287  
7288 185 ▶sub import {
Subroutine import: called CC=29 SCAR=43.0
7289 185 my $class = shift;
7290 185 $IMPORT++; # remember we did import()
7291 185 my @a; # unrecognized arguments
7292  
7293 185 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 33 185 218
7294 33 my $param = shift;
7295  
7296 # Enable overloading of constants.
7297  
7298 33 ▶ if ($param eq ':constant') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq ":constant") 1 32 33
7299 overload::constant
7300  
7301 2 ▶ integer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
7302 2 $class -> new(shift);
7303 },
7304  
7305 0 ▶ float => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
7306 0 $class -> new(shift);
7307 },
7308  
7309 0 ▶ binary => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
7310 # E.g., a literal 0377 shall result in an object whose
7311 # value is decimal 255, but new("0377") returns decimal
7312 # 377.
7313 0 ▶ return $class -> from_oct($_[0]) if $_[0] =~ /^0_*[0-7]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^0_*[0-7]/ 0 0 0
7314 0 $class -> new(shift);
7315 1 };
7316 1 next;
7317 }
7318  
7319 # Upgrading.
7320  
7321 32 ▶ if ($param eq 'upgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "upgrade") 4 28 32
7322 4 $class -> upgrade(shift);
7323 4 next;
7324 }
7325  
7326 # Downgrading.
7327  
7328 28 ▶ if ($param eq 'downgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "downgrade") 0 28 28
7329 0 $class -> downgrade(shift);
7330 0 next;
7331 }
7332  
7333 # Accuracy.
7334  
7335 28 ▶ if ($param eq 'accuracy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "accuracy") 0 28 28
7336 0 $class -> accuracy(shift);
7337 0 next;
7338 }
7339  
7340 # Precision.
7341  
7342 28 ▶ if ($param eq 'precision') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "precision") 0 28 28
7343 0 $class -> precision(shift);
7344 0 next;
7345 }
7346  
7347 # Rounding mode.
7348  
7349 28 ▶ if ($param eq 'round_mode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "round_mode") 0 28 28
7350 0 $class -> round_mode(shift);
7351 0 next;
7352 }
7353  
7354 # Fall-back accuracy.
7355  
7356 28 ▶ if ($param eq 'div_scale') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "div_scale") 0 28 28
7357 0 $class -> div_scale(shift);
7358 0 next;
7359 }
7360  
7361 # Backend library.
7362  
7363 28 ▶ if ($param =~ /^(lib|try|only)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(lib|try|only)\z/) 24 4 28
7364 # try => 0 (no warn if unavailable module)
7365 # lib => 1 (warn on fallback)
7366 # only => 2 (die on fallback)
7367  
7368 # Get the list of user-specified libraries.
7369  
7370 24 ▶ croak "Library argument for import parameter '$param' is missing"
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 24 24
7371 unless @_;
7372 24 my $libs = shift;
7373 24 ▶ croak "Library argument for import parameter '$param' is undefined"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $libs 0 24 24
7374 unless defined($libs);
7375  
7376 # Check and clean up the list of user-specified libraries.
7377  
7378 24 my @libs;
7379 24 for my $lib (split /,/, $libs) {
7380 24 $lib =~ s/^\s+//;
7381 24 $lib =~ s/\s+$//;
7382  
7383 24 ▶ if ($lib =~ /[^a-zA-Z0-9_:]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lib =~ /[^a-zA-Z0-9_:]/) 0 24 24
7384 0 carp "Library name '$lib' contains invalid characters";
7385 0 next;
7386 }
7387  
7388 24 ▶ if (! CORE::length $lib) {
Branch: 1 branch
BranchTrueFalseTotal
unless (CORE::length $lib) 0 24 24
7389 0 carp "Library name is empty";
7390 0 next;
7391 }
7392  
7393 24 ▶ $lib = "Math::BigInt::$lib" if $lib !~ /^Math::BigInt::/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $lib =~ /^Math::BigInt::/i 19 5 24
7394  
7395 # If a library has already been loaded, that is OK only if the
7396 # requested library is identical to the loaded one.
7397  
7398 24 ▶ if (defined($LIB)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $LIB) 8 16 24
7399 8 ▶ if ($lib ne $LIB) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lib ne $LIB) 0 8 8
7400 #carp "Library '$LIB' has already been loaded, so",
7401 # " ignoring requested library '$lib'";
7402 }
7403 8 next;
7404 }
7405  
7406 16 push @libs, $lib;
7407 }
7408  
7409 24 ▶ next if defined $LIB;
Branch: 1 branch
BranchTrueFalseTotal
if defined $LIB 8 16 24
7410  
7411 16 ▶ croak "Library list contains no valid libraries" unless @libs;
Branch: 1 branch
BranchTrueFalseTotal
unless @libs 0 16 16
7412  
7413 # Try to load the specified libraries, if any.
7414  
7415 16 ▶ for (my $i = 0 ; $i <= $#libs ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#libs) 16 0 16
7416 16 my $lib = $libs[$i];
7417 16 eval "require $lib";
7418 16 ▶ unless ($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 16 0 16
7419 16 $LIB = $lib;
7420 16 last;
7421 }
7422 }
7423  
7424 16 ▶ next if defined $LIB;
Branch: 1 branch
BranchTrueFalseTotal
if defined $LIB 16 0 16
7425  
7426 # No library has been loaded, and none of the requested libraries
7427 # could be loaded, and fallback and the user doesn't allow
7428 # fallback.
7429  
7430 0 ▶ if ($param eq 'only') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "only") 0 0 0
7431 0 croak "Couldn't load the specified math lib(s) ",
7432 join(", ", map "'$_'", @libs),
7433 ", and fallback to '$DEFAULT_LIB' is not allowed";
7434 }
7435  
7436 # No library has been loaded, and none of the requested libraries
7437 # could be loaded, but the user accepts the use of a fallback
7438 # library, so try to load it.
7439  
7440 0 eval "require $DEFAULT_LIB";
7441 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
7442 0 croak "Couldn't load the specified math lib(s) ",
7443 join(", ", map "'$_'", @libs),
7444 ", not even the fallback lib '$DEFAULT_LIB'";
7445 }
7446  
7447 # The fallback library was successfully loaded, but the user
7448 # might want to know that we are using the fallback.
7449  
7450 0 ▶ if ($param eq 'lib') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "lib") 0 0 0
7451 0 carp "Couldn't load the specified math lib(s) ",
7452 join(", ", map "'$_'", @libs),
7453 ", so using fallback lib '$DEFAULT_LIB'";
7454 }
7455  
7456 0 next;
7457 }
7458  
7459 # Unrecognized parameter.
7460  
7461 4 push @a, $param;
7462 }
7463  
7464 # Any non-':constant' stuff is handled by our parent, Exporter
7465  
7466 185 $class -> SUPER::import(@a); # for subclasses
7467 185 ▶ $class -> export_to_level(1, $class, @a) if @a; # need this, too
Branch: 1 branch
BranchTrueFalseTotal
if @a 3 182 185
7468  
7469 # We might not have loaded any backend library yet, either because the user
7470 # didn't specify any, or because the specified libraries failed to load and
7471 # the user allows the use of a fallback library.
7472  
7473 185 ▶ unless (defined $LIB) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $LIB) 34 151 185
7474 34 eval "require $DEFAULT_LIB";
7475 34 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 34 34
7476 0 croak "No lib specified, and couldn't load the default",
7477 " lib '$DEFAULT_LIB'";
7478 }
7479 34 $LIB = $DEFAULT_LIB;
7480 }
7481  
7482 # import done
7483 }
7484  
7485 50 1;
7486  
7487 __END__
« BigFloat.pm ↑ Summary BigInt/Lib.pm »