BigRat.pm

filter: Statementstmt 55.1% 939 / 1702 Branchbran 42.0% 588 / 1400 Conditioncond 42.5% 276 / 649 MC/DCmcdc 30.3% 120 / 396 Subroutinesub 58.8% 93 / 158 Podpod 98.9% 97 / 98 totaltotal 47.9% 2113 / 4403 CC 922 SCAR 49.3
CC
922
Cov
48%
bmuladd
1482.0
btdiv
992.0
bfib
420.0
CRAP 137.8
« BigInt/Calc.pm ↑ Summary BigFloat.pm »
1 #
2 # "Tax the rat farms." - Lord Vetinari
3 #
4  
5 # The following hash values are used:
6  
7 # sign : "+", "-", "+inf", "-inf", or "NaN"
8 # _d : denominator
9 # _n : numerator (value = _n/_d)
10 # accuracy : accuracy
11 # precision : precision
12  
13 # You should not look at the innards of a BigRat - use the methods for this.
14  
15 package Math::BigRat;
16  
17 16 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 16 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 16 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 16 ▶use Carp qw< carp croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 16 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 16 ▶use Math::BigFloat qw<>;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 16 our $VERSION = '2.005003';
26 16 $VERSION =~ tr/_//d;
27  
28 16 require Exporter;
29 16 our @ISA = qw< Math::BigFloat >;
30  
31 use overload
32  
33 # overload key: with_assign
34  
35 190 ▶ '+' => sub { $_[0] -> copy() -> badd($_[1]); },
Subroutine BEGIN: called CC=1 SCAR=1.9
Subroutine __ANON__: called CC=1 SCAR=0.0
36  
37 204 ▶ '-' => sub { my $c = $_[0] -> copy;
Subroutine __ANON__: called CC=2 SCAR=7.2
38 204 ▶ $_[2] ? $c -> bneg() -> badd( $_[1])
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 204 204
39 : $c -> bsub($_[1]); },
40  
41 208 ▶ '*' => sub { $_[0] -> copy() -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 186 ▶ '/' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bfdiv($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 186 186
44 : $_[0] -> copy() -> bfdiv($_[1]); },
45  
46 75 ▶ '%' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bfmod($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 75 75
47 : $_[0] -> copy() -> bfmod($_[1]); },
48  
49 1 ▶ '**' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bpow($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 1 1
50 : $_[0] -> copy() -> bpow($_[1]); },
51  
52 0 ▶ '<<' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bblsft($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
53 : $_[0] -> copy() -> bblsft($_[1]); },
54  
55 0 ▶ '>>' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bbrsft($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
56 : $_[0] -> copy() -> bbrsft($_[1]); },
57  
58 # overload key: assign
59  
60 1 ▶ '+=' => sub { $_[0] -> badd($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
61  
62 1 ▶ '-=' => sub { $_[0] -> bsub($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
63  
64 1 ▶ '*=' => sub { $_[0] -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 0 ▶ '/=' => sub { scalar $_[0] -> bfdiv($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67  
68 0 ▶ '%=' => sub { $_[0] -> bfmod($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
69  
70 0 ▶ '**=' => sub { $_[0] -> bpow($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
71  
72 0 ▶ '<<=' => sub { $_[0] -> bblsft($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73  
74 0 ▶ '>>=' => sub { $_[0] -> bbrsft($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75  
76 # 'x=' => sub { },
77  
78 # '.=' => sub { },
79  
80 # overload key: num_comparison
81  
82 64 ▶ '<' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> blt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 64 64
83 : $_[0] -> blt($_[1]); },
84  
85 0 ▶ '<=' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> ble($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
86 : $_[0] -> ble($_[1]); },
87  
88 84 ▶ '>' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bgt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 84 84
89 : $_[0] -> bgt($_[1]); },
90  
91 0 ▶ '>=' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bge($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
92 : $_[0] -> bge($_[1]); },
93  
94 19 ▶ '==' => sub { $_[0] -> beq($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
95  
96 0 ▶ '!=' => sub { $_[0] -> bne($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
97  
98 # overload key: 3way_comparison
99  
100 0 ▶ '<=>' => sub { my $cmp = $_[0] -> bcmp($_[1]);
Subroutine __ANON__: not called CC=3 SCAR=24.8
101 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
102  
103 3525 ▶ 'cmp' => sub { $_[2] ? "$_[1]" cmp $_[0] -> bstr()
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 3525 3525
104 : $_[0] -> bstr() cmp "$_[1]"; },
105  
106 # overload key: str_comparison
107  
108 # 'lt' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrlt($_[0])
109 # : $_[0] -> bstrlt($_[1]); },
110 #
111 # 'le' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrle($_[0])
112 # : $_[0] -> bstrle($_[1]); },
113 #
114 # 'gt' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrgt($_[0])
115 # : $_[0] -> bstrgt($_[1]); },
116 #
117 # 'ge' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrge($_[0])
118 # : $_[0] -> bstrge($_[1]); },
119 #
120 # 'eq' => sub { $_[0] -> bstreq($_[1]); },
121 #
122 # 'ne' => sub { $_[0] -> bstrne($_[1]); },
123  
124 # overload key: binary
125  
126 289 ▶ '&' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> band($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 289 289
127 : $_[0] -> copy() -> band($_[1]); },
128  
129 0 ▶ '&=' => sub { $_[0] -> band($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
130  
131 289 ▶ '|' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bior($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 289 289
132 : $_[0] -> copy() -> bior($_[1]); },
133  
134 0 ▶ '|=' => sub { $_[0] -> bior($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135  
136 289 ▶ '^' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bxor($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 289 289
137 : $_[0] -> copy() -> bxor($_[1]); },
138  
139 0 ▶ '^=' => sub { $_[0] -> bxor($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
140  
141 # '&.' => sub { },
142  
143 # '&.=' => sub { },
144  
145 # '|.' => sub { },
146  
147 # '|.=' => sub { },
148  
149 # '^.' => sub { },
150  
151 # '^.=' => sub { },
152  
153 # overload key: unary
154  
155 0 ▶ 'neg' => sub { $_[0] -> copy() -> bneg(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
156  
157 # '!' => sub { },
158  
159 0 ▶ '~' => sub { $_[0] -> copy() -> bnot(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
160  
161 # '~.' => sub { },
162  
163 # overload key: mutators
164  
165 20 ▶ '++' => sub { $_[0] -> binc() },
Subroutine __ANON__: called CC=1 SCAR=0.0
166  
167 20 ▶ '--' => sub { $_[0] -> bdec() },
Subroutine __ANON__: called CC=1 SCAR=0.0
168  
169 # overload key: func
170  
171 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
172 : $_[0] -> copy() -> batan2($_[1]); },
173  
174 0 ▶ 'cos' => sub { $_[0] -> copy() -> bcos(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
175  
176 0 ▶ 'sin' => sub { $_[0] -> copy() -> bsin(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
177  
178 0 ▶ 'exp' => sub { $_[0] -> copy() -> bexp($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
179  
180 0 ▶ 'abs' => sub { $_[0] -> copy() -> babs(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
181  
182 28 ▶ 'log' => sub { $_[0] -> copy() -> blog(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
183  
184 0 ▶ 'sqrt' => sub { $_[0] -> copy() -> bsqrt(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
185  
186 2 ▶ 'int' => sub { $_[0] -> copy() -> bint(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
187  
188 # overload key: conversion
189  
190 0 ▶ 'bool' => sub { $_[0] -> is_zero() ? '' : 1; },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_zero ? : 0 0 0
191  
192 3972 ▶ '""' => sub { $_[0] -> bstr(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
193  
194 6 ▶ '0+' => sub { $_[0] -> numify(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
195  
196 0 ▶ '=' => sub { $_[0]->copy(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
197  
198 16 ;
199  
200 16 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
201 16 *objectify = \&Math::BigInt::objectify;
202  
203 16 *AUTOLOAD = \&Math::BigFloat::AUTOLOAD; # can't inherit AUTOLOAD
204 16 *as_number = \&as_int;
205 16 *is_pos = \&is_positive;
206 16 *is_neg = \&is_negative;
207 }
208  
209 ##############################################################################
210 # Global constants and flags. Access these only via the accessor methods!
211  
212 16 our $accuracy = undef;
213 16 our $precision = undef;
214 16 our $round_mode = 'even';
215 16 our $div_scale = 40;
216  
217 16 our $upgrade = undef;
218 16 our $downgrade = undef;
219  
220 16 our $_trap_nan = 0; # croak on NaNs?
221 16 our $_trap_inf = 0; # croak on Infs?
222  
223 16 my $nan = 'NaN'; # constant for easier life
224  
225 16 my $LIB = Math::BigInt -> config('lib'); # math backend library
226  
227 # Has import() been called yet? This variable is needed to make "require" work.
228  
229 16 my $IMPORT = 0;
230  
231 # Compare the following function with @ISA above. This inheritance mess needs a
232 # clean up. When doing so, also consider the BEGIN block and the AUTOLOAD code.
233 # Fixme!
234  
235 3409 ▶sub isa {
Subroutine isa: called CC=2 SCAR=6.9
Pod undocumented
236 3409 ▶ return 0 if $_[1] =~ /^Math::Big(Int|Float)/; # we aren't
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /^Math::Big(Int|Float)/ 2217 1192 3409
237 1192 UNIVERSAL::isa(@_);
238 }
239  
240 ##############################################################################
241  
242 9958 ▶sub new {
Subroutine new: called CC=39 SCAR=37.7
Pod documented
243 # Create a new Math::BigFloat object from a string or another Math::BigInt,
244 # Math::BigFloat, or Math::BigRat object. See hash keys documented at top.
245  
246 9958 my $self = shift;
247 9958 my $selfref = ref $self;
248 9958 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
099580
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
249  
250 # Make "require" work.
251  
252 9958 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 9958 9958
253  
254 9958 ▶ if (@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 0 9958 9958
255 0 carp("Superfluous arguments to new() ignored.");
256 }
257  
258 # Calling new() with no input arguments has been discouraged for more than
259 # 10 years, but people apparently still use it, so we still support it.
260 # Also, if any of the arguments is undefined, return zero.
261  
262 9958 ▶ if (@_ == 0 ||
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0 or @_ == 1 and not defined $_[0] or @_ == 2 and not defined $_[0] && defined $_[1]) 0 9958 9958
Condition46%7/15
@_ == 0 or @_ == 1 and not defined $_[0] or @_ == 2 and not defined $_[0] && defined $_[1]
l!l&&r!l&&!r
009958
@_ == 0 or @_ == 1 and not defined $_[0]
l!l&&r!l&&!r
009958
@_ == 1 and not defined $_[0]
!ll&&!rl&&r
499540
@_ == 2 and not defined $_[0] && defined $_[1]
!ll&&!rl&&r
995440
defined $_[0] && defined $_[1]
!ll&&!rl&&r
004
MC/DC0%0/6
@_ == 0 or @_ == 1 and not defined $_[0] or @_ == 2 and not defined $_[0] && defined $_[1]
@_ == 0 @_ == 1 not defined $_[0] @_ == 2 defined $_[0] defined $_[1]
Truth table20%2/10
A or B and not(C) or D and not(E && F)
A= @_ == 0B= @_ == 1C= not defined $_[0]D= @_ == 2E= defined $_[0]F= defined $_[1]
ABCDEFresult
1XXXXX1
011XXX1
00X10X1
00X1101
01010X1
0101101
00X0XX0
00X1110
0100XX0
0101110
263 @_ == 1 && !defined($_[0]) ||
264 @_ == 2 && (!defined($_[0]) || !defined($_[1])))
265 {
266 #carp("Use of uninitialized value in new()");
267 0 return $class -> bzero();
268 }
269  
270 9958 my @args = @_;
271  
272 # Initialize a new object.
273  
274 9958 $self = bless {}, $class;
275  
276 # Special cases for speed and to avoid infinite recursion. The methods
277 # Math::BigInt->as_rat() and Math::BigFloat->as_rat() call
278 # Math::BigRat->as_rat() (i.e., this method) with a scalar (non-ref)
279 # integer argument.
280  
281 9958 ▶ if (@args == 1 && !ref($args[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and not ref $args[0]) 9942 16 9958
Condition100%3/3
@args == 1 and not ref $args[0]
!ll&&!rl&&r
4129942
MC/DC100%2/2
@args == 1 and not ref $args[0]
@args == 1 not ref $args[0]
Truth table100%3/3
A and not(B)
A= @args == 1B= not ref $args[0]
ABresult
0X0
100
111
282  
283 # "3", "+3", "-3", "+001_2_3e+4"
284  
285 9942 ▶ if ($args[0] =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] =~ / ^ \s* # optional sign ( [+-]? ) # integer mantissa with optional leading zeros 0* ( [1-9] \d* (?: _ \d+ )* | 0 ) # optional non-negative exponent (?: [eE] \+? ( \d+ (?: _ \d+ )* ) )? \s* $ /x) 5299 4643 9942
286 ^
287 \s*
288  
289 # optional sign
290 ( [+-]? )
291  
292 # integer mantissa with optional leading zeros
293 0* ( [1-9] \d* (?: _ \d+ )* | 0 )
294  
295 # optional non-negative exponent
296 (?: [eE] \+? ( \d+ (?: _ \d+ )* ) )?
297  
298 \s*
299 $
300 }x)
301 {
302 5299 my $sign = $1;
303 5299 (my $mant = $2) =~ tr/_//d;
304 5299 my $expo = $3;
305 5299 ▶ $mant .= "0" x $expo if defined($expo) && $mant ne "0";
Branch: 1 branch
BranchTrueFalseTotal
if defined $expo and $mant ne 0 14 5285 5299
Condition66%2/3
defined $expo and $mant ne 0
!ll&&!rl&&r
5285014
MC/DC50%1/2
defined $expo and $mant ne 0
defined $expo $mant ne 0
Truth table66%2/3
A and B
A= defined $expoB= $mant ne 0
ABresult
0X0
100
111
306  
307 5299 $self -> {_n} = $LIB -> _new($mant);
308 5299 $self -> {_d} = $LIB -> _one();
309 5299 ▶ $self -> {sign} = $sign eq "-" && $mant ne "0" ? "-" : "+";
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' && $mant ne '0' ? : 1014 4285 5299
Condition100%3/3
$sign eq "-" and $mant ne 0
!ll&&!rl&&r
4268171014
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
310 5299 $self -> _dng();
311 5299 return $self;
312 }
313  
314 # "3/5", "+3/5", "-3/5", "+001_2_3e+4 / 05_6e7"
315  
316 4643 ▶ if ($args[0] =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] =~ m[ ^ \s* # optional leading sign ( [+-]? ) # integer mantissa with optional leading zeros 0* ( [1-9] \d* (?: _ \d+ )* | 0 ) # optional non-negative exponent (?: [eE] \+? ( \d+ (?: _ \d+ )* ) )? # fraction \s* / \s* # non-zero integer mantissa with optional leading zeros 0* ( [1-9] \d* (?: _ \d+ )* ) # optional non-negative exponent (?: [eE] \+? ( \d+ (?: _ \d+ )* ) )? \s* $ ]x) 2074 2569 4643
317 ^
318 \s*
319  
320 # optional leading sign
321 ( [+-]? )
322  
323 # integer mantissa with optional leading zeros
324 0* ( [1-9] \d* (?: _ \d+ )* | 0 )
325  
326 # optional non-negative exponent
327 (?: [eE] \+? ( \d+ (?: _ \d+ )* ) )?
328  
329 # fraction
330 \s* / \s*
331  
332 # non-zero integer mantissa with optional leading zeros
333 0* ( [1-9] \d* (?: _ \d+ )* )
334  
335 # optional non-negative exponent
336 (?: [eE] \+? ( \d+ (?: _ \d+ )* ) )?
337  
338 \s*
339 $
340 }x)
341 {
342 2074 my $sign = $1;
343  
344 2074 (my $mant1 = $2) =~ tr/_//d;
345 2074 my $expo1 = $3;
346 2074 ▶ $mant1 .= "0" x $expo1 if defined($expo1) && $mant1 ne "0";
Branch: 1 branch
BranchTrueFalseTotal
if defined $expo1 and $mant1 ne 0 6 2068 2074
Condition66%2/3
defined $expo1 and $mant1 ne 0
!ll&&!rl&&r
206806
MC/DC50%1/2
defined $expo1 and $mant1 ne 0
defined $expo1 $mant1 ne 0
Truth table66%2/3
A and B
A= defined $expo1B= $mant1 ne 0
ABresult
0X0
100
111
347  
348 2074 (my $mant2 = $4) =~ tr/_//d;
349 2074 my $expo2 = $5;
350 2074 ▶ $mant2 .= "0" x $expo2 if defined($expo2) && $mant2 ne "0";
Branch: 1 branch
BranchTrueFalseTotal
if defined $expo2 and $mant2 ne 0 7 2067 2074
Condition66%2/3
defined $expo2 and $mant2 ne 0
!ll&&!rl&&r
206707
MC/DC50%1/2
defined $expo2 and $mant2 ne 0
defined $expo2 $mant2 ne 0
Truth table66%2/3
A and B
A= defined $expo2B= $mant2 ne 0
ABresult
0X0
100
111
351  
352 2074 $self -> {_n} = $LIB -> _new($mant1);
353 2074 $self -> {_d} = $LIB -> _new($mant2);
354 2074 ▶ $self -> {sign} = $sign eq "-" && $mant1 ne "0" ? "-" : "+";
Branch: 1 branch
BranchTrueFalseTotal
$sign eq '-' && $mant1 ne '0' ? : 483 1591 2074
Condition100%3/3
$sign eq "-" and $mant1 ne 0
!ll&&!rl&&r
15874483
MC/DC100%2/2
$sign eq "-" and $mant1 ne 0
$sign eq "-" $mant1 ne 0
Truth table100%3/3
A and B
A= $sign eq "-"B= $mant1 ne 0
ABresult
0X0
100
111
355  
356 my $gcd = $LIB -> _gcd($LIB -> _copy($self -> {_n}),
357 2074 $self -> {_d});
358 2074 ▶ unless ($LIB -> _is_one($gcd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($gcd)) 174 1900 2074
359 174 $self -> {_n} = $LIB -> _div($self->{_n}, $gcd);
360 174 $self -> {_d} = $LIB -> _div($self->{_d}, $gcd);
361 }
362  
363 2074 ▶ $self -> _dng() if $self -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int 159 1915 2074
364 2074 return $self;
365 }
366  
367 }
368  
369 # If given exactly one argument which is a string that looks like a
370 # fraction, replace this argument with the fraction's numerator and
371 # denominator.
372  
373 2585 ▶ if (@args == 1 && !ref($args[0]) &&
Branch: 1 branch
BranchTrueFalseTotal
if (@args == 1 and not ref $args[0] and $args[0] =~ m[ ^ \s* ( \S+ ) \s* / \s* ( \S+ ) \s* $ ]x) 92 2493 2585
Condition100%6/6
@args == 1 and not ref $args[0] and $args[0] =~ m[ ^ \s* ( \S+ ) \s* / \s* ( \S+ ) \s* $ ]x
!ll&&!rl&&r
16247792
@args == 1 and not ref $args[0]
!ll&&!rl&&r
4122569
MC/DC100%3/3
@args == 1 and not ref $args[0] and $args[0] =~ m[ ^ \s* ( \S+ ) \s* / \s* ( \S+ ) \s* $ ]x
@args == 1 not ref $args[0] $args[0] =~ m[ ^ \s* ( \S+ ) \s* / \s* ( \S+ ) \s* $ ]x
Truth table100%4/4
A and not(B) and C
A= @args == 1B= not ref $args[0]C= $args[0] =~ m[ ^ \s* ( \S+ ) \s* / \s* ( \S+ ) \s* $ ]x
ABCresult
0XX0
10X0
1100
1111
374 $args[0] =~ m{ ^ \s* ( \S+ ) \s* / \s* ( \S+ ) \s* $ }x)
375 {
376 92 @args = ($1, $2);
377 }
378  
379 # Now get the numerator and denominator either by calling as_rat() or by
380 # letting Math::BigFloat->new() parse the argument as a string.
381  
382 2585 my ($n, $d);
383  
384 2585 ▶ if (@args >= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args >= 1) 2585 0 2585
385 2585 ▶ if (ref($args[0]) && $args[0] -> can('as_rat')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] and $args[0]->can('as_rat')) { } 16 2569 2585
Condition66%2/3
ref $args[0] and $args[0]->can("as_rat")
!ll&&!rl&&r
2569016
MC/DC50%1/2
ref $args[0] and $args[0]->can("as_rat")
ref $args[0] $args[0]->can("as_rat")
Truth table66%2/3
A and B
A= ref $args[0]B= $args[0]->can("as_rat")
ABresult
0X0
100
111
386 16 $n = $args[0] -> as_rat();
387 } else {
388 2569 $n = Math::BigFloat -> new($args[0], undef, undef) -> as_rat();
389 }
390 }
391  
392 2571 ▶ if (@args >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args >= 2) 92 2479 2571
393 92 ▶ if (ref($args[1]) && $args[1] -> can('as_rat')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[1] and $args[1]->can('as_rat')) { } 4 88 92
Condition66%2/3
ref $args[1] and $args[1]->can("as_rat")
!ll&&!rl&&r
8804
MC/DC50%1/2
ref $args[1] and $args[1]->can("as_rat")
ref $args[1] $args[1]->can("as_rat")
Truth table66%2/3
A and B
A= ref $args[1]B= $args[1]->can("as_rat")
ABresult
0X0
100
111
394 4 $d = $args[1] -> as_rat();
395 } else {
396 88 $d = Math::BigFloat -> new($args[1], undef, undef) -> as_rat();
397 }
398 }
399  
400 2571 ▶ $n -> bdiv($d) if defined $d;
Branch: 1 branch
BranchTrueFalseTotal
if defined $d 92 2479 2571
401  
402 2571 $self -> {sign} = $n -> {sign};
403 2571 $self -> {_n} = $n -> {_n};
404 2571 $self -> {_d} = $n -> {_d};
405  
406 2571 ▶ $self -> _dng() if ($self -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int or $self->is_inf or $self->is_nan 1568 1003 2571
Condition100%6/6
$self->is_int or $self->is_inf or $self->is_nan
l!l&&r!l&&!r
9106581003
$self->is_int or $self->is_inf
l!l&&r!l&&!r
678431661
MC/DC100%3/3
$self->is_int or $self->is_inf or $self->is_nan
$self->is_int $self->is_inf $self->is_nan
Truth table100%4/4
A or B or C
A= $self->is_intB= $self->is_infC= $self->is_nan
ABCresult
1XX1
01X1
0011
0000
407 $self -> is_inf() ||
408 $self -> is_nan());
409 2571 return $self;
410 }
411  
412 # Create a Math::BigRat from a decimal string. This is an equivalent to
413 # from_hex(), from_oct(), and from_bin(). It is like new() except that it does
414 # not accept anything but a string representing a finite decimal number.
415  
416 0 ▶sub from_dec {
Subroutine from_dec: not called CC=10 SCAR=47.0
Pod documented
417 0 my $self = shift;
418 0 my $selfref = ref $self;
419 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
420  
421 # Make "require" work.
422  
423 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
424  
425 # Don't modify constant (read-only) objects.
426  
427 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
428  
429 0 my $str = shift;
430 0 my @r = @_;
431  
432 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
433  
434 # If called as a class method, initialize a new object.
435  
436 0 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 0 0 0
437 0 $self = bless {}, $class;
438 0 $self -> _init();
439 }
440  
441 0 my ($mant_sgn, $mant_abs, $expo_sgn, $expo_abs) = @parts;
442  
443 0 $self->{sign} = $mant_sgn;
444 0 $self->{_n} = $mant_abs;
445  
446 0 ▶ if ($expo_sgn eq "+") {
Branch: 1 branch
BranchTrueFalseTotal
if ($expo_sgn eq '+') { } 0 0 0
447 0 $self->{_n} = $LIB -> _lsft($self->{_n}, $expo_abs, 10);
448 0 $self->{_d} = $LIB -> _one();
449 } else {
450 0 $self->{_d} = $LIB -> _1ex($expo_abs);
451 }
452  
453 0 my $gcd = $LIB -> _gcd($LIB -> _copy($self->{_n}), $self->{_d});
454 0 ▶ if (!$LIB -> _is_one($gcd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($gcd)) 0 0 0
455 0 $self->{_n} = $LIB -> _div($self->{_n}, $gcd);
456 0 $self->{_d} = $LIB -> _div($self->{_d}, $gcd);
457 }
458  
459 0 ▶ $self -> _dng() if $self -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int 0 0 0
460 0 return $self;
461 }
462  
463 0 return $self -> bnan(@r);
464 }
465  
466 3 ▶sub from_hex {
Subroutine from_hex: called CC=10 SCAR=28.0
Pod documented
467 3 my $self = shift;
468 3 my $selfref = ref $self;
469 3 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
030
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
470  
471 # Make "require" work.
472  
473 3 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 3 3
474  
475 # Don't modify constant (read-only) objects.
476  
477 3 ▶ return $self if $selfref && $self -> modify('from_hex');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_hex") 0 3 3
Condition33%1/3
$selfref and $self->modify("from_hex")
!ll&&!rl&&r
300
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
478  
479 3 my $str = shift;
480 3 my @r = @_;
481  
482 3 ▶ 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)) 3 0 3
483  
484 # If called as a class method, initialize a new object.
485  
486 3 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 3 0 3
487 3 $self = bless {}, $class;
488 3 $self -> _init();
489 }
490  
491 3 my ($mant_sgn, $mant_abs, $expo_sgn, $expo_abs) = @parts;
492  
493 3 $self->{sign} = $mant_sgn;
494 3 $self->{_n} = $mant_abs;
495  
496 3 ▶ if ($expo_sgn eq "+") {
Branch: 1 branch
BranchTrueFalseTotal
if ($expo_sgn eq '+') { } 3 0 3
497  
498 # e.g., 345e+2 => 34500/1
499 3 $self->{_n} = $LIB -> _lsft($self->{_n}, $expo_abs, 10);
500 3 $self->{_d} = $LIB -> _one();
501  
502 } else {
503  
504 # e.g., 345e-2 => 345/100
505 0 $self->{_d} = $LIB -> _1ex($expo_abs);
506  
507 # e.g., 345/100 => 69/20
508 0 my $gcd = $LIB -> _gcd($LIB -> _copy($self->{_n}), $self->{_d});
509 0 ▶ unless ($LIB -> _is_one($gcd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($gcd)) 0 0 0
510 0 $self->{_n} = $LIB -> _div($self->{_n}, $gcd);
511 0 $self->{_d} = $LIB -> _div($self->{_d}, $gcd);
512 }
513 }
514  
515 3 ▶ $self -> _dng() if $self -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int 3 0 3
516 3 return $self;
517 }
518  
519 0 return $self -> bnan(@r);
520 }
521  
522 3 ▶sub from_oct {
Subroutine from_oct: called CC=10 SCAR=28.0
Pod documented
523 3 my $self = shift;
524 3 my $selfref = ref $self;
525 3 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
030
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
526  
527 # Make "require" work.
528  
529 3 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 3 3
530  
531 # Don't modify constant (read-only) objects.
532  
533 3 ▶ return $self if $selfref && $self -> modify('from_oct');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_oct") 0 3 3
Condition33%1/3
$selfref and $self->modify("from_oct")
!ll&&!rl&&r
300
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
534  
535 3 my $str = shift;
536 3 my @r = @_;
537  
538 3 ▶ 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)) 3 0 3
539  
540 # If called as a class method, initialize a new object.
541  
542 3 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 3 0 3
543 3 $self = bless {}, $class;
544 3 $self -> _init();
545 }
546  
547 3 my ($mant_sgn, $mant_abs, $expo_sgn, $expo_abs) = @parts;
548  
549 3 $self->{sign} = $mant_sgn;
550 3 $self->{_n} = $mant_abs;
551  
552 3 ▶ if ($expo_sgn eq "+") {
Branch: 1 branch
BranchTrueFalseTotal
if ($expo_sgn eq '+') { } 3 0 3
553  
554 # e.g., 345e+2 => 34500/1
555 3 $self->{_n} = $LIB -> _lsft($self->{_n}, $expo_abs, 10);
556 3 $self->{_d} = $LIB -> _one();
557  
558 } else {
559  
560 # e.g., 345e-2 => 345/100
561 0 $self->{_d} = $LIB -> _1ex($expo_abs);
562  
563 # e.g., 345/100 => 69/20
564 0 my $gcd = $LIB -> _gcd($LIB -> _copy($self->{_n}), $self->{_d});
565 0 ▶ unless ($LIB -> _is_one($gcd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($gcd)) 0 0 0
566 0 $self->{_n} = $LIB -> _div($self->{_n}, $gcd);
567 0 $self->{_d} = $LIB -> _div($self->{_d}, $gcd);
568 }
569 }
570  
571 3 ▶ $self -> _dng() if $self -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int 3 0 3
572 3 return $self;
573 }
574  
575 0 return $self -> bnan(@r);
576 }
577  
578 3 ▶sub from_bin {
Subroutine from_bin: called CC=10 SCAR=28.0
Pod documented
579 3 my $self = shift;
580 3 my $selfref = ref $self;
581 3 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
030
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
582  
583 # Make "require" work.
584  
585 3 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 3 3
586  
587 # Don't modify constant (read-only) objects.
588  
589 3 ▶ return $self if $selfref && $self -> modify('from_bin');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_bin") 0 3 3
Condition33%1/3
$selfref and $self->modify("from_bin")
!ll&&!rl&&r
300
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
590  
591 3 my $str = shift;
592 3 my @r = @_;
593  
594 3 ▶ 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)) 3 0 3
595  
596 # If called as a class method, initialize a new object.
597  
598 3 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 3 0 3
599 3 $self = bless {}, $class;
600 3 $self -> _init();
601 }
602  
603 3 my ($mant_sgn, $mant_abs, $expo_sgn, $expo_abs) = @parts;
604  
605 3 $self->{sign} = $mant_sgn;
606 3 $self->{_n} = $mant_abs;
607  
608 3 ▶ if ($expo_sgn eq "+") {
Branch: 1 branch
BranchTrueFalseTotal
if ($expo_sgn eq '+') { } 3 0 3
609  
610 # e.g., 345e+2 => 34500/1
611 3 $self->{_n} = $LIB -> _lsft($self->{_n}, $expo_abs, 10);
612 3 $self->{_d} = $LIB -> _one();
613  
614 } else {
615  
616 # e.g., 345e-2 => 345/100
617 0 $self->{_d} = $LIB -> _1ex($expo_abs);
618  
619 # e.g., 345/100 => 69/20
620 0 my $gcd = $LIB -> _gcd($LIB -> _copy($self->{_n}), $self->{_d});
621 0 ▶ unless ($LIB -> _is_one($gcd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($gcd)) 0 0 0
622 0 $self->{_n} = $LIB -> _div($self->{_n}, $gcd);
623 0 $self->{_d} = $LIB -> _div($self->{_d}, $gcd);
624 }
625 }
626  
627 3 ▶ $self -> _dng() if $self -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int 3 0 3
628 3 return $self;
629 }
630  
631 0 return $self -> bnan(@r);
632 }
633  
634 0 ▶sub from_bytes {
Subroutine from_bytes: not called CC=6 SCAR=37.4
Pod documented
635 0 my $self = shift;
636 0 my $selfref = ref $self;
637 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
638  
639 # Make "require" work.
640  
641 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
642  
643 # Don't modify constant (read-only) objects.
644  
645 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
646  
647 0 my $str = shift;
648 0 my @r = @_;
649  
650 # If called as a class method, initialize a new object.
651  
652 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
653  
654 0 $self -> {sign} = "+";
655 0 $self -> {_n} = $LIB -> _from_bytes($str);
656 0 $self -> {_d} = $LIB -> _one();
657  
658 0 $self -> _dng();
659 0 return $self;
660 }
661  
662 0 ▶sub from_ieee754 {
Subroutine from_ieee754: not called CC=7 SCAR=40.3
Pod documented
663 0 my $self = shift;
664 0 my $selfref = ref $self;
665 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
666  
667 # Make "require" work.
668  
669 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
670  
671 # Don't modify constant (read-only) objects.
672  
673 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
674  
675 0 my $in = shift;
676 0 my $format = shift;
677 0 my @r = @_;
678  
679 0 my $tmp = Math::BigFloat -> from_ieee754($in, $format, @r);
680  
681 0 $tmp = $tmp -> as_rat();
682  
683 # If called as a class method, initialize a new object.
684  
685 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
686 0 $self -> {sign} = $tmp -> {sign};
687 0 $self -> {_n} = $tmp -> {_n};
688 0 $self -> {_d} = $tmp -> {_d};
689  
690 0 ▶ $self -> _dng() if $self -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int 0 0 0
691 0 return $self;
692 }
693  
694 0 ▶sub from_fp80 {
Subroutine from_fp80: not called CC=7 SCAR=40.3
Pod documented
695 0 my $self = shift;
696 0 my $selfref = ref $self;
697 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
698  
699 # Make "require" work.
700  
701 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
702  
703 # Don't modify constant (read-only) objects.
704  
705 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
706  
707 0 my $in = shift;
708 0 my @r = @_;
709  
710 0 my $tmp = Math::BigFloat -> from_fp80($in, @r);
711  
712 0 $tmp = $tmp -> as_rat();
713  
714 # If called as a class method, initialize a new object.
715  
716 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
717 0 $self -> {sign} = $tmp -> {sign};
718 0 $self -> {_n} = $tmp -> {_n};
719 0 $self -> {_d} = $tmp -> {_d};
720  
721 0 ▶ $self -> _dng() if $self -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int 0 0 0
722 0 return $self;
723 }
724  
725 0 ▶sub from_base {
Subroutine from_base: not called CC=16 SCAR=56.1
Pod documented
726 0 my $self = shift;
727 0 my $selfref = ref $self;
728 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
729  
730 # Make "require" work.
731  
732 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
733  
734 # Don't modify constant (read-only) objects.
735  
736 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
737  
738 0 my ($str, $base, $cs, @r) = @_; # $cs is the collation sequence
739  
740 0 ▶ $base = $class -> new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
741  
742 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
743 if $base < 2 || ! $base -> is_int();
744  
745 # If called as a class method, initialize a new object.
746  
747 0 ▶ $self = $class -> bzero() unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
748  
749 # If no collating sequence is given, pass some of the conversions to
750 # methods optimized for those cases.
751  
752 0 ▶ unless (defined $cs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cs) 0 0 0
753 0 ▶ return $self -> from_bin($str, @r) if $base == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 2 0 0 0
754 0 ▶ return $self -> from_oct($str, @r) if $base == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 8 0 0 0
755 0 ▶ return $self -> from_hex($str, @r) if $base == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 16 0 0 0
756 0 ▶ return $self -> from_dec($str, @r) if $base == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 10 0 0 0
757 }
758  
759 0 ▶ croak("from_base() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_from_base") 0 0 0
760 unless $LIB -> can('_from_base');
761  
762 0 $self -> {sign} = '+';
763 $self -> {_n} = $LIB->_from_base($str, $base -> {_n},
764 0 ▶ defined($cs) ? $cs : ());
Branch: 1 branch
BranchTrueFalseTotal
defined $cs ? : 0 0 0
765 0 $self -> {_d} = $LIB->_one();
766 0 $self -> bnorm();
767  
768 0 $self -> _dng();
769 0 return $self;
770 }
771  
772 152 ▶sub bzero {
Subroutine bzero: called CC=14 SCAR=30.9
Pod documented
773 152 my $self = shift;
774 152 my $selfref = ref $self;
775 152 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
14930
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
776  
777 # Make "require" work.
778  
779 152 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 152 152
780  
781 # Don't modify constant (read-only) objects.
782  
783 152 ▶ return $self if $selfref && $self -> modify('bzero');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bzero") 0 152 152
Condition66%2/3
$selfref and $self->modify("bzero")
!ll&&!rl&&r
31490
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
784  
785 # Downgrade?
786  
787 152 my $dng = $class -> downgrade();
788 152 ▶ if ($dng && $dng ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dng and $dng ne $class) 2 150 152
Condition66%2/3
$dng and $dng ne $class
!ll&&!rl&&r
15002
MC/DC50%1/2
$dng and $dng ne $class
$dng $dng ne $class
Truth table66%2/3
A and B
A= $dngB= $dng ne $class
ABresult
0X0
100
111
789 2 ▶ return $self -> _dng() -> bzero(@_) if $selfref;
Branch: 1 branch
BranchTrueFalseTotal
if $selfref 1 1 2
790 1 return $dng -> bzero(@_);
791 }
792  
793 # Get the rounding parameters, if any.
794  
795 150 my @r = @_;
796  
797 # If called as a class method, initialize a new object.
798  
799 150 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 2 148 150
800  
801 150 $self -> {sign} = '+';
802 150 $self -> {_n} = $LIB -> _zero();
803 150 $self -> {_d} = $LIB -> _one();
804  
805 # If rounding parameters are given as arguments, use them. If no rounding
806 # parameters are given, and if called as a class method initialize the new
807 # instance with the class variables.
808  
809 #return $self -> round(@r); # this should work, but doesnt; fixme!
810  
811 150 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 0 150 150
812 0 ▶ 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 0 0
Condition0%0/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
000
@r >= 2 and defined $r[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table0%0/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
813 0 carp "can't specify both accuracy and precision";
814 0 return $self -> bnan();
815 }
816 0 $self->{accuracy} = $r[0];
817 0 $self->{precision} = $r[1];
818 } else {
819 150 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 2 148 150
820 2 $self->{accuracy} = $class -> accuracy();
821 2 $self->{precision} = $class -> precision();
822 }
823 }
824  
825 150 return $self;
826 }
827  
828 64 ▶sub bone {
Subroutine bone: called CC=16 SCAR=29.0
Pod documented
829 64 my $self = shift;
830 64 my $selfref = ref $self;
831 64 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
5950
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
832  
833 # Make "require" work.
834  
835 64 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 64 64
836  
837 # Don't modify constant (read-only) objects.
838  
839 64 ▶ return $self if $selfref && $self -> modify('bone');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bone") 0 64 64
Condition66%2/3
$selfref and $self->modify("bone")
!ll&&!rl&&r
5590
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
840  
841 # Downgrade?
842  
843 64 my $dng = $class -> downgrade();
844 64 ▶ if ($dng && $dng ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dng and $dng ne $class) 1 63 64
Condition66%2/3
$dng and $dng ne $class
!ll&&!rl&&r
6301
MC/DC50%1/2
$dng and $dng ne $class
$dng $dng ne $class
Truth table66%2/3
A and B
A= $dngB= $dng ne $class
ABresult
0X0
100
111
845 1 ▶ return $self -> _dng() -> bone(@_) if $selfref;
Branch: 1 branch
BranchTrueFalseTotal
if $selfref 0 1 1
846 1 return $dng -> bone(@_);
847 }
848  
849 # Get the sign.
850  
851 63 my $sign = '+'; # default is to return +1
852 63 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/) 20 43 63
Condition100%3/3
defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/
!ll&&!rl&&r
271620
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
853 20 $sign = $1;
854 20 shift;
855 }
856  
857 # Get the rounding parameters, if any.
858  
859 63 my @r = @_;
860  
861 # If called as a class method, initialize a new object.
862  
863 63 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 4 59 63
864  
865 63 $self -> {sign} = $sign;
866 63 $self -> {_n} = $LIB -> _one();
867 63 $self -> {_d} = $LIB -> _one();
868  
869 # If rounding parameters are given as arguments, use them. If no rounding
870 # parameters are given, and if called as a class method initialize the new
871 # instance with the class variables.
872  
873 #return $self -> round(@r); # this should work, but doesnt; fixme!
874  
875 63 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 16 47 63
876 16 ▶ 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 16 16
Condition33%2/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
1600
@r >= 2 and defined $r[0]
!ll&&!rl&&r
1600
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
877 0 carp "can't specify both accuracy and precision";
878 0 return $self -> bnan();
879 }
880 16 $self->{accuracy} = $r[0];
881 16 $self->{precision} = $r[1];
882 } else {
883 47 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 4 43 47
884 4 $self->{accuracy} = $class -> accuracy();
885 4 $self->{precision} = $class -> precision();
886 }
887 }
888  
889 63 return $self;
890 }
891  
892 1073 ▶sub binf {
Subroutine binf: called CC=17 SCAR=29.1
Pod documented
893 1073 my $self = shift;
894 1073 my $selfref = ref $self;
895 1073 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
2048690
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
896  
897 {
898 16 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
899 1073 ▶ if (${"${class}::_trap_inf"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_inf";}) 3 1070 1073
900 3 croak("Tried to create +-inf in $class->binf()");
901 }
902 }
903  
904 # Make "require" work.
905  
906 1070 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 1070 1070
907  
908 # Don't modify constant (read-only) objects.
909  
910 1070 ▶ return $self if $selfref && $self -> modify('binf');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("binf") 0 1070 1070
Condition66%2/3
$selfref and $self->modify("binf")
!ll&&!rl&&r
8692010
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
911  
912 # Get the sign.
913  
914 1070 my $sign = '+'; # default is to return positive infinity
915 1070 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])(inf|$)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i) 1015 55 1070
Condition100%3/3
defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i
!ll&&!rl&&r
5141015
MC/DC100%2/2
defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i
defined $_[0] $_[0] =~ /^\s*([+-])(inf|$)/i
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] =~ /^\s*([+-])(inf|$)/i
ABresult
0X0
100
111
916 1015 $sign = $1;
917 1015 shift;
918 }
919  
920 # Get the rounding parameters, if any.
921  
922 1070 my @r = @_;
923  
924 # Downgrade?
925  
926 1070 my $dng = $class -> downgrade();
927 1070 ▶ if ($dng && $dng ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dng and $dng ne $class) 6 1064 1070
Condition66%2/3
$dng and $dng ne $class
!ll&&!rl&&r
106406
MC/DC50%1/2
$dng and $dng ne $class
$dng $dng ne $class
Truth table66%2/3
A and B
A= $dngB= $dng ne $class
ABresult
0X0
100
111
928 6 ▶ return $self -> _dng() -> binf($sign, @r) if $selfref;
Branch: 1 branch
BranchTrueFalseTotal
if $selfref 5 1 6
929 1 return $dng -> binf($sign, @r);
930 }
931  
932 # If called as a class method, initialize a new object.
933  
934 1064 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 868 196 1064
935  
936 1064 $self -> {sign} = $sign . 'inf';
937 1064 $self -> {_n} = $LIB -> _zero();
938 1064 $self -> {_d} = $LIB -> _one();
939  
940 # If rounding parameters are given as arguments, use them. If no rounding
941 # parameters are given, and if called as a class method initialize the new
942 # instance with the class variables.
943  
944 #return $self -> round(@r); # this should work, but doesnt; fixme!
945  
946 1064 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 4 1060 1064
947 4 ▶ 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 4 4
Condition33%2/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
400
@r >= 2 and defined $r[0]
!ll&&!rl&&r
400
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
948 0 carp "can't specify both accuracy and precision";
949 0 return $self -> bnan();
950 }
951 4 $self->{accuracy} = $r[0];
952 4 $self->{precision} = $r[1];
953 } else {
954 1060 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 868 192 1060
955 868 $self->{accuracy} = $class -> accuracy();
956 868 $self->{precision} = $class -> precision();
957 }
958 }
959  
960 1064 return $self;
961 }
962  
963 1179 ▶sub bnan {
Subroutine bnan: called CC=15 SCAR=30.4
Pod documented
964 1179 my $self = shift;
965 1179 my $selfref = ref $self;
966 1179 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
5236560
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
967  
968 {
969 16 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
970 1179 ▶ if (${"${class}::_trap_nan"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_nan";}) 3 1176 1179
971 3 croak("Tried to create NaN in $class->bnan()");
972 }
973 }
974  
975 # Make "require" work.
976  
977 1176 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 1176 1176
978  
979 # Don't modify constant (read-only) objects.
980  
981 1176 ▶ return $self if $selfref && $self -> modify('bnan');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bnan") 0 1176 1176
Condition66%2/3
$selfref and $self->modify("bnan")
!ll&&!rl&&r
6565200
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
982  
983 1176 my $dng = $class -> downgrade();
984 1176 ▶ if ($dng && $dng ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dng and $dng ne $class) 9 1167 1176
Condition66%2/3
$dng and $dng ne $class
!ll&&!rl&&r
116709
MC/DC50%1/2
$dng and $dng ne $class
$dng $dng ne $class
Truth table66%2/3
A and B
A= $dngB= $dng ne $class
ABresult
0X0
100
111
985 9 ▶ return $self -> _dng() -> bnan(@_) if $selfref;
Branch: 1 branch
BranchTrueFalseTotal
if $selfref 8 1 9
986 1 return $dng -> bnan(@_);
987 }
988  
989 # Get the rounding parameters, if any.
990  
991 1167 my @r = @_;
992  
993 # If called as a class method, initialize a new object.
994  
995 1167 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 655 512 1167
996  
997 1167 $self -> {sign} = $nan;
998 1167 $self -> {_n} = $LIB -> _zero();
999 1167 $self -> {_d} = $LIB -> _one();
1000  
1001 # If rounding parameters are given as arguments, use them. If no rounding
1002 # parameters are given, and if called as a class method initialize the new
1003 # instance with the class variables.
1004  
1005 #return $self -> round(@r); # this should work, but doesnt; fixme!
1006  
1007 1167 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 0 1167 1167
1008 0 ▶ 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 0 0
Condition0%0/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
000
@r >= 2 and defined $r[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table0%0/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
1009 0 carp "can't specify both accuracy and precision";
1010 0 return $self -> bnan();
1011 }
1012 0 $self->{accuracy} = $r[0];
1013 0 $self->{precision} = $r[1];
1014 } else {
1015 1167 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 655 512 1167
1016 655 $self->{accuracy} = $class -> accuracy();
1017 655 $self->{precision} = $class -> precision();
1018 }
1019 }
1020  
1021 1167 return $self;
1022 }
1023  
1024 0 ▶sub bpi {
Subroutine bpi: not called CC=11 SCAR=48.8
Pod documented
1025 0 my $self = shift;
1026 0 my $selfref = ref $self;
1027 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
1028 0 my @r = @_; # rounding paramters
1029  
1030 # Make "require" work.
1031  
1032 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
1033  
1034 # Don't modify constant (read-only) objects.
1035  
1036 0 ▶ return $self if $selfref && $self -> modify('bpi');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bpi") 0 0 0
Condition0%0/3
$selfref and $self->modify("bpi")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("bpi")
$selfref $self->modify("bpi")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("bpi")
ABresult
0X0
100
111
1037  
1038 # If called as a class method, initialize a new object.
1039  
1040 0 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
1041  
1042 0 ($self, @r) = $self -> _find_round_parameters(@r);
1043  
1044 # The accuracy, i.e., the number of digits. Pi has one digit before the
1045 # dot, so a precision of 4 digits is equivalent to an accuracy of 5 digits.
1046  
1047 0 ▶ my $n = defined $r[0] ? $r[0]
Branch: 2 branches
BranchTrueFalseTotal
defined $r[0] ? : 0 0 0
defined $r[1] ? : 0 0 0
1048 : defined $r[1] ? 1 - $r[1]
1049 : $self -> div_scale();
1050  
1051 # The algorithm below creates a fraction from a floating point number. The
1052 # worst case is the number (1 + sqrt(5))/2 (golden ratio), which takes
1053 # almost 2.4*N iterations to find a fraction that is accurate to N digits,
1054 # i.e., the relative error is less than 10**(-N).
1055 #
1056 # This algorithm might be useful in general, so it should probably be moved
1057 # out to a method of its own. XXX
1058  
1059 0 my $max_iter = $n * 2.4;
1060  
1061 0 my $x = Math::BigFloat -> bpi($n + 10);
1062  
1063 0 my $tol = $class -> new("1/10") -> bpow("$n") -> bmul($x);
1064  
1065 0 my $n0 = $class -> bzero();
1066 0 my $d0 = $class -> bone();
1067  
1068 0 my $n1 = $class -> bone();
1069 0 my $d1 = $class -> bzero();
1070  
1071 0 my ($n2, $d2);
1072  
1073 0 my $xtmp = $x -> copy();
1074  
1075 0 ▶ for (my $iter = 0 ; $iter <= $max_iter ; $iter++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iter <= $max_iter) 0 0 0
1076 0 my $t = $xtmp -> copy() -> bint();
1077  
1078 0 $n2 = $n1 -> copy() -> bmul($t) -> badd($n0);
1079 0 $d2 = $d1 -> copy() -> bmul($t) -> badd($d0);
1080  
1081 0 my $err = $n2 -> copy() -> bdiv($d2) -> bsub($x);
1082 0 ▶ last if $err -> copy() -> babs() -> ble($tol);
Branch: 1 branch
BranchTrueFalseTotal
if $err->copy->babs->ble($tol) 0 0 0
1083  
1084 0 $xtmp -> bsub($t);
1085 0 ▶ last if $xtmp -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $xtmp->is_zero 0 0 0
1086 0 $xtmp -> binv();
1087  
1088 0 ($n1, $n0) = ($n2, $n1);
1089 0 ($d1, $d0) = ($d2, $d1);
1090 }
1091  
1092 0 my $mbr = $n2 -> bdiv($d2);
1093 0 %$self = %$mbr;
1094 0 return $self;
1095 }
1096  
1097 4080 ▶sub copy {
Subroutine copy: called CC=5 SCAR=16.8
Pod documented
1098 4080 my $self = shift;
1099 4080 my $selfref = ref $self;
1100 4080 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
408000
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1101  
1102 # If called as a class method, the object to copy is the next argument.
1103  
1104 4080 ▶ $self = shift() unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 4080 4080
1105  
1106 4080 my $copy = bless {}, $class;
1107  
1108 4080 $copy->{sign} = $self->{sign};
1109 4080 $copy->{_d} = $LIB->_copy($self->{_d});
1110 4080 $copy->{_n} = $LIB->_copy($self->{_n});
1111 4080 ▶ $copy->{accuracy} = $self->{accuracy} if defined $self->{accuracy};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"accuracy"} 0 4080 4080
1112 4080 ▶ $copy->{precision} = $self->{precision} if defined $self->{precision};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"precision"} 0 4080 4080
1113  
1114 #($copy, $copy->{accuracy}, $copy->{precision})
1115 # = $copy->_find_round_parameters(@_);
1116  
1117 4080 return $copy;
1118 }
1119  
1120 1792 ▶sub as_int {
Subroutine as_int: called CC=5 SCAR=16.2
Pod documented
1121 1792 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1792 0 1792
1122  
1123 # Temporarily disable upgrading and downgrading.
1124  
1125 1792 my $upg = Math::BigInt -> upgrade();
1126 1792 my $dng = Math::BigInt -> downgrade();
1127 1792 Math::BigInt -> upgrade(undef);
1128 1792 Math::BigInt -> downgrade(undef);
1129  
1130 1792 my $y;
1131 1792 ▶ if ($x -> isa("Math::BigInt")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigInt')) { } 0 1792 1792
1132 0 $y = $x -> copy();
1133 } else {
1134 1792 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 16 1776 1792
elsif ($x->is_nan) { } 8 1768 1776
1135 16 $y = Math::BigInt -> binf($x -> sign());
1136 } elsif ($x -> is_nan()) {
1137 8 $y = Math::BigInt -> bnan();
1138 } else {
1139 1768 $y = Math::BigInt -> new($x -> copy() -> bint() -> bdstr());
1140 }
1141  
1142 # Copy the remaining instance variables.
1143  
1144 1792 ($y->{accuracy}, $y->{precision}) = ($x->{accuracy}, $x->{precision});
1145 }
1146  
1147 1792 $y -> round(@r);
1148  
1149 # Restore upgrading and downgrading.
1150  
1151 1792 Math::BigInt -> upgrade($upg);
1152 1792 Math::BigInt -> downgrade($dng);
1153  
1154 1792 return $y;
1155 }
1156  
1157 2 ▶sub as_rat {
Subroutine as_rat: called CC=5 SCAR=19.3
Pod documented
1158 2 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
1159  
1160 # Temporarily disable upgrading and downgrading.
1161  
1162 2 require Math::BigRat;
1163 2 my $upg = Math::BigRat -> upgrade();
1164 2 my $dng = Math::BigRat -> downgrade();
1165 2 Math::BigRat -> upgrade(undef);
1166 2 Math::BigRat -> downgrade(undef);
1167  
1168 2 my $y;
1169 2 ▶ if ($x -> isa("Math::BigRat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigRat')) { } 2 0 2
1170 2 $y = $x -> copy();
1171 } else {
1172  
1173 0 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 0 0 0
elsif ($x->is_nan) { } 0 0 0
1174 0 $y = Math::BigRat -> binf($x -> sign());
1175 } elsif ($x -> is_nan()) {
1176 0 $y = Math::BigRat -> bnan();
1177 } else {
1178 0 $y = Math::BigRat -> new($x -> bfstr());
1179 }
1180  
1181 # Copy the remaining instance variables.
1182  
1183 0 ($y->{accuracy}, $y->{precision}) = ($x->{accuracy}, $x->{precision});
1184 }
1185  
1186 2 $y -> round(@r);
1187  
1188 # Restore upgrading and downgrading.
1189  
1190 2 Math::BigRat -> upgrade($upg);
1191 2 Math::BigRat -> downgrade($dng);
1192  
1193 2 return $y;
1194 }
1195  
1196 9 ▶sub as_float {
Subroutine as_float: called CC=7 SCAR=20.5
Pod documented
1197 9 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 9 0 9
1198  
1199 # Disable upgrading and downgrading.
1200  
1201 9 require Math::BigFloat;
1202 9 my $upg = Math::BigFloat -> upgrade();
1203 9 my $dng = Math::BigFloat -> downgrade();
1204 9 Math::BigFloat -> upgrade(undef);
1205 9 Math::BigFloat -> downgrade(undef);
1206  
1207 9 my $y;
1208 9 ▶ if ($x -> isa("Math::BigFloat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigFloat')) { } 0 9 9
1209 0 $y = $x -> copy();
1210 } else {
1211 9 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 0 9 9
elsif ($x->is_nan) { } 0 9 9
1212 0 $y = Math::BigFloat -> binf($x -> sign());
1213 } elsif ($x -> is_nan()) {
1214 0 $y = Math::BigFloat -> bnan();
1215 } else {
1216 9 ▶ if ($x -> isa("Math::BigRat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigRat')) { } 9 0 9
1217 9 ▶ if ($x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_int) { } 7 2 9
1218 7 $y = Math::BigFloat -> new($x -> bdstr());
1219 } else {
1220 2 my ($num, $den) = $x -> fparts();
1221 2 my $str = $num -> as_float() -> bdiv($den, @r) -> bdstr();
1222 2 $y = Math::BigFloat -> new($str);
1223 }
1224 } else {
1225 0 $y = Math::BigFloat -> new($x -> bdstr());
1226 }
1227 }
1228  
1229 # Copy the remaining instance variables.
1230  
1231 9 ($y->{accuracy}, $y->{precision}) = ($x->{accuracy}, $x->{precision});
1232 }
1233  
1234 9 $y -> round(@r);
1235  
1236 # Restore upgrading and downgrading.
1237  
1238 9 Math::BigFloat -> upgrade($upg);
1239 9 Math::BigFloat -> downgrade($dng);
1240  
1241 9 return $y;
1242 }
1243  
1244 2095 ▶sub is_zero {
Subroutine is_zero: called CC=4 SCAR=13.9
Pod documented
1245 # return true if arg (BRAT or num_str) is zero
1246 2095 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2095 0 2095
1247  
1248 2095 ▶ return 1 if $x->{sign} eq '+' && $LIB->_is_zero($x->{_n});
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_n"}) 269 1826 2095
Condition100%3/3
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_n"})
!ll&&!rl&&r
6121214269
MC/DC100%2/2
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_n"})
$x->{"sign"} eq "+" $LIB->_is_zero($x->{"_n"})
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $LIB->_is_zero($x->{"_n"})
ABresult
0X0
100
111
1249 1826 return 0;
1250 }
1251  
1252 1148 ▶sub is_one {
Subroutine is_one: called CC=8 SCAR=21.0
Pod documented
1253 # return true if arg (BRAT or num_str) is +1 or -1 if signis given
1254 1148 ▶ my (undef, $x, $sign) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1148 0 1148
1255  
1256 1148 ▶ if (defined($sign)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sign) { } 742 406 1148
1257 742 ▶ croak 'is_one(): sign argument must be "+" or "-"'
Branch: 1 branch
BranchTrueFalseTotal
unless $sign eq "+" or $sign eq "-" 0 742 742
Condition66%2/3
$sign eq "+" or $sign eq "-"
l!l&&r!l&&!r
3953470
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
1258 unless $sign eq '+' || $sign eq '-';
1259 } else {
1260 406 $sign = '+';
1261 }
1262  
1263 1148 ▶ return 0 if $x->{sign} ne $sign;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $sign 511 637 1148
1264 637 ▶ return 1 if $LIB->_is_one($x->{_n}) && $LIB->_is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_one($x->{"_n"}) and $LIB->_is_one($x->{"_d"}) 154 483 637
Condition100%3/3
$LIB->_is_one($x->{"_n"}) and $LIB->_is_one($x->{"_d"})
!ll&&!rl&&r
41469154
MC/DC100%2/2
$LIB->_is_one($x->{"_n"}) and $LIB->_is_one($x->{"_d"})
$LIB->_is_one($x->{"_n"}) $LIB->_is_one($x->{"_d"})
Truth table100%3/3
A and B
A= $LIB->_is_one($x->{"_n"})B= $LIB->_is_one($x->{"_d"})
ABresult
0X0
100
111
1265 483 return 0;
1266 }
1267  
1268 100 ▶sub is_odd {
Subroutine is_odd: called CC=5 SCAR=16.1
Pod documented
1269 # return true if arg (BFLOAT or num_str) is odd or false if even
1270 100 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 100 0 100
1271  
1272 100 ▶ return 0 unless $x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 16 84 100
1273 84 ▶ return 1 if $LIB->_is_one($x->{_d}) && $LIB->_is_odd($x->{_n});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_one($x->{"_d"}) and $LIB->_is_odd($x->{"_n"}) 40 44 84
Condition100%3/3
$LIB->_is_one($x->{"_d"}) and $LIB->_is_odd($x->{"_n"})
!ll&&!rl&&r
162840
MC/DC100%2/2
$LIB->_is_one($x->{"_d"}) and $LIB->_is_odd($x->{"_n"})
$LIB->_is_one($x->{"_d"}) $LIB->_is_odd($x->{"_n"})
Truth table100%3/3
A and B
A= $LIB->_is_one($x->{"_d"})B= $LIB->_is_odd($x->{"_n"})
ABresult
0X0
100
111
1274 44 return 0;
1275 }
1276  
1277 72 ▶sub is_even {
Subroutine is_even: called CC=5 SCAR=16.1
Pod documented
1278 # return true if arg (BINT or num_str) is even or false if odd
1279 72 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 72 0 72
1280  
1281 72 ▶ return 0 unless $x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 12 60 72
1282 60 ▶ return 1 if $LIB->_is_one($x->{_d}) && $LIB->_is_even($x->{_n});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_one($x->{"_d"}) and $LIB->_is_even($x->{"_n"}) 24 36 60
Condition100%3/3
$LIB->_is_one($x->{"_d"}) and $LIB->_is_even($x->{"_n"})
!ll&&!rl&&r
162024
MC/DC100%2/2
$LIB->_is_one($x->{"_d"}) and $LIB->_is_even($x->{"_n"})
$LIB->_is_one($x->{"_d"}) $LIB->_is_even($x->{"_n"})
Truth table100%3/3
A and B
A= $LIB->_is_one($x->{"_d"})B= $LIB->_is_even($x->{"_n"})
ABresult
0X0
100
111
1283 36 return 0;
1284 }
1285  
1286 15376 ▶sub is_int {
Subroutine is_int: called CC=4 SCAR=13.9
Pod documented
1287 # return true if arg (BRAT or num_str) is an integer
1288 15376 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 15376 0 15376
1289  
1290 15376 ▶ return 1 if $x -> is_finite() && $LIB->_is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_finite and $LIB->_is_one($x->{"_d"}) 7535 7841 15376
Condition100%3/3
$x->is_finite and $LIB->_is_one($x->{"_d"})
!ll&&!rl&&r
317546667535
MC/DC100%2/2
$x->is_finite and $LIB->_is_one($x->{"_d"})
$x->is_finite $LIB->_is_one($x->{"_d"})
Truth table100%3/3
A and B
A= $x->is_finiteB= $LIB->_is_one($x->{"_d"})
ABresult
0X0
100
111
1291 7841 return 0;
1292 }
1293  
1294 ##############################################################################
1295  
1296 217 ▶sub config {
Subroutine config: called CC=8 SCAR=20.9
Pod documented
1297 217 my $self = shift;
1298 217 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition60%3/5
ref $self || $self || __PACKAGE__
l!l
2170
ref $self or $self
l!l&&r!l&&!r
311860
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
1299  
1300 # Getter/accessor.
1301  
1302 217 ▶ if (@_ == 1 && ref($_[0]) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] ne "HASH") 155 62 217
Condition100%3/3
@_ == 1 and ref $_[0] ne "HASH"
!ll&&!rl&&r
4517155
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
1303 155 my $param = shift;
1304 155 ▶ return $class if $param eq 'class';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "class" 1 154 155
1305 154 ▶ return $LIB if $param eq 'with';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "with" 0 154 154
1306 154 return $self -> SUPER::config($param);
1307 }
1308  
1309 # Setter.
1310  
1311 62 my $cfg = $self -> SUPER::config(@_);
1312  
1313 # We need only to override the ones that are different from our parent.
1314  
1315 60 ▶ unless (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 48 12 60
1316 48 $cfg->{class} = $class;
1317 48 $cfg->{with} = $LIB;
1318 }
1319  
1320 60 $cfg;
1321 }
1322  
1323 ##############################################################################
1324 # comparing
1325  
1326 200 ▶sub bcmp {
Subroutine bcmp: called CC=24 SCAR=34.5
Pod documented
1327 # compare two signed numbers
1328  
1329 # set up parameters
1330 200 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 168 32 200
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
032168
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
1331 ? (ref($_[0]), @_)
1332 : objectify(2, @_);
1333  
1334 200 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 200 200
1335  
1336 200 ▶ if (!$x -> is_finite() || !$y -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 16 184 200
Condition66%2/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
160184
MC/DC50%1/2
$x->is_finite and $y->is_finite
$x->is_finite $y->is_finite
Truth table66%2/3
A and B
A= $x->is_finiteB= $y->is_finite
ABresult
0X0
100
111
1337 # $x is NaN and/or $y is NaN
1338 16 ▶ return if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 0 16 16
Condition33%1/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
0016
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
1339 # $x and $y are both either +inf or -inf
1340 16 ▶ return 0 if $x->{sign} eq $y->{sign} && $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $y->{"sign"} and $x->is_inf 0 16 16
Condition33%1/3
$x->{"sign"} eq $y->{"sign"} and $x->is_inf
!ll&&!rl&&r
1600
MC/DC0%0/2
$x->{"sign"} eq $y->{"sign"} and $x->is_inf
$x->{"sign"} eq $y->{"sign"} $x->is_inf
Truth table33%1/3
A and B
A= $x->{"sign"} eq $y->{"sign"}B= $x->is_inf
ABresult
0X0
100
111
1341 # $x = +inf and $y < +inf
1342 16 ▶ return +1 if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 8 8 16
1343 # $x = -inf and $y > -inf
1344 8 ▶ return -1 if $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("-") 8 0 8
1345 # $x < +inf and $y = +inf
1346 0 ▶ return -1 if $y -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_inf("+") 0 0 0
1347 # $x > -inf and $y = -inf
1348 0 return +1;
1349 }
1350  
1351 # $x >= 0 and $y < 0
1352 184 ▶ return 1 if $x->{sign} eq '+' && $y->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+" and $y->{"sign"} eq "-" 56 128 184
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "-"
!ll&&!rl&&r
587056
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
1353 # $x < 0 and $y >= 0
1354 128 ▶ return -1 if $x->{sign} eq '-' && $y->{sign} eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" and $y->{"sign"} eq "+" 19 109 128
Condition100%3/3
$x->{"sign"} eq "-" and $y->{"sign"} eq "+"
!ll&&!rl&&r
703919
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
1355  
1356 # At this point, we know that $x and $y have the same sign.
1357  
1358 # shortcut
1359 109 my $xz = $LIB->_is_zero($x->{_n});
1360 109 my $yz = $LIB->_is_zero($y->{_n});
1361 109 ▶ return 0 if $xz && $yz; # 0 <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $yz 0 109 109
Condition66%2/3
$xz and $yz
!ll&&!rl&&r
97120
MC/DC0%0/2
$xz and $yz
$xz $yz
Truth table66%2/3
A and B
A= $xzB= $yz
ABresult
0X0
100
111
1362 109 ▶ return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $y->{"sign"} eq "+" 12 97 109
Condition66%2/3
$xz and $y->{"sign"} eq "+"
!ll&&!rl&&r
97012
MC/DC50%1/2
$xz and $y->{"sign"} eq "+"
$xz $y->{"sign"} eq "+"
Truth table66%2/3
A and B
A= $xzB= $y->{"sign"} eq "+"
ABresult
0X0
100
111
1363 97 ▶ return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $yz and $x->{"sign"} eq "+" 8 89 97
Condition66%2/3
$yz and $x->{"sign"} eq "+"
!ll&&!rl&&r
8908
MC/DC50%1/2
$yz and $x->{"sign"} eq "+"
$yz $x->{"sign"} eq "+"
Truth table66%2/3
A and B
A= $yzB= $x->{"sign"} eq "+"
ABresult
0X0
100
111
1364  
1365 89 my $t = $LIB->_mul($LIB->_copy($x->{_n}), $y->{_d});
1366 89 my $u = $LIB->_mul($LIB->_copy($y->{_n}), $x->{_d});
1367  
1368 89 my $cmp = $LIB->_acmp($t, $u); # signs are equal
1369 89 ▶ $cmp = -$cmp if $x->{sign} eq '-'; # both are '-' => reverse
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 39 50 89
1370 89 $cmp;
1371 }
1372  
1373 197 ▶sub bacmp {
Subroutine bacmp: called CC=12 SCAR=25.1
Pod documented
1374 # compare two numbers (as unsigned)
1375  
1376 # set up parameters
1377 197 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 197 0 197
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00197
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
1378 ? (ref($_[0]), @_)
1379 : objectify(2, @_);
1380  
1381 197 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 197 197
1382  
1383 # handle +-inf and NaN
1384 197 ▶ if (!$x -> is_finite() || !$y -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 140 57 197
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
885257
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
1385 140 ▶ return if ($x -> is_nan() || $y -> is_nan());
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 28 112 140
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1612112
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
1386 112 ▶ return 0 if $x -> is_inf() && $y -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf and $y->is_inf 16 96 112
Condition100%3/3
$x->is_inf and $y->is_inf
!ll&&!rl&&r
484816
MC/DC100%2/2
$x->is_inf and $y->is_inf
$x->is_inf $y->is_inf
Truth table100%3/3
A and B
A= $x->is_infB= $y->is_inf
ABresult
0X0
100
111
1387 96 ▶ return 1 if $x -> is_inf() && !$y -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf and not $y->is_inf 48 48 96
Condition66%2/3
$x->is_inf and not $y->is_inf
!ll&&!rl&&r
48048
MC/DC50%1/2
$x->is_inf and not $y->is_inf
$x->is_inf not $y->is_inf
Truth table66%2/3
A and not(B)
A= $x->is_infB= not $y->is_inf
ABresult
0X0
100
111
1388 48 return -1;
1389 }
1390  
1391 57 my $t = $LIB->_mul($LIB->_copy($x->{_n}), $y->{_d});
1392 57 my $u = $LIB->_mul($LIB->_copy($y->{_n}), $x->{_d});
1393 57 $LIB->_acmp($t, $u); # ignore signs
1394 }
1395  
1396 ##############################################################################
1397 # sign manipulation
1398  
1399 87 ▶sub bneg {
Subroutine bneg: called CC=8 SCAR=20.8
Pod documented
1400 # (BRAT or num_str) return BRAT
1401 # negate number or make a negated number from string
1402 87 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 87 0 87
1403  
1404 # Don't modify constant (read-only) objects.
1405  
1406 87 ▶ return $x if $x -> modify('bneg');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bneg") 0 87 87
1407  
1408 # for +0 do not negate (to have always normalized +0). Does nothing for 'NaN'
1409 $x->{sign} =~ tr/+-/-+/
1410 87 ▶ unless ($x->{sign} eq '+' && $LIB->_is_zero($x->{_n}));
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_n"}) 82 5 87
Condition100%3/3
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_n"})
!ll&&!rl&&r
62205
MC/DC100%2/2
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_n"})
$x->{"sign"} eq "+" $LIB->_is_zero($x->{"_n"})
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $LIB->_is_zero($x->{"_n"})
ABresult
0X0
100
111
1411  
1412 87 $x -> round(@r);
1413 87 ▶ $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 65 22 87
Condition100%6/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
60522
$x->is_int or $x->is_inf
l!l&&r!l&&!r
352527
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
1414 87 return $x;
1415 }
1416  
1417 1292 ▶sub bnorm {
Subroutine bnorm: called CC=9 SCAR=22.1
Pod documented
1418 # reduce the number to the shortest form
1419 1292 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1268 24 1292
1420  
1421 # Both parts must be objects of whatever we are using today.
1422 1292 ▶ if (my $c = $LIB->_check($x->{_n})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $LIB->_check($x->{"_n"})) 0 1292 1292
1423 0 croak("n did not pass the self-check ($c) in bnorm()");
1424 }
1425 1292 ▶ if (my $c = $LIB->_check($x->{_d})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $LIB->_check($x->{"_d"})) 0 1292 1292
1426 0 croak("d did not pass the self-check ($c) in bnorm()");
1427 }
1428  
1429 # no normalize for NaN, inf etc.
1430 1292 ▶ if (!$x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 51 1241 1292
1431 51 $x -> _dng();
1432 51 return $x;
1433 }
1434  
1435 # normalize zeros to 0/1
1436 1241 ▶ if ($LIB->_is_zero($x->{_n})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{"_n"})) 155 1086 1241
1437 155 $x->{sign} = '+'; # never leave a -0
1438 155 ▶ $x->{_d} = $LIB->_one() unless $LIB->_is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_one($x->{"_d"}) 38 117 155
1439 155 $x -> _dng();
1440 155 return $x;
1441 }
1442  
1443 # n/1
1444 1086 ▶ if ($LIB->_is_one($x->{_d})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_one($x->{"_d"})) 581 505 1086
1445 581 $x -> _dng();
1446 581 return $x; # no need to reduce
1447 }
1448  
1449 # Compute the GCD.
1450 505 my $gcd = $LIB->_gcd($LIB->_copy($x->{_n}), $x->{_d});
1451 505 ▶ if (!$LIB->_is_one($gcd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($gcd)) 152 353 505
1452 152 $x->{_n} = $LIB->_div($x->{_n}, $gcd);
1453 152 $x->{_d} = $LIB->_div($x->{_d}, $gcd);
1454 }
1455  
1456 505 $x;
1457 }
1458  
1459 27 ▶sub binc {
Subroutine binc: called CC=9 SCAR=22.1
Pod documented
1460 # increment value (add 1)
1461 27 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 27 0 27
1462  
1463 # Don't modify constant (read-only) objects.
1464  
1465 27 ▶ return $x if $x -> modify('binc');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("binc") 0 27 27
1466  
1467 27 ▶ if (!$x -> is_finite()) { # NaN, inf, -inf
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 6 21 27
1468 6 $x -> round(@r);
1469 6 $x -> _dng();
1470 6 return $x;
1471 }
1472  
1473 21 ▶ if ($x->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '-') { } 12 9 21
1474 12 ▶ if ($LIB->_acmp($x->{_n}, $x->{_d}) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($x->{'_n'}, $x->{'_d'}) < 0) { } 8 4 12
1475 # -1/3 ++ => 2/3 (overflow at 0)
1476 8 $x->{_n} = $LIB->_sub($LIB->_copy($x->{_d}), $x->{_n});
1477 8 $x->{sign} = '+';
1478 } else {
1479 4 $x->{_n} = $LIB->_sub($x->{_n}, $x->{_d}); # -5/2 => -3/2
1480 }
1481 } else {
1482 9 $x->{_n} = $LIB->_add($x->{_n}, $x->{_d}); # 5/2 => 7/2
1483 }
1484  
1485 21 $x -> bnorm(); # is this necessary? check! XXX
1486 21 $x -> round(@r);
1487 21 ▶ $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 4 17 21
Condition66%4/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
4017
$x->is_int or $x->is_inf
l!l&&r!l&&!r
4017
MC/DC33%1/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table50%2/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
1488 21 return $x;
1489 }
1490  
1491 27 ▶sub bdec {
Subroutine bdec: called CC=9 SCAR=22.1
Pod documented
1492 # decrement value (subtract 1)
1493 27 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 27 0 27
1494  
1495 # Don't modify constant (read-only) objects.
1496  
1497 27 ▶ return $x if $x -> modify('bdec');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdec") 0 27 27
1498  
1499 27 ▶ if (!$x -> is_finite()) { # NaN, inf, -inf
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 6 21 27
1500 6 $x -> round(@r);
1501 6 $x -> _dng();
1502 6 return $x;
1503 }
1504  
1505 21 ▶ if ($x->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '-') { } 4 17 21
1506 4 $x->{_n} = $LIB->_add($x->{_n}, $x->{_d}); # -5/2 => -7/2
1507 } else {
1508 17 ▶ if ($LIB->_acmp($x->{_n}, $x->{_d}) < 0) # n < d?
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($x->{'_n'}, $x->{'_d'}) < 0) { } 9 8 17
1509 {
1510 # 1/3 -- => -2/3
1511 9 $x->{_n} = $LIB->_sub($LIB->_copy($x->{_d}), $x->{_n});
1512 9 $x->{sign} = '-';
1513 } else {
1514 8 $x->{_n} = $LIB->_sub($x->{_n}, $x->{_d}); # 5/2 => 3/2
1515 }
1516 }
1517  
1518 21 $x -> bnorm(); # is this necessary? check! XXX
1519 21 $x -> round(@r);
1520 21 ▶ $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 4 17 21
Condition66%4/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
4017
$x->is_int or $x->is_inf
l!l&&r!l&&!r
4017
MC/DC33%1/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table50%2/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
1521 21 return $x;
1522 }
1523  
1524 ##############################################################################
1525 # mul/add/div etc
1526  
1527 516 ▶sub badd {
Subroutine badd: called CC=14 SCAR=26.7
Pod documented
1528 # add two rational numbers
1529  
1530 # set up parameters
1531 516 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 510 6 516
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
06510
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
1532 ? (ref($_[0]), @_)
1533 : objectify(2, @_);
1534  
1535 # Don't modify constant (read-only) objects.
1536  
1537 516 ▶ return $x if $x -> modify('badd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("badd") 0 516 516
1538  
1539 516 ▶ unless ($x -> is_finite() && $y -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 96 420 516
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
8412420
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
1540 96 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 5 branches
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) { } 60 36 96
elsif ($x->is_inf('+')) { } 17 19 36
elsif ($x->is_inf('-')) { } 17 2 19
elsif ($y->is_inf('+')) { } 2 0 2
elsif ($y->is_inf('-')) { } 0 0 0
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
342636
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
1541 60 return $x -> bnan(@r);
1542 } elsif ($x -> is_inf("+")) {
1543 17 ▶ return $x -> bnan(@r) if $y -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_inf("-") 8 9 17
1544 9 return $x -> binf("+", @r);
1545 } elsif ($x -> is_inf("-")) {
1546 17 ▶ return $x -> bnan(@r) if $y -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_inf("+") 8 9 17
1547 9 return $x -> binf("-", @r);
1548 } elsif ($y -> is_inf("+")) {
1549 2 return $x -> binf("+", @r);
1550 } elsif ($y -> is_inf("-")) {
1551 0 return $x -> binf("-", @r);
1552 }
1553 }
1554  
1555 # 1 1 gcd(3, 4) = 1 1*3 + 1*4 7
1556 # - + - = --------- = --
1557 # 4 3 4*3 12
1558  
1559 # we do not compute the gcd() here, but simple do:
1560 # 5 7 5*3 + 7*4 43
1561 # - + - = --------- = --
1562 # 4 3 4*3 12
1563  
1564 # and bnorm() will then take care of the rest
1565  
1566 # 5 * 3
1567 420 $x->{_n} = $LIB->_mul($x->{_n}, $y->{_d});
1568  
1569 # 7 * 4
1570 420 my $m = $LIB->_mul($LIB->_copy($y->{_n}), $x->{_d});
1571  
1572 # 5 * 3 + 7 * 4
1573 420 ($x->{_n}, $x->{sign}) = $LIB -> _sadd($x->{_n}, $x->{sign}, $m, $y->{sign});
1574  
1575 # 4 * 3
1576 420 $x->{_d} = $LIB->_mul($x->{_d}, $y->{_d});
1577  
1578 # normalize result, and possible round
1579 420 $x -> bnorm() -> round(@r);
1580 }
1581  
1582 312 ▶sub bsub {
Subroutine bsub: called CC=8 SCAR=20.8
Pod documented
1583 # subtract two rational numbers
1584  
1585 # set up parameters
1586 312 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 307 5 312
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
05307
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
1587 ? (ref($_[0]), @_)
1588 : objectify(2, @_);
1589  
1590 # Don't modify constant (read-only) objects.
1591  
1592 312 ▶ return $x if $x -> modify('bsub');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsub") 0 312 312
1593  
1594 # flip sign of $x, call badd(), then flip sign of result
1595 $x->{sign} =~ tr/+-/-+/
1596 312 ▶ unless $x->{sign} eq '+' && $x -> is_zero(); # not -0
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+" and $x->is_zero 283 29 312
Condition100%3/3
$x->{"sign"} eq "+" and $x->is_zero
!ll&&!rl&&r
10617729
MC/DC100%2/2
$x->{"sign"} eq "+" and $x->is_zero
$x->{"sign"} eq "+" $x->is_zero
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $x->is_zero
ABresult
0X0
100
111
1597 312 $x = $x -> badd($y, @r); # does norm and round
1598 $x->{sign} =~ tr/+-/-+/
1599 312 ▶ unless $x->{sign} eq '+' && $x -> is_zero(); # not -0
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+" and $x->is_zero 247 65 312
Condition100%3/3
$x->{"sign"} eq "+" and $x->is_zero
!ll&&!rl&&r
2064165
MC/DC100%2/2
$x->{"sign"} eq "+" and $x->is_zero
$x->{"sign"} eq "+" $x->is_zero
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $x->is_zero
ABresult
0X0
100
111
1600  
1601 312 $x -> bnorm();
1602 }
1603  
1604 316 ▶sub bmul {
Subroutine bmul: called CC=19 SCAR=29.6
Pod documented
1605 # multiply two rational numbers
1606  
1607 # set up parameters
1608 316 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 310 6 316
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
06310
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
1609 ? (ref($_[0]), @_)
1610 : objectify(2, @_);
1611  
1612 # Don't modify constant (read-only) objects.
1613  
1614 316 ▶ return $x if $x -> modify('bmul');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmul") 0 316 316
1615  
1616 316 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 29 287 316
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1712287
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
1617  
1618 # inf handling
1619 287 ▶ if ($x -> is_inf() || $y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf or $y->is_inf) 49 238 287
Condition100%3/3
$x->is_inf or $y->is_inf
l!l&&r!l&&!r
3316238
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
1620 49 ▶ return $x -> bnan(@r) if $x -> is_zero() || $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $y->is_zero 0 49 49
Condition33%1/3
$x->is_zero or $y->is_zero
l!l&&r!l&&!r
0049
MC/DC0%0/2
$x->is_zero or $y->is_zero
$x->is_zero $y->is_zero
Truth table33%1/3
A or B
A= $x->is_zeroB= $y->is_zero
ABresult
1X1
011
000
1621 # result will always be +-inf:
1622 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
1623 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
1624 49 ▶ return $x -> binf(@r) if $x -> is_positive() && $y -> is_positive();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_positive and $y->is_positive 17 32 49
Condition100%3/3
$x->is_positive and $y->is_positive
!ll&&!rl&&r
161617
MC/DC100%2/2
$x->is_positive and $y->is_positive
$x->is_positive $y->is_positive
Truth table100%3/3
A and B
A= $x->is_positiveB= $y->is_positive
ABresult
0X0
100
111
1625 32 ▶ return $x -> binf(@r) if $x -> is_negative() && $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_negative and $y->is_negative 8 24 32
Condition100%3/3
$x->is_negative and $y->is_negative
!ll&&!rl&&r
1688
MC/DC100%2/2
$x->is_negative and $y->is_negative
$x->is_negative $y->is_negative
Truth table100%3/3
A and B
A= $x->is_negativeB= $y->is_negative
ABresult
0X0
100
111
1626 24 return $x -> binf('-', @r);
1627 }
1628  
1629 238 ▶ return $x -> _upg() -> bmul($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 238 238
1630  
1631 238 ▶ if ($x -> is_zero() || $y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero or $y->is_zero) 33 205 238
Condition100%3/3
$x->is_zero or $y->is_zero
l!l&&r!l&&!r
2112205
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
1632 33 return $x -> bzero(@r);
1633 }
1634  
1635 # According to Knuth, this can be optimized by doing gcd twice (for d
1636 # and n) and reducing in one step.
1637 #
1638 # p s p * s (p / gcd(p, r)) * (s / gcd(s, q))
1639 # - * - = ----- = ---------------------------------
1640 # q r q * r (q / gcd(s, q)) * (r / gcd(p, r))
1641  
1642 205 my $gcd_pr = $LIB -> _gcd($LIB -> _copy($x->{_n}), $y->{_d});
1643 205 my $gcd_sq = $LIB -> _gcd($LIB -> _copy($y->{_n}), $x->{_d});
1644  
1645 $x->{_n} = $LIB -> _mul(scalar $LIB -> _div($x->{_n}, $gcd_pr),
1646 205 scalar $LIB -> _div($LIB -> _copy($y->{_n}),
1647 $gcd_sq));
1648 $x->{_d} = $LIB -> _mul(scalar $LIB -> _div($x->{_d}, $gcd_sq),
1649 205 scalar $LIB -> _div($LIB -> _copy($y->{_d}),
1650 $gcd_pr));
1651  
1652 # compute new sign
1653 205 ▶ $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 157 48 205
1654  
1655 205 $x -> bnorm(); # this is probably redundant; check XXX
1656 205 $x -> round(@r);
1657 205 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 131 74 205
1658 205 return $x;
1659 }
1660  
1661 16 *bdiv = \&bfdiv;
1662 16 *bmod = \&bfmod;
1663  
1664 395 ▶sub bfdiv {
Subroutine bfdiv: called CC=28 SCAR=35.7
Pod documented
1665 # (dividend: BRAT or num_str, divisor: BRAT or num_str) return
1666 # (BRAT, BRAT) (quo, rem) or BRAT (only rem)
1667  
1668 # Set up parameters.
1669 395 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 387 8 395
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
08387
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
1670 ? (ref($_[0]), @_)
1671 : objectify(2, @_);
1672  
1673 ###########################################################################
1674 # Code for all classes that share the common interface.
1675 ###########################################################################
1676  
1677 # Don't modify constant (read-only) objects.
1678  
1679 395 ▶ return $x if $x -> modify('bfdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfdiv") 0 395 395
1680  
1681 395 my $wantarray = wantarray; # call only once
1682  
1683 # At least one argument is NaN. This is handled the same way as in
1684 # Math::BigInt -> bfdiv().
1685  
1686 395 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 22 373 395
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1012373
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
1687 22 ▶ return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 22 22
1688 : $x -> bnan(@r);
1689 }
1690  
1691 # Divide by zero and modulo zero. This is handled the same way as in
1692 # Math::BigInt -> bfdiv(). See the comments in the code implementing that
1693 # method.
1694  
1695 373 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 36 337 373
1696 36 my $rem;
1697 36 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 12 24 36
1698 12 $rem = $x -> copy() -> round(@r);
1699 12 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 12 0 12
1700 }
1701 36 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 10 26 36
1702 10 $x -> bnan(@r);
1703 } else {
1704 26 $x -> binf($x -> {sign}, @r);
1705 }
1706 32 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 12 20 32
1707 }
1708  
1709 # Numerator (dividend) is +/-inf. This is handled the same way as in
1710 # Math::BigInt -> bfdiv(). See the comment in the code for Math::BigInt ->
1711 # bfdiv() for further details.
1712  
1713 337 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 20 317 337
1714 20 my $rem;
1715 20 ▶ $rem = $class -> bnan(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 0 20 20
1716 20 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 4 16 20
1717 4 $x -> bnan(@r);
1718 } else {
1719 16 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 8 8 16
1720 16 $x -> binf($sign, @r);
1721 }
1722 20 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 20 20
1723 }
1724  
1725 # Denominator (divisor) is +/-inf. This is handled the same way as in
1726 # Math::BigFloat -> bfdiv(). See the comments in the code implementing that
1727 # method.
1728  
1729 317 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 24 293 317
1730 24 my $rem;
1731 24 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 24 24
1732 0 ▶ 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)) { } 0 0 0
Condition0%0/3
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
$x->is_zero $x->bcmp(0) == $y->bcmp(0)
Truth table0%0/3
A or B
A= $x->is_zeroB= $x->bcmp(0) == $y->bcmp(0)
ABresult
1X1
011
000
1733 0 $rem = $x -> copy() -> round(@r);
1734 0 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 0 0 0
1735 0 $x -> bzero(@r);
1736 } else {
1737 0 $rem = $class -> binf($y -> {sign}, @r);
1738 0 $x -> bone('-', @r);
1739 }
1740 } else {
1741 24 $x -> bzero(@r);
1742 }
1743 24 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 24 24
1744 }
1745  
1746 # At this point, both the numerator and denominator are finite, non-zero
1747 # numbers.
1748  
1749 # According to Knuth, this can be optimized by doing gcd twice (for d and n)
1750 # and reducing in one step. This would save us the bnorm().
1751 #
1752 # p r p * s (p / gcd(p, r)) * (s / gcd(s, q))
1753 # - / - = ----- = ---------------------------------
1754 # q s q * r (q / gcd(s, q)) * (r / gcd(p, r))
1755  
1756 293 $x->{_n} = $LIB->_mul($x->{_n}, $y->{_d});
1757 293 $x->{_d} = $LIB->_mul($x->{_d}, $y->{_n});
1758  
1759 # compute new sign
1760 293 ▶ $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 223 70 293
1761  
1762 293 $x -> bnorm();
1763 293 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 28 265 293
1764 28 my $rem = $x -> copy();
1765 28 $x -> bfloor();
1766 28 $x -> round(@r);
1767 28 $rem -> bsub($x -> copy()) -> bmul($y);
1768 28 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 28 0 28
1769 28 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 20 8 28
1770 28 return $x, $rem;
1771 }
1772  
1773 265 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 148 117 265
1774 265 return $x;
1775 }
1776  
1777 75 ▶sub bfmod {
Subroutine bfmod: called CC=13 SCAR=35.3
Pod documented
1778 # This is the remainder after floored division.
1779  
1780 # Set up parameters.
1781 75 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 75 0 75
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0075
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
1782 ? (ref($_[0]), @_)
1783 : objectify(2, @_);
1784  
1785 ###########################################################################
1786 # Code for all classes that share the common interface.
1787 ###########################################################################
1788  
1789 # Don't modify constant (read-only) objects.
1790  
1791 75 ▶ return $x if $x -> modify('bfmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfmod") 0 75 75
1792  
1793 # At least one argument is NaN. This is handled the same way as in
1794 # Math::BigInt -> bfmod().
1795  
1796 75 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 8 67 75
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
4467
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
1797 8 return $x -> bnan();
1798 }
1799  
1800 # Modulo zero. This is handled the same way as in Math::BigInt -> bfmod().
1801  
1802 67 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 0 67 67
1803 0 return $x -> round();
1804 }
1805  
1806 # Numerator (dividend) is +/-inf. This is handled the same way as in
1807 # Math::BigInt -> bfmod().
1808  
1809 67 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 67 67
1810 0 return $x -> bnan();
1811 }
1812  
1813 # Denominator (divisor) is +/-inf. This is handled the same way as in
1814 # Math::BigInt -> bfmod().
1815  
1816 67 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 0 67 67
1817 0 ▶ 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)) { } 0 0 0
Condition0%0/3
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
$x->is_zero $x->bcmp(0) == $y->bcmp(0)
Truth table0%0/3
A or B
A= $x->is_zeroB= $x->bcmp(0) == $y->bcmp(0)
ABresult
1X1
011
000
1818 0 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 0 0
1819 0 return $x;
1820 } else {
1821 0 return $x -> binf($y -> sign());
1822 }
1823 }
1824  
1825 # At this point, both the numerator and denominator are finite numbers, and
1826 # the denominator (divisor) is non-zero.
1827  
1828 67 ▶ if ($x -> is_zero()) { # 0 / 7 = 0, mod 0
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 0 67 67
1829 0 return $x -> bzero();
1830 }
1831  
1832 # Compute $x - $y * floor($x / $y). This can be optimized by working on the
1833 # library thingies directly. XXX
1834  
1835 67 $x -> bsub($x -> copy() -> bfdiv($y) -> bfloor() -> bmul($y));
1836 67 return $x -> round(@r);
1837 }
1838  
1839 0 ▶sub btdiv {
Subroutine btdiv: not called CC=31 SCAR=69.0
Pod documented
1840 # (dividend: BRAT or num_str, divisor: BRAT or num_str) return
1841 # (BRAT, BRAT) (quo, rem) or BRAT (only rem)
1842  
1843 # Set up parameters.
1844 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
1845 ? (ref($_[0]), @_)
1846 : objectify(2, @_);
1847  
1848 ###########################################################################
1849 # Code for all classes that share the common interface.
1850 ###########################################################################
1851  
1852 # Don't modify constant (read-only) objects.
1853  
1854 0 ▶ return $x if $x -> modify('btdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btdiv") 0 0 0
1855  
1856 0 my $wantarray = wantarray; # call only once
1857  
1858 # At least one argument is NaN. This is handled the same way as in
1859 # Math::BigInt -> btdiv().
1860  
1861 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
1862 0 ▶ return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
1863 : $x -> bnan(@r);
1864 }
1865  
1866 # Divide by zero and modulo zero. This is handled the same way as in
1867 # Math::BigInt -> btdiv(). See the comments in the code implementing that
1868 # method.
1869  
1870 0 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 0 0 0
1871 0 my $rem;
1872 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 0 0 0
1873 0 $rem = $x -> copy() -> round(@r);
1874 0 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 0 0 0
1875 }
1876 0 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 0 0 0
1877 0 $x -> bnan(@r);
1878 } else {
1879 0 $x -> binf($x -> {sign}, @r);
1880 }
1881 0 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
1882 }
1883  
1884 # Numerator (dividend) is +/-inf. This is handled the same way as in
1885 # Math::BigInt -> btdiv(). See the comment in the code for Math::BigInt ->
1886 # btdiv() for further details.
1887  
1888 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
1889 0 my $rem;
1890 0 ▶ $rem = $class -> bnan(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 0 0 0
1891 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 0 0 0
1892 0 $x -> bnan(@r);
1893 } else {
1894 0 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 0 0 0
1895 0 $x -> binf($sign, @r);
1896 }
1897 0 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
1898 }
1899  
1900 # Denominator (divisor) is +/-inf. This is handled the same way as in
1901 # Math::BigFloat -> btdiv(). See the comments in the code implementing that
1902 # method.
1903  
1904 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 0 0 0
1905 0 my $rem;
1906 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 0 0
1907 0 $rem = $x -> copy();
1908 0 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 0 0 0
1909 0 $x -> bzero();
1910 0 return $x, $rem;
1911 } else {
1912 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 0 0 0
1913 0 ▶ 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
1914 0 return $x -> bnan();
1915 } else {
1916 0 return $x -> bzero();
1917 }
1918 }
1919 }
1920 }
1921  
1922 0 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 0 0 0
1923 0 $x -> round(@r);
1924 0 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 0 0
1925 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 0 0 0
1926 0 my $rem = $class -> bzero(@r);
1927 0 return $x, $rem;
1928 }
1929 0 return $x;
1930 }
1931  
1932 # At this point, both the numerator and denominator are finite, non-zero
1933 # numbers.
1934  
1935 # According to Knuth, this can be optimized by doing gcd twice (for d and n)
1936 # and reducing in one step. This would save us the bnorm().
1937 #
1938 # p r p * s (p / gcd(p, r)) * (s / gcd(s, q))
1939 # - / - = ----- = ---------------------------------
1940 # q s q * r (q / gcd(s, q)) * (r / gcd(p, r))
1941  
1942 0 $x->{_n} = $LIB->_mul($x->{_n}, $y->{_d});
1943 0 $x->{_d} = $LIB->_mul($x->{_d}, $y->{_n});
1944  
1945 # compute new sign
1946 0 ▶ $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 0 0 0
1947  
1948 0 $x -> bnorm();
1949 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 0 0 0
1950 0 my $rem = $x -> copy();
1951 0 $x -> bint();
1952 0 $x -> round(@r);
1953 0 $rem -> bsub($x -> copy()) -> bmul($y);
1954 0 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 0 0
1955 0 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 0 0 0
1956 0 return $x, $rem;
1957 }
1958  
1959 0 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 0 0
1960 0 return $x;
1961 }
1962  
1963 0 ▶sub btmod {
Subroutine btmod: not called CC=11 SCAR=48.8
Pod documented
1964 # This is the remainder after floored division.
1965  
1966 # Set up parameters.
1967 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
1968 ? (ref($_[0]), @_)
1969 : objectify(2, @_);
1970  
1971 ###########################################################################
1972 # Code for all classes that share the common interface.
1973 ###########################################################################
1974  
1975 # Don't modify constant (read-only) objects.
1976  
1977 0 ▶ return $x if $x -> modify('btmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btmod") 0 0 0
1978  
1979 # At least one argument is NaN. This is handled the same way as in
1980 # Math::BigInt -> btmod().
1981  
1982 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
1983 0 return $x -> bnan();
1984 }
1985  
1986 # Modulo zero. This is handled the same way as in Math::BigInt -> btmod().
1987  
1988 0 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 0 0 0
1989 0 return $x -> round();
1990 }
1991  
1992 # Numerator (dividend) is +/-inf. This is handled the same way as in
1993 # Math::BigInt -> btmod().
1994  
1995 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
1996 0 return $x -> bnan();
1997 }
1998  
1999 # Denominator (divisor) is +/-inf. This is handled the same way as in
2000 # Math::BigInt -> btmod().
2001  
2002 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 0 0 0
2003 0 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 0 0
2004 0 return $x;
2005 }
2006  
2007 # At this point, both the numerator and denominator are finite numbers, and
2008 # the denominator (divisor) is non-zero.
2009  
2010 0 ▶ if ($x -> is_zero()) { # 0 / 7 = 0, mod 0
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 0 0 0
2011 0 return $x -> bzero();
2012 }
2013  
2014 # Compute $x - $y * int($x / $y).
2015 #
2016 # p r (p * s / gcd(q, s)) mod (r * q / gcd(q, s))
2017 # - mod - = -------------------------------------------
2018 # q s q * s / gcd(q, s)
2019 #
2020 # u mod v u = p * (s / gcd(q, s))
2021 # = ------- where v = r * (q / gcd(q, s))
2022 # w w = q * (s / gcd(q, s))
2023  
2024 0 my $p = $x -> {_n};
2025 0 my $q = $x -> {_d};
2026 0 my $r = $y -> {_n};
2027 0 my $s = $y -> {_d};
2028  
2029 0 my $gcd_qs = $LIB -> _gcd($LIB -> _copy($q), $s);
2030 0 my $s_by_gcd_qs = $LIB -> _div($LIB -> _copy($s), $gcd_qs);
2031 0 my $q_by_gcd_qs = $LIB -> _div($LIB -> _copy($q), $gcd_qs);
2032  
2033 0 my $u = $LIB -> _mul($LIB -> _copy($p), $s_by_gcd_qs);
2034 0 my $v = $LIB -> _mul($LIB -> _copy($r), $q_by_gcd_qs);
2035 0 my $w = $LIB -> _mul($LIB -> _copy($q), $s_by_gcd_qs);
2036  
2037 0 $x->{_n} = $LIB -> _mod($u, $v);
2038 0 $x->{_d} = $w;
2039  
2040 0 $x -> bnorm();
2041 0 return $x -> round(@r);
2042 }
2043  
2044 0 ▶sub binv {
Subroutine binv: not called CC=9 SCAR=45.0
Pod documented
2045 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2046  
2047 # Don't modify constant (read-only) objects.
2048  
2049 0 ▶ return $x if $x -> modify('binv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("binv") 0 0 0
2050  
2051 0 ▶ return $x -> round(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
2052 0 ▶ return $x -> bzero(@r) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
2053 0 ▶ return $x -> binf("+", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
2054  
2055 0 ($x -> {_n}, $x -> {_d}) = ($x -> {_d}, $x -> {_n});
2056  
2057 0 $x -> round(@r);
2058 0 ▶ $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 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
2059 0 return $x;
2060 }
2061  
2062 59 ▶sub bsqrt {
Subroutine bsqrt: called CC=9 SCAR=22.0
Pod documented
2063 59 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 59 0 59
2064  
2065 # Don't modify constant (read-only) objects.
2066  
2067 59 ▶ return $x if $x -> modify('bsqrt');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsqrt") 0 59 59
2068  
2069 59 ▶ return $x -> bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+]/ 13 46 59
2070 46 ▶ return $x if $x -> is_inf("+"); # sqrt(inf) == inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 4 42 46
2071 42 ▶ return $x -> round(@r) if $x -> is_zero() || $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one 8 34 42
Condition100%3/3
$x->is_zero or $x->is_one
l!l&&r!l&&!r
4434
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
2072  
2073 34 my $n = $x -> {_n};
2074 34 my $d = $x -> {_d};
2075  
2076 # Look for an exact solution. For the numerator and the denominator, take
2077 # the square root and square it and see if we got the original value. If we
2078 # did, for both the numerator and the denominator, we have an exact
2079 # solution.
2080  
2081 {
2082 34 my $nsqrt = $LIB -> _sqrt($LIB -> _copy($n));
2083 34 my $n2 = $LIB -> _mul($LIB -> _copy($nsqrt), $nsqrt);
2084 34 ▶ if ($LIB -> _acmp($n, $n2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($n, $n2) == 0) 32 2 34
2085 32 my $dsqrt = $LIB -> _sqrt($LIB -> _copy($d));
2086 32 my $d2 = $LIB -> _mul($LIB -> _copy($dsqrt), $dsqrt);
2087 32 ▶ if ($LIB -> _acmp($d, $d2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($d, $d2) == 0) 31 1 32
2088 31 $x -> {_n} = $nsqrt;
2089 31 $x -> {_d} = $dsqrt;
2090 31 return $x -> round(@r);
2091 }
2092 }
2093 }
2094  
2095 3 local $Math::BigFloat::upgrade = undef;
2096 3 local $Math::BigFloat::downgrade = undef;
2097 3 local $Math::BigFloat::precision = undef;
2098 3 local $Math::BigFloat::accuracy = undef;
2099 3 local $Math::BigInt::upgrade = undef;
2100 3 local $Math::BigInt::precision = undef;
2101 3 local $Math::BigInt::accuracy = undef;
2102  
2103 3 my $xn = Math::BigFloat -> new($LIB -> _str($n));
2104 3 my $xd = Math::BigFloat -> new($LIB -> _str($d));
2105  
2106 3 my $xtmp = Math::BigRat -> new($xn -> bfdiv($xd) -> bsqrt() -> bfstr());
2107  
2108 3 $x -> {sign} = $xtmp -> {sign};
2109 3 $x -> {_n} = $xtmp -> {_n};
2110 3 $x -> {_d} = $xtmp -> {_d};
2111  
2112 3 $x -> round(@r);
2113 }
2114  
2115 735 ▶sub bpow {
Subroutine bpow: called CC=39 SCAR=36.6
Pod documented
2116 # power ($x ** $y)
2117  
2118 # Set up parameters.
2119 735 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 729 6 735
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
06729
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
2120 ? (ref($_[0]), @_)
2121 : objectify(2, @_);
2122  
2123 # Don't modify constant (read-only) objects.
2124  
2125 735 ▶ return $x if $x -> modify('bpow');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bpow") 0 735 735
2126  
2127 # $x and/or $y is a NaN
2128 735 ▶ return $x -> bnan() if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 116 619 735
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
6056619
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
2129  
2130 # $x and/or $y is a +/-Inf
2131 619 ▶ if ($x -> is_inf("-")) {
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 52 567 619
elsif ($x->is_inf('+')) { } 52 515 567
elsif ($y->is_inf('-')) { } 44 471 515
elsif ($y->is_inf('+')) { } 44 427 471
2132 52 ▶ return $x -> bzero() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 24 28 52
2133 28 ▶ return $x -> bnan() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 24 28
2134 24 ▶ return $x if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 8 16 24
2135 16 return $x -> bneg();
2136 } elsif ($x -> is_inf("+")) {
2137 52 ▶ return $x -> bzero() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 24 28 52
2138 28 ▶ return $x -> bnan() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 24 28
2139 24 return $x;
2140 } elsif ($y -> is_inf("-")) {
2141 44 ▶ return $x -> bnan() if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 4 40 44
2142 40 ▶ return $x -> binf("+") if $x > -1 && $x < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x > -1 and $x < 1 12 28 40
Condition100%3/3
$x > -1 and $x < 1
!ll&&!rl&&r
121612
MC/DC100%2/2
$x > -1 and $x < 1
$x > -1 $x < 1
Truth table100%3/3
A and B
A= $x > -1B= $x < 1
ABresult
0X0
100
111
2143 28 ▶ return $x -> bone() if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 4 24 28
2144 24 return $x -> bzero();
2145 } elsif ($y -> is_inf("+")) {
2146 44 ▶ return $x -> bnan() if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 4 40 44
2147 40 ▶ return $x -> bzero() if $x > -1 && $x < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x > -1 and $x < 1 12 28 40
Condition100%3/3
$x > -1 and $x < 1
!ll&&!rl&&r
121612
MC/DC100%2/2
$x > -1 and $x < 1
$x > -1 $x < 1
Truth table100%3/3
A and B
A= $x > -1B= $x < 1
ABresult
0X0
100
111
2148 28 ▶ return $x -> bone() if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 4 24 28
2149 24 return $x -> binf("+");
2150 }
2151  
2152 427 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 44 383 427
2153 44 ▶ return $x -> bone() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 40 44
2154 40 ▶ return $x -> binf() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 20 20 40
2155 20 return $x;
2156 }
2157  
2158 # We don't support complex numbers, so upgrade or return NaN.
2159  
2160 383 ▶ if ($x -> is_negative() && !$y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_negative and not $y->is_int) 80 303 383
Condition100%3/3
$x->is_negative and not $y->is_int
!ll&&!rl&&r
16314080
MC/DC100%2/2
$x->is_negative and not $y->is_int
$x->is_negative not $y->is_int
Truth table100%3/3
A and not(B)
A= $x->is_negativeB= not $y->is_int
ABresult
0X0
100
111
2161 80 ▶ return $x -> _upg() -> bpow($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 80 80
2162 80 return $x -> bnan();
2163 }
2164  
2165 303 ▶ if ($x -> is_one("+") || $y -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("+") or $y->is_one) 80 223 303
Condition100%3/3
$x->is_one("+") or $y->is_one
l!l&&r!l&&!r
4436223
MC/DC100%2/2
$x->is_one("+") or $y->is_one
$x->is_one("+") $y->is_one
Truth table100%3/3
A or B
A= $x->is_one("+")B= $y->is_one
ABresult
1X1
011
000
2166 80 return $x;
2167 }
2168  
2169 223 ▶ if ($x -> is_one("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("-")) 24 199 223
2170 24 ▶ return $x if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 12 12 24
2171 12 return $x -> bneg();
2172 }
2173  
2174 # (a/b)^-(c/d) = (b/a)^(c/d)
2175 199 ▶ ($x->{_n}, $x->{_d}) = ($x->{_d}, $x->{_n}) if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 96 103 199
2176  
2177 199 ▶ unless ($LIB->_is_one($y->{_n})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($y->{"_n"})) 166 33 199
2178 166 $x->{_n} = $LIB->_pow($x->{_n}, $y->{_n});
2179 166 $x->{_d} = $LIB->_pow($x->{_d}, $y->{_n});
2180 166 ▶ $x->{sign} = '+' if $x->{sign} eq '-' && $LIB->_is_even($y->{_n});
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" and $LIB->_is_even($y->{"_n"}) 48 118 166
Condition100%3/3
$x->{"sign"} eq "-" and $LIB->_is_even($y->{"_n"})
!ll&&!rl&&r
863248
MC/DC100%2/2
$x->{"sign"} eq "-" and $LIB->_is_even($y->{"_n"})
$x->{"sign"} eq "-" $LIB->_is_even($y->{"_n"})
Truth table100%3/3
A and B
A= $x->{"sign"} eq "-"B= $LIB->_is_even($y->{"_n"})
ABresult
0X0
100
111
2181 }
2182  
2183 199 ▶ unless ($LIB->_is_one($y->{_d})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($y->{"_d"})) 2 197 199
2184 2 ▶ return $x -> bsqrt(@r) if $LIB->_is_two($y->{_d}); # 1/2 => sqrt
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_two($y->{"_d"}) 1 1 2
2185 1 return $x -> broot($LIB->_str($y->{_d}), @r); # 1/N => root(N)
2186 }
2187  
2188 197 return $x -> round(@r);
2189 }
2190  
2191 7 ▶sub broot {
Subroutine broot: called CC=4 SCAR=13.9
Pod documented
2192 # set up parameters
2193 7 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 2 5 7
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
052
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
2194 ? (ref($_[0]), @_)
2195 : objectify(2, @_);
2196  
2197 # Don't modify constant (read-only) objects.
2198  
2199 7 ▶ return $x if $x -> modify('broot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("broot") 0 7 7
2200  
2201 # Convert $x into a Math::BigFloat.
2202  
2203 7 my $xd = Math::BigFloat -> new($LIB -> _str($x->{_d}));
2204 7 my $xflt = Math::BigFloat -> new($LIB -> _str($x->{_n})) -> bfdiv($xd);
2205 7 $xflt -> {sign} = $x -> {sign};
2206  
2207 # Convert $y into a Math::BigFloat.
2208  
2209 7 my $yd = Math::BigFloat -> new($LIB -> _str($y->{_d}));
2210 7 my $yflt = Math::BigFloat -> new($LIB -> _str($y->{_n})) -> bfdiv($yd);
2211 7 $yflt -> {sign} = $y -> {sign};
2212  
2213 # Compute the root and convert back to a Math::BigRat.
2214  
2215 7 $xflt -> broot($yflt, @r);
2216 7 my $xtmp = Math::BigRat -> new($xflt -> bfstr());
2217  
2218 7 $x -> {sign} = $xtmp -> {sign};
2219 7 $x -> {_n} = $xtmp -> {_n};
2220 7 $x -> {_d} = $xtmp -> {_d};
2221  
2222 7 return $x;
2223 }
2224  
2225 0 ▶sub bmuladd {
Subroutine bmuladd: not called CC=38 SCAR=73.0
Pod documented
2226 # multiply two numbers and then add the third to the result
2227 # (BINT or num_str, BINT or num_str, BINT or num_str) return BINT
2228  
2229 # set up parameters
2230 0 ▶ my ($class, $x, $y, $z, @r)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 0 0 0
Condition0%0/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
000
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
000
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 table0%0/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
2231 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
2232 ? (ref($_[0]), @_)
2233 : objectify(3, @_);
2234  
2235 # Don't modify constant (read-only) objects.
2236  
2237 0 ▶ return $x if $x -> modify('bmuladd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmuladd") 0 0 0
2238  
2239 # At least one of x, y, and z is a NaN
2240  
2241 0 ▶ return $x -> bnan(@r) if ($x -> is_nan() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan or $z->is_nan 0 0 0
Condition0%0/6
$x->is_nan or $y->is_nan or $z->is_nan
l!l&&r!l&&!r
000
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->is_nan or $y->is_nan or $z->is_nan
$x->is_nan $y->is_nan $z->is_nan
Truth table0%0/4
A or B or C
A= $x->is_nanB= $y->is_nanC= $z->is_nan
ABCresult
1XX1
01X1
0011
0000
2242 $y -> is_nan() ||
2243 $z -> is_nan());
2244  
2245 # At least one of x, y, and z is an Inf
2246  
2247 0 ▶ if ($x -> is_inf("-")) {
Branch: 5 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 0 0 0
elsif ($x->is_inf('+')) { } 0 0 0
elsif ($x->is_neg) { } 0 0 0
elsif ($x->is_zero) { } 0 0 0
elsif ($x->is_pos) { } 0 0 0
2248  
2249 0 ▶ if ($y -> is_neg()) { # x = -inf, y < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_neg) { } 0 0 0
elsif ($y->is_zero) { } 0 0 0
2250 0 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
2251 0 return $x -> bnan(@r);
2252 } else {
2253 0 return $x -> binf("+", @r);
2254 }
2255 } elsif ($y -> is_zero()) { # x = -inf, y = 0
2256 0 return $x -> bnan(@r);
2257 } else { # x = -inf, y > 0
2258 0 ▶ if ($z -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('+')) { } 0 0 0
2259 0 return $x -> bnan(@r);
2260 } else {
2261 0 return $x -> binf("-", @r);
2262 }
2263 }
2264  
2265 } elsif ($x -> is_inf("+")) {
2266  
2267 0 ▶ if ($y -> is_neg()) { # x = +inf, y < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_neg) { } 0 0 0
elsif ($y->is_zero) { } 0 0 0
2268 0 ▶ if ($z -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('+')) { } 0 0 0
2269 0 return $x -> bnan(@r);
2270 } else {
2271 0 return $x -> binf("-", @r);
2272 }
2273 } elsif ($y -> is_zero()) { # x = +inf, y = 0
2274 0 return $x -> bnan(@r);
2275 } else { # x = +inf, y > 0
2276 0 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
2277 0 return $x -> bnan(@r);
2278 } else {
2279 0 return $x -> binf("+", @r);
2280 }
2281 }
2282  
2283 } elsif ($x -> is_neg()) {
2284  
2285 0 ▶ if ($y -> is_inf("-")) { # -inf < x < 0, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('-')) { } 0 0 0
elsif ($y->is_inf('+')) { } 0 0 0
2286 0 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
2287 0 return $x -> bnan(@r);
2288 } else {
2289 0 return $x -> binf("+", @r);
2290 }
2291 } elsif ($y -> is_inf("+")) { # -inf < x < 0, y = +inf
2292 0 ▶ if ($z -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('+')) { } 0 0 0
2293 0 return $x -> bnan(@r);
2294 } else {
2295 0 return $x -> binf("-", @r);
2296 }
2297 } else { # -inf < x < 0, -inf < y < +inf
2298 0 ▶ if ($z -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
elsif ($z->is_inf('+')) { } 0 0 0
2299 0 return $x -> binf("-", @r);
2300 } elsif ($z -> is_inf("+")) {
2301 0 return $x -> binf("+", @r);
2302 }
2303 }
2304  
2305 } elsif ($x -> is_zero()) {
2306  
2307 0 ▶ if ($y -> is_inf("-")) { # x = 0, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('-')) { } 0 0 0
elsif ($y->is_inf('+')) { } 0 0 0
2308 0 return $x -> bnan(@r);
2309 } elsif ($y -> is_inf("+")) { # x = 0, y = +inf
2310 0 return $x -> bnan(@r);
2311 } else { # x = 0, -inf < y < +inf
2312 0 ▶ if ($z -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
elsif ($z->is_inf('+')) { } 0 0 0
2313 0 return $x -> binf("-", @r);
2314 } elsif ($z -> is_inf("+")) {
2315 0 return $x -> binf("+", @r);
2316 }
2317 }
2318  
2319 } elsif ($x -> is_pos()) {
2320  
2321 0 ▶ if ($y -> is_inf("-")) { # 0 < x < +inf, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('-')) { } 0 0 0
elsif ($y->is_inf('+')) { } 0 0 0
2322 0 ▶ if ($z -> is_inf("+")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('+')) { } 0 0 0
2323 0 return $x -> bnan(@r);
2324 } else {
2325 0 return $x -> binf("-", @r);
2326 }
2327 } elsif ($y -> is_inf("+")) { # 0 < x < +inf, y = +inf
2328 0 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
2329 0 return $x -> bnan(@r);
2330 } else {
2331 0 return $x -> binf("+", @r);
2332 }
2333 } else { # 0 < x < +inf, -inf < y < +inf
2334 0 ▶ if ($z -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
elsif ($z->is_inf('+')) { } 0 0 0
2335 0 return $x -> binf("-", @r);
2336 } elsif ($z -> is_inf("+")) {
2337 0 return $x -> binf("+", @r);
2338 }
2339 }
2340 }
2341  
2342 # The code below might be faster if we compute the GCD earlier than in the
2343 # call to bnorm().
2344 #
2345 # xs * xn ys * yn zs * zn / xs: sign of x \
2346 # ------- * ------- + ------- | xn: numerator of x |
2347 # xd yd zd | xd: denominator of x |
2348 # \ ditto for y and z /
2349 # xs * ys * xn * yn zs * zn
2350 # = ----------------- + -------
2351 # xd * yd zd
2352 #
2353 # xs * ys * xn * yn * zd + zs * xd * yd * zn
2354 # = ------------------------------------------
2355 # xd * yd * zd
2356  
2357 0 my $xn_yn = $LIB -> _mul($LIB -> _copy($x->{_n}), $y->{_n});
2358 0 my $xn_yn_zd = $LIB -> _mul($xn_yn, $z->{_d});
2359  
2360 0 my $xd_yd = $LIB -> _mul($x->{_d}, $y->{_d});
2361 0 my $xd_yd_zn = $LIB -> _mul($LIB -> _copy($xd_yd), $z->{_n});
2362  
2363 0 my $xd_yd_zd = $LIB -> _mul($xd_yd, $z->{_d});
2364  
2365 0 ▶ my $sgn1 = $x->{sign} eq $y->{sign} ? "+" : "-";
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 0 0 0
2366 0 my $sgn2 = $z->{sign};
2367  
2368 0 ($x->{_n}, $x->{sign}) = $LIB -> _sadd($xn_yn_zd, $sgn1,
2369 $xd_yd_zn, $sgn2);
2370 0 $x->{_d} = $xd_yd_zd;
2371 0 $x -> bnorm();
2372  
2373 0 return $x;
2374 }
2375  
2376 70 ▶sub bmodpow {
Subroutine bmodpow: called CC=5 SCAR=17.2
Pod documented
2377 # set up parameters
2378 70 ▶ my ($class, $x, $y, $m, @r)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 70 0 70
Condition33%2/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
0070
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0070
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
2379 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
2380 ? (ref($_[0]), @_)
2381 : objectify(3, @_);
2382  
2383 # Don't modify constant (read-only) objects.
2384  
2385 70 ▶ return $x if $x -> modify('bmodpow');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmodpow") 0 70 70
2386  
2387 # Convert $x, $y, and $m into Math::BigInt objects.
2388  
2389 70 my $xint = Math::BigInt -> new($x -> copy() -> bint());
2390 70 my $yint = Math::BigInt -> new($y -> copy() -> bint());
2391 70 my $mint = Math::BigInt -> new($m -> copy() -> bint());
2392  
2393 70 $xint -> bmodpow($yint, $mint, @r);
2394 70 my $xtmp = Math::BigRat -> new($xint -> bfstr());
2395  
2396 70 $x -> {sign} = $xtmp -> {sign};
2397 70 $x -> {_n} = $xtmp -> {_n};
2398 70 $x -> {_d} = $xtmp -> {_d};
2399 70 return $x;
2400 }
2401  
2402 62 ▶sub bmodinv {
Subroutine bmodinv: called CC=4 SCAR=14.4
Pod documented
2403 # set up parameters
2404 62 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 62 0 62
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0062
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
2405 ? (ref($_[0]), @_)
2406 : objectify(2, @_);
2407  
2408 # Don't modify constant (read-only) objects.
2409  
2410 62 ▶ return $x if $x -> modify('bmodinv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmodinv") 0 62 62
2411  
2412 # Convert $x and $y into Math::BigInt objects.
2413  
2414 62 my $xint = Math::BigInt -> new($x -> copy() -> bint());
2415 62 my $yint = Math::BigInt -> new($y -> copy() -> bint());
2416  
2417 62 $xint -> bmodinv($yint, @r);
2418 62 my $xtmp = Math::BigRat -> new($xint -> bfstr());
2419  
2420 62 $x -> {sign} = $xtmp -> {sign};
2421 62 $x -> {_n} = $xtmp -> {_n};
2422 62 $x -> {_d} = $xtmp -> {_d};
2423 62 return $x;
2424 }
2425  
2426 74 ▶sub blog {
Subroutine blog: called CC=33 SCAR=41.2
Pod documented
2427 # Return the logarithm of the operand. If a second operand is defined, that
2428 # value is used as the base, otherwise the base is assumed to be Euler's
2429 # constant.
2430  
2431 74 my ($class, $x, $base, @r);
2432  
2433 # Don't objectify the base, since an undefined base, as in $x->blog() or
2434 # $x->blog(undef) signals that the base is Euler's number.
2435  
2436 74 ▶ if (!ref($_[0]) && $_[0] =~ /^[A-Za-z]|::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/) { } 0 74 74
Condition33%1/3
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
!ll&&!rl&&r
7400
MC/DC0%0/2
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
not ref $_[0] $_[0] =~ /^[A-Za-z]|::/
Truth table33%1/3
not(A) and B
A= not ref $_[0]B= $_[0] =~ /^[A-Za-z]|::/
ABresult
0X0
100
111
2437 # E.g., Math::BigRat->blog(256, 2)
2438 0 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
2439 defined $_[2] ? objectify(2, @_) : objectify(1, @_);
2440 } else {
2441 # E.g., Math::BigRat::blog(256, 2) or $x->blog(2)
2442 74 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 34 40 74
2443 defined $_[1] ? objectify(2, @_) : objectify(1, @_);
2444 }
2445  
2446 # Don't modify constant (read-only) objects.
2447  
2448 74 ▶ return $x if $x -> modify('blog');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blog") 0 74 74
2449  
2450 # Handle all exception cases and all trivial cases. I have used Wolfram Alpha
2451 # (http://www.wolframalpha.com) as the reference for these cases.
2452  
2453 74 ▶ return $x -> bnan() if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 20 54 74
2454  
2455 54 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 22 32 54
2456 22 ▶ $base = $class -> new($base) unless ref $base;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 22 22
2457 22 ▶ if ($base -> is_nan() || $base -> is_one()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($base->is_nan or $base->is_one) { } 8 14 22
elsif ($base->is_inf or $base->is_zero) { } 0 14 14
elsif ($base->is_negative) { } 8 6 14
Condition66%2/3
$base->is_nan or $base->is_one
l!l&&r!l&&!r
8014
MC/DC50%1/2
$base->is_nan or $base->is_one
$base->is_nan $base->is_one
Truth table66%2/3
A or B
A= $base->is_nanB= $base->is_one
ABresult
1X1
011
000
2458 8 return $x -> bnan();
2459 ▶ } elsif ($base -> is_inf() || $base -> is_zero()) {
Condition33%1/3
$base->is_inf or $base->is_zero
l!l&&r!l&&!r
0014
MC/DC0%0/2
$base->is_inf or $base->is_zero
$base->is_inf $base->is_zero
Truth table33%1/3
A or B
A= $base->is_infB= $base->is_zero
ABresult
1X1
011
000
2460 0 ▶ return $x -> bnan() if $x -> is_inf() || $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf or $x->is_zero 0 0 0
Condition0%0/3
$x->is_inf or $x->is_zero
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_inf or $x->is_zero
$x->is_inf $x->is_zero
Truth table0%0/3
A or B
A= $x->is_infB= $x->is_zero
ABresult
1X1
011
000
2461 0 return $x -> bzero();
2462 } elsif ($base -> is_negative()) { # -inf < base < 0
2463 8 ▶ return $x -> bzero() if $x -> is_one(); # x = 1
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 0 8 8
2464 8 ▶ return $x -> bone() if $x == $base; # x = base
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 0 8 8
2465 8 return $x -> bnan(); # otherwise
2466 }
2467 6 ▶ return $x -> bone() if $x == $base; # 0 < base && 0 < x < inf
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 0 6 6
2468 }
2469  
2470 # We now know that the base is either undefined or positive and finite.
2471  
2472 38 ▶ if ($x -> is_inf()) { # x = +/-inf
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 8 30 38
elsif ($x->is_neg) { } 8 22 30
elsif ($x->is_one) { } 4 18 22
elsif ($x->is_zero) { } 12 6 18
2473 8 ▶ my $sign = defined $base && $base < 1 ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
defined $base && $base < 1 ? : 0 8 8
Condition33%1/3
defined $base and $base < 1
!ll&&!rl&&r
800
MC/DC0%0/2
defined $base and $base < 1
defined $base $base < 1
Truth table33%1/3
A and B
A= defined $baseB= $base < 1
ABresult
0X0
100
111
2474 8 return $x -> binf($sign);
2475 } elsif ($x -> is_neg()) { # -inf < x < 0
2476 8 return $x -> bnan();
2477 } elsif ($x -> is_one()) { # x = 1
2478 4 return $x -> bzero();
2479 } elsif ($x -> is_zero()) { # x = 0
2480 12 ▶ my $sign = defined $base && $base < 1 ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
defined $base && $base < 1 ? : 0 12 12
Condition66%2/3
defined $base and $base < 1
!ll&&!rl&&r
840
MC/DC0%0/2
defined $base and $base < 1
defined $base $base < 1
Truth table66%2/3
A and B
A= defined $baseB= $base < 1
ABresult
0X0
100
111
2481 12 return $x -> binf($sign);
2482 }
2483  
2484 # Now take care of the cases where $x and/or $base is 1/N.
2485 #
2486 # log(1/N) / log(B) = -log(N)/log(B)
2487 # log(1/N) / log(1/B) = log(N)/log(B)
2488 # log(N) / log(1/B) = -log(N)/log(B)
2489  
2490 6 my $neg = 0;
2491 6 ▶ if ($x -> numerator() -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->numerator->is_one) 0 6 6
2492 0 $x -> binv();
2493 0 $neg = !$neg;
2494 }
2495 6 ▶ if (defined(blessed($base)) && $base -> isa($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($base) and $base->isa($class)) 2 4 6
Condition66%2/3
defined builtin::blessed($base) and $base->isa($class)
!ll&&!rl&&r
402
MC/DC50%1/2
defined builtin::blessed($base) and $base->isa($class)
defined builtin::blessed($base) $base->isa($class)
Truth table66%2/3
A and B
A= defined builtin::blessed($base)B= $base->isa($class)
ABresult
0X0
100
111
2496 2 ▶ if ($base -> numerator() -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base->numerator->is_one) 0 2 2
2497 0 $base = $base -> copy() -> binv();
2498 0 $neg = !$neg;
2499 }
2500 }
2501  
2502 # disable upgrading and downgrading
2503  
2504 6 require Math::BigFloat;
2505 6 my $upg = Math::BigFloat -> upgrade();
2506 6 my $dng = Math::BigFloat -> downgrade();
2507 6 Math::BigFloat -> upgrade(undef);
2508 6 Math::BigFloat -> downgrade(undef);
2509  
2510 # At this point we are done handling all exception cases and trivial cases.
2511  
2512 6 ▶ $base = Math::BigFloat -> new($base) if defined $base;
Branch: 1 branch
BranchTrueFalseTotal
if defined $base 2 4 6
2513 6 my $xnum = Math::BigFloat -> new($LIB -> _str($x->{_n}));
2514 6 my $xden = Math::BigFloat -> new($LIB -> _str($x->{_d}));
2515 6 my $xstr = $xnum -> bfdiv($xden) -> blog($base, @r) -> bfstr();
2516  
2517 # reset upgrading and downgrading
2518  
2519 6 Math::BigFloat -> upgrade($upg);
2520 6 Math::BigFloat -> downgrade($dng);
2521  
2522 6 my $xobj = Math::BigRat -> new($xstr);
2523 6 $x -> {sign} = $xobj -> {sign};
2524 6 $x -> {_n} = $xobj -> {_n};
2525 6 $x -> {_d} = $xobj -> {_d};
2526  
2527 6 ▶ return $neg ? $x -> bneg() : $x;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 6 6
2528 }
2529  
2530 1 ▶sub bexp {
Subroutine bexp: called CC=14 SCAR=36.2
Pod documented
2531 # set up parameters
2532 1 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 0 1
2533  
2534 # Don't modify constant (read-only) objects.
2535  
2536 1 ▶ return $x if $x -> modify('bexp');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bexp") 0 1 1
2537  
2538 1 ▶ return $x -> binf(@r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 1 1
2539 1 ▶ return $x -> bzero(@r) if $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("-") 0 1 1
2540  
2541 # we need to limit the accuracy to protect against overflow
2542 1 my $fallback = 0;
2543 1 my ($scale, @params);
2544 1 ($x, @params) = $x->_find_round_parameters(@r);
2545  
2546 # also takes care of the "error in _find_round_parameters?" case
2547 1 ▶ return $x if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 1 1
2548  
2549 # no rounding at all, so must use fallback
2550 1 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 1 0 1
2551 # simulate old behaviour
2552 1 $params[0] = $class -> div_scale(); # and round to it as accuracy
2553 1 $params[1] = undef; # P = undef
2554 1 $scale = $params[0]+4; # at least four more for proper round
2555 1 $params[2] = $r[2]; # round mode by caller or undef
2556 1 $fallback = 1; # to clear a/p afterwards
2557 } else {
2558 # the 4 below is empirical, and there might be cases where it's not enough...
2559 0 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition0%0/3
$params[0] || $params[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$params[0] || $params[1]
$params[0] $params[1]
Truth table0%0/3
A || B
A= $params[0]B= $params[1]
ABresult
1X1
011
000
2560 }
2561  
2562 1 ▶ return $x -> bone(@params) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 1 1
2563  
2564 # See the comments in Math::BigFloat on how this algorithm works.
2565 # Basically we calculate A and B (where B is faculty(N)) so that A/B = e
2566  
2567 1 my $x_org = $x -> copy();
2568 1 ▶ if ($scale <= 75) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= 75) { } 1 0 1
2569 # set $x directly from a cached string form
2570 $x->{_n} =
2571 1 $LIB->_new("90933395208605785401971970164779391644753259799242");
2572 $x->{_d} =
2573 1 $LIB->_new("33452526613163807108170062053440751665152000000000");
2574 1 $x->{sign} = '+';
2575 } else {
2576 # compute A and B so that e = A / B.
2577  
2578 # After some terms we end up with this, so we use it as a starting point:
2579 0 my $A = $LIB->_new("90933395208605785401971970164779391644753259799242");
2580 0 my $F = $LIB->_new(42); my $step = 42;
2581  
2582 # Compute how many steps we need to take to get $A and $B sufficiently big
2583 0 my $steps = Math::BigFloat::_len_to_steps($scale - 4);
2584 # print STDERR "# Doing $steps steps for ", $scale-4, " digits\n";
2585 0 ▶ while ($step++ <= $steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step++ <= $steps) 0 0 0
2586 # calculate $a * $f + 1
2587 0 $A = $LIB->_mul($A, $F);
2588 0 $A = $LIB->_inc($A);
2589 # increment f
2590 0 $F = $LIB->_inc($F);
2591 }
2592 # compute $B as factorial of $steps (this is faster than doing it manually)
2593 0 my $B = $LIB->_fac($LIB->_new($steps));
2594  
2595 # print "A ", $LIB->_str($A), "\nB ", $LIB->_str($B), "\n";
2596  
2597 0 $x->{_n} = $A;
2598 0 $x->{_d} = $B;
2599 0 $x->{sign} = '+';
2600 }
2601  
2602 # $x contains now an estimate of e, with some surplus digits, so we can round
2603 1 ▶ if (!$x_org -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $x_org->is_one) { } 1 0 1
2604 # raise $x to the wanted power and round it in one step:
2605 1 $x -> bpow($x_org, @params);
2606 } else {
2607 # else just round the already computed result
2608 0 delete $x->{accuracy}; delete $x->{precision};
2609 # shortcut to not run through _find_round_parameters again
2610 0 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 0 0 0
2611 0 $x -> bround($params[0], $params[2]); # then round accordingly
2612 } else {
2613 0 $x -> bfround($params[1], $params[2]); # then round accordingly
2614 }
2615 }
2616 1 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 1 0 1
2617 # clear a/p after round, since user did not request it
2618 1 delete $x->{accuracy}; delete $x->{precision};
2619 }
2620  
2621 1 $x;
2622 }
2623  
2624 0 ▶sub bilog2 {
Subroutine bilog2: not called CC=8 SCAR=42.8
Pod documented
2625 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2626  
2627 # Don't modify constant (read-only) objects.
2628  
2629 0 ▶ return $x if $x -> modify('bilog2');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bilog2") 0 0 0
2630  
2631 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
2632 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
2633 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
2634  
2635 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
2636 0 ▶ return $x -> _upg() -> bilog2(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
2637 0 return $x -> bnan(@r);
2638 }
2639  
2640 0 $x->{_n} = $LIB -> _div($x->{_n}, $x->{_d});
2641 0 $x->{_n} = $LIB -> _ilog2($x->{_n});
2642 0 $x->{_d} = $LIB -> _one();
2643 0 $x -> bnorm() -> round(@r);
2644 0 $x -> _dng();
2645 0 return $x;
2646 }
2647  
2648 0 ▶sub bilog10 {
Subroutine bilog10: not called CC=8 SCAR=42.8
Pod documented
2649 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2650  
2651 # Don't modify constant (read-only) objects.
2652  
2653 0 ▶ return $x if $x -> modify('bilog10');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bilog10") 0 0 0
2654  
2655 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
2656 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
2657 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
2658  
2659 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
2660 0 ▶ return $x -> _upg() -> bilog10(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
2661 0 return $x -> bnan(@r);
2662 }
2663  
2664 0 $x->{_n} = $LIB -> _div($x->{_n}, $x->{_d});
2665 0 $x->{_n} = $LIB -> _ilog10($x->{_n});
2666 0 $x->{_d} = $LIB -> _one();
2667 0 $x -> bnorm() -> round(@r);
2668 0 $x -> _dng();
2669 0 return $x;
2670 }
2671  
2672 0 ▶sub bclog2 {
Subroutine bclog2: not called CC=8 SCAR=42.8
Pod documented
2673 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2674  
2675 # Don't modify constant (read-only) objects.
2676  
2677 0 ▶ return $x if $x -> modify('bclog2');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bclog2") 0 0 0
2678  
2679 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
2680 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
2681 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
2682  
2683 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
2684 0 ▶ return $x -> _upg() -> bclog2(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
2685 0 return $x -> bnan(@r);
2686 }
2687  
2688 0 $x->{_n} = $LIB -> _div($x->{_n}, $x->{_d});
2689 0 $x->{_n} = $LIB -> _clog2($x->{_n});
2690 0 $x->{_d} = $LIB -> _one();
2691 0 $x -> bnorm() -> round(@r);
2692 0 $x -> _dng();
2693 0 return $x;
2694 }
2695  
2696 0 ▶sub bclog10 {
Subroutine bclog10: not called CC=8 SCAR=42.8
Pod documented
2697 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2698  
2699 # Don't modify constant (read-only) objects.
2700  
2701 0 ▶ return $x if $x -> modify('bclog10');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bclog10") 0 0 0
2702  
2703 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
2704 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
2705 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
2706  
2707 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
2708 0 ▶ return $x -> _upg() -> bclog10(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
2709 0 return $x -> bnan(@r);
2710 }
2711  
2712 0 $x->{_n} = $LIB -> _div($x->{_n}, $x->{_d});
2713 0 $x->{_n} = $LIB -> _clog10($x->{_n});
2714 0 $x->{_d} = $LIB -> _one();
2715 0 $x -> bnorm() -> round(@r);
2716 0 $x -> _dng();
2717 0 return $x;
2718 }
2719  
2720 0 ▶sub bnok {
Subroutine bnok: not called CC=11 SCAR=48.8
Pod documented
2721 # set up parameters
2722 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
2723 ? (ref($_[0]), @_)
2724 : objectify(2, @_);
2725  
2726 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
2727  
2728 # Don't modify constant (read-only) objects.
2729  
2730 0 ▶ return $x if $x -> modify('bnok');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bnok") 0 0 0
2731  
2732 0 ▶ return $x -> bnan() 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
2733 0 ▶ return $x -> bnan() if (($x -> is_finite() && !$x -> is_int()) ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int 0 0 0
Condition0%0/9
$x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int
l!l&&r!l&&!r
000
$x->is_finite and not $x->is_int
!ll&&!rl&&r
000
$y->is_finite and not $y->is_int
!ll&&!rl&&r
000
MC/DC0%0/4
$x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int
$x->is_finite not $x->is_int $y->is_finite not $y->is_int
Truth table0%0/7
A and not(B) or C and not(D)
A= $x->is_finiteB= not $x->is_intC= $y->is_finiteD= not $y->is_int
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
2734 ($y -> is_finite() && !$y -> is_int()));
2735  
2736 0 my $xint = Math::BigInt -> new($x -> bstr());
2737 0 my $yint = Math::BigInt -> new($y -> bstr());
2738 0 $xint -> bnok($yint);
2739 0 my $xrat = Math::BigRat -> new($xint);
2740  
2741 0 $x -> {sign} = $xrat -> {sign};
2742 0 $x -> {_n} = $xrat -> {_n};
2743 0 $x -> {_d} = $xrat -> {_d};
2744  
2745 0 return $x;
2746 }
2747  
2748 0 ▶sub bperm {
Subroutine bperm: not called CC=11 SCAR=48.8
Pod documented
2749 # set up parameters
2750 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
2751 ? (ref($_[0]), @_)
2752 : objectify(2, @_);
2753  
2754 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
2755  
2756 # Don't modify constant (read-only) objects.
2757  
2758 0 ▶ return $x if $x -> modify('bperm');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bperm") 0 0 0
2759  
2760 0 ▶ return $x -> bnan() 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
2761 0 ▶ return $x -> bnan() if (($x -> is_finite() && !$x -> is_int()) ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int 0 0 0
Condition0%0/9
$x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int
l!l&&r!l&&!r
000
$x->is_finite and not $x->is_int
!ll&&!rl&&r
000
$y->is_finite and not $y->is_int
!ll&&!rl&&r
000
MC/DC0%0/4
$x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int
$x->is_finite not $x->is_int $y->is_finite not $y->is_int
Truth table0%0/7
A and not(B) or C and not(D)
A= $x->is_finiteB= not $x->is_intC= $y->is_finiteD= not $y->is_int
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
2762 ($y -> is_finite() && !$y -> is_int()));
2763  
2764 0 my $xint = Math::BigInt -> new($x -> bstr());
2765 0 my $yint = Math::BigInt -> new($y -> bstr());
2766 0 $xint -> bperm($yint);
2767 0 my $xrat = Math::BigRat -> new($xint);
2768  
2769 0 $x -> {sign} = $xrat -> {sign};
2770 0 $x -> {_n} = $xrat -> {_n};
2771 0 $x -> {_d} = $xrat -> {_d};
2772  
2773 0 return $x;
2774 }
2775  
2776 22 ▶sub bfac {
Subroutine bfac: called CC=10 SCAR=23.4
Pod documented
2777 22 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 22 0 22
2778  
2779 # Don't modify constant (read-only) objects.
2780  
2781 22 ▶ return $x if $x -> modify('bfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfac") 0 22 22
2782  
2783 22 ▶ return $x -> bnan(@r) if $x -> is_nan() || $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 4 18 22
Condition66%2/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
4018
MC/DC50%1/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table66%2/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
2784 18 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 18 18
2785 18 ▶ return $x -> bnan(@r) if $x -> is_neg() || !$x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg or not $x->is_int 5 13 18
Condition66%2/3
$x->is_neg or not $x->is_int
l!l&&r!l&&!r
5013
MC/DC50%1/2
$x->is_neg or not $x->is_int
$x->is_neg not $x->is_int
Truth table66%2/3
A or not(B)
A= $x->is_negB= not $x->is_int
ABresult
1X1
011
000
2786 13 ▶ return $x -> bone(@r) if $x -> is_zero() || $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one 7 6 13
Condition100%3/3
$x->is_zero or $x->is_one
l!l&&r!l&&!r
166
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
2787  
2788 6 $x->{_n} = $LIB->_fac($x->{_n});
2789 # since _d is 1, we don't need to reduce/norm the result
2790 6 $x -> round(@r);
2791 6 $x -> _dng();
2792 6 return $x;
2793 }
2794  
2795 0 ▶sub bdfac {
Subroutine bdfac: not called CC=10 SCAR=47.0
Pod documented
2796 # compute double factorial, modify $x in place
2797 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2798  
2799 # Don't modify constant (read-only) objects.
2800  
2801 0 ▶ return $x if $x -> modify('bdfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdfac") 0 0 0
2802  
2803 0 ▶ return $x -> bnan(@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
2804 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
2805 0 ▶ return $x -> bnan(@r) if $x <= -2 || !$x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x <= -2 or not $x->is_int 0 0 0
Condition0%0/3
$x <= -2 or not $x->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$x <= -2 or not $x->is_int
$x <= -2 not $x->is_int
Truth table0%0/3
A or not(B)
A= $x <= -2B= not $x->is_int
ABresult
1X1
011
000
2806 0 ▶ return $x -> bone(@r) if $x <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= 1 0 0 0
2807  
2808 0 ▶ croak("bdfac() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_dfac") 0 0 0
2809 unless $LIB -> can('_dfac');
2810  
2811 0 $x->{_n} = $LIB->_dfac($x->{_n});
2812 # since _d is 1, we don't need to reduce/norm the result
2813 0 $x -> round(@r);
2814 0 $x -> _dng();
2815 0 return $x;
2816 }
2817  
2818 0 ▶sub btfac {
Subroutine btfac: not called CC=9 SCAR=45.0
Pod documented
2819 # compute triple factorial, modify $x in place
2820 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2821  
2822 # Don't modify constant (read-only) objects.
2823  
2824 0 ▶ return $x if $x -> modify('btfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btfac") 0 0 0
2825  
2826 0 ▶ return $x -> bnan(@r) 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
2827 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
2828  
2829 0 my $k = $class -> new("3");
2830 0 ▶ return $x -> bnan(@r) if $x <= -$k;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= -$k 0 0 0
2831  
2832 0 my $one = $class -> bone();
2833 0 ▶ return $x -> bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 0 0 0
2834  
2835 0 my $f = $x -> copy();
2836 0 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 0 0 0
2837 0 $x -> bmul($f);
2838 }
2839 0 $x -> round(@r);
2840 0 $x -> _dng();
2841 0 return $x;
2842 }
2843  
2844 0 ▶sub bmfac {
Subroutine bmfac: not called CC=15 SCAR=54.8
Pod documented
2845 # compute multi-factorial
2846  
2847 0 ▶ my ($class, $x, $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
2848 ? (ref($_[0]), @_) : objectify(2, @_);
2849  
2850 # Don't modify constant (read-only) objects.
2851  
2852 0 ▶ return $x if $x -> modify('bmfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmfac") 0 0 0
2853  
2854 0 ▶ return $x -> bnan(@r) if $x -> is_nan() || $x -> is_inf("-") ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") or not $k->is_pos 0 0 0
Condition0%0/6
$x->is_nan or $x->is_inf("-") or not $k->is_pos
l!l&&r!l&&!r
000
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->is_nan or $x->is_inf("-") or not $k->is_pos
$x->is_nan $x->is_inf("-") not $k->is_pos
Truth table0%0/4
A or B or not(C)
A= $x->is_nanB= $x->is_inf("-")C= not $k->is_pos
ABCresult
1XX1
01X1
0011
0000
2855 !$k -> is_pos();
2856 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
2857 0 ▶ return $x -> bround(@r) if $k -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $k->is_inf("+") 0 0 0
2858 0 ▶ return $x -> bnan(@r) if !$x -> is_int() || !$k -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int and $k->is_int 0 0 0
Condition0%0/3
$x->is_int and $k->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
$x->is_int and $k->is_int
$x->is_int $k->is_int
Truth table0%0/3
A and B
A= $x->is_intB= $k->is_int
ABresult
0X0
100
111
2859 0 ▶ return $x -> bnan(@r) if $k < 1 || $x <= -$k;
Branch: 1 branch
BranchTrueFalseTotal
if $k < 1 or $x <= -$k 0 0 0
Condition0%0/3
$k < 1 or $x <= -$k
l!l&&r!l&&!r
000
MC/DC0%0/2
$k < 1 or $x <= -$k
$k < 1 $x <= -$k
Truth table0%0/3
A or B
A= $k < 1B= $x <= -$k
ABresult
1X1
011
000
2860  
2861 0 my $one = $class -> bone();
2862 0 ▶ return $x -> bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 0 0 0
2863  
2864 0 my $f = $x -> copy();
2865 0 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 0 0 0
2866 0 $x -> bmul($f);
2867 }
2868 0 $x -> round(@r);
2869 0 $x -> _dng();
2870 0 return $x;
2871 }
2872  
2873 0 ▶sub bfib {
Subroutine bfib: not called CC=20 SCAR=60.4
Pod documented
2874 # compute Fibonacci number(s)
2875 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2876  
2877 0 ▶ croak("bfib() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_fib") 0 0 0
2878 unless $LIB -> can('_fib');
2879  
2880 # Don't modify constant (read-only) objects.
2881  
2882 0 ▶ return $x if $x -> modify('bfib');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfib") 0 0 0
2883  
2884 # List context.
2885  
2886 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
2887 0 ▶ croak("bfib() can't return an infinitely long list of numbers")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
2888 if $x -> is_inf();
2889  
2890 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
2891  
2892 # The following places a limit on how large $x can be. Should this
2893 # limit be removed? XXX
2894  
2895 0 my $n = $x -> numify();
2896  
2897 0 my @y;
2898 {
2899 0 $y[0] = $x -> copy() -> babs();
2900 0 $y[0]{_n} = $LIB -> _zero();
2901 0 $y[0]{_d} = $LIB -> _one();
2902 0 ▶ last if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
2903  
2904 0 $y[1] = $y[0] -> copy();
2905 0 $y[1]{_n} = $LIB -> _one();
2906 0 $y[1]{_d} = $LIB -> _one();
2907 0 ▶ last if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
2908  
2909 0 ▶ for (my $i = 2 ; $i <= abs($n) ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= abs $n) 0 0 0
2910 0 $y[$i] = $y[$i - 1] -> copy();
2911 $y[$i]{_n} = $LIB -> _add($LIB -> _copy($y[$i - 1]{_n}),
2912 0 $y[$i - 2]{_n});
2913 }
2914  
2915 # If negative, insert sign as appropriate.
2916  
2917 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
2918 0 ▶ for (my $i = 2 ; $i <= $#y ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#y) 0 0 0
2919 0 $y[$i]{sign} = '-';
2920 }
2921 }
2922  
2923 # The last element in the array is the invocand.
2924  
2925 0 $x->{sign} = $y[-1]{sign};
2926 0 $x->{_n} = $y[-1]{_n};
2927 0 $x->{_d} = $y[-1]{_d};
2928 0 $y[-1] = $x;
2929 }
2930  
2931 0 for (@y) {
2932 0 $_ -> bnorm();
2933 0 $_ -> round(@r);
2934 }
2935  
2936 0 return @y;
2937 }
2938  
2939 # Scalar context.
2940  
2941 else {
2942 0 ▶ return $x if $x -> is_inf('+');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
2943 0 ▶ return $x -> bnan() if $x -> is_nan() || $x -> is_inf('-') ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") or not $x->is_int 0 0 0
Condition0%0/6
$x->is_nan or $x->is_inf("-") or not $x->is_int
l!l&&r!l&&!r
000
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->is_nan or $x->is_inf("-") or not $x->is_int
$x->is_nan $x->is_inf("-") not $x->is_int
Truth table0%0/4
A or B or not(C)
A= $x->is_nanB= $x->is_inf("-")C= not $x->is_int
ABCresult
1XX1
01X1
0011
0000
2944 !$x -> is_int();
2945  
2946 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
2947 0 $x->{_n} = $LIB -> _fib($x->{_n});
2948 0 $x->{_d} = $LIB -> _one();
2949 0 $x -> bnorm();
2950 0 return $x -> round(@r);
2951 }
2952 }
2953  
2954 0 ▶sub blucas {
Subroutine blucas: not called CC=19 SCAR=59.4
Pod documented
2955 # compute Lucas number(s)
2956 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2957  
2958 0 ▶ croak("blucas() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_lucas") 0 0 0
2959 unless $LIB -> can('_lucas');
2960  
2961 # Don't modify constant (read-only) objects.
2962  
2963 0 ▶ return $x if $x -> modify('blucas');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blucas") 0 0 0
2964  
2965 # List context.
2966  
2967 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
2968 0 ▶ croak("blucas() can't return an infinitely long list of numbers")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
2969 if $x -> is_inf();
2970  
2971 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
2972  
2973 # The following places a limit on how large $x can be, at least on 32
2974 # bit systems. Should this limit be removed? XXX
2975  
2976 0 my $n = $x -> numify();
2977  
2978 0 my @y;
2979 {
2980 0 $y[0] = $x -> copy() -> babs();
2981 0 $y[0]{_n} = $LIB -> _two();
2982 0 ▶ last if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
2983  
2984 0 $y[1] = $y[0] -> copy();
2985 0 $y[1]{_n} = $LIB -> _one();
2986 0 ▶ last if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
2987  
2988 0 ▶ for (my $i = 2 ; $i <= abs($n) ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= abs $n) 0 0 0
2989 0 $y[$i] = $y[$i - 1] -> copy();
2990 $y[$i]{_n} = $LIB -> _add($LIB -> _copy($y[$i - 1]{_n}),
2991 0 $y[$i - 2]{_n});
2992 }
2993  
2994 # If negative, insert sign as appropriate.
2995  
2996 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
2997 0 ▶ for (my $i = 2 ; $i <= $#y ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#y) 0 0 0
2998 0 $y[$i]{sign} = '-';
2999 }
3000 }
3001  
3002 # The last element in the array is the invocand.
3003  
3004 0 $x->{_n} = $y[-1]{_n};
3005 0 $x->{sign} = $y[-1]{sign};
3006 0 $y[-1] = $x;
3007 }
3008  
3009 0 @y = map { $_ -> round(@r) } @y;
3010 0 return @y;
3011 }
3012  
3013 # Scalar context.
3014  
3015 else {
3016 0 ▶ return $x if $x -> is_inf('+');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
3017 0 ▶ return $x -> bnan() if $x -> is_nan() || $x -> is_inf('-') ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") or not $x->is_int 0 0 0
Condition0%0/6
$x->is_nan or $x->is_inf("-") or not $x->is_int
l!l&&r!l&&!r
000
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->is_nan or $x->is_inf("-") or not $x->is_int
$x->is_nan $x->is_inf("-") not $x->is_int
Truth table0%0/4
A or B or not(C)
A= $x->is_nanB= $x->is_inf("-")C= not $x->is_int
ABCresult
1XX1
01X1
0011
0000
3018 !$x -> is_int();
3019  
3020 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
3021 0 $x->{_n} = $LIB -> _lucas($x->{_n});
3022 0 return $x -> round(@r);
3023 }
3024 }
3025  
3026 0 ▶sub blsft {
Subroutine blsft: not called CC=13 SCAR=52.0
Pod documented
3027 0 my ($class, $x, $y, $b, @r);
3028  
3029 # Objectify the base only when it is defined, since an undefined base, as
3030 # in $x->blsft(3) or $x->blog(3, undef) means use the default base 2.
3031  
3032 0 ▶ if (!ref($_[0]) && $_[0] =~ /^[A-Za-z]|::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/) { } 0 0 0
Condition0%0/3
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
not ref $_[0] $_[0] =~ /^[A-Za-z]|::/
Truth table0%0/3
not(A) and B
A= not ref $_[0]B= $_[0] =~ /^[A-Za-z]|::/
ABresult
0X0
100
111
3033 # E.g., Math::BigInt->blog(256, 5, 2)
3034 0 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 0 0
3035 defined $_[3] ? objectify(3, @_) : objectify(2, @_);
3036 } else {
3037 # E.g., Math::BigInt::blog(256, 5, 2) or $x->blog(5, 2)
3038 0 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
3039 defined $_[2] ? objectify(3, @_) : objectify(2, @_);
3040 }
3041  
3042 # Don't modify constant (read-only) objects.
3043  
3044 0 ▶ return $x if $x -> modify('blsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blsft") 0 0 0
3045  
3046 0 ▶ $b = 2 unless defined($b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 0 0
3047 0 ▶ $b = $class -> new($b) unless ref($b) && $b -> isa($class);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b and $b->isa($class) 0 0 0
Condition0%0/3
ref $b and $b->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $b and $b->isa($class)
ref $b $b->isa($class)
Truth table0%0/3
A and B
A= ref $bB= $b->isa($class)
ABresult
0X0
100
111
3048  
3049 0 ▶ return $x -> bnan() if $x -> is_nan() || $y -> is_nan() || $b -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan or $b->is_nan 0 0 0
Condition0%0/6
$x->is_nan or $y->is_nan or $b->is_nan
l!l&&r!l&&!r
000
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
000
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 table0%0/4
A or B or C
A= $x->is_nanB= $y->is_nanC= $b->is_nan
ABCresult
1XX1
01X1
0011
0000
3050  
3051 # shift by a negative amount?
3052 0 ▶ return $x -> brsft($y -> copy() -> babs(), $b) if $y -> {sign} =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} =~ /^-/ 0 0 0
3053  
3054 0 $x -> bmul($b -> bpow($y));
3055 }
3056  
3057 0 ▶sub brsft {
Subroutine brsft: not called CC=13 SCAR=52.0
Pod documented
3058 0 my ($class, $x, $y, $b, @r);
3059  
3060 # Objectify the base only when it is defined, since an undefined base, as
3061 # in $x->blsft(3) or $x->blog(3, undef) means use the default base 2.
3062  
3063 0 ▶ if (!ref($_[0]) && $_[0] =~ /^[A-Za-z]|::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/) { } 0 0 0
Condition0%0/3
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
not ref $_[0] $_[0] =~ /^[A-Za-z]|::/
Truth table0%0/3
not(A) and B
A= not ref $_[0]B= $_[0] =~ /^[A-Za-z]|::/
ABresult
0X0
100
111
3064 # E.g., Math::BigInt->blog(256, 5, 2)
3065 0 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[3] ? : 0 0 0
3066 defined $_[3] ? objectify(3, @_) : objectify(2, @_);
3067 } else {
3068 # E.g., Math::BigInt::blog(256, 5, 2) or $x->blog(5, 2)
3069 0 ▶ ($class, $x, $y, $b, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
3070 defined $_[2] ? objectify(3, @_) : objectify(2, @_);
3071 }
3072  
3073 # Don't modify constant (read-only) objects.
3074  
3075 0 ▶ return $x if $x -> modify('brsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("brsft") 0 0 0
3076  
3077 0 ▶ $b = 2 unless defined($b);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 0 0
3078 0 ▶ $b = $class -> new($b) unless ref($b) && $b -> isa($class);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $b and $b->isa($class) 0 0 0
Condition0%0/3
ref $b and $b->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $b and $b->isa($class)
ref $b $b->isa($class)
Truth table0%0/3
A and B
A= ref $bB= $b->isa($class)
ABresult
0X0
100
111
3079  
3080 0 ▶ return $x -> bnan() if $x -> is_nan() || $y -> is_nan() || $b -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan or $b->is_nan 0 0 0
Condition0%0/6
$x->is_nan or $y->is_nan or $b->is_nan
l!l&&r!l&&!r
000
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
000
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 table0%0/4
A or B or C
A= $x->is_nanB= $y->is_nanC= $b->is_nan
ABCresult
1XX1
01X1
0011
0000
3081  
3082 # shift by a negative amount?
3083 0 ▶ return $x -> blsft($y -> copy() -> babs(), $b) if $y -> {sign} =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} =~ /^-/ 0 0 0
3084  
3085 # the following call to bfdiv() will return either quotient (scalar context)
3086 # or quotient and remainder (list context).
3087 0 $x -> bfdiv($b -> bpow($y));
3088 }
3089  
3090 ###############################################################################
3091 # Bitwise methods
3092 ###############################################################################
3093  
3094 # Bitwise left shift.
3095  
3096 0 ▶sub bblsft {
Subroutine bblsft: not called CC=6 SCAR=37.4
Pod documented
3097 # We don't call objectify(), because the bitwise methods should not
3098 # upgrade, even when upgrading is enabled.
3099  
3100 0 ▶ my ($class, $x, $y, @r) = ref($_[0]) ? (ref($_[0]), @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3101  
3102 # Don't modify constant (read-only) objects.
3103  
3104 0 ▶ return $x if ref($x) && $x -> modify('bblsft');
Branch: 1 branch
BranchTrueFalseTotal
if ref $x and $x->modify("bblsft") 0 0 0
Condition0%0/3
ref $x and $x->modify("bblsft")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $x and $x->modify("bblsft")
ref $x $x->modify("bblsft")
Truth table0%0/3
A and B
A= ref $xB= $x->modify("bblsft")
ABresult
0X0
100
111
3105  
3106 # Let Math::BigInt do the job.
3107  
3108 0 my $xint = Math::BigInt -> bblsft($x, $y, @r);
3109  
3110 # Temporarily disable downgrading.
3111  
3112 0 my $dng = $class -> downgrade();
3113 0 $class -> downgrade(undef);
3114  
3115 # Convert to our class without downgrading.
3116  
3117 0 my $xrat = $class -> new($xint);
3118  
3119 # Reset downgrading.
3120  
3121 0 $class -> downgrade($dng);
3122  
3123 # If we are called as a class method, the first operand might not be an
3124 # object of this class, so check.
3125  
3126 0 ▶ if (defined(blessed($x)) && $x -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($x) and $x->isa(__PACKAGE__)) { } 0 0 0
Condition0%0/3
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
defined builtin::blessed($x) $x->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= defined builtin::blessed($x)B= $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3127 0 $x -> {sign} = $xrat -> {sign};
3128 0 $x -> {_n} = $xrat -> {_n};
3129 0 $x -> {_d} = $xrat -> {_d};
3130 } else {
3131 0 $x = $xrat;
3132 }
3133  
3134 # Now we might downgrade.
3135  
3136 0 $x -> round(@r);
3137 0 $x -> _dng();
3138 0 return $x;
3139 }
3140  
3141 # Bitwise right shift.
3142  
3143 0 ▶sub bbrsft {
Subroutine bbrsft: not called CC=6 SCAR=37.4
Pod documented
3144 # We don't call objectify(), because the bitwise methods should not
3145 # upgrade/downgrade, even when upgrading/downgrading is enabled.
3146  
3147 0 ▶ my ($class, $x, $y, @r) = ref($_[0]) ? (ref($_[0]), @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3148  
3149 # Don't modify constant (read-only) objects.
3150  
3151 0 ▶ return $x if ref($x) && $x -> modify('bbrsft');
Branch: 1 branch
BranchTrueFalseTotal
if ref $x and $x->modify("bbrsft") 0 0 0
Condition0%0/3
ref $x and $x->modify("bbrsft")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $x and $x->modify("bbrsft")
ref $x $x->modify("bbrsft")
Truth table0%0/3
A and B
A= ref $xB= $x->modify("bbrsft")
ABresult
0X0
100
111
3152  
3153 # Let Math::BigInt do the job.
3154  
3155 0 my $xint = Math::BigInt -> bbrsft($x, $y, @r);
3156  
3157 # Temporarily disable downgrading.
3158  
3159 0 my $dng = $class -> downgrade();
3160 0 $class -> downgrade(undef);
3161  
3162 # Convert to our class without downgrading.
3163  
3164 0 my $xrat = $class -> new($xint);
3165  
3166 # Reset downgrading.
3167  
3168 0 $class -> downgrade($dng);
3169  
3170 # If we are called as a class method, the first operand might not be an
3171 # object of this class, so check.
3172  
3173 0 ▶ if (defined(blessed($x)) && $x -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($x) and $x->isa(__PACKAGE__)) { } 0 0 0
Condition0%0/3
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
defined builtin::blessed($x) $x->isa(__PACKAGE__)
Truth table0%0/3
A and B
A= defined builtin::blessed($x)B= $x->isa(__PACKAGE__)
ABresult
0X0
100
111
3174 0 $x -> {sign} = $xrat -> {sign};
3175 0 $x -> {_n} = $xrat -> {_n};
3176 0 $x -> {_d} = $xrat -> {_d};
3177 } else {
3178 0 $x = $xrat;
3179 }
3180  
3181 # Now we might downgrade.
3182  
3183 0 $x -> round(@r);
3184 0 $x -> _dng();
3185 0 return $x;
3186 }
3187  
3188 289 ▶sub band {
Subroutine band: called CC=6 SCAR=18.7
Pod documented
3189 289 my $x = shift;
3190 289 my $xref = ref($x);
3191 289 ▶ my $class = $xref || $x;
Condition33%1/3
$xref || $x
l!l&&r!l&&!r
28900
MC/DC0%0/2
$xref || $x
$xref $x
Truth table33%1/3
A || B
A= $xrefB= $x
ABresult
1X1
011
000
3192  
3193 # Don't modify constant (read-only) objects.
3194  
3195 289 ▶ return $x if $x -> modify('band');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("band") 0 289 289
3196  
3197 289 ▶ croak 'band() is an instance method, not a class method' unless $xref;
Branch: 1 branch
BranchTrueFalseTotal
unless $xref 0 289 289
3198 289 ▶ croak 'Not enough arguments for band()' if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 289 289
3199  
3200 289 my $y = shift;
3201 289 ▶ $y = $class -> new($y) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 289 289
3202  
3203 289 my @r = @_;
3204  
3205 289 my $xtmp = $x -> as_int() -> band($y -> as_int()) -> as_rat();
3206 289 $x -> {sign} = $xtmp -> {sign};
3207 289 $x -> {_n} = $xtmp -> {_n};
3208 289 $x -> {_d} = $xtmp -> {_d};
3209  
3210 289 return $x -> round(@r);
3211 }
3212  
3213 289 ▶sub bior {
Subroutine bior: called CC=6 SCAR=18.7
Pod documented
3214 289 my $x = shift;
3215 289 my $xref = ref($x);
3216 289 ▶ my $class = $xref || $x;
Condition33%1/3
$xref || $x
l!l&&r!l&&!r
28900
MC/DC0%0/2
$xref || $x
$xref $x
Truth table33%1/3
A || B
A= $xrefB= $x
ABresult
1X1
011
000
3217  
3218 # Don't modify constant (read-only) objects.
3219  
3220 289 ▶ return $x if $x -> modify('bior');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bior") 0 289 289
3221  
3222 289 ▶ croak 'bior() is an instance method, not a class method' unless $xref;
Branch: 1 branch
BranchTrueFalseTotal
unless $xref 0 289 289
3223 289 ▶ croak 'Not enough arguments for bior()' if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 289 289
3224  
3225 289 my $y = shift;
3226 289 ▶ $y = $class -> new($y) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 289 289
3227  
3228 289 my @r = @_;
3229  
3230 289 my $xtmp = $x -> as_int() -> bior($y -> as_int()) -> as_rat();
3231 289 $x -> {sign} = $xtmp -> {sign};
3232 289 $x -> {_n} = $xtmp -> {_n};
3233 289 $x -> {_d} = $xtmp -> {_d};
3234  
3235 289 return $x -> round(@r);
3236 }
3237  
3238 289 ▶sub bxor {
Subroutine bxor: called CC=6 SCAR=18.7
Pod documented
3239 289 my $x = shift;
3240 289 my $xref = ref($x);
3241 289 ▶ my $class = $xref || $x;
Condition33%1/3
$xref || $x
l!l&&r!l&&!r
28900
MC/DC0%0/2
$xref || $x
$xref $x
Truth table33%1/3
A || B
A= $xrefB= $x
ABresult
1X1
011
000
3242  
3243 # Don't modify constant (read-only) objects.
3244  
3245 289 ▶ return $x if $x -> modify('bxor');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bxor") 0 289 289
3246  
3247 289 ▶ croak 'bxor() is an instance method, not a class method' unless $xref;
Branch: 1 branch
BranchTrueFalseTotal
unless $xref 0 289 289
3248 289 ▶ croak 'Not enough arguments for bxor()' if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 289 289
3249  
3250 289 my $y = shift;
3251 289 ▶ $y = $class -> new($y) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 289 289
3252  
3253 289 my @r = @_;
3254  
3255 289 my $xtmp = $x -> as_int() -> bxor($y -> as_int()) -> as_rat();
3256 289 $x -> {sign} = $xtmp -> {sign};
3257 289 $x -> {_n} = $xtmp -> {_n};
3258 289 $x -> {_d} = $xtmp -> {_d};
3259  
3260 289 return $x -> round(@r);
3261 }
3262  
3263 0 ▶sub bnot {
Subroutine bnot: not called CC=4 SCAR=30.0
Pod documented
3264 0 my $x = shift;
3265 0 my $xref = ref($x);
3266 0 ▶ my $class = $xref || $x;
Condition0%0/3
$xref || $x
l!l&&r!l&&!r
000
MC/DC0%0/2
$xref || $x
$xref $x
Truth table0%0/3
A || B
A= $xrefB= $x
ABresult
1X1
011
000
3267  
3268 # Don't modify constant (read-only) objects.
3269  
3270 0 ▶ return $x if $x -> modify('bnot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bnot") 0 0 0
3271  
3272 0 ▶ croak 'bnot() is an instance method, not a class method' unless $xref;
Branch: 1 branch
BranchTrueFalseTotal
unless $xref 0 0 0
3273  
3274 0 my @r = @_;
3275  
3276 0 my $xtmp = $x -> as_int() -> bnot() -> as_rat();
3277 0 $x -> {sign} = $xtmp -> {sign};
3278 0 $x -> {_n} = $xtmp -> {_n};
3279 0 $x -> {_d} = $xtmp -> {_d};
3280  
3281 0 return $x -> round(@r);
3282 }
3283  
3284 ##############################################################################
3285 # round
3286  
3287 7867 ▶sub round {
Subroutine round: called CC=5 SCAR=16.1
Pod documented
3288 7867 my $x = shift;
3289  
3290 # Don't modify constant (read-only) objects.
3291  
3292 7867 ▶ return $x if $x -> modify('round');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("round") 0 7867 7867
3293  
3294 7867 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 6480 1387 7867
Condition100%6/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
58076731387
$x->is_int or $x->is_inf
l!l&&r!l&&!r
48629452060
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
3295 $x -> is_inf() ||
3296 $x -> is_nan());
3297 7867 $x;
3298 }
3299  
3300 7 ▶sub bround {
Subroutine bround: called CC=5 SCAR=16.6
Pod documented
3301 7 my $x = shift;
3302  
3303 # Don't modify constant (read-only) objects.
3304  
3305 7 ▶ return $x if $x -> modify('bround');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bround") 0 7 7
3306  
3307 7 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 7 0 7
Condition83%5/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
610
$x->is_int or $x->is_inf
l!l&&r!l&&!r
511
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
3308 $x -> is_inf() ||
3309 $x -> is_nan());
3310 7 $x;
3311 }
3312  
3313 8 ▶sub bfround {
Subroutine bfround: called CC=5 SCAR=16.6
Pod documented
3314 8 my $x = shift;
3315  
3316 # Don't modify constant (read-only) objects.
3317  
3318 8 ▶ return $x if $x -> modify('bfround');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfround") 0 8 8
3319  
3320 8 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 8 0 8
Condition83%5/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
710
$x->is_int or $x->is_inf
l!l&&r!l&&!r
611
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
3321 $x -> is_inf() ||
3322 $x -> is_nan());
3323 8 $x;
3324 }
3325  
3326 188 ▶sub bfloor {
Subroutine bfloor: called CC=7 SCAR=19.5
Pod documented
3327 188 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 188 0 188
3328  
3329 # Don't modify constant (read-only) objects.
3330  
3331 188 ▶ return $x if $x -> modify('bfloor');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfloor") 0 188 188
3332  
3333 188 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 183 188
3334  
3335 183 ▶ if (!$x -> is_finite() || # NaN or inf or
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->is_finite or $LIB->_is_one($x->{"_d"})) 86 97 183
Condition100%3/3
not $x->is_finite or $LIB->_is_one($x->{"_d"})
l!l&&r!l&&!r
97797
MC/DC100%2/2
not $x->is_finite or $LIB->_is_one($x->{"_d"})
not $x->is_finite $LIB->_is_one($x->{"_d"})
Truth table100%3/3
not(A) or B
A= not $x->is_finiteB= $LIB->_is_one($x->{"_d"})
ABresult
1X1
011
000
3336 $LIB->_is_one($x->{_d})) # integer
3337 {
3338 86 $x -> round(@r);
3339 86 $x -> _dng();
3340 86 return $x;
3341 }
3342  
3343 97 $x->{_n} = $LIB->_div($x->{_n}, $x->{_d}); # 22/7 => 3/1 w/ truncate
3344 97 $x->{_d} = $LIB->_one(); # d => 1
3345 97 ▶ $x->{_n} = $LIB->_inc($x->{_n}) if $x->{sign} eq '-'; # -22/7 => -4/1
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 44 53 97
3346  
3347 97 $x -> round(@r);
3348 97 $x -> _dng();
3349 97 return $x;
3350 }
3351  
3352 84 ▶sub bceil {
Subroutine bceil: called CC=9 SCAR=22.0
Pod documented
3353 84 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 84 0 84
3354  
3355 # Don't modify constant (read-only) objects.
3356  
3357 84 ▶ return $x if $x -> modify('bceil');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bceil") 0 84 84
3358  
3359 84 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 79 84
3360  
3361 79 ▶ if (!$x -> is_finite() || # NaN or inf or
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->is_finite or $LIB->_is_one($x->{"_d"})) 29 50 79
Condition100%3/3
not $x->is_finite or $LIB->_is_one($x->{"_d"})
l!l&&r!l&&!r
92050
MC/DC100%2/2
not $x->is_finite or $LIB->_is_one($x->{"_d"})
not $x->is_finite $LIB->_is_one($x->{"_d"})
Truth table100%3/3
not(A) or B
A= not $x->is_finiteB= $LIB->_is_one($x->{"_d"})
ABresult
1X1
011
000
3362 $LIB->_is_one($x->{_d})) # integer
3363 {
3364 29 $x -> round(@r);
3365 29 $x -> _dng();
3366 29 return $x;
3367 }
3368  
3369 50 $x->{_n} = $LIB->_div($x->{_n}, $x->{_d}); # 22/7 => 3/1 w/ truncate
3370 50 $x->{_d} = $LIB->_one(); # d => 1
3371 50 ▶ $x->{_n} = $LIB->_inc($x->{_n}) if $x->{sign} eq '+'; # +22/7 => 4/1
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+" 26 24 50
3372 50 ▶ $x->{sign} = '+' if $x->{sign} eq '-' && $LIB->_is_zero($x->{_n}); # -0 => 0
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" and $LIB->_is_zero($x->{"_n"}) 8 42 50
Condition100%3/3
$x->{"sign"} eq "-" and $LIB->_is_zero($x->{"_n"})
!ll&&!rl&&r
26168
MC/DC100%2/2
$x->{"sign"} eq "-" and $LIB->_is_zero($x->{"_n"})
$x->{"sign"} eq "-" $LIB->_is_zero($x->{"_n"})
Truth table100%3/3
A and B
A= $x->{"sign"} eq "-"B= $LIB->_is_zero($x->{"_n"})
ABresult
0X0
100
111
3373  
3374 50 $x -> round(@r);
3375 50 $x -> _dng();
3376 50 return $x;
3377 }
3378  
3379 2107 ▶sub bint {
Subroutine bint: called CC=8 SCAR=20.9
Pod documented
3380 2107 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2107 0 2107
3381  
3382 # Don't modify constant (read-only) objects.
3383  
3384 2107 ▶ return $x if $x -> modify('bint');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bint") 0 2107 2107
3385  
3386 2107 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 65 2042 2107
3387  
3388 2042 ▶ if (!$x -> is_finite() || # NaN or inf or
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->is_finite or $LIB->_is_one($x->{"_d"})) 1215 827 2042
Condition100%3/3
not $x->is_finite or $LIB->_is_one($x->{"_d"})
l!l&&r!l&&!r
251190827
MC/DC100%2/2
not $x->is_finite or $LIB->_is_one($x->{"_d"})
not $x->is_finite $LIB->_is_one($x->{"_d"})
Truth table100%3/3
not(A) or B
A= not $x->is_finiteB= $LIB->_is_one($x->{"_d"})
ABresult
1X1
011
000
3389 $LIB->_is_one($x->{_d})) # integer
3390 {
3391 1215 $x -> round(@r);
3392 1215 $x -> _dng();
3393 1215 return $x;
3394 }
3395  
3396 827 $x->{_n} = $LIB->_div($x->{_n}, $x->{_d}); # 22/7 => 3/1 w/ truncate
3397 827 $x->{_d} = $LIB->_one(); # d => 1
3398 827 ▶ $x->{sign} = '+' if $x->{sign} eq '-' && $LIB -> _is_zero($x->{_n});
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" and $LIB->_is_zero($x->{"_n"}) 1 826 827
Condition66%2/3
$x->{"sign"} eq "-" and $LIB->_is_zero($x->{"_n"})
!ll&&!rl&&r
82601
MC/DC50%1/2
$x->{"sign"} eq "-" and $LIB->_is_zero($x->{"_n"})
$x->{"sign"} eq "-" $LIB->_is_zero($x->{"_n"})
Truth table66%2/3
A and B
A= $x->{"sign"} eq "-"B= $LIB->_is_zero($x->{"_n"})
ABresult
0X0
100
111
3399  
3400 827 $x -> round(@r);
3401 827 $x -> _dng();
3402 827 return $x;
3403 }
3404  
3405 0 ▶sub bgcd {
Subroutine bgcd: not called CC=12 SCAR=50.5
Pod documented
3406 # GCD -- Euclid's algorithm, variant C (Knuth Vol 3, pg 341 ff)
3407  
3408 # Class::method(...) -> Class->method(...)
3409 0 ▶ 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)) 0 0 0
Condition0%0/12
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
!ll&&!rl&&r
000
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
000
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
000
$_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
3410 ($_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i &&
3411 $_[0] !~ /^(inf|nan)/i)))
3412 {
3413 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
3414 # " use is as a method instead";
3415 0 unshift @_, __PACKAGE__;
3416 }
3417  
3418 0 my ($class, @args) = objectify(0, @_);
3419  
3420 # Pre-process list of operands.
3421  
3422 0 for my $arg (@args) {
3423 0 ▶ return $class -> bnan() unless $arg -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->is_finite 0 0 0
3424 }
3425  
3426 # Temporarily disable downgrading.
3427  
3428 0 my $dng = $class -> downgrade();
3429 0 $class -> downgrade(undef);
3430  
3431 0 my $x = shift @args;
3432 0 $x = $x -> copy(); # bgcd() and blcm() never modify any operands
3433  
3434 0 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
3435 0 my $y = shift @args;
3436  
3437 # greatest common divisor
3438 0 ▶ while (! $y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->is_zero) 0 0 0
3439 0 ($x, $y) = ($y -> copy(), $x -> copy() -> bmod($y));
3440 }
3441  
3442 0 ▶ last if $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 0 0 0
3443 }
3444 0 $x -> babs();
3445  
3446 # Restore downgrading.
3447  
3448 0 $class -> downgrade($dng);
3449  
3450 0 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 0 0
3451 0 return $x;
3452 }
3453  
3454 0 ▶sub blcm {
Subroutine blcm: not called CC=11 SCAR=48.8
Pod documented
3455 # Least Common Multiple
3456  
3457 # Class::method(...) -> Class->method(...)
3458 0 ▶ 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)) 0 0 0
Condition0%0/12
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
!ll&&!rl&&r
000
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
000
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
000
$_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
!ll&&!rl&&r
000
MC/DC0%0/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 table0%0/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
3459 ($_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i &&
3460 $_[0] !~ /^(inf|nan)/i)))
3461 {
3462 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
3463 # " use is as a method instead";
3464 0 unshift @_, __PACKAGE__;
3465 }
3466  
3467 0 my ($class, @args) = objectify(0, @_);
3468  
3469 # Pre-process list of operands.
3470  
3471 0 for my $arg (@args) {
3472 0 ▶ return $class -> bnan() unless $arg -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->is_finite 0 0 0
3473 }
3474  
3475 0 for my $arg (@args) {
3476 0 ▶ return $class -> bzero() if $arg -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $arg->is_zero 0 0 0
3477 }
3478  
3479 0 my $x = shift @args;
3480 0 $x = $x -> copy(); # bgcd() and blcm() never modify any operands
3481  
3482 0 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 0 0 0
3483 0 my $y = shift @args;
3484 0 my $gcd = $x -> copy() -> bgcd($y);
3485 0 $x -> bdiv($gcd) -> bmul($y);
3486 }
3487  
3488 0 $x -> babs(); # might downgrade
3489 0 return $x;
3490 }
3491  
3492 44 ▶sub digit {
Subroutine digit: called CC=4 SCAR=14.3
Pod documented
3493 44 ▶ my ($class, $x, $n) = ref($_[0]) ? (undef, $_[0], $_[1]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 44 0 44
3494  
3495 44 ▶ return $nan unless $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 44 44
3496 44 ▶ $LIB->_digit($x->{_n}, $n || 0); # digit(-123/1, 2) => digit(123, 2)
Condition100%2/2
$n || 0
l!l
368
MC/DC100%1/1
$n || 0
$n
Truth table100%2/2
A
A= $n
Aresult
11
00
3497 }
3498  
3499 20 ▶sub length {
Subroutine length: called CC=3 SCAR=11.7
Pod documented
3500 20 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 20 0 20
3501  
3502 20 ▶ return $nan unless $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 20 20
3503 20 $LIB->_len($x->{_n}); # length(-123/1) => length(123)
3504 }
3505  
3506 40 ▶sub parts {
Subroutine parts: called CC=5 SCAR=16.1
Pod documented
3507 40 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
3508  
3509 40 my $c = 'Math::BigInt';
3510  
3511 40 ▶ return ($c -> bnan(), $c -> bnan()) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 36 40
3512 36 ▶ return ($c -> binf(), $c -> binf()) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 4 32 36
3513 32 ▶ return ($c -> binf('-'), $c -> binf()) if $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("-") 4 28 32
3514  
3515 28 my $n = $c -> new($LIB->_str($x->{_n}));
3516 28 $n->{sign} = $x->{sign};
3517 28 my $d = $c -> new($LIB->_str($x->{_d}));
3518 28 ($n, $d);
3519 }
3520  
3521 0 ▶sub dparts {
Subroutine dparts: not called CC=7 SCAR=40.3
Pod documented
3522 0 my $x = shift;
3523 0 my $class = ref $x;
3524  
3525 0 ▶ croak("dparts() is an instance method") unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 0 0
3526  
3527 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
3528 0 ▶ return $class -> bnan(), $class -> bnan() if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
3529 0 return $class -> bnan();
3530 }
3531  
3532 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
3533 0 ▶ return $class -> binf($x -> sign()), $class -> bzero() if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
3534 0 return $class -> binf($x -> sign());
3535 }
3536  
3537 # 355/113 => 3 + 16/113
3538  
3539 0 my ($q, $r) = $LIB -> _div($LIB -> _copy($x -> {_n}), $x -> {_d});
3540  
3541 0 my $int = Math::BigRat -> new($x -> {sign} . $LIB -> _str($q));
3542 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
3543  
3544 my $frc = Math::BigRat -> new($x -> {sign} . $LIB -> _str($r),
3545 0 $LIB -> _str($x -> {_d}));
3546  
3547 0 return $int, $frc;
3548 }
3549  
3550 2 ▶sub fparts {
Subroutine fparts: called CC=6 SCAR=21.7
Pod documented
3551 2 my $x = shift;
3552 2 my $class = ref $x;
3553  
3554 # NaN => NaN/NaN
3555  
3556 2 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 2 2
3557 0 ▶ return $class -> bnan(), $class -> bnan() if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
3558 0 return $class -> bnan();
3559 }
3560  
3561 # ±Inf => ±Inf/1
3562  
3563 2 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 2 2
3564 0 ▶ return $class -> binf($x -> sign()), $class -> bone() if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
3565 0 return $class -> binf($x -> sign());
3566 }
3567  
3568 # -3/2 -> -3/1
3569  
3570 2 my $numer = $x -> copy();
3571 2 $numer -> {_d} = $LIB -> _one();
3572 2 ▶ return $numer unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 2 2
3573  
3574 # -3/2 -> 2/1
3575  
3576 2 my $denom = $x -> copy();
3577 2 $denom -> {sign} = "+";
3578 2 $denom -> {_n} = $denom -> {_d};
3579 2 $denom -> {_d} = $LIB -> _one();
3580  
3581 2 return $numer, $denom;
3582 }
3583  
3584 52 ▶sub numerator {
Subroutine numerator: called CC=3 SCAR=11.0
Pod documented
3585 52 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 52 0 52
3586  
3587 # NaN, inf, -inf
3588 52 ▶ return Math::BigInt -> new($x->{sign}) if !$x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 21 31 52
3589  
3590 31 my $n = Math::BigInt -> new($LIB->_str($x->{_n}));
3591 31 $n->{sign} = $x->{sign};
3592 31 $n;
3593 }
3594  
3595 48 ▶sub denominator {
Subroutine denominator: called CC=4 SCAR=13.9
Pod documented
3596 48 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 48 0 48
3597  
3598 # NaN
3599 48 ▶ return Math::BigInt -> new($x->{sign}) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 11 37 48
3600 # inf, -inf
3601 37 ▶ return Math::BigInt -> bone() if !$x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 10 27 37
3602  
3603 27 Math::BigInt -> new($LIB->_str($x->{_d}));
3604 }
3605  
3606 ###############################################################################
3607 # String conversion methods
3608 ###############################################################################
3609  
3610 7497 ▶sub bstr {
Subroutine bstr: called CC=10 SCAR=23.4
Pod documented
3611 7497 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 7497 0 7497
3612  
3613 7497 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 7497 7497
3614  
3615 # Inf and NaN
3616  
3617 7497 ▶ if (!$x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 1004 6493 7497
3618 1004 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 769 235 1004
3619 235 return 'inf'; # +inf
3620 }
3621  
3622 # Upgrade?
3623  
3624 6493 ▶ return $x -> _upg() -> bstr(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa($class) 0 6493 6493
Condition33%1/3
$class->upgrade and not $x->isa($class)
!ll&&!rl&&r
649300
MC/DC0%0/2
$class->upgrade and not $x->isa($class)
$class->upgrade not $x->isa($class)
Truth table33%1/3
A and not(B)
A= $class->upgradeB= not $x->isa($class)
ABresult
0X0
100
111
3625 if $class -> upgrade() && !$x -> isa($class);
3626  
3627 # Finite number
3628  
3629 6493 my $s = '';
3630 6493 ▶ $s = $x->{sign} if $x->{sign} ne '+'; # '+3/2' => '3/2'
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne "+" 383 6110 6493
3631  
3632 6493 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 383 6110 6493
3633 6493 $str .= $LIB->_str($x->{_n});
3634 6493 ▶ $str .= '/' . $LIB->_str($x->{_d}) unless $LIB -> _is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_one($x->{"_d"}) 2086 4407 6493
3635 6493 return $str;
3636 }
3637  
3638 26 ▶sub bsstr {
Subroutine bsstr: called CC=9 SCAR=22.5
Pod documented
3639 26 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 26 0 26
3640  
3641 26 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 26 26
3642  
3643 # Inf and NaN
3644  
3645 26 ▶ if (!$x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 12 14 26
3646 12 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 8 4 12
3647 4 return 'inf'; # +inf
3648 }
3649  
3650 # Upgrade?
3651  
3652 14 ▶ return $x -> _upg() -> bsstr(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa($class) 0 14 14
Condition33%1/3
$class->upgrade and not $x->isa($class)
!ll&&!rl&&r
1400
MC/DC0%0/2
$class->upgrade and not $x->isa($class)
$class->upgrade not $x->isa($class)
Truth table33%1/3
A and not(B)
A= $class->upgradeB= not $x->isa($class)
ABresult
0X0
100
111
3653 if $class -> upgrade() && !$x -> isa($class);
3654  
3655 # Finite number
3656  
3657 14 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 1 13 14
3658 14 $str .= $LIB->_str($x->{_n});
3659 14 ▶ $str .= '/' . $LIB->_str($x->{_d}) unless $LIB -> _is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_one($x->{"_d"}) 6 8 14
3660 14 return $str;
3661 }
3662  
3663 0 ▶sub bnstr {
Subroutine bnstr: not called CC=7 SCAR=40.3
Pod documented
3664 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3665  
3666 # Inf and NaN
3667  
3668 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
3669 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
3670 0 return 'inf'; # +inf
3671 }
3672  
3673 # Upgrade?
3674  
3675 0 ▶ $x -> _upg() -> bnstr(@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
3676 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
3677  
3678 0 return $x -> as_float(@r) -> bnstr();
3679 }
3680  
3681 0 ▶sub bestr {
Subroutine bestr: not called CC=7 SCAR=40.3
Pod documented
3682 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3683  
3684 # Inf and NaN
3685  
3686 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
3687 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
3688 0 return 'inf'; # +inf
3689 }
3690  
3691 # Upgrade?
3692  
3693 0 ▶ $x -> _upg() -> bestr(@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
3694 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
3695  
3696 0 return $x -> as_float(@r) -> bestr();
3697 }
3698  
3699 1775 ▶sub bdstr {
Subroutine bdstr: called CC=9 SCAR=32.8
Pod documented
3700 1775 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1775 0 1775
3701  
3702 # Inf and NaN
3703  
3704 1775 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 0 1775 1775
Condition66%2/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
1759160
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
3705 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
3706 0 return 'inf'; # +inf
3707 }
3708  
3709 return ($x->{sign} eq '-' ? '-' : '') . $LIB->_str($x->{_n})
3710 1775 ▶ if $x -> is_int();
Branch: 2 branches
BranchTrueFalseTotal
if $x->is_int 1775 0 1775
$x->{'sign'} eq '-' ? : 16 1759 1775
3711  
3712 # Upgrade?
3713  
3714 0 ▶ $x -> _upg() -> bdstr(@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
3715 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
3716  
3717 # Integer number
3718  
3719 0 return $x -> as_float(@r) -> bdstr();
3720 }
3721  
3722 0 ▶sub bfstr {
Subroutine bfstr: not called CC=9 SCAR=45.0
Pod documented
3723 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3724  
3725 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
3726  
3727 # Inf and NaN
3728  
3729 0 ▶ if (!$x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 0 0 0
3730 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
3731 0 return 'inf'; # +inf
3732 }
3733  
3734 # Upgrade?
3735  
3736 0 ▶ return $x -> _upg() -> bfstr(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa($class) 0 0 0
Condition0%0/3
$class->upgrade and not $x->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->upgrade and not $x->isa($class)
$class->upgrade not $x->isa($class)
Truth table0%0/3
A and not(B)
A= $class->upgradeB= not $x->isa($class)
ABresult
0X0
100
111
3737 if $class -> upgrade() && !$x -> isa($class);
3738  
3739 # Finite number
3740  
3741 0 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
3742 0 $str .= $LIB->_str($x->{_n});
3743 0 ▶ $str .= '/' . $LIB->_str($x->{_d}) unless $LIB -> _is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_one($x->{"_d"}) 0 0 0
3744 0 return $str;
3745 }
3746  
3747 0 ▶sub to_hex {
Subroutine to_hex: not called CC=6 SCAR=37.4
Pod documented
3748 0 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3749  
3750 # Inf and NaN
3751  
3752 0 ▶ if (!$x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 0 0 0
3753 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
3754 0 return 'inf'; # +inf
3755 }
3756  
3757 0 ▶ return $nan unless $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 0 0
3758  
3759 0 my $str = $LIB->_to_hex($x->{_n});
3760 0 ▶ return $x->{sign} eq "-" ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
3761 }
3762  
3763 0 ▶sub to_oct {
Subroutine to_oct: not called CC=6 SCAR=37.4
Pod documented
3764 0 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3765  
3766 # Inf and NaN
3767  
3768 0 ▶ if (!$x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 0 0 0
3769 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
3770 0 return 'inf'; # +inf
3771 }
3772  
3773 0 ▶ return $nan unless $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 0 0
3774  
3775 0 my $str = $LIB->_to_oct($x->{_n});
3776 0 ▶ return $x->{sign} eq "-" ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
3777 }
3778  
3779 0 ▶sub to_bin {
Subroutine to_bin: not called CC=6 SCAR=37.4
Pod documented
3780 0 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3781  
3782 # Inf and NaN
3783  
3784 0 ▶ if (!$x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite) 0 0 0
3785 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
3786 0 return 'inf'; # +inf
3787 }
3788  
3789 0 ▶ return $nan unless $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 0 0
3790  
3791 0 my $str = $LIB->_to_bin($x->{_n});
3792 0 ▶ return $x->{sign} eq "-" ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
3793 }
3794  
3795 0 ▶sub to_bytes {
Subroutine to_bytes: not called CC=8 SCAR=42.8
Pod documented
3796 # return a byte string
3797  
3798 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3799  
3800 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
3801  
3802 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
3803 if $x -> is_neg() || ! $x -> is_int();
3804  
3805 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
3806 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
3807  
3808 0 ▶ croak("to_bytes() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_to_bytes") 0 0 0
3809 unless $LIB -> can('_to_bytes');
3810  
3811 0 return $LIB->_to_bytes($x->{_n});
3812 }
3813  
3814 0 ▶sub to_ieee754 {
Subroutine to_ieee754: not called CC=3 SCAR=24.8
Pod documented
3815 0 ▶ my ($class, $x, $format, @r) = ref($_[0]) ? (ref($_[0]), @_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3816 : objectify(1, @_);
3817  
3818 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
3819  
3820 0 return $x -> as_float() -> to_ieee754($format);
3821 }
3822  
3823 0 ▶sub to_fp80 {
Subroutine to_fp80: not called CC=2 SCAR=17.9
Pod documented
3824 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
3825 : objectify(1, @_);
3826  
3827 0 return $x -> as_float(@r) -> to_fp80();
3828 }
3829  
3830 2 ▶sub as_hex {
Subroutine as_hex: called CC=4 SCAR=14.6
Pod documented
3831 2 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
3832  
3833 2 ▶ return $x unless $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 2 2
3834  
3835 2 ▶ my $s = $x->{sign}; $s = '' if $s eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "+" 2 0 2
3836 2 $s . $LIB->_as_hex($x->{_n});
3837 }
3838  
3839 2 ▶sub as_oct {
Subroutine as_oct: called CC=4 SCAR=14.6
Pod documented
3840 2 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
3841  
3842 2 ▶ return $x unless $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 2 2
3843  
3844 2 ▶ my $s = $x->{sign}; $s = '' if $s eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "+" 2 0 2
3845 2 $s . $LIB->_as_oct($x->{_n});
3846 }
3847  
3848 2 ▶sub as_bin {
Subroutine as_bin: called CC=4 SCAR=14.6
Pod documented
3849 2 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
3850  
3851 2 ▶ return $x unless $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 2 2
3852  
3853 2 my $s = $x->{sign};
3854 2 ▶ $s = '' if $s eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "+" 2 0 2
3855 2 $s . $LIB->_as_bin($x->{_n});
3856 }
3857  
3858 39 ▶sub numify {
Subroutine numify: called CC=7 SCAR=19.5
Pod documented
3859 # convert 17/8 => float (aka 2.125)
3860 39 ▶ my ($self, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 39 0 39
3861  
3862 # Non-finite number.
3863  
3864 39 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 1 38 39
3865 1 require Math::Complex;
3866 1 my $inf = $Math::Complex::Inf;
3867 1 return $inf - $inf;
3868 }
3869  
3870 38 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 2 36 38
3871 2 require Math::Complex;
3872 2 my $inf = $Math::Complex::Inf;
3873 2 ▶ return $x -> is_negative() ? -$inf : $inf;
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative ? : 1 1 2
3874 }
3875  
3876 # Finite number.
3877  
3878 my $abs = $LIB->_is_one($x->{_d})
3879 ? $LIB->_num($x->{_n})
3880 : Math::BigFloat -> new($LIB->_str($x->{_n}))
3881 36 ▶ -> bfdiv($LIB->_str($x->{_d}))
Branch: 1 branch
BranchTrueFalseTotal
$LIB->_is_one($x->{'_d'}) ? : 15 21 36
3882 -> bstr();
3883 36 ▶ return $x->{sign} eq '-' ? 0 - $abs : 0 + $abs;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 3 33 36
3884 }
3885  
3886 ##############################################################################
3887 # import
3888  
3889 18 ▶sub import {
Subroutine import: called CC=13 SCAR=34.5
3890 18 my $class = shift;
3891 18 $IMPORT++; # remember we did import()
3892 18 my @a; # unrecognized arguments
3893  
3894 18 my @import = ();
3895  
3896 18 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 18 22
3897 4 my $param = shift;
3898  
3899 # Enable overloading of constants.
3900  
3901 4 ▶ if ($param eq ':constant') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq ":constant") 0 4 4
3902 overload::constant
3903  
3904 0 ▶ integer => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
3905 0 $class -> new(shift);
3906 },
3907  
3908 0 ▶ float => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
3909 0 $class -> new(shift);
3910 },
3911  
3912 0 ▶ binary => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
3913 # E.g., a literal 0377 shall result in an object whose value
3914 # is decimal 255, but new("0377") returns decimal 377.
3915 0 ▶ return $class -> from_oct($_[0]) if $_[0] =~ /^0_*[0-7]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^0_*[0-7]/ 0 0 0
3916 0 $class -> new(shift);
3917 0 };
3918 0 next;
3919 }
3920  
3921 # Upgrading.
3922  
3923 4 ▶ if ($param eq 'upgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "upgrade") 0 4 4
3924 0 $class -> upgrade(shift);
3925 0 next;
3926 }
3927  
3928 # Downgrading.
3929  
3930 4 ▶ if ($param eq 'downgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "downgrade") 1 3 4
3931 1 $class -> downgrade(shift);
3932 1 next;
3933 }
3934  
3935 # Accuracy.
3936  
3937 3 ▶ if ($param eq 'accuracy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "accuracy") 0 3 3
3938 0 $class -> accuracy(shift);
3939 0 next;
3940 }
3941  
3942 # Precision.
3943  
3944 3 ▶ if ($param eq 'precision') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "precision") 0 3 3
3945 0 $class -> precision(shift);
3946 0 next;
3947 }
3948  
3949 # Rounding mode.
3950  
3951 3 ▶ if ($param eq 'round_mode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "round_mode") 0 3 3
3952 0 $class -> round_mode(shift);
3953 0 next;
3954 }
3955  
3956 # Fall-back accuracy.
3957  
3958 3 ▶ if ($param eq 'div_scale') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "div_scale") 0 3 3
3959 0 $class -> div_scale(shift);
3960 0 next;
3961 }
3962  
3963 # Backend library.
3964  
3965 3 ▶ if ($param =~ /^(lib|try|only)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(lib|try|only)\z/) 3 0 3
3966 3 push @import, $param;
3967 3 ▶ push @import, shift() if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 3 0 3
3968 3 next;
3969 }
3970  
3971 0 ▶ if ($param eq 'with') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "with") 0 0 0
3972 # alternative class for our private parts()
3973 # XXX: no longer supported
3974 # $LIB = shift() || 'Calc';
3975 # carp "'with' is no longer supported, use 'lib', 'try', or 'only'";
3976 0 shift;
3977 0 next;
3978 }
3979  
3980 # Unrecognized parameter.
3981  
3982 0 push @a, $param;
3983 }
3984  
3985 18 Math::BigInt -> import(@import);
3986  
3987 # find out which library was actually loaded
3988 18 $LIB = Math::BigInt -> config("lib");
3989  
3990 18 $class -> SUPER::import(@a); # for subclasses
3991 18 ▶ $class -> export_to_level(1, $class, @a) if @a; # need this, too
Branch: 1 branch
BranchTrueFalseTotal
if @a 0 18 18
3992 }
3993  
3994 16 1;
3995  
3996 __END__
« BigInt/Calc.pm ↑ Summary BigFloat.pm »