blib/lib/Math/BigRat.pm

filter: Statementstmt 81.1% 871 / 1073 Branchbran 66.5% 558 / 838 Conditioncond 57.6% 244 / 423 MC/DCmcdc 38.3% 100 / 261 Subroutinesub 75.1% 97 / 129 Podpod 98.5% 70 / 71 totaltotal 69.4% 1940 / 2795 SCAR 89.6
CC
563
Cov
72%
bdiv
365.3
new
196.6
bfstr
110.0
CRAP 7762.8
↑ Summary
1 #
2 # "Tax the rat farms." - Lord Vetinari
3 #
4  
5 # The following hash values are used:
6 # sign : +,-,NaN,+inf,-inf
7 # _d : denominator
8 # _n : numerator (value = _n/_d)
9 # _a : accuracy
10 # _p : precision
11 # You should not look at the innards of a BigRat - use the methods for this.
12  
13 package Math::BigRat;
14  
15 19 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 19 ▶use Carp qw< carp croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 19 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 19 ▶use Math::BigFloat ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 19 our $VERSION = '0.2624';
25  
26 19 our @ISA = qw(Math::BigFloat);
27  
28 19 our ($accuracy, $precision, $round_mode, $div_scale,
29 $upgrade, $downgrade, $_trap_nan, $_trap_inf);
30  
31 use overload
32  
33 # overload key: with_assign
34  
35 49 ▶ '+' => sub { $_[0] -> copy() -> badd($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
36  
37 53 ▶ '-' => sub { my $c = $_[0] -> copy;
Subroutine __ANON__: called CC=2 SCAR=7.2
38 53 ▶ $_[2] ? $c -> bneg() -> badd( $_[1])
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 53 53
39 : $c -> bsub($_[1]); },
40  
41 55 ▶ '*' => sub { $_[0] -> copy() -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 49 ▶ '/' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bdiv($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 49 49
44 : $_[0] -> copy() -> bdiv($_[1]); },
45  
46 21 ▶ '%' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bmod($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 21 21
47 : $_[0] -> copy() -> bmod($_[1]); },
48  
49 2 ▶ '**' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bpow($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 2 2
50 : $_[0] -> copy() -> bpow($_[1]); },
51  
52 0 ▶ '<<' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> blsft($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
53 : $_[0] -> copy() -> blsft($_[1]); },
54  
55 0 ▶ '>>' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> brsft($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
56 : $_[0] -> copy() -> brsft($_[1]); },
57  
58 # overload key: assign
59  
60 0 ▶ '+=' => sub { $_[0]->badd($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
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]->bdiv($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67  
68 0 ▶ '%=' => sub { $_[0]->bmod($_[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]->blsft($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
73  
74 0 ▶ '>>=' => sub { $_[0]->brsft($_[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 19 ▶ '<' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> blt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 19 19
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 24 ▶ '>' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bgt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 24 24
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 10 ▶ '==' => 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 1861 ▶ 'cmp' => sub { $_[2] ? "$_[1]" cmp $_[0] -> bstr()
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 1861 1861
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 6 ▶ '++' => sub { $_[0] -> binc() },
Subroutine __ANON__: called CC=1 SCAR=0.0
166  
167 5 ▶ '--' => 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 7 ▶ '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 4511 ▶ '""' => sub { $_[0] -> bstr(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
193  
194 10 ▶ '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 19 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
199  
200 BEGIN {
201 19 ▶ *objectify = \&Math::BigInt::objectify; # inherit this from BigInt
Subroutine BEGIN: called CC=1 SCAR=0.0
202 19 *AUTOLOAD = \&Math::BigFloat::AUTOLOAD; # can't inherit AUTOLOAD
203 19 *as_number = \&as_int;
204 19 *is_pos = \&is_positive;
205 19 *is_neg = \&is_negative;
206 }
207  
208 ##############################################################################
209 # Global constants and flags. Access these only via the accessor methods!
210  
211 19 $accuracy = $precision = undef;
212 19 $round_mode = 'even';
213 19 $div_scale = 40;
214 19 $upgrade = undef;
215 19 $downgrade = undef;
216  
217 # These are internally, and not to be used from the outside at all!
218  
219 19 $_trap_nan = 0; # are NaNs ok? set w/ config()
220 19 $_trap_inf = 0; # are infs ok? set w/ config()
221  
222 # the math backend library
223  
224 19 my $LIB = 'Math::BigInt::Calc';
225  
226 19 my $nan = 'NaN';
227 #my $class = 'Math::BigRat';
228  
229 sub isa {
230 6316 ▶ return 0 if $_[1] =~ /^Math::Big(Int|Float)/; # we aren't
Subroutine isa: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] =~ /^Math::Big(Int|Float)/ 2764 3552 6316
231 3552 UNIVERSAL::isa(@_);
232 }
233  
234 ##############################################################################
235  
236 sub new {
237 19741 ▶ my $proto = shift;
Subroutine new: called CC=58 SCAR=52.8
Pod documented
238 19741 my $protoref = ref $proto;
239 19741 ▶ my $class = $protoref || $proto;
Condition33%1/3
$protoref || $proto
l!l&&r!l&&!r
0197410
MC/DC0%0/2
$protoref || $proto
$protoref $proto
Truth table33%1/3
A || B
A= $protorefB= $proto
ABresult
1X1
011
000
240  
241 # Check the way we are called.
242  
243 19741 ▶ if ($protoref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($protoref) 0 19741 19741
244 0 croak("new() is a class method, not an instance method");
245 }
246  
247 19741 ▶ if (@_ < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ < 1) 1 19740 19741
248 #carp("Using new() with no argument is deprecated;",
249 # " use bzero() or new(0) instead");
250 1 return $class -> bzero();
251 }
252  
253 19740 ▶ if (@_ > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 2) 0 19740 19740
254 0 carp("Superfluous arguments to new() ignored.");
255 }
256  
257 # Get numerator and denominator. If any of the arguments is undefined,
258 # return zero.
259  
260 19740 my ($n, $d) = @_;
261  
262 19740 ▶ if (@_ == 1 && !defined $n ||
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not defined $n or @_ == 2 and not defined $n && defined $d) 1 19739 19740
Condition66%8/12
@_ == 1 and not defined $n or @_ == 2 and not defined $n && defined $d
l!l&&r!l&&!r
1019739
@_ == 1 and not defined $n
!ll&&!rl&&r
10197291
@_ == 2 and not defined $n && defined $d
!ll&&!rl&&r
19729100
defined $n && defined $d
!ll&&!rl&&r
0010
MC/DC40%2/5
@_ == 1 and not defined $n or @_ == 2 and not defined $n && defined $d
@_ == 1 not defined $n @_ == 2 defined $n defined $d
Truth table33%3/9
A and not(B) or C and not(D && E)
A= @_ == 1B= not defined $nC= @_ == 2D= defined $nE= defined $d
ABCDEresult
11XXX1
0X10X1
0X1101
1010X1
101101
0X0XX0
0X1110
100XX0
101110
263 @_ == 2 && (!defined $n || !defined $d))
264 {
265 #carp("Use of uninitialized value in new()");
266 1 return $class -> bzero();
267 }
268  
269 # Initialize a new object.
270  
271 19739 my $self = bless {}, $class;
272  
273 # One or two input arguments may be given. First handle the numerator $n.
274  
275 19739 ▶ if (ref($n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $n) { } 5817 13922 19739
276 5817 ▶ $n = Math::BigFloat -> new($n, undef, undef)
Branch: 1 branch
BranchTrueFalseTotal
unless $n->isa("Math::BigRat") or $n->isa("Math::BigInt") or $n->isa("Math::BigFloat") 0 5817 5817
Condition66%4/6
$n->isa("Math::BigRat") or $n->isa("Math::BigInt") or $n->isa("Math::BigFloat")
l!l&&r!l&&!r
581340
$n->isa("Math::BigRat") or $n->isa("Math::BigInt")
l!l&&r!l&&!r
058134
MC/DC0%0/3
$n->isa("Math::BigRat") or $n->isa("Math::BigInt") or $n->isa("Math::BigFloat")
$n->isa("Math::BigRat") $n->isa("Math::BigInt") $n->isa("Math::BigFloat")
Truth table50%2/4
A or B or C
A= $n->isa("Math::BigRat")B= $n->isa("Math::BigInt")C= $n->isa("Math::BigFloat")
ABCresult
1XX1
01X1
0011
0000
277 unless ($n -> isa('Math::BigRat') ||
278 $n -> isa('Math::BigInt') ||
279 $n -> isa('Math::BigFloat'));
280 } else {
281 13922 ▶ if (defined $d) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $d) { } 5 13917 13922
282 # If the denominator is defined, the numerator is not a string
283 # fraction, e.g., "355/113".
284 5 $n = Math::BigFloat -> new($n, undef, undef);
285 } else {
286 # If the denominator is undefined, the numerator might be a string
287 # fraction, e.g., "355/113".
288 13917 ▶ if ($n =~ m| ^ \s* (\S+) \s* / \s* (\S+) \s* $ |x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n =~ m[ ^ \s* (\S+) \s* / \s* (\S+) \s* $ ]x) { } 402 13515 13917
289 402 $n = Math::BigFloat -> new($1, undef, undef);
290 402 $d = Math::BigFloat -> new($2, undef, undef);
291 } else {
292 13515 $n = Math::BigFloat -> new($n, undef, undef);
293 }
294 }
295 }
296  
297 # At this point $n is an object and $d is either an object or undefined. An
298 # undefined $d means that $d was not specified by the caller (not that $d
299 # was specified as an undefined value).
300  
301 19739 ▶ unless (defined $d) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $d) 19327 412 19739
302 #return $n -> copy($n) if $n -> isa('Math::BigRat');
303 19327 ▶ if ($n -> isa('Math::BigRat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isa("Math::BigRat")) 0 19327 19327
304 0 ▶ return $downgrade -> new($n)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $n->is_int 0 0 0
Condition0%0/3
defined $Math::BigRat::downgrade and $n->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigRat::downgrade and $n->is_int
defined $Math::BigRat::downgrade $n->is_int
Truth table0%0/3
A and B
A= defined $Math::BigRat::downgradeB= $n->is_int
ABresult
0X0
100
111
305 if defined($downgrade) && $n -> is_int();
306 0 return $class -> copy($n);
307 }
308  
309 19327 ▶ if ($n -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->is_nan) 333 18994 19327
310 333 return $class -> bnan();
311 }
312  
313 18994 ▶ if ($n -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->is_inf) 563 18431 18994
314 563 return $class -> binf($n -> sign());
315 }
316  
317 18431 ▶ if ($n -> isa('Math::BigInt')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isa("Math::BigInt")) 5712 12719 18431
318 5712 $self -> {_n} = $LIB -> _new($n -> copy() -> babs(undef, undef)
319 -> bstr());
320 5712 $self -> {_d} = $LIB -> _one();
321 5712 $self -> {sign} = $n -> sign();
322 5712 ▶ return $downgrade -> new($n) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 1 5711 5712
323 5711 return $self;
324 }
325  
326 12719 ▶ if ($n -> isa('Math::BigFloat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isa("Math::BigFloat")) 12719 0 12719
327 12719 my $m = $n -> mantissa(undef, undef) -> babs(undef, undef);
328 12719 my $e = $n -> exponent(undef, undef);
329 12719 $self -> {_n} = $LIB -> _new($m -> bstr());
330 12719 $self -> {_d} = $LIB -> _one();
331  
332 12719 ▶ if ($e > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e > 0) { } 747 11972 12719
elsif ($e < 0) { } 899 11073 11972
333 $self -> {_n} = $LIB -> _lsft($self -> {_n},
334 747 $LIB -> _new($e -> bstr()), 10);
335 } elsif ($e < 0) {
336 $self -> {_d} = $LIB -> _lsft($self -> {_d},
337 899 $LIB -> _new(-$e -> bstr()), 10);
338  
339 my $gcd = $LIB -> _gcd($LIB -> _copy($self -> {_n}),
340 899 $self -> {_d});
341 899 ▶ if (!$LIB -> _is_one($gcd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($gcd)) 876 23 899
342 876 $self -> {_n} = $LIB -> _div($self->{_n}, $gcd);
343 876 $self -> {_d} = $LIB -> _div($self->{_d}, $gcd);
344 }
345 }
346  
347 12719 $self -> {sign} = $n -> sign();
348 12719 ▶ return $downgrade -> new($n, undef, undef)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $n->is_int 5 12714 12719
Condition100%3/3
defined $Math::BigRat::downgrade and $n->is_int
!ll&&!rl&&r
1271315
MC/DC100%2/2
defined $Math::BigRat::downgrade and $n->is_int
defined $Math::BigRat::downgrade $n->is_int
Truth table100%3/3
A and B
A= defined $Math::BigRat::downgradeB= $n->is_int
ABresult
0X0
100
111
349 if defined($downgrade) && $n -> is_int();
350 12714 return $self;
351 }
352  
353 0 die "I don't know how to handle this"; # should never get here
354 }
355  
356 # At the point we know that both $n and $d are defined. We know that $n is
357 # an object, but $d might still be a scalar. Now handle $d.
358  
359 412 ▶ $d = Math::BigFloat -> new($d, undef, undef)
Branch: 1 branch
BranchTrueFalseTotal
unless ref $d and $d->isa("Math::BigRat") || $d->isa("Math::BigInt") || $d->isa("Math::BigFloat") 5 407 412
Condition77%7/9
ref $d and $d->isa("Math::BigRat") || $d->isa("Math::BigInt") || $d->isa("Math::BigFloat")
!ll&&!rl&&r
51406
$d->isa("Math::BigRat") or $d->isa("Math::BigInt") or $d->isa("Math::BigFloat")
l!l&&r!l&&!r
54020
$d->isa("Math::BigRat") or $d->isa("Math::BigInt")
l!l&&r!l&&!r
05402
MC/DC25%1/4
ref $d and $d->isa("Math::BigRat") || $d->isa("Math::BigInt") || $d->isa("Math::BigFloat")
ref $d $d->isa("Math::BigRat") $d->isa("Math::BigInt") $d->isa("Math::BigFloat")
Truth table40%2/5
A and B or C or D
A= ref $dB= $d->isa("Math::BigRat")C= $d->isa("Math::BigInt")D= $d->isa("Math::BigFloat")
ABCDresult
0XXX0
10000
11XX1
101X1
10011
360 unless ref($d) && ($d -> isa('Math::BigRat') ||
361 $d -> isa('Math::BigInt') ||
362 $d -> isa('Math::BigFloat'));
363  
364 # At this point both $n and $d are objects.
365  
366 412 ▶ if ($n -> is_nan() || $d -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->is_nan or $d->is_nan) 3 409 412
Condition66%2/3
$n->is_nan or $d->is_nan
l!l&&r!l&&!r
30409
MC/DC50%1/2
$n->is_nan or $d->is_nan
$n->is_nan $d->is_nan
Truth table66%2/3
A or B
A= $n->is_nanB= $d->is_nan
ABresult
1X1
011
000
367 3 return $class -> bnan();
368 }
369  
370 # At this point neither $n nor $d is a NaN.
371  
372 409 ▶ if ($n -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->is_zero) 6 403 409
373 6 ▶ if ($d -> is_zero()) { # 0/0 = NaN
Branch: 1 branch
BranchTrueFalseTotal
if ($d->is_zero) 0 6 6
374 0 return $class -> bnan();
375 }
376 6 return $class -> bzero();
377 }
378  
379 403 ▶ if ($d -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->is_zero) 0 403 403
380 0 return $class -> binf($d -> sign());
381 }
382  
383 # At this point, neither $n nor $d is a NaN or a zero.
384  
385 # Copy them now before manipulating them.
386  
387 403 $n = $n -> copy();
388 403 $d = $d -> copy();
389  
390 403 ▶ if ($d < 0) { # make sure denominator is positive
Branch: 1 branch
BranchTrueFalseTotal
if ($d < 0) 10 393 403
391 10 $n -> bneg();
392 10 $d -> bneg();
393 }
394  
395 403 ▶ if ($n -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->is_inf) 8 395 403
396 8 ▶ return $class -> bnan() if $d -> is_inf(); # Inf/Inf = NaN
Branch: 1 branch
BranchTrueFalseTotal
if $d->is_inf 1 7 8
397 7 return $class -> binf($n -> sign());
398 }
399  
400 # At this point $n is finite.
401  
402 395 ▶ return $class -> bzero() if $d -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $d->is_inf 4 391 395
403 391 ▶ return $class -> binf($d -> sign()) if $d -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $d->is_zero 0 391 391
404  
405 # At this point both $n and $d are finite and non-zero.
406  
407 391 ▶ if ($n < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < 0) { } 129 262 391
408 129 $n -> bneg();
409 129 $self -> {sign} = '-';
410 } else {
411 262 $self -> {sign} = '+';
412 }
413  
414 391 ▶ if ($n -> isa('Math::BigRat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n->isa('Math::BigRat')) { } 0 391 391
415  
416 0 ▶ if ($d -> isa('Math::BigRat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->isa("Math::BigRat")) 0 0 0
417  
418 # At this point both $n and $d is a Math::BigRat.
419  
420 # p r p * s (p / gcd(p, r)) * (s / gcd(s, q))
421 # - / - = ----- = ---------------------------------
422 # q s q * r (q / gcd(s, q)) * (r / gcd(p, r))
423  
424 0 my $p = $n -> {_n};
425 0 my $q = $n -> {_d};
426 0 my $r = $d -> {_n};
427 0 my $s = $d -> {_d};
428 0 my $gcd_pr = $LIB -> _gcd($LIB -> _copy($p), $r);
429 0 my $gcd_sq = $LIB -> _gcd($LIB -> _copy($s), $q);
430 0 $self -> {_n} = $LIB -> _mul($LIB -> _div($LIB -> _copy($p), $gcd_pr),
431 $LIB -> _div($LIB -> _copy($s), $gcd_sq));
432 0 $self -> {_d} = $LIB -> _mul($LIB -> _div($LIB -> _copy($q), $gcd_sq),
433 $LIB -> _div($LIB -> _copy($r), $gcd_pr));
434  
435 0 ▶ return $downgrade -> new($n->bstr())
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $self->is_int 0 0 0
Condition0%0/3
defined $Math::BigRat::downgrade and $self->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigRat::downgrade and $self->is_int
defined $Math::BigRat::downgrade $self->is_int
Truth table0%0/3
A and B
A= defined $Math::BigRat::downgradeB= $self->is_int
ABresult
0X0
100
111
436 if defined($downgrade) && $self -> is_int();
437 0 return $self; # no need for $self -> bnorm() here
438 }
439  
440 # At this point, $n is a Math::BigRat and $d is a Math::Big(Int|Float).
441  
442 0 my $p = $n -> {_n};
443 0 my $q = $n -> {_d};
444 0 my $m = $d -> mantissa();
445 0 my $e = $d -> exponent();
446  
447 # / p
448 # | ------------ if e > 0
449 # | q * m * 10^e
450 # |
451 # p | p
452 # - / (m * 10^e) = | ----- if e == 0
453 # q | q * m
454 # |
455 # | p * 10^-e
456 # | -------- if e < 0
457 # \ q * m
458  
459 0 $self -> {_n} = $LIB -> _copy($p);
460 0 $self -> {_d} = $LIB -> _mul($LIB -> _copy($q), $m);
461 0 ▶ if ($e > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e > 0) { } 0 0 0
elsif ($e < 0) { } 0 0 0
462 0 $self -> {_d} = $LIB -> _lsft($self -> {_d}, $e, 10);
463 } elsif ($e < 0) {
464 0 $self -> {_n} = $LIB -> _lsft($self -> {_n}, -$e, 10);
465 }
466  
467 0 return $self -> bnorm();
468  
469 } else {
470  
471 391 ▶ if ($d -> isa('Math::BigRat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d->isa('Math::BigRat')) { } 0 391 391
472  
473 # At this point $n is a Math::Big(Int|Float) and $d is a
474 # Math::BigRat.
475  
476 0 my $m = $n -> mantissa();
477 0 my $e = $n -> exponent();
478 0 my $p = $d -> {_n};
479 0 my $q = $d -> {_d};
480  
481 # / q * m * 10^e
482 # | ------------ if e > 0
483 # | p
484 # |
485 # p | m * q
486 # (m * 10^e) / - = | ----- if e == 0
487 # q | p
488 # |
489 # | q * m
490 # | --------- if e < 0
491 # \ p * 10^-e
492  
493 0 $self -> {_n} = $LIB -> _mul($LIB -> _copy($q), $m);
494 0 $self -> {_d} = $LIB -> _copy($p);
495 0 ▶ if ($e > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e > 0) { } 0 0 0
elsif ($e < 0) { } 0 0 0
496 0 $self -> {_n} = $LIB -> _lsft($self -> {_n}, $e, 10);
497 } elsif ($e < 0) {
498 0 $self -> {_d} = $LIB -> _lsft($self -> {_d}, -$e, 10);
499 }
500 0 return $self -> bnorm();
501  
502 } else {
503  
504 # At this point $n and $d are both a Math::Big(Int|Float)
505  
506 391 my $m1 = $n -> mantissa();
507 391 my $e1 = $n -> exponent();
508 391 my $m2 = $d -> mantissa();
509 391 my $e2 = $d -> exponent();
510  
511 # /
512 # | m1 * 10^(e1 - e2)
513 # | ----------------- if e1 > e2
514 # | m2
515 # |
516 # m1 * 10^e1 | m1
517 # ---------- = | -- if e1 = e2
518 # m2 * 10^e2 | m2
519 # |
520 # | m1
521 # | ----------------- if e1 < e2
522 # | m2 * 10^(e2 - e1)
523 # \
524  
525 391 $self -> {_n} = $LIB -> _new($m1 -> bstr());
526 391 $self -> {_d} = $LIB -> _new($m2 -> bstr());
527 391 my $ediff = $e1 - $e2;
528 391 ▶ if ($ediff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ediff > 0) { } 24 367 391
elsif ($ediff < 0) { } 27 340 367
529 $self -> {_n} = $LIB -> _lsft($self -> {_n},
530 24 $LIB -> _new($ediff -> bstr()),
531 10);
532 } elsif ($ediff < 0) {
533 $self -> {_d} = $LIB -> _lsft($self -> {_d},
534 27 $LIB -> _new(-$ediff -> bstr()),
535 10);
536 }
537  
538 391 return $self -> bnorm();
539 }
540 }
541  
542 0 ▶ return $downgrade -> new($self -> bstr())
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $self->is_int 0 0 0
Condition0%0/3
defined $Math::BigRat::downgrade and $self->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigRat::downgrade and $self->is_int
defined $Math::BigRat::downgrade $self->is_int
Truth table0%0/3
A and B
A= defined $Math::BigRat::downgradeB= $self->is_int
ABresult
0X0
100
111
543 if defined($downgrade) && $self -> is_int();
544 0 return $self;
545 }
546  
547 sub copy {
548 1319 ▶ my $self = shift;
Subroutine copy: called CC=5 SCAR=16.8
Pod documented
549 1319 my $selfref = ref $self;
550 1319 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
131900
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
551  
552 # If called as a class method, the object to copy is the next argument.
553  
554 1319 ▶ $self = shift() unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 1319 1319
555  
556 1319 my $copy = bless {}, $class;
557  
558 1319 $copy->{sign} = $self->{sign};
559 1319 $copy->{_d} = $LIB->_copy($self->{_d});
560 1319 $copy->{_n} = $LIB->_copy($self->{_n});
561 1319 ▶ $copy->{_a} = $self->{_a} if defined $self->{_a};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_a"} 0 1319 1319
562 1319 ▶ $copy->{_p} = $self->{_p} if defined $self->{_p};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_p"} 0 1319 1319
563  
564 #($copy, $copy->{_a}, $copy->{_p})
565 # = $copy->_find_round_parameters(@_);
566  
567 1319 return $copy;
568 }
569  
570 sub bnan {
571 537 ▶ my $self = shift;
Subroutine bnan: called CC=5 SCAR=16.1
Pod documented
572 537 my $selfref = ref $self;
573 537 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
1933440
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
574  
575 537 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 344 193 537
576  
577 537 ▶ if ($_trap_nan) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Math::BigRat::_trap_nan) 5 532 537
578 5 croak ("Tried to set a variable to NaN in $class->bnan()");
579 }
580  
581 532 ▶ return $downgrade -> bnan() if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 7 525 532
582  
583 525 $self -> {sign} = $nan;
584 525 $self -> {_n} = $LIB -> _zero();
585 525 $self -> {_d} = $LIB -> _one();
586  
587 ($self, $self->{_a}, $self->{_p})
588 525 = $self->_find_round_parameters(@_);
589  
590 525 return $self;
591 }
592  
593 sub binf {
594 751 ▶ my $self = shift;
Subroutine binf: called CC=7 SCAR=19.5
Pod documented
595 751 my $selfref = ref $self;
596 751 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
1755760
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
597  
598 751 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 576 175 751
599  
600 751 my $sign = shift();
601 751 ▶ $sign = defined($sign) && substr($sign, 0, 1) eq '-' ? '-inf' : '+inf';
Branch: 1 branch
BranchTrueFalseTotal
defined $sign && substr($sign, 0, 1) eq '-' ? : 351 400 751
Condition100%3/3
defined $sign and substr($sign, 0, 1) eq "-"
!ll&&!rl&&r
16384351
MC/DC100%2/2
defined $sign and substr($sign, 0, 1) eq "-"
defined $sign substr($sign, 0, 1) eq "-"
Truth table100%3/3
A and B
A= defined $signB= substr($sign, 0, 1) eq "-"
ABresult
0X0
100
111
602  
603 751 ▶ if ($_trap_inf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Math::BigRat::_trap_inf) 12 739 751
604 12 croak ("Tried to set a variable to +-inf in $class->binf()");
605 }
606  
607 739 ▶ return $downgrade -> binf($sign) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 7 732 739
608  
609 732 $self -> {sign} = $sign;
610 732 $self -> {_n} = $LIB -> _zero();
611 732 $self -> {_d} = $LIB -> _one();
612  
613 ($self, $self->{_a}, $self->{_p})
614 732 = $self->_find_round_parameters(@_);
615  
616 732 return $self;
617 }
618  
619 sub bone {
620 13 ▶ my $self = shift;
Subroutine bone: called CC=6 SCAR=18.0
Pod documented
621 13 my $selfref = ref $self;
622 13 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
1210
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
623  
624 13 my $sign = shift();
625 13 ▶ $sign = '+' unless defined($sign) && $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sign and $sign eq "-" 11 2 13
Condition100%3/3
defined $sign and $sign eq "-"
!ll&&!rl&&r
562
MC/DC100%2/2
defined $sign and $sign eq "-"
defined $sign $sign eq "-"
Truth table100%3/3
A and B
A= defined $signB= $sign eq "-"
ABresult
0X0
100
111
626  
627 13 ▶ return $downgrade -> bone($sign) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 1 12 13
628  
629 12 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 12 12
630 12 $self -> {sign} = $sign;
631 12 $self -> {_n} = $LIB -> _one();
632 12 $self -> {_d} = $LIB -> _one();
633  
634 ($self, $self->{_a}, $self->{_p})
635 12 = $self->_find_round_parameters(@_);
636  
637 12 return $self;
638 }
639  
640 sub bzero {
641 62 ▶ my $self = shift;
Subroutine bzero: called CC=4 SCAR=13.9
Pod documented
642 62 my $selfref = ref $self;
643 62 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
32300
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
644  
645 62 ▶ return $downgrade -> bzero() if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 1 61 62
646  
647 61 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 29 32 61
648 61 $self -> {sign} = '+';
649 61 $self -> {_n} = $LIB -> _zero();
650 61 $self -> {_d} = $LIB -> _one();
651  
652 ($self, $self->{_a}, $self->{_p})
653 61 = $self->_find_round_parameters(@_);
654  
655 61 return $self;
656 }
657  
658 ##############################################################################
659  
660 sub config {
661 # return (later set?) configuration data as hash ref
662 12 ▶ my $class = shift() || 'Math::BigRat';
Subroutine config: called CC=4 SCAR=13.9
Pod documented
Condition50%1/2
shift() || 'Math::BigRat'
l!l
120
MC/DC0%0/1
shift() || 'Math::BigRat'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
663  
664 12 ▶ if (@_ == 1 && ref($_[0]) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] ne "HASH") 6 6 12
Condition100%3/3
@_ == 1 and ref $_[0] ne "HASH"
!ll&&!rl&&r
516
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
665 6 my $cfg = $class->SUPER::config();
666 6 return $cfg->{$_[0]};
667 }
668  
669 6 my $cfg = $class->SUPER::config(@_);
670  
671 # now we need only to override the ones that are different from our parent
672 6 $cfg->{class} = $class;
673 6 $cfg->{with} = $LIB;
674  
675 6 $cfg;
676 }
677  
678 ###############################################################################
679 # String conversion methods
680 ###############################################################################
681  
682 sub bstr {
683 29076 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine bstr: called CC=11 SCAR=24.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 29076 0 29076
684  
685 29076 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 29076 29076
686  
687 # Inf and NaN
688  
689 29076 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 1135 27941 29076
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
1995579861135
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
690 1135 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 796 339 1135
691 339 return 'inf'; # +inf
692 }
693  
694 # Upgrade?
695  
696 27941 ▶ return $upgrade -> bstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::upgrade and not $x->isa($class) 0 27941 27941
Condition33%1/3
defined $Math::BigRat::upgrade and not $x->isa($class)
!ll&&!rl&&r
2794100
MC/DC0%0/2
defined $Math::BigRat::upgrade and not $x->isa($class)
defined $Math::BigRat::upgrade not $x->isa($class)
Truth table33%1/3
A and not(B)
A= defined $Math::BigRat::upgradeB= not $x->isa($class)
ABresult
0X0
100
111
697 if defined($upgrade) && !$x -> isa($class);
698  
699 # Finite number
700  
701 27941 my $s = '';
702 27941 ▶ $s = $x->{sign} if $x->{sign} ne '+'; # '+3/2' => '3/2'
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne "+" 7986 19955 27941
703  
704 27941 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 7986 19955 27941
705 27941 $str .= $LIB->_str($x->{_n});
706 27941 ▶ $str .= '/' . $LIB->_str($x->{_d}) unless $LIB -> _is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_one($x->{"_d"}) 1847 26094 27941
707 27941 return $str;
708 }
709  
710 sub bsstr {
711 8 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine bsstr: called CC=10 SCAR=23.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 8 0 8
712  
713 8 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 8 8
714  
715 # Inf and NaN
716  
717 8 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 3 5 8
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
413
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
718 3 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 2 1 3
719 1 return 'inf'; # +inf
720 }
721  
722 # Upgrade?
723  
724 5 ▶ return $upgrade -> bsstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::upgrade and not $x->isa($class) 0 5 5
Condition33%1/3
defined $Math::BigRat::upgrade and not $x->isa($class)
!ll&&!rl&&r
500
MC/DC0%0/2
defined $Math::BigRat::upgrade and not $x->isa($class)
defined $Math::BigRat::upgrade not $x->isa($class)
Truth table33%1/3
A and not(B)
A= defined $Math::BigRat::upgradeB= not $x->isa($class)
ABresult
0X0
100
111
725 if defined($upgrade) && !$x -> isa($class);
726  
727 # Finite number
728  
729 5 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 1 4 5
730 5 $str .= $LIB->_str($x->{_n});
731 5 ▶ $str .= '/' . $LIB->_str($x->{_d}) unless $LIB -> _is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_one($x->{"_d"}) 3 2 5
732 5 return $str;
733 }
734  
735 sub bfstr {
736 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine bfstr: not called CC=10 SCAR=47.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
737  
738 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
739  
740 # Inf and NaN
741  
742 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
743 0 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 0 0 0
744 0 return 'inf'; # +inf
745 }
746  
747 # Upgrade?
748  
749 0 ▶ return $upgrade -> bfstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::upgrade and not $x->isa($class) 0 0 0
Condition0%0/3
defined $Math::BigRat::upgrade and not $x->isa($class)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigRat::upgrade and not $x->isa($class)
defined $Math::BigRat::upgrade not $x->isa($class)
Truth table0%0/3
A and not(B)
A= defined $Math::BigRat::upgradeB= not $x->isa($class)
ABresult
0X0
100
111
750 if defined($upgrade) && !$x -> isa($class);
751  
752 # Finite number
753  
754 0 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
755 0 $str .= $LIB->_str($x->{_n});
756 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
757 0 return $str;
758 }
759  
760 sub bnorm {
761 # reduce the number to the shortest form
762 774 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine bnorm: called CC=12 SCAR=24.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 750 24 774
763  
764 # Both parts must be objects of whatever we are using today.
765 774 ▶ if (my $c = $LIB->_check($x->{_n})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $LIB->_check($x->{"_n"})) 0 774 774
766 0 croak("n did not pass the self-check ($c) in bnorm()");
767 }
768 774 ▶ if (my $c = $LIB->_check($x->{_d})) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = $LIB->_check($x->{"_d"})) 0 774 774
769 0 croak("d did not pass the self-check ($c) in bnorm()");
770 }
771  
772 # no normalize for NaN, inf etc.
773 774 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 18 756 774
774 18 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 2 16 18
775 16 return $x;
776 }
777  
778 # normalize zeros to 0/1
779 756 ▶ if ($LIB->_is_zero($x->{_n})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{"_n"})) 36 720 756
780 36 ▶ return $downgrade -> bzero() if defined($downgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 3 33 36
781 33 $x->{sign} = '+'; # never leave a -0
782 33 ▶ $x->{_d} = $LIB->_one() unless $LIB->_is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_one($x->{"_d"}) 9 24 33
783 33 return $x;
784 }
785  
786 # n/1
787 720 ▶ if ($LIB->_is_one($x->{_d})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_one($x->{"_d"})) 216 504 720
788 216 ▶ return $downgrade -> new($x) if defined($downgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 11 205 216
789 205 return $x; # no need to reduce
790 }
791  
792 # Compute the GCD.
793 504 my $gcd = $LIB->_gcd($LIB->_copy($x->{_n}), $x->{_d});
794 504 ▶ if (!$LIB->_is_one($gcd)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($gcd)) 97 407 504
795 97 $x->{_n} = $LIB->_div($x->{_n}, $gcd);
796 97 $x->{_d} = $LIB->_div($x->{_d}, $gcd);
797 }
798  
799 504 $x;
800 }
801  
802 ##############################################################################
803 # sign manipulation
804  
805 sub bneg {
806 # (BRAT or num_str) return BRAT
807 # negate number or make a negated number from string
808 26 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine bneg: called CC=7 SCAR=19.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 26 0 26
809  
810 26 ▶ return $x if $x->modify('bneg');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bneg") 0 26 26
811  
812 # for +0 do not negate (to have always normalized +0). Does nothing for 'NaN'
813 $x->{sign} =~ tr/+-/-+/
814 26 ▶ unless ($x->{sign} eq '+' && $LIB->_is_zero($x->{_n}));
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_n"}) 24 2 26
Condition100%3/3
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_n"})
!ll&&!rl&&r
1772
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
815  
816 return $downgrade -> new($x)
817 26 ▶ if defined($downgrade) && $LIB -> _is_one($x->{_d});
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $LIB->_is_one($x->{"_d"}) 4 22 26
Condition100%3/3
defined $Math::BigRat::downgrade and $LIB->_is_one($x->{"_d"})
!ll&&!rl&&r
2024
MC/DC100%2/2
defined $Math::BigRat::downgrade and $LIB->_is_one($x->{"_d"})
defined $Math::BigRat::downgrade $LIB->_is_one($x->{"_d"})
Truth table100%3/3
A and B
A= defined $Math::BigRat::downgradeB= $LIB->_is_one($x->{"_d"})
ABresult
0X0
100
111
818 22 $x;
819 }
820  
821 ##############################################################################
822 # mul/add/div etc
823  
824 sub badd {
825 # add two rational numbers
826  
827 # set up parameters
828 325 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine badd: called CC=13 SCAR=25.7
Pod documented
829 # objectify is costly, so avoid it
830 325 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 91 234 325
Condition66%2/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
091234
MC/DC50%1/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table66%2/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
831 91 ($class, $x, $y, @r) = objectify(2, @_);
832 }
833  
834 325 ▶ unless ($x -> is_finite() && $y -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 202 123 325
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
12874123
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
835 202 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 5 branches
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) { } 62 140 202
elsif ($x->is_inf('+')) { } 41 99 140
elsif ($x->is_inf('-')) { } 41 58 99
elsif ($y->is_inf('+')) { } 30 28 58
elsif ($y->is_inf('-')) { } 28 0 28
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
3428140
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
836 62 return $x -> bnan(@r);
837 } elsif ($x -> is_inf("+")) {
838 41 ▶ return $x -> bnan(@r) if $y -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_inf("-") 6 35 41
839 35 return $x -> binf("+", @r);
840 } elsif ($x -> is_inf("-")) {
841 41 ▶ return $x -> bnan(@r) if $y -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_inf("+") 6 35 41
842 35 return $x -> binf("-", @r);
843 } elsif ($y -> is_inf("+")) {
844 30 return $x -> binf("+", @r);
845 } elsif ($y -> is_inf("-")) {
846 28 return $x -> binf("-", @r);
847 }
848 }
849  
850 # 1 1 gcd(3, 4) = 1 1*3 + 1*4 7
851 # - + - = --------- = --
852 # 4 3 4*3 12
853  
854 # we do not compute the gcd() here, but simple do:
855 # 5 7 5*3 + 7*4 43
856 # - + - = --------- = --
857 # 4 3 4*3 12
858  
859 # and bnorm() will then take care of the rest
860  
861 # 5 * 3
862 123 $x->{_n} = $LIB->_mul($x->{_n}, $y->{_d});
863  
864 # 7 * 4
865 123 my $m = $LIB->_mul($LIB->_copy($y->{_n}), $x->{_d});
866  
867 # 5 * 3 + 7 * 4
868 123 ($x->{_n}, $x->{sign}) = $LIB -> _sadd($x->{_n}, $x->{sign}, $m, $y->{sign});
869  
870 # 4 * 3
871 123 $x->{_d} = $LIB->_mul($x->{_d}, $y->{_d});
872  
873 # normalize result, and possible round
874 123 $x->bnorm()->round(@r);
875 }
876  
877 sub bsub {
878 # subtract two rational numbers
879  
880 # set up parameters
881 91 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine bsub: called CC=7 SCAR=19.5
Pod documented
882 # objectify is costly, so avoid it
883 91 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 5 86 91
Condition66%2/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
0586
MC/DC50%1/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table66%2/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
884 5 ($class, $x, $y, @r) = objectify(2, @_);
885 }
886  
887 # flip sign of $x, call badd(), then flip sign of result
888 $x->{sign} =~ tr/+-/-+/
889 91 ▶ unless $x->{sign} eq '+' && $x -> is_zero(); # not -0
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+" and $x->is_zero 84 7 91
Condition100%3/3
$x->{"sign"} eq "+" and $x->is_zero
!ll&&!rl&&r
28567
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
890 91 $x = $x->badd($y, @r); # does norm and round
891 $x->{sign} =~ tr/+-/-+/
892 91 ▶ unless $x->{sign} eq '+' && $x -> is_zero(); # not -0
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+" and $x->is_zero 75 16 91
Condition100%3/3
$x->{"sign"} eq "+" and $x->is_zero
!ll&&!rl&&r
611416
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
893  
894 91 $x->bnorm();
895 }
896  
897 sub bmul {
898 # multiply two rational numbers
899  
900 # set up parameters
901 93 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine bmul: called CC=20 SCAR=30.2
Pod documented
902 # objectify is costly, so avoid it
903 93 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 6 87 93
Condition66%2/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
0687
MC/DC50%1/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table66%2/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
904 6 ($class, $x, $y, @r) = objectify(2, @_);
905 }
906  
907 93 ▶ return $x->bnan() if $x->{sign} eq 'NaN' || $y->{sign} eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "NaN" or $y->{"sign"} eq "NaN" 8 85 93
Condition100%3/3
$x->{"sign"} eq "NaN" or $y->{"sign"} eq "NaN"
l!l&&r!l&&!r
5385
MC/DC100%2/2
$x->{"sign"} eq "NaN" or $y->{"sign"} eq "NaN"
$x->{"sign"} eq "NaN" $y->{"sign"} eq "NaN"
Truth table100%3/3
A or B
A= $x->{"sign"} eq "NaN"B= $y->{"sign"} eq "NaN"
ABresult
1X1
011
000
908  
909 # inf handling
910 85 ▶ if ($x->{sign} =~ /^[+-]inf$/ || $y->{sign} =~ /^[+-]inf$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/) 13 72 85
Condition100%3/3
$x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/
l!l&&r!l&&!r
9472
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} =~ /^[+-]inf$/ $y->{"sign"} =~ /^[+-]inf$/
Truth table100%3/3
A or B
A= $x->{"sign"} =~ /^[+-]inf$/B= $y->{"sign"} =~ /^[+-]inf$/
ABresult
1X1
011
000
911 13 ▶ return $x->bnan() if $x->is_zero() || $y->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $y->is_zero 0 13 13
Condition33%1/3
$x->is_zero or $y->is_zero
l!l&&r!l&&!r
0013
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
912 # result will always be +-inf:
913 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
914 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
915 13 ▶ return $x->binf() if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/ 5 8 13
Condition100%3/3
$x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/
!ll&&!rl&&r
445
MC/DC100%2/2
$x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/
$x->{"sign"} =~ /^\+/ $y->{"sign"} =~ /^\+/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^\+/B= $y->{"sign"} =~ /^\+/
ABresult
0X0
100
111
916 8 ▶ return $x->binf() if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/ 2 6 8
Condition100%3/3
$x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/
!ll&&!rl&&r
422
MC/DC100%2/2
$x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/
$x->{"sign"} =~ /^-/ $y->{"sign"} =~ /^-/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^-/B= $y->{"sign"} =~ /^-/
ABresult
0X0
100
111
917 6 return $x->binf('-');
918 }
919  
920 # x == 0 # also: or y == 1 or y == -1
921 72 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 5 67 72
922 5 ▶ $x = $downgrade -> bzero($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 1 4 5
923 5 ▶ return wantarray ? ($x, $class->bzero()) : $x;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 5 5
924 }
925  
926 67 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 3 64 67
927 3 ▶ $x = defined($downgrade) ? $downgrade -> bzero($x) : $x -> bzero();
Branch: 1 branch
BranchTrueFalseTotal
defined $Math::BigRat::downgrade ? : 0 3 3
928 3 ▶ return wantarray ? ($x, $class->bzero()) : $x;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
929 }
930  
931 # According to Knuth, this can be optimized by doing gcd twice (for d
932 # and n) and reducing in one step. This saves us a bnorm() at the end.
933 #
934 # p s p * s (p / gcd(p, r)) * (s / gcd(s, q))
935 # - * - = ----- = ---------------------------------
936 # q r q * r (q / gcd(s, q)) * (r / gcd(p, r))
937  
938 64 my $gcd_pr = $LIB -> _gcd($LIB -> _copy($x->{_n}), $y->{_d});
939 64 my $gcd_sq = $LIB -> _gcd($LIB -> _copy($y->{_n}), $x->{_d});
940  
941 $x->{_n} = $LIB -> _mul(scalar $LIB -> _div($x->{_n}, $gcd_pr),
942 64 scalar $LIB -> _div($LIB -> _copy($y->{_n}),
943 $gcd_sq));
944 $x->{_d} = $LIB -> _mul(scalar $LIB -> _div($x->{_d}, $gcd_sq),
945 64 scalar $LIB -> _div($LIB -> _copy($y->{_d}),
946 $gcd_pr));
947  
948 # compute new sign
949 64 ▶ $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 52 12 64
950  
951 64 $x->bnorm()->round(@r);
952 }
953  
954 sub bdiv {
955 # (dividend: BRAT or num_str, divisor: BRAT or num_str) return
956 # (BRAT, BRAT) (quo, rem) or BRAT (only rem)
957  
958 # set up parameters
959 87 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine bdiv: called CC=51 SCAR=59.0
Pod documented
960 # objectify is costly, so avoid it
961 87 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 7 80 87
Condition66%2/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
0780
MC/DC50%1/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table66%2/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
962 7 ($class, $x, $y, @r) = objectify(2, @_);
963 }
964  
965 87 ▶ return $x if $x->modify('bdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdiv") 0 87 87
966  
967 87 my $wantarray = wantarray; # call only once
968  
969 # At least one argument is NaN. This is handled the same way as in
970 # Math::BigInt -> bdiv(). See the comments in the code implementing that
971 # method.
972  
973 87 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 5 82 87
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
2382
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
974 5 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 5 5
975 0 ▶ return $downgrade -> bnan(), $downgrade -> bnan()
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 0 0 0
976 if defined($downgrade);
977 0 return $x -> bnan(), $class -> bnan();
978 } else {
979 5 ▶ return $downgrade -> bnan()
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 0 5 5
980 if defined($downgrade);
981 5 return $x -> bnan();
982 }
983 }
984  
985 # Divide by zero and modulo zero. This is handled the same way as in
986 # Math::BigInt -> bdiv(). See the comments in the code implementing that
987 # method.
988  
989 82 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 11 71 82
990 11 my ($quo, $rem);
991 11 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 3 8 11
992 3 $rem = $x -> copy();
993 }
994 11 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 3 8 11
995 3 $quo = $x -> bnan();
996 } else {
997 8 $quo = $x -> binf($x -> {sign});
998 }
999  
1000 8 ▶ $quo = $downgrade -> new($quo)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $quo->is_int 0 8 8
Condition33%1/3
defined $Math::BigRat::downgrade and $quo->is_int
!ll&&!rl&&r
800
MC/DC0%0/2
defined $Math::BigRat::downgrade and $quo->is_int
defined $Math::BigRat::downgrade $quo->is_int
Truth table33%1/3
A and B
A= defined $Math::BigRat::downgradeB= $quo->is_int
ABresult
0X0
100
111
1001 if defined($downgrade) && $quo -> is_int();
1002 8 ▶ $rem = $downgrade -> new($rem)
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray and defined $Math::BigRat::downgrade and $rem->is_int 0 8 8
Condition50%3/6
$wantarray and defined $Math::BigRat::downgrade and $rem->is_int
!ll&&!rl&&r
800
$wantarray and defined $Math::BigRat::downgrade
!ll&&!rl&&r
530
MC/DC0%0/3
$wantarray and defined $Math::BigRat::downgrade and $rem->is_int
$wantarray defined $Math::BigRat::downgrade $rem->is_int
Truth table50%2/4
A and B and C
A= $wantarrayB= defined $Math::BigRat::downgradeC= $rem->is_int
ABCresult
0XX0
10X0
1100
1111
1003 if $wantarray && defined($downgrade) && $rem -> is_int();
1004 8 ▶ return $wantarray ? ($quo, $rem) : $quo;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 3 5 8
1005 }
1006  
1007 # Numerator (dividend) is +/-inf. This is handled the same way as in
1008 # Math::BigInt -> bdiv(). See the comments in the code implementing that
1009 # method.
1010  
1011 71 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 71 71
1012 0 my ($quo, $rem);
1013 0 ▶ $rem = $class -> bnan() if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 0 0 0
1014 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 0 0 0
1015 0 $quo = $x -> bnan();
1016 } else {
1017 0 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 0 0 0
1018 0 $quo = $x -> binf($sign);
1019 }
1020  
1021 0 ▶ $quo = $downgrade -> new($quo)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $quo->is_int 0 0 0
Condition0%0/3
defined $Math::BigRat::downgrade and $quo->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigRat::downgrade and $quo->is_int
defined $Math::BigRat::downgrade $quo->is_int
Truth table0%0/3
A and B
A= defined $Math::BigRat::downgradeB= $quo->is_int
ABresult
0X0
100
111
1022 if defined($downgrade) && $quo -> is_int();
1023 0 ▶ $rem = $downgrade -> new($rem)
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray and defined $Math::BigRat::downgrade and $rem->is_int 0 0 0
Condition0%0/6
$wantarray and defined $Math::BigRat::downgrade and $rem->is_int
!ll&&!rl&&r
000
$wantarray and defined $Math::BigRat::downgrade
!ll&&!rl&&r
000
MC/DC0%0/3
$wantarray and defined $Math::BigRat::downgrade and $rem->is_int
$wantarray defined $Math::BigRat::downgrade $rem->is_int
Truth table0%0/4
A and B and C
A= $wantarrayB= defined $Math::BigRat::downgradeC= $rem->is_int
ABCresult
0XX0
10X0
1100
1111
1024 if $wantarray && defined($downgrade) && $rem -> is_int();
1025 0 ▶ return $wantarray ? ($quo, $rem) : $quo;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
1026 }
1027  
1028 # Denominator (divisor) is +/-inf. This is handled the same way as in
1029 # Math::BigFloat -> bdiv(). See the comments in the code implementing that
1030 # method.
1031  
1032 71 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 2 69 71
1033 2 my ($quo, $rem);
1034 2 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 0 2 2
1035 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
1036 0 $rem = $x -> copy();
1037 0 $quo = $x -> bzero();
1038 } else {
1039 0 $rem = $class -> binf($y -> {sign});
1040 0 $quo = $x -> bone('-');
1041 }
1042 0 ▶ $quo = $downgrade -> new($quo)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $quo->is_int 0 0 0
Condition0%0/3
defined $Math::BigRat::downgrade and $quo->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigRat::downgrade and $quo->is_int
defined $Math::BigRat::downgrade $quo->is_int
Truth table0%0/3
A and B
A= defined $Math::BigRat::downgradeB= $quo->is_int
ABresult
0X0
100
111
1043 if defined($downgrade) && $quo -> is_int();
1044 0 ▶ $rem = $downgrade -> new($rem)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $rem->is_int 0 0 0
Condition0%0/3
defined $Math::BigRat::downgrade and $rem->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigRat::downgrade and $rem->is_int
defined $Math::BigRat::downgrade $rem->is_int
Truth table0%0/3
A and B
A= defined $Math::BigRat::downgradeB= $rem->is_int
ABresult
0X0
100
111
1045 if defined($downgrade) && $rem -> is_int();
1046 0 return ($quo, $rem);
1047 } else {
1048 2 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 2 0 2
1049 2 ▶ if ($x -> is_nan() || $x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $x->is_inf) { } 0 2 2
Condition33%1/3
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
002
MC/DC0%0/2
$x->is_nan or $x->is_inf
$x->is_nan $x->is_inf
Truth table33%1/3
A or B
A= $x->is_nanB= $x->is_inf
ABresult
1X1
011
000
1050 0 ▶ return $downgrade -> bnan() if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 0 0 0
1051 0 return $x -> bnan();
1052 } else {
1053 2 ▶ return $downgrade -> bzero() if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 0 2 2
1054 2 return $x -> bzero();
1055 }
1056 }
1057 }
1058 }
1059  
1060 # At this point, both the numerator and denominator are finite numbers, and
1061 # the denominator (divisor) is non-zero.
1062  
1063 # x == 0?
1064 69 ▶ if ($x->is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 3 66 69
1065 3 ▶ return $wantarray ? ($downgrade -> bzero(), $downgrade -> bzero())
Branch: 2 branches
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 0 3 3
$wantarray ? : 0 0 0
1066 : $downgrade -> bzero() if defined $downgrade;
1067 3 ▶ return $wantarray ? ($x, $class->bzero()) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 1 2 3
1068 }
1069  
1070 # XXX TODO: list context, upgrade
1071 # According to Knuth, this can be optimized by doing gcd twice (for d and n)
1072 # and reducing in one step. This would save us the bnorm() at the end.
1073 #
1074 # p r p * s (p / gcd(p, r)) * (s / gcd(s, q))
1075 # - / - = ----- = ---------------------------------
1076 # q s q * r (q / gcd(s, q)) * (r / gcd(p, r))
1077  
1078 66 $x->{_n} = $LIB->_mul($x->{_n}, $y->{_d});
1079 66 $x->{_d} = $LIB->_mul($x->{_d}, $y->{_n});
1080  
1081 # compute new sign
1082 66 ▶ $x->{sign} = $x->{sign} eq $y->{sign} ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $y->{'sign'} ? : 54 12 66
1083  
1084 66 $x -> bnorm();
1085 66 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 6 60 66
1086 6 my $rem = $x -> copy();
1087 6 $x = $x -> bfloor();
1088 6 $x = $x -> round(@r);
1089 6 $rem = $rem -> bsub($x -> copy()) -> bmul($y);
1090 6 ▶ $x = $downgrade -> new($x) if defined($downgrade) && $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $x->is_int 0 6 6
Condition33%1/3
defined $Math::BigRat::downgrade and $x->is_int
!ll&&!rl&&r
600
MC/DC0%0/2
defined $Math::BigRat::downgrade and $x->is_int
defined $Math::BigRat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigRat::downgradeB= $x->is_int
ABresult
0X0
100
111
1091 6 ▶ $rem = $downgrade -> new($rem) if defined($downgrade) && $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $rem->is_int 0 6 6
Condition33%1/3
defined $Math::BigRat::downgrade and $rem->is_int
!ll&&!rl&&r
600
MC/DC0%0/2
defined $Math::BigRat::downgrade and $rem->is_int
defined $Math::BigRat::downgrade $rem->is_int
Truth table33%1/3
A and B
A= defined $Math::BigRat::downgradeB= $rem->is_int
ABresult
0X0
100
111
1092 6 return $x, $rem;
1093 } else {
1094 60 return $x -> round(@r);
1095 }
1096 }
1097  
1098 sub bmod {
1099 # compute "remainder" (in Perl way) of $x / $y
1100  
1101 # set up parameters
1102 21 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine bmod: called CC=17 SCAR=43.9
Pod documented
1103 # objectify is costly, so avoid it
1104 21 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 0 21 21
Condition33%1/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
0021
MC/DC0%0/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table33%1/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
1105 0 ($class, $x, $y, @r) = objectify(2, @_);
1106 }
1107  
1108 21 ▶ return $x if $x->modify('bmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmod") 0 21 21
1109  
1110 # At least one argument is NaN. This is handled the same way as in
1111 # Math::BigInt -> bmod().
1112  
1113 21 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 2 19 21
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1119
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
1114 2 return $x -> bnan();
1115 }
1116  
1117 # Modulo zero. This is handled the same way as in Math::BigInt -> bmod().
1118  
1119 19 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 0 19 19
1120 0 ▶ return $downgrade -> bzero() if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 0 0 0
1121 0 return $x;
1122 }
1123  
1124 # Numerator (dividend) is +/-inf. This is handled the same way as in
1125 # Math::BigInt -> bmod().
1126  
1127 19 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 19 19
1128 0 return $x -> bnan();
1129 }
1130  
1131 # Denominator (divisor) is +/-inf. This is handled the same way as in
1132 # Math::BigInt -> bmod().
1133  
1134 19 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 0 19 19
1135 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
1136 0 ▶ return $downgrade -> new($x) if defined($downgrade) && $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $x->is_int 0 0 0
Condition0%0/3
defined $Math::BigRat::downgrade and $x->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigRat::downgrade and $x->is_int
defined $Math::BigRat::downgrade $x->is_int
Truth table0%0/3
A and B
A= defined $Math::BigRat::downgradeB= $x->is_int
ABresult
0X0
100
111
1137 0 return $x;
1138 } else {
1139 0 ▶ return $downgrade -> binf($y -> sign()) if defined($downgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 0 0 0
1140 0 return $x -> binf($y -> sign());
1141 }
1142 }
1143  
1144 # At this point, both the numerator and denominator are finite numbers, and
1145 # the denominator (divisor) is non-zero.
1146  
1147 19 ▶ if ($x->is_zero()) { # 0 / 7 = 0, mod 0
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 0 19 19
1148 0 ▶ return $downgrade -> bzero() if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 0 0 0
1149 0 return $x;
1150 }
1151  
1152 # Compute $x - $y * floor($x/$y). This can probably be optimized by working
1153 # on a lower level.
1154  
1155 19 $x -> bsub($x -> copy() -> bdiv($y) -> bfloor() -> bmul($y));
1156 19 return $x -> round(@r);
1157 }
1158  
1159 ##############################################################################
1160 # bdec/binc
1161  
1162 sub bdec {
1163 # decrement value (subtract 1)
1164 12 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bdec: called CC=6 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 12 0 12
1165  
1166 12 ▶ if ($x->{sign} !~ /^[+-]$/) { # NaN, inf, -inf
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 3 9 12
1167 3 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 2 1 3
1168 1 return $x;
1169 }
1170  
1171 9 ▶ if ($x->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '-') { } 1 8 9
1172 1 $x->{_n} = $LIB->_add($x->{_n}, $x->{_d}); # -5/2 => -7/2
1173 } else {
1174 8 ▶ if ($LIB->_acmp($x->{_n}, $x->{_d}) < 0) # n < d?
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($x->{'_n'}, $x->{'_d'}) < 0) { } 3 5 8
1175 {
1176 # 1/3 -- => -2/3
1177 3 $x->{_n} = $LIB->_sub($LIB->_copy($x->{_d}), $x->{_n});
1178 3 $x->{sign} = '-';
1179 } else {
1180 5 $x->{_n} = $LIB->_sub($x->{_n}, $x->{_d}); # 5/2 => 3/2
1181 }
1182 }
1183 9 $x->bnorm()->round(@r);
1184 }
1185  
1186 sub binc {
1187 # increment value (add 1)
1188 13 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine binc: called CC=6 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 13 0 13
1189  
1190 13 ▶ if ($x->{sign} !~ /^[+-]$/) { # NaN, inf, -inf
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 3 10 13
1191 3 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 2 1 3
1192 1 return $x;
1193 }
1194  
1195 10 ▶ if ($x->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '-') { } 3 7 10
1196 3 ▶ if ($LIB->_acmp($x->{_n}, $x->{_d}) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($x->{'_n'}, $x->{'_d'}) < 0) { } 2 1 3
1197 # -1/3 ++ => 2/3 (overflow at 0)
1198 2 $x->{_n} = $LIB->_sub($LIB->_copy($x->{_d}), $x->{_n});
1199 2 $x->{sign} = '+';
1200 } else {
1201 1 $x->{_n} = $LIB->_sub($x->{_n}, $x->{_d}); # -5/2 => -3/2
1202 }
1203 } else {
1204 7 $x->{_n} = $LIB->_add($x->{_n}, $x->{_d}); # 5/2 => 7/2
1205 }
1206 10 $x->bnorm()->round(@r);
1207 }
1208  
1209 sub binv {
1210 20 ▶ my $x = shift;
Subroutine binv: called CC=5 SCAR=16.1
Pod documented
1211 20 my @r = @_;
1212  
1213 20 ▶ return $x if $x->modify('binv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("binv") 0 20 20
1214  
1215 20 ▶ return $x if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 2 18 20
1216 18 ▶ return $x -> bzero() if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 4 14 18
1217 14 ▶ return $x -> binf("+") if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 2 12 14
1218  
1219 12 ($x -> {_n}, $x -> {_d}) = ($x -> {_d}, $x -> {_n});
1220 12 $x -> round(@r);
1221 }
1222  
1223 ##############################################################################
1224 # is_foo methods (the rest is inherited)
1225  
1226 sub is_int {
1227 # return true if arg (BRAT or num_str) is an integer
1228 9830 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine is_int: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 9830 0 9830
1229  
1230 return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't
1231 9830 ▶ $LIB->_is_one($x->{_d}); # x/y && y != 1 => no integer
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^[+-]$/ and $LIB->_is_one($x->{"_d"}) 9794 36 9830
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $LIB->_is_one($x->{"_d"})
!ll&&!rl&&r
10269794
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]$/ and $LIB->_is_one($x->{"_d"})
$x->{"sign"} =~ /^[+-]$/ $LIB->_is_one($x->{"_d"})
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $LIB->_is_one($x->{"_d"})
ABresult
0X0
100
111
1232 36 0;
1233 }
1234  
1235 sub is_zero {
1236 # return true if arg (BRAT or num_str) is zero
1237 649 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine is_zero: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 649 0 649
1238  
1239 649 ▶ 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"}) 73 576 649
Condition100%3/3
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_n"})
!ll&&!rl&&r
15941773
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
1240 576 0;
1241 }
1242  
1243 sub is_one {
1244 # return true if arg (BRAT or num_str) is +1 or -1 if signis given
1245 311 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine is_one: called CC=8 SCAR=20.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 311 0 311
1246  
1247 311 ▶ croak "too many arguments for is_one()" if @_ > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 311 311
1248 311 ▶ my $sign = $_[1] || '';
Condition100%2/2
$_[1] || ''
l!l
198113
MC/DC100%1/1
$_[1] || ''
$_[1]
Truth table100%2/2
A
A= $_[1]
Aresult
11
00
1249 311 ▶ $sign = '+' if $sign ne '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign ne "-" 218 93 311
1250 return 1 if ($x->{sign} eq $sign &&
1251 311 ▶ $LIB->_is_one($x->{_n}) && $LIB->_is_one($x->{_d}));
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $sign and $LIB->_is_one($x->{"_n"}) and $LIB->_is_one($x->{"_d"}) 37 274 311
Condition100%6/6
$x->{"sign"} eq $sign and $LIB->_is_one($x->{"_n"}) and $LIB->_is_one($x->{"_d"})
!ll&&!rl&&r
2532137
$x->{"sign"} eq $sign and $LIB->_is_one($x->{"_n"})
!ll&&!rl&&r
13411958
MC/DC100%3/3
$x->{"sign"} eq $sign and $LIB->_is_one($x->{"_n"}) and $LIB->_is_one($x->{"_d"})
$x->{"sign"} eq $sign $LIB->_is_one($x->{"_n"}) $LIB->_is_one($x->{"_d"})
Truth table100%4/4
A and B and C
A= $x->{"sign"} eq $signB= $LIB->_is_one($x->{"_n"})C= $LIB->_is_one($x->{"_d"})
ABCresult
0XX0
10X0
1100
1111
1252 274 0;
1253 }
1254  
1255 sub is_odd {
1256 # return true if arg (BFLOAT or num_str) is odd or false if even
1257 25 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine is_odd: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 25 0 25
1258  
1259 return 1 if ($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
1260 25 ▶ ($LIB->_is_one($x->{_d}) && $LIB->_is_odd($x->{_n})); # x/2 is not, but 3/1
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^[+-]$/ and ($LIB->_is_one($x->{"_d"}) and $LIB->_is_odd($x->{"_n"})) 10 15 25
Condition100%6/6
$x->{"sign"} =~ /^[+-]$/ and ($LIB->_is_one($x->{"_d"}) and $LIB->_is_odd($x->{"_n"}))
!ll&&!rl&&r
41011
$LIB->_is_one($x->{"_d"}) and $LIB->_is_odd($x->{"_n"})
!ll&&!rl&&r
4710
MC/DC66%2/3
$x->{"sign"} =~ /^[+-]$/ and ($LIB->_is_one($x->{"_d"}) and $LIB->_is_odd($x->{"_n"}))
$x->{"sign"} =~ /^[+-]$/ $LIB->_is_one($x->{"_d"}) $LIB->_is_odd($x->{"_n"})
Truth table75%3/4
A and B and C
A= $x->{"sign"} =~ /^[+-]$/B= $LIB->_is_one($x->{"_d"})C= $LIB->_is_odd($x->{"_n"})
ABCresult
0XX0
10X0
1100
1111
1261 15 0;
1262 }
1263  
1264 sub is_even {
1265 # return true if arg (BINT or num_str) is even or false if odd
1266 18 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine is_even: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 18 0 18
1267  
1268 18 ▶ return 0 if $x->{sign} !~ /^[+-]$/; # NaN & +-inf aren't
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 3 15 18
1269 return 1 if ($LIB->_is_one($x->{_d}) # x/3 is never
1270 15 ▶ && $LIB->_is_even($x->{_n})); # but 4/1 is
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_one($x->{"_d"}) and $LIB->_is_even($x->{"_n"}) 6 9 15
Condition100%3/3
$LIB->_is_one($x->{"_d"}) and $LIB->_is_even($x->{"_n"})
!ll&&!rl&&r
456
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
1271 9 0;
1272 }
1273  
1274 ##############################################################################
1275 # parts() and friends
1276  
1277 sub numerator {
1278 39 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine numerator: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 39 0 39
1279  
1280 # NaN, inf, -inf
1281 39 ▶ return Math::BigInt->new($x->{sign}) if ($x->{sign} !~ /^[+-]$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 15 24 39
1282  
1283 24 my $n = Math::BigInt->new($LIB->_str($x->{_n}));
1284 24 $n->{sign} = $x->{sign};
1285 24 $n;
1286 }
1287  
1288 sub denominator {
1289 35 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine denominator: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 35 0 35
1290  
1291 # NaN
1292 35 ▶ return Math::BigInt->new($x->{sign}) if $x->{sign} eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "NaN" 9 26 35
1293 # inf, -inf
1294 26 ▶ return Math::BigInt->bone() if $x->{sign} !~ /^[+-]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 6 20 26
1295  
1296 20 Math::BigInt->new($LIB->_str($x->{_d}));
1297 }
1298  
1299 sub parts {
1300 11 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine parts: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 11 0 11
1301  
1302 11 my $c = 'Math::BigInt';
1303  
1304 11 ▶ return ($c->bnan(), $c->bnan()) if $x->{sign} eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "NaN" 1 10 11
1305 10 ▶ return ($c->binf(), $c->binf()) if $x->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 1 9 10
1306 9 ▶ return ($c->binf('-'), $c->binf()) if $x->{sign} eq '-inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-inf" 1 8 9
1307  
1308 8 my $n = $c->new($LIB->_str($x->{_n}));
1309 8 $n->{sign} = $x->{sign};
1310 8 my $d = $c->new($LIB->_str($x->{_d}));
1311 8 ($n, $d);
1312 }
1313  
1314 sub dparts {
1315 16 ▶ my $x = shift;
Subroutine dparts: called CC=7 SCAR=19.5
Pod documented
1316 16 my $class = ref $x;
1317  
1318 16 ▶ croak("dparts() is an instance method") unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 16 16
1319  
1320 16 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 2 14 16
1321 2 ▶ return $class -> bnan(), $class -> bnan() if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 1 1 2
1322 1 return $class -> bnan();
1323 }
1324  
1325 14 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 4 10 14
1326 4 ▶ return $class -> binf($x -> sign()), $class -> bzero() if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 2 2 4
1327 2 return $class -> binf($x -> sign());
1328 }
1329  
1330 # 355/113 => 3 + 16/113
1331  
1332 10 my ($q, $r) = $LIB -> _div($LIB -> _copy($x -> {_n}), $x -> {_d});
1333  
1334 10 my $int = Math::BigRat -> new($x -> {sign} . $LIB -> _str($q));
1335 10 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 5 5 10
1336  
1337 my $frc = Math::BigRat -> new($x -> {sign} . $LIB -> _str($r),
1338 5 $LIB -> _str($x -> {_d}));
1339  
1340 5 return $int, $frc;
1341 }
1342  
1343 sub fparts {
1344 14 ▶ my $x = shift;
Subroutine fparts: called CC=3 SCAR=11.0
Pod documented
1345 14 my $class = ref $x;
1346  
1347 14 ▶ croak("fparts() is an instance method") unless $class;
Branch: 1 branch
BranchTrueFalseTotal
unless $class 0 14 14
1348  
1349 14 ▶ return ($class -> bnan(),
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 1 13 14
1350 $class -> bnan()) if $x -> is_nan();
1351  
1352 13 my $numer = $x -> copy();
1353 13 my $denom = $class -> bzero();
1354  
1355 13 $denom -> {_n} = $numer -> {_d};
1356 13 $numer -> {_d} = $LIB -> _one();
1357  
1358 13 return ($numer, $denom);
1359 }
1360  
1361 sub length {
1362 5 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine length: called CC=3 SCAR=11.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 0 5
1363  
1364 5 ▶ return $nan unless $x->is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 5 5
1365 5 $LIB->_len($x->{_n}); # length(-123/1) => length(123)
1366 }
1367  
1368 sub digit {
1369 11 ▶ my ($class, $x, $n) = ref($_[0]) ? (undef, $_[0], $_[1]) : objectify(1, @_);
Subroutine digit: called CC=4 SCAR=14.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 11 0 11
1370  
1371 11 ▶ return $nan unless $x->is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 11 11
1372 11 ▶ $LIB->_digit($x->{_n}, $n || 0); # digit(-123/1, 2) => digit(123, 2)
Condition100%2/2
$n || 0
l!l
92
MC/DC100%1/1
$n || 0
$n
Truth table100%2/2
A
A= $n
Aresult
11
00
1373 }
1374  
1375 ##############################################################################
1376 # special calc routines
1377  
1378 sub bceil {
1379 24 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine bceil: called CC=9 SCAR=22.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 24 0 24
1380  
1381 24 ▶ if ($x->{sign} !~ /^[+-]$/ || # NaN or inf or
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->{"sign"} =~ /^[+-]$/ or $LIB->_is_one($x->{"_d"})) 10 14 24
Condition100%3/3
not $x->{"sign"} =~ /^[+-]$/ or $LIB->_is_one($x->{"_d"})
l!l&&r!l&&!r
5514
MC/DC100%2/2
not $x->{"sign"} =~ /^[+-]$/ or $LIB->_is_one($x->{"_d"})
not $x->{"sign"} =~ /^[+-]$/ $LIB->_is_one($x->{"_d"})
Truth table100%3/3
not(A) or B
A= not $x->{"sign"} =~ /^[+-]$/B= $LIB->_is_one($x->{"_d"})
ABresult
1X1
011
000
1382 $LIB->_is_one($x->{_d})) # integer
1383 {
1384 10 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 2 8 10
1385 8 return $x;
1386 }
1387  
1388 14 $x->{_n} = $LIB->_div($x->{_n}, $x->{_d}); # 22/7 => 3/1 w/ truncate
1389 14 $x->{_d} = $LIB->_one(); # d => 1
1390 14 ▶ $x->{_n} = $LIB->_inc($x->{_n}) if $x->{sign} eq '+'; # +22/7 => 4/1
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+" 8 6 14
1391 14 ▶ $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"}) 2 12 14
Condition100%3/3
$x->{"sign"} eq "-" and $LIB->_is_zero($x->{"_n"})
!ll&&!rl&&r
842
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
1392 14 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 1 13 14
1393 13 $x;
1394 }
1395  
1396 sub bfloor {
1397 52 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine bfloor: called CC=7 SCAR=19.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 52 0 52
1398  
1399 52 ▶ if ($x->{sign} !~ /^[+-]$/ || # NaN or inf or
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->{"sign"} =~ /^[+-]$/ or $LIB->_is_one($x->{"_d"})) 24 28 52
Condition100%3/3
not $x->{"sign"} =~ /^[+-]$/ or $LIB->_is_one($x->{"_d"})
l!l&&r!l&&!r
51928
MC/DC100%2/2
not $x->{"sign"} =~ /^[+-]$/ or $LIB->_is_one($x->{"_d"})
not $x->{"sign"} =~ /^[+-]$/ $LIB->_is_one($x->{"_d"})
Truth table100%3/3
not(A) or B
A= not $x->{"sign"} =~ /^[+-]$/B= $LIB->_is_one($x->{"_d"})
ABresult
1X1
011
000
1400 $LIB->_is_one($x->{_d})) # integer
1401 {
1402 24 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 2 22 24
1403 22 return $x;
1404 }
1405  
1406 28 $x->{_n} = $LIB->_div($x->{_n}, $x->{_d}); # 22/7 => 3/1 w/ truncate
1407 28 $x->{_d} = $LIB->_one(); # d => 1
1408 28 ▶ $x->{_n} = $LIB->_inc($x->{_n}) if $x->{sign} eq '-'; # -22/7 => -4/1
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 11 17 28
1409 28 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 1 27 28
1410 27 $x;
1411 }
1412  
1413 sub bint {
1414 963 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine bint: called CC=8 SCAR=20.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 963 0 963
1415  
1416 963 ▶ if ($x->{sign} !~ /^[+-]$/ || # NaN or inf or
Branch: 1 branch
BranchTrueFalseTotal
if (not $x->{"sign"} =~ /^[+-]$/ or $LIB->_is_one($x->{"_d"})) 552 411 963
Condition100%3/3
not $x->{"sign"} =~ /^[+-]$/ or $LIB->_is_one($x->{"_d"})
l!l&&r!l&&!r
24528411
MC/DC100%2/2
not $x->{"sign"} =~ /^[+-]$/ or $LIB->_is_one($x->{"_d"})
not $x->{"sign"} =~ /^[+-]$/ $LIB->_is_one($x->{"_d"})
Truth table100%3/3
not(A) or B
A= not $x->{"sign"} =~ /^[+-]$/B= $LIB->_is_one($x->{"_d"})
ABresult
1X1
011
000
1417 $LIB->_is_one($x->{_d})) # integer
1418 {
1419 552 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 2 550 552
1420 550 return $x;
1421 }
1422  
1423 411 $x->{_n} = $LIB->_div($x->{_n}, $x->{_d}); # 22/7 => 3/1 w/ truncate
1424 411 $x->{_d} = $LIB->_one(); # d => 1
1425 411 ▶ $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 410 411
Condition66%2/3
$x->{"sign"} eq "-" and $LIB->_is_zero($x->{"_n"})
!ll&&!rl&&r
41001
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
1426 411 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade 1 410 411
1427 410 return $x;
1428 }
1429  
1430 sub bfac {
1431 13 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bfac: called CC=4 SCAR=14.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 13 0 13
1432  
1433 # if $x is not an integer
1434 13 ▶ if (($x->{sign} ne '+') || (!$LIB->_is_one($x->{_d}))) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" or not $LIB->_is_one($x->{"_d"})) 3 10 13
Condition66%2/3
$x->{"sign"} ne "+" or not $LIB->_is_one($x->{"_d"})
l!l&&r!l&&!r
3010
MC/DC50%1/2
$x->{"sign"} ne "+" or not $LIB->_is_one($x->{"_d"})
$x->{"sign"} ne "+" not $LIB->_is_one($x->{"_d"})
Truth table66%2/3
A or not(B)
A= $x->{"sign"} ne "+"B= not $LIB->_is_one($x->{"_d"})
ABresult
1X1
011
000
1435 3 return $x->bnan();
1436 }
1437  
1438 10 $x->{_n} = $LIB->_fac($x->{_n});
1439 # since _d is 1, we don't need to reduce/norm the result
1440 10 $x->round(@r);
1441 }
1442  
1443 sub bpow {
1444 # power ($x ** $y)
1445  
1446 # set up parameters
1447 190 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine bpow: called CC=39 SCAR=36.6
Pod documented
1448  
1449 # objectify is costly, so avoid it
1450 190 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 6 184 190
Condition66%2/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
06184
MC/DC50%1/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table66%2/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
1451 6 ($class, $x, $y, @r) = objectify(2, @_);
1452 }
1453  
1454 190 ▶ return $x if $x->modify('bpow');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bpow") 0 190 190
1455  
1456 # $x and/or $y is a NaN
1457 190 ▶ return $x->bnan() if $x->is_nan() || $y->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 29 161 190
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1514161
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
1458  
1459 # $x and/or $y is a +/-Inf
1460 161 ▶ if ($x->is_inf("-")) {
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 13 148 161
elsif ($x->is_inf('+')) { } 13 135 148
elsif ($y->is_inf('-')) { } 11 124 135
elsif ($y->is_inf('+')) { } 11 113 124
1461 13 ▶ return $x->bzero() if $y->is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 6 7 13
1462 7 ▶ return $x->bnan() if $y->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 1 6 7
1463 6 ▶ return $x if $y->is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 2 4 6
1464 4 return $x->bneg();
1465 } elsif ($x->is_inf("+")) {
1466 13 ▶ return $x->bzero() if $y->is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 6 7 13
1467 7 ▶ return $x->bnan() if $y->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 1 6 7
1468 6 return $x;
1469 } elsif ($y->is_inf("-")) {
1470 11 ▶ return $x->bnan() if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 1 10 11
1471 10 ▶ return $x->binf("+") if $x > -1 && $x < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x > -1 and $x < 1 3 7 10
Condition100%3/3
$x > -1 and $x < 1
!ll&&!rl&&r
343
MC/DC0%0/2
$x > -1 and $x < 1
$x > -1 $x < 1
Truth table33%1/3
A and B
A= $x > -1B= $x < 1
ABresult
0X0
100
111
1472 7 ▶ return $x->bone() if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 1 6 7
1473 6 return $x->bzero();
1474 } elsif ($y->is_inf("+")) {
1475 11 ▶ return $x->bnan() if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 1 10 11
1476 10 ▶ return $x->bzero() if $x > -1 && $x < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x > -1 and $x < 1 3 7 10
Condition100%3/3
$x > -1 and $x < 1
!ll&&!rl&&r
343
MC/DC0%0/2
$x > -1 and $x < 1
$x > -1 $x < 1
Truth table33%1/3
A and B
A= $x > -1B= $x < 1
ABresult
0X0
100
111
1477 7 ▶ return $x->bone() if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 1 6 7
1478 6 return $x->binf("+");
1479 }
1480  
1481 113 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 11 102 113
1482 11 ▶ return $x -> bone() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 1 10 11
1483 10 ▶ return $x -> binf() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 5 5 10
1484 5 return $x;
1485 }
1486  
1487 # We don't support complex numbers, so upgrade or return NaN.
1488  
1489 102 ▶ if ($x -> is_negative() && !$y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_negative and not $y->is_int) 20 82 102
Condition100%3/3
$x->is_negative and not $y->is_int
!ll&&!rl&&r
473520
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
1490 20 ▶ return $upgrade -> bpow($upgrade -> new($x), $y, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::upgrade 0 20 20
1491 if defined $upgrade;
1492 20 return $x -> bnan();
1493 }
1494  
1495 82 ▶ if ($x -> is_one("+") || $y -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("+") or $y->is_one) 20 62 82
Condition100%3/3
$x->is_one("+") or $y->is_one
l!l&&r!l&&!r
11962
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
1496 20 return $x;
1497 }
1498  
1499 62 ▶ if ($x -> is_one("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("-")) 6 56 62
1500 6 ▶ return $x if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 3 3 6
1501 3 return $x -> bneg();
1502 }
1503  
1504 # (a/b)^-(c/d) = (b/a)^(c/d)
1505 56 ▶ ($x->{_n}, $x->{_d}) = ($x->{_d}, $x->{_n}) if $y->is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 24 32 56
1506  
1507 56 ▶ unless ($LIB->_is_one($y->{_n})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($y->{"_n"})) 47 9 56
1508 47 $x->{_n} = $LIB->_pow($x->{_n}, $y->{_n});
1509 47 $x->{_d} = $LIB->_pow($x->{_d}, $y->{_n});
1510 47 ▶ $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"}) 12 35 47
Condition100%3/3
$x->{"sign"} eq "-" and $LIB->_is_even($y->{"_n"})
!ll&&!rl&&r
27812
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
1511 }
1512  
1513 56 ▶ unless ($LIB->_is_one($y->{_d})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($y->{"_d"})) 2 54 56
1514 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
1515 1 return $x->broot($LIB->_str($y->{_d}), @r); # 1/N => root(N)
1516 }
1517  
1518 54 return $x->round(@r);
1519 }
1520  
1521 sub blog {
1522 # Return the logarithm of the operand. If a second operand is defined, that
1523 # value is used as the base, otherwise the base is assumed to be Euler's
1524 # constant.
1525  
1526 20 ▶ my ($class, $x, $base, @r);
Subroutine blog: called CC=33 SCAR=41.2
Pod documented
1527  
1528 # Don't objectify the base, since an undefined base, as in $x->blog() or
1529 # $x->blog(undef) signals that the base is Euler's number.
1530  
1531 20 ▶ if (!ref($_[0]) && $_[0] =~ /^[A-Za-z]|::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/) { } 0 20 20
Condition33%1/3
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
!ll&&!rl&&r
2000
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
1532 # E.g., Math::BigRat->blog(256, 2)
1533 0 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 0 0 0
1534 defined $_[2] ? objectify(2, @_) : objectify(1, @_);
1535 } else {
1536 # E.g., Math::BigRat::blog(256, 2) or $x->blog(2)
1537 20 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 10 10 20
1538 defined $_[1] ? objectify(2, @_) : objectify(1, @_);
1539 }
1540  
1541 20 ▶ return $x if $x->modify('blog');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blog") 0 20 20
1542  
1543 # Handle all exception cases and all trivial cases. I have used Wolfram Alpha
1544 # (http://www.wolframalpha.com) as the reference for these cases.
1545  
1546 20 ▶ return $x -> bnan() if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 15 20
1547  
1548 15 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 7 8 15
1549 7 ▶ $base = $class -> new($base) unless ref $base;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 7 7
1550 7 ▶ if ($base -> is_nan() || $base -> is_one()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($base->is_nan or $base->is_one) { } 2 5 7
elsif ($base->is_inf or $base->is_zero) { } 0 5 5
elsif ($base->is_negative) { } 2 3 5
Condition66%2/3
$base->is_nan or $base->is_one
l!l&&r!l&&!r
205
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
1551 2 return $x -> bnan();
1552 ▶ } elsif ($base -> is_inf() || $base -> is_zero()) {
Condition33%1/3
$base->is_inf or $base->is_zero
l!l&&r!l&&!r
005
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
1553 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
1554 0 return $x -> bzero();
1555 } elsif ($base -> is_negative()) { # -inf < base < 0
1556 2 ▶ return $x -> bzero() if $x -> is_one(); # x = 1
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 0 2 2
1557 2 ▶ return $x -> bone() if $x == $base; # x = base
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 0 2 2
1558 2 return $x -> bnan(); # otherwise
1559 }
1560 3 ▶ return $x -> bone() if $x == $base; # 0 < base && 0 < x < inf
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 0 3 3
1561 }
1562  
1563 # We now know that the base is either undefined or positive and finite.
1564  
1565 11 ▶ if ($x -> is_inf()) { # x = +/-inf
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 2 9 11
elsif ($x->is_neg) { } 2 7 9
elsif ($x->is_one) { } 1 6 7
elsif ($x->is_zero) { } 3 3 6
1566 2 ▶ my $sign = defined $base && $base < 1 ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
defined $base && $base < 1 ? : 0 2 2
Condition33%1/3
defined $base and $base < 1
!ll&&!rl&&r
200
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
1567 2 return $x -> binf($sign);
1568 } elsif ($x -> is_neg()) { # -inf < x < 0
1569 2 return $x -> bnan();
1570 } elsif ($x -> is_one()) { # x = 1
1571 1 return $x -> bzero();
1572 } elsif ($x -> is_zero()) { # x = 0
1573 3 ▶ my $sign = defined $base && $base < 1 ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
defined $base && $base < 1 ? : 0 3 3
Condition66%2/3
defined $base and $base < 1
!ll&&!rl&&r
210
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
1574 3 return $x -> binf($sign);
1575 }
1576  
1577 # Now take care of the cases where $x and/or $base is 1/N.
1578 #
1579 # log(1/N) / log(B) = -log(N)/log(B)
1580 # log(1/N) / log(1/B) = log(N)/log(B)
1581 # log(N) / log(1/B) = -log(N)/log(B)
1582  
1583 3 my $neg = 0;
1584 3 ▶ if ($x -> numerator() -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->numerator->is_one) 0 3 3
1585 0 $x -> binv();
1586 0 $neg = !$neg;
1587 }
1588 3 ▶ if (defined(blessed($base)) && $base -> isa($class)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($base) and $base->isa($class)) 2 1 3
Condition66%2/3
defined builtin::blessed($base) and $base->isa($class)
!ll&&!rl&&r
102
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
1589 2 ▶ if ($base -> numerator() -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($base->numerator->is_one) 0 2 2
1590 0 $base = $base -> copy() -> binv();
1591 0 $neg = !$neg;
1592 }
1593 }
1594  
1595 # disable upgrading and downgrading
1596  
1597 3 require Math::BigFloat;
1598 3 my $upg = Math::BigFloat -> upgrade();
1599 3 my $dng = Math::BigFloat -> downgrade();
1600 3 Math::BigFloat -> upgrade(undef);
1601 3 Math::BigFloat -> downgrade(undef);
1602  
1603 # At this point we are done handling all exception cases and trivial cases.
1604  
1605 3 ▶ $base = Math::BigFloat -> new($base) if defined $base;
Branch: 1 branch
BranchTrueFalseTotal
if defined $base 2 1 3
1606 3 my $xnum = Math::BigFloat -> new($LIB -> _str($x->{_n}));
1607 3 my $xden = Math::BigFloat -> new($LIB -> _str($x->{_d}));
1608 3 my $xstr = $xnum -> bdiv($xden) -> blog($base, @r) -> bsstr();
1609  
1610 # reset upgrading and downgrading
1611  
1612 3 Math::BigFloat -> upgrade($upg);
1613 3 Math::BigFloat -> downgrade($dng);
1614  
1615 3 my $xobj = Math::BigRat -> new($xstr);
1616 3 $x -> {sign} = $xobj -> {sign};
1617 3 $x -> {_n} = $xobj -> {_n};
1618 3 $x -> {_d} = $xobj -> {_d};
1619  
1620 3 ▶ return $neg ? $x -> bneg() : $x;
Branch: 1 branch
BranchTrueFalseTotal
$neg ? : 0 3 3
1621 }
1622  
1623 sub bexp {
1624 # set up parameters
1625 1 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine bexp: called CC=14 SCAR=36.2
Pod documented
1626  
1627 # objectify is costly, so avoid it
1628 1 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 1 0 1
Condition33%1/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
010
MC/DC0%0/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table33%1/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
1629 1 ($class, $x, $y, @r) = objectify(1, @_);
1630 }
1631  
1632 1 ▶ return $x->binf(@r) if $x->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 0 1 1
1633 1 ▶ return $x->bzero(@r) if $x->{sign} eq '-inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-inf" 0 1 1
1634  
1635 # we need to limit the accuracy to protect against overflow
1636 1 my $fallback = 0;
1637 1 my ($scale, @params);
1638 1 ($x, @params) = $x->_find_round_parameters(@r);
1639  
1640 # also takes care of the "error in _find_round_parameters?" case
1641 1 ▶ return $x if $x->{sign} eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "NaN" 0 1 1
1642  
1643 # no rounding at all, so must use fallback
1644 1 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 1 0 1
1645 # simulate old behaviour
1646 1 $params[0] = $class->div_scale(); # and round to it as accuracy
1647 1 $params[1] = undef; # P = undef
1648 1 $scale = $params[0]+4; # at least four more for proper round
1649 1 $params[2] = $r[2]; # round mode by caller or undef
1650 1 $fallback = 1; # to clear a/p afterwards
1651 } else {
1652 # the 4 below is empirical, and there might be cases where it's not enough...
1653 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
1654 }
1655  
1656 1 ▶ return $x->bone(@params) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 1 1
1657  
1658 # See the comments in Math::BigFloat on how this algorithm works.
1659 # Basically we calculate A and B (where B is faculty(N)) so that A/B = e
1660  
1661 1 my $x_org = $x->copy();
1662 1 ▶ if ($scale <= 75) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= 75) { } 1 0 1
1663 # set $x directly from a cached string form
1664 $x->{_n} =
1665 1 $LIB->_new("90933395208605785401971970164779391644753259799242");
1666 $x->{_d} =
1667 1 $LIB->_new("33452526613163807108170062053440751665152000000000");
1668 1 $x->{sign} = '+';
1669 } else {
1670 # compute A and B so that e = A / B.
1671  
1672 # After some terms we end up with this, so we use it as a starting point:
1673 0 my $A = $LIB->_new("90933395208605785401971970164779391644753259799242");
1674 0 my $F = $LIB->_new(42); my $step = 42;
1675  
1676 # Compute how many steps we need to take to get $A and $B sufficiently big
1677 0 my $steps = Math::BigFloat::_len_to_steps($scale - 4);
1678 # print STDERR "# Doing $steps steps for ", $scale-4, " digits\n";
1679 0 ▶ while ($step++ <= $steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step++ <= $steps) 0 0 0
1680 # calculate $a * $f + 1
1681 0 $A = $LIB->_mul($A, $F);
1682 0 $A = $LIB->_inc($A);
1683 # increment f
1684 0 $F = $LIB->_inc($F);
1685 }
1686 # compute $B as factorial of $steps (this is faster than doing it manually)
1687 0 my $B = $LIB->_fac($LIB->_new($steps));
1688  
1689 # print "A ", $LIB->_str($A), "\nB ", $LIB->_str($B), "\n";
1690  
1691 0 $x->{_n} = $A;
1692 0 $x->{_d} = $B;
1693 0 $x->{sign} = '+';
1694 }
1695  
1696 # $x contains now an estimate of e, with some surplus digits, so we can round
1697 1 ▶ if (!$x_org->is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $x_org->is_one) { } 1 0 1
1698 # raise $x to the wanted power and round it in one step:
1699 1 $x->bpow($x_org, @params);
1700 } else {
1701 # else just round the already computed result
1702 0 delete $x->{_a}; delete $x->{_p};
1703 # shortcut to not run through _find_round_parameters again
1704 0 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 0 0 0
1705 0 $x->bround($params[0], $params[2]); # then round accordingly
1706 } else {
1707 0 $x->bfround($params[1], $params[2]); # then round accordingly
1708 }
1709 }
1710 1 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 1 0 1
1711 # clear a/p after round, since user did not request it
1712 1 delete $x->{_a}; delete $x->{_p};
1713 }
1714  
1715 1 $x;
1716 }
1717  
1718 sub bnok {
1719 # set up parameters
1720 4956 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine bnok: called CC=9 SCAR=22.5
Pod documented
1721  
1722 # objectify is costly, so avoid it
1723 4956 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 2478 2478 4956
Condition66%2/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
024782478
MC/DC50%1/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table66%2/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
1724 2478 ($class, $x, $y, @r) = objectify(2, @_);
1725 }
1726  
1727 4956 ▶ return $x->bnan() if $x->is_nan() || $y->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 24 4932 4956
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1684932
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
1728 4932 ▶ 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 4932 4932
Condition55%5/9
$x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int
l!l&&r!l&&!r
004932
$x->is_finite and not $x->is_int
!ll&&!rl&&r
7248600
$y->is_finite and not $y->is_int
!ll&&!rl&&r
7248600
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 table28%2/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
1729 ($y->is_finite() && !$y->is_int()));
1730  
1731 4932 my $xint = Math::BigInt -> new($x -> bstr());
1732 4932 my $yint = Math::BigInt -> new($y -> bstr());
1733 4932 $xint -> bnok($yint);
1734 4932 my $xrat = Math::BigRat -> new($xint);
1735  
1736 4932 $x -> {sign} = $xrat -> {sign};
1737 4932 $x -> {_n} = $xrat -> {_n};
1738 4932 $x -> {_d} = $xrat -> {_d};
1739  
1740 4932 return $x;
1741 }
1742  
1743 sub broot {
1744 # set up parameters
1745 7 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine broot: called CC=3 SCAR=11.0
Pod documented
1746 # objectify is costly, so avoid it
1747 7 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 5 2 7
Condition66%2/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
052
MC/DC50%1/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table66%2/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
1748 5 ($class, $x, $y, @r) = objectify(2, @_);
1749 }
1750  
1751 # Convert $x into a Math::BigFloat.
1752  
1753 7 my $xd = Math::BigFloat -> new($LIB -> _str($x->{_d}));
1754 7 my $xflt = Math::BigFloat -> new($LIB -> _str($x->{_n})) -> bdiv($xd);
1755 7 $xflt -> {sign} = $x -> {sign};
1756  
1757 # Convert $y into a Math::BigFloat.
1758  
1759 7 my $yd = Math::BigFloat -> new($LIB -> _str($y->{_d}));
1760 7 my $yflt = Math::BigFloat -> new($LIB -> _str($y->{_n})) -> bdiv($yd);
1761 7 $yflt -> {sign} = $y -> {sign};
1762  
1763 # Compute the root and convert back to a Math::BigRat.
1764  
1765 7 $xflt -> broot($yflt, @r);
1766 7 my $xtmp = Math::BigRat -> new($xflt -> bsstr());
1767  
1768 7 $x -> {sign} = $xtmp -> {sign};
1769 7 $x -> {_n} = $xtmp -> {_n};
1770 7 $x -> {_d} = $xtmp -> {_d};
1771  
1772 7 return $x;
1773 }
1774  
1775 sub bmodpow {
1776 # set up parameters
1777 19 ▶ my ($class, $x, $y, $m, @r) = (ref($_[0]), @_);
Subroutine bmodpow: called CC=3 SCAR=11.4
Pod documented
1778 # objectify is costly, so avoid it
1779 19 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 0 19 19
Condition33%1/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
0019
MC/DC0%0/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table33%1/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
1780 0 ($class, $x, $y, $m, @r) = objectify(3, @_);
1781 }
1782  
1783 # Convert $x, $y, and $m into Math::BigInt objects.
1784  
1785 19 my $xint = Math::BigInt -> new($x -> copy() -> bint());
1786 19 my $yint = Math::BigInt -> new($y -> copy() -> bint());
1787 19 my $mint = Math::BigInt -> new($m -> copy() -> bint());
1788  
1789 19 $xint -> bmodpow($yint, $mint, @r);
1790 19 my $xtmp = Math::BigRat -> new($xint -> bsstr());
1791  
1792 19 $x -> {sign} = $xtmp -> {sign};
1793 19 $x -> {_n} = $xtmp -> {_n};
1794 19 $x -> {_d} = $xtmp -> {_d};
1795 19 return $x;
1796 }
1797  
1798 sub bmodinv {
1799 # set up parameters
1800 17 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine bmodinv: called CC=3 SCAR=11.4
Pod documented
1801 # objectify is costly, so avoid it
1802 17 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 0 17 17
Condition33%1/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
0017
MC/DC0%0/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table33%1/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
1803 0 ($class, $x, $y, @r) = objectify(2, @_);
1804 }
1805  
1806 # Convert $x and $y into Math::BigInt objects.
1807  
1808 17 my $xint = Math::BigInt -> new($x -> copy() -> bint());
1809 17 my $yint = Math::BigInt -> new($y -> copy() -> bint());
1810  
1811 17 $xint -> bmodinv($yint, @r);
1812 17 my $xtmp = Math::BigRat -> new($xint -> bsstr());
1813  
1814 17 $x -> {sign} = $xtmp -> {sign};
1815 17 $x -> {_n} = $xtmp -> {_n};
1816 17 $x -> {_d} = $xtmp -> {_d};
1817 17 return $x;
1818 }
1819  
1820 sub bsqrt {
1821 20 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bsqrt: called CC=8 SCAR=20.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 20 0 20
1822  
1823 20 ▶ return $x->bnan() if $x->{sign} !~ /^[+]/; # NaN, -inf or < 0
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+]/ 4 16 20
1824 16 ▶ return $x if $x->{sign} eq '+inf'; # sqrt(inf) == inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 1 15 16
1825 15 ▶ return $x->round(@r) if $x->is_zero() || $x->is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one 2 13 15
Condition100%3/3
$x->is_zero or $x->is_one
l!l&&r!l&&!r
1113
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
1826  
1827 13 my $n = $x -> {_n};
1828 13 my $d = $x -> {_d};
1829  
1830 # Look for an exact solution. For the numerator and the denominator, take
1831 # the square root and square it and see if we got the original value. If we
1832 # did, for both the numerator and the denominator, we have an exact
1833 # solution.
1834  
1835 {
1836 13 my $nsqrt = $LIB -> _sqrt($LIB -> _copy($n));
1837 13 my $n2 = $LIB -> _mul($LIB -> _copy($nsqrt), $nsqrt);
1838 13 ▶ if ($LIB -> _acmp($n, $n2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($n, $n2) == 0) 11 2 13
1839 11 my $dsqrt = $LIB -> _sqrt($LIB -> _copy($d));
1840 11 my $d2 = $LIB -> _mul($LIB -> _copy($dsqrt), $dsqrt);
1841 11 ▶ if ($LIB -> _acmp($d, $d2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($d, $d2) == 0) 10 1 11
1842 10 $x -> {_n} = $nsqrt;
1843 10 $x -> {_d} = $dsqrt;
1844 10 return $x->round(@r);
1845 }
1846 }
1847 }
1848  
1849 3 local $Math::BigFloat::upgrade = undef;
1850 3 local $Math::BigFloat::downgrade = undef;
1851 3 local $Math::BigFloat::precision = undef;
1852 3 local $Math::BigFloat::accuracy = undef;
1853 3 local $Math::BigInt::upgrade = undef;
1854 3 local $Math::BigInt::precision = undef;
1855 3 local $Math::BigInt::accuracy = undef;
1856  
1857 3 my $xn = Math::BigFloat -> new($LIB -> _str($n));
1858 3 my $xd = Math::BigFloat -> new($LIB -> _str($d));
1859  
1860 3 my $xtmp = Math::BigRat -> new($xn -> bdiv($xd) -> bsqrt() -> bsstr());
1861  
1862 3 $x -> {sign} = $xtmp -> {sign};
1863 3 $x -> {_n} = $xtmp -> {_n};
1864 3 $x -> {_d} = $xtmp -> {_d};
1865  
1866 3 $x->round(@r);
1867 }
1868  
1869 sub blsft {
1870 0 ▶ my ($class, $x, $y, $b) = objectify(2, @_);
Subroutine blsft: not called CC=8 SCAR=42.8
Pod documented
1871  
1872 0 ▶ $b = 2 if !defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 0 0
1873 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
1874  
1875 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
1876  
1877 # shift by a negative amount?
1878 0 ▶ return $x -> brsft($y -> copy() -> babs(), $b) if $y -> {sign} =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} =~ /^-/ 0 0 0
1879  
1880 0 $x -> bmul($b -> bpow($y));
1881 }
1882  
1883 sub brsft {
1884 0 ▶ my ($class, $x, $y, $b) = objectify(2, @_);
Subroutine brsft: not called CC=8 SCAR=42.8
Pod documented
1885  
1886 0 ▶ $b = 2 if !defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 0 0 0
1887 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
1888  
1889 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
1890  
1891 # shift by a negative amount?
1892 0 ▶ return $x -> blsft($y -> copy() -> babs(), $b) if $y -> {sign} =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} =~ /^-/ 0 0 0
1893  
1894 # the following call to bdiv() will return either quotient (scalar context)
1895 # or quotient and remainder (list context).
1896 0 $x -> bdiv($b -> bpow($y));
1897 }
1898  
1899 sub band {
1900 289 ▶ my $x = shift;
Subroutine band: called CC=5 SCAR=16.5
Pod documented
1901 289 my $xref = ref($x);
1902 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
1903  
1904 289 ▶ croak 'band() is an instance method, not a class method' unless $xref;
Branch: 1 branch
BranchTrueFalseTotal
unless $xref 0 289 289
1905 289 ▶ croak 'Not enough arguments for band()' if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 289 289
1906  
1907 289 my $y = shift;
1908 289 ▶ $y = $class -> new($y) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 289 289
1909  
1910 289 my @r = @_;
1911  
1912 289 my $xtmp = Math::BigInt -> new($x -> bint()); # to Math::BigInt
1913 289 $xtmp -> band($y);
1914 289 $xtmp = $class -> new($xtmp); # back to Math::BigRat
1915  
1916 289 $x -> {sign} = $xtmp -> {sign};
1917 289 $x -> {_n} = $xtmp -> {_n};
1918 289 $x -> {_d} = $xtmp -> {_d};
1919  
1920 289 return $x -> round(@r);
1921 }
1922  
1923 sub bior {
1924 289 ▶ my $x = shift;
Subroutine bior: called CC=5 SCAR=16.5
Pod documented
1925 289 my $xref = ref($x);
1926 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
1927  
1928 289 ▶ croak 'bior() is an instance method, not a class method' unless $xref;
Branch: 1 branch
BranchTrueFalseTotal
unless $xref 0 289 289
1929 289 ▶ croak 'Not enough arguments for bior()' if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 289 289
1930  
1931 289 my $y = shift;
1932 289 ▶ $y = $class -> new($y) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 289 289
1933  
1934 289 my @r = @_;
1935  
1936 289 my $xtmp = Math::BigInt -> new($x -> bint()); # to Math::BigInt
1937 289 $xtmp -> bior($y);
1938 289 $xtmp = $class -> new($xtmp); # back to Math::BigRat
1939  
1940 289 $x -> {sign} = $xtmp -> {sign};
1941 289 $x -> {_n} = $xtmp -> {_n};
1942 289 $x -> {_d} = $xtmp -> {_d};
1943  
1944 289 return $x -> round(@r);
1945 }
1946  
1947 sub bxor {
1948 289 ▶ my $x = shift;
Subroutine bxor: called CC=5 SCAR=16.5
Pod documented
1949 289 my $xref = ref($x);
1950 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
1951  
1952 289 ▶ croak 'bxor() is an instance method, not a class method' unless $xref;
Branch: 1 branch
BranchTrueFalseTotal
unless $xref 0 289 289
1953 289 ▶ croak 'Not enough arguments for bxor()' if @_ < 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 1 0 289 289
1954  
1955 289 my $y = shift;
1956 289 ▶ $y = $class -> new($y) unless ref($y);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $y 0 289 289
1957  
1958 289 my @r = @_;
1959  
1960 289 my $xtmp = Math::BigInt -> new($x -> bint()); # to Math::BigInt
1961 289 $xtmp -> bxor($y);
1962 289 $xtmp = $class -> new($xtmp); # back to Math::BigRat
1963  
1964 289 $x -> {sign} = $xtmp -> {sign};
1965 289 $x -> {_n} = $xtmp -> {_n};
1966 289 $x -> {_d} = $xtmp -> {_d};
1967  
1968 289 return $x -> round(@r);
1969 }
1970  
1971 sub bnot {
1972 0 ▶ my $x = shift;
Subroutine bnot: not called CC=3 SCAR=24.8
Pod documented
1973 0 my $xref = ref($x);
1974 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
1975  
1976 0 ▶ croak 'bnot() is an instance method, not a class method' unless $xref;
Branch: 1 branch
BranchTrueFalseTotal
unless $xref 0 0 0
1977  
1978 0 my @r = @_;
1979  
1980 0 my $xtmp = Math::BigInt -> new($x -> bint()); # to Math::BigInt
1981 0 $xtmp -> bnot();
1982 0 $xtmp = $class -> new($xtmp); # back to Math::BigRat
1983  
1984 0 $x -> {sign} = $xtmp -> {sign};
1985 0 $x -> {_n} = $xtmp -> {_n};
1986 0 $x -> {_d} = $xtmp -> {_d};
1987  
1988 0 return $x -> round(@r);
1989 }
1990  
1991 ##############################################################################
1992 # round
1993  
1994 sub round {
1995 1258 ▶ my $x = shift;
Subroutine round: called CC=5 SCAR=19.4
Pod documented
1996 1258 ▶ return $downgrade -> new($x) if defined($downgrade) &&
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 1256 1256
Condition44%4/9
defined $Math::BigRat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
125620
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
200
$x->is_int or $x->is_inf
l!l&&r!l&&!r
200
MC/DC0%0/4
defined $Math::BigRat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigRat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigRat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1997 ($x -> is_int() || $x -> is_inf() || $x -> is_nan());
1998 1256 $x;
1999 }
2000  
2001 sub bround {
2002 4 ▶ my $x = shift;
Subroutine bround: called CC=5 SCAR=18.1
Pod documented
2003 4 ▶ return $downgrade -> new($x) if defined($downgrade) &&
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 2 0 2
Condition66%6/9
defined $Math::BigRat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
004
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
310
$x->is_int or $x->is_inf
l!l&&r!l&&!r
211
MC/DC0%0/4
defined $Math::BigRat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigRat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigRat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
2004 ($x -> is_int() || $x -> is_inf() || $x -> is_nan());
2005 0 $x;
2006 }
2007  
2008 sub bfround {
2009 4 ▶ my $x = shift;
Subroutine bfround: called CC=5 SCAR=18.1
Pod documented
2010 4 ▶ return $downgrade -> new($x) if defined($downgrade) &&
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigRat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 2 0 2
Condition66%6/9
defined $Math::BigRat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
004
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
310
$x->is_int or $x->is_inf
l!l&&r!l&&!r
211
MC/DC0%0/4
defined $Math::BigRat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigRat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigRat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
2011 ($x -> is_int() || $x -> is_inf() || $x -> is_nan());
2012 0 $x;
2013 }
2014  
2015 ##############################################################################
2016 # comparing
2017  
2018 sub bcmp {
2019 # compare two signed numbers
2020  
2021 # set up parameters
2022 54 ▶ my ($class, $x, $y) = (ref($_[0]), @_);
Subroutine bcmp: called CC=23 SCAR=41.6
Pod documented
2023  
2024 # objectify is costly, so avoid it
2025 54 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 36 18 54
Condition66%2/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
03618
MC/DC50%1/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table66%2/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
2026 36 ($class, $x, $y) = objectify(2, @_);
2027 }
2028  
2029 54 ▶ if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/) 0 54 54
Condition33%1/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
0054
MC/DC0%0/2
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
$x->{"sign"} =~ /^[+-]$/ $y->{"sign"} =~ /^[+-]$/
Truth table33%1/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $y->{"sign"} =~ /^[+-]$/
ABresult
0X0
100
111
2030 # $x is NaN and/or $y is NaN
2031 0 ▶ return if $x->{sign} eq $nan || $y->{sign} eq $nan;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 0 0 0
Condition0%0/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table0%0/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
2032 # $x and $y are both either +inf or -inf
2033 0 ▶ return 0 if $x->{sign} eq $y->{sign} && $x->{sign} =~ /^[+-]inf$/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $y->{"sign"} and $x->{"sign"} =~ /^[+-]inf$/ 0 0 0
Condition0%0/3
$x->{"sign"} eq $y->{"sign"} and $x->{"sign"} =~ /^[+-]inf$/
!ll&&!rl&&r
000
MC/DC0%0/2
$x->{"sign"} eq $y->{"sign"} and $x->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} eq $y->{"sign"} $x->{"sign"} =~ /^[+-]inf$/
Truth table0%0/3
A and B
A= $x->{"sign"} eq $y->{"sign"}B= $x->{"sign"} =~ /^[+-]inf$/
ABresult
0X0
100
111
2034 # $x = +inf and $y < +inf
2035 0 ▶ return +1 if $x->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 0 0 0
2036 # $x = -inf and $y > -inf
2037 0 ▶ return -1 if $x->{sign} eq '-inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-inf" 0 0 0
2038 # $x < +inf and $y = +inf
2039 0 ▶ return -1 if $y->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} eq "+inf" 0 0 0
2040 # $x > -inf and $y = -inf
2041 0 return +1;
2042 }
2043  
2044 # $x >= 0 and $y < 0
2045 54 ▶ return 1 if $x->{sign} eq '+' && $y->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+" and $y->{"sign"} eq "-" 14 40 54
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "-"
!ll&&!rl&&r
202014
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
2046 # $x < 0 and $y >= 0
2047 40 ▶ return -1 if $x->{sign} eq '-' && $y->{sign} eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" and $y->{"sign"} eq "+" 5 35 40
Condition100%3/3
$x->{"sign"} eq "-" and $y->{"sign"} eq "+"
!ll&&!rl&&r
20155
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
2048  
2049 # At this point, we know that $x and $y have the same sign.
2050  
2051 # shortcut
2052 35 my $xz = $LIB->_is_zero($x->{_n});
2053 35 my $yz = $LIB->_is_zero($y->{_n});
2054 35 ▶ return 0 if $xz && $yz; # 0 <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $yz 0 35 35
Condition66%2/3
$xz and $yz
!ll&&!rl&&r
3230
MC/DC0%0/2
$xz and $yz
$xz $yz
Truth table66%2/3
A and B
A= $xzB= $yz
ABresult
0X0
100
111
2055 35 ▶ return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $y->{"sign"} eq "+" 3 32 35
Condition66%2/3
$xz and $y->{"sign"} eq "+"
!ll&&!rl&&r
3203
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
2056 32 ▶ return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $yz and $x->{"sign"} eq "+" 0 32 32
Condition33%1/3
$yz and $x->{"sign"} eq "+"
!ll&&!rl&&r
3200
MC/DC0%0/2
$yz and $x->{"sign"} eq "+"
$yz $x->{"sign"} eq "+"
Truth table33%1/3
A and B
A= $yzB= $x->{"sign"} eq "+"
ABresult
0X0
100
111
2057  
2058 32 my $t = $LIB->_mul($LIB->_copy($x->{_n}), $y->{_d});
2059 32 my $u = $LIB->_mul($LIB->_copy($y->{_n}), $x->{_d});
2060  
2061 32 my $cmp = $LIB->_acmp($t, $u); # signs are equal
2062 32 ▶ $cmp = -$cmp if $x->{sign} eq '-'; # both are '-' => reverse
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 15 17 32
2063 32 $cmp;
2064 }
2065  
2066 sub bacmp {
2067 # compare two numbers (as unsigned)
2068  
2069 # set up parameters
2070 50 ▶ my ($class, $x, $y) = (ref($_[0]), @_);
Subroutine bacmp: called CC=11 SCAR=24.3
Pod documented
2071 # objectify is costly, so avoid it
2072 50 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 0 50 50
Condition33%1/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
0050
MC/DC0%0/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table33%1/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
2073 0 ($class, $x, $y) = objectify(2, @_);
2074 }
2075  
2076 50 ▶ if (($x->{sign} !~ /^[+-]$/) || ($y->{sign} !~ /^[+-]$/)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/) 35 15 50
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
221315
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
$x->{"sign"} =~ /^[+-]$/ $y->{"sign"} =~ /^[+-]$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $y->{"sign"} =~ /^[+-]$/
ABresult
0X0
100
111
2077 # handle +-inf and NaN
2078 35 ▶ return if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 7 28 35
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
4328
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
2079 28 ▶ return 0 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/ 4 24 28
Condition100%3/3
$x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/
!ll&&!rl&&r
12124
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} =~ /^[+-]inf$/ $y->{"sign"} =~ /^[+-]inf$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]inf$/B= $y->{"sign"} =~ /^[+-]inf$/
ABresult
0X0
100
111
2080 24 ▶ return 1 if $x->{sign} =~ /^[+-]inf$/ && $y->{sign} !~ /^[+-]inf$/;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^[+-]inf$/ and not $y->{"sign"} =~ /^[+-]inf$/ 12 12 24
Condition66%2/3
$x->{"sign"} =~ /^[+-]inf$/ and not $y->{"sign"} =~ /^[+-]inf$/
!ll&&!rl&&r
12012
MC/DC50%1/2
$x->{"sign"} =~ /^[+-]inf$/ and not $y->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} =~ /^[+-]inf$/ not $y->{"sign"} =~ /^[+-]inf$/
Truth table66%2/3
A and not(B)
A= $x->{"sign"} =~ /^[+-]inf$/B= not $y->{"sign"} =~ /^[+-]inf$/
ABresult
0X0
100
111
2081 12 return -1;
2082 }
2083  
2084 15 my $t = $LIB->_mul($LIB->_copy($x->{_n}), $y->{_d});
2085 15 my $u = $LIB->_mul($LIB->_copy($y->{_n}), $x->{_d});
2086 15 $LIB->_acmp($t, $u); # ignore signs
2087 }
2088  
2089 sub beq {
2090 10 ▶ my $self = shift;
Subroutine beq: called CC=4 SCAR=14.3
Pod documented
2091 10 my $selfref = ref $self;
2092 #my $class = $selfref || $self;
2093  
2094 10 ▶ croak 'beq() is an instance method, not a class method' unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 10 10
2095 10 ▶ croak 'Wrong number of arguments for beq()' unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 10 10
2096  
2097 10 my $cmp = $self -> bcmp(shift);
2098 10 ▶ return defined($cmp) && ! $cmp;
Condition66%2/3
defined $cmp && !$cmp
!ll&&!rl&&r
082
MC/DC50%1/2
defined $cmp && !$cmp
defined $cmp !$cmp
Truth table66%2/3
A && not(B)
A= defined $cmpB= !$cmp
ABresult
0X0
100
111
2099 }
2100  
2101 sub bne {
2102 0 ▶ my $self = shift;
Subroutine bne: not called CC=5 SCAR=34.0
Pod documented
2103 0 my $selfref = ref $self;
2104 #my $class = $selfref || $self;
2105  
2106 0 ▶ croak 'bne() is an instance method, not a class method' unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
2107 0 ▶ croak 'Wrong number of arguments for bne()' unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
2108  
2109 0 my $cmp = $self -> bcmp(shift);
2110 0 ▶ return defined($cmp) && ! $cmp ? '' : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $cmp && !$cmp ? : 0 0 0
Condition0%0/3
defined $cmp and not $cmp
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cmp and not $cmp
defined $cmp not $cmp
Truth table0%0/3
A and not(B)
A= defined $cmpB= not $cmp
ABresult
0X0
100
111
2111 }
2112  
2113 sub blt {
2114 19 ▶ my $self = shift;
Subroutine blt: called CC=4 SCAR=14.3
Pod documented
2115 19 my $selfref = ref $self;
2116 #my $class = $selfref || $self;
2117  
2118 19 ▶ croak 'blt() is an instance method, not a class method' unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 19 19
2119 19 ▶ croak 'Wrong number of arguments for blt()' unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 19 19
2120  
2121 19 my $cmp = $self -> bcmp(shift);
2122 19 ▶ return defined($cmp) && $cmp < 0;
Condition66%2/3
defined $cmp && $cmp < 0
!ll&&!rl&&r
0109
MC/DC50%1/2
defined $cmp && $cmp < 0
defined $cmp $cmp < 0
Truth table66%2/3
A && B
A= defined $cmpB= $cmp < 0
ABresult
0X0
100
111
2123 }
2124  
2125 sub ble {
2126 0 ▶ my $self = shift;
Subroutine ble: not called CC=4 SCAR=30.0
Pod documented
2127 0 my $selfref = ref $self;
2128 #my $class = $selfref || $self;
2129  
2130 0 ▶ croak 'ble() is an instance method, not a class method' unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
2131 0 ▶ croak 'Wrong number of arguments for ble()' unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
2132  
2133 0 my $cmp = $self -> bcmp(shift);
2134 0 ▶ return defined($cmp) && $cmp <= 0;
Condition0%0/3
defined $cmp && $cmp <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cmp && $cmp <= 0
defined $cmp $cmp <= 0
Truth table0%0/3
A && B
A= defined $cmpB= $cmp <= 0
ABresult
0X0
100
111
2135 }
2136  
2137 sub bgt {
2138 24 ▶ my $self = shift;
Subroutine bgt: called CC=4 SCAR=14.3
Pod documented
2139 24 my $selfref = ref $self;
2140 #my $class = $selfref || $self;
2141  
2142 24 ▶ croak 'bgt() is an instance method, not a class method' unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 24 24
2143 24 ▶ croak 'Wrong number of arguments for bgt()' unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 24 24
2144  
2145 24 my $cmp = $self -> bcmp(shift);
2146 24 ▶ return defined($cmp) && $cmp > 0;
Condition66%2/3
defined $cmp && $cmp > 0
!ll&&!rl&&r
01014
MC/DC50%1/2
defined $cmp && $cmp > 0
defined $cmp $cmp > 0
Truth table66%2/3
A && B
A= defined $cmpB= $cmp > 0
ABresult
0X0
100
111
2147 }
2148  
2149 sub bge {
2150 0 ▶ my $self = shift;
Subroutine bge: not called CC=4 SCAR=30.0
Pod documented
2151 0 my $selfref = ref $self;
2152 #my $class = $selfref || $self;
2153  
2154 0 ▶ croak 'bge() is an instance method, not a class method'
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
2155 unless $selfref;
2156 0 ▶ croak 'Wrong number of arguments for bge()' unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
2157  
2158 0 my $cmp = $self -> bcmp(shift);
2159 0 ▶ return defined($cmp) && $cmp >= 0;
Condition0%0/3
defined $cmp && $cmp >= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $cmp && $cmp >= 0
defined $cmp $cmp >= 0
Truth table0%0/3
A && B
A= defined $cmpB= $cmp >= 0
ABresult
0X0
100
111
2160 }
2161  
2162 ##############################################################################
2163 # output conversion
2164  
2165 sub numify {
2166 # convert 17/8 => float (aka 2.125)
2167 22 ▶ my ($self, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine numify: called CC=7 SCAR=19.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 22 0 22
2168  
2169 # Non-finite number.
2170  
2171 22 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 1 21 22
2172 1 require Math::Complex;
2173 1 my $inf = $Math::Complex::Inf;
2174 1 return $inf - $inf;
2175 }
2176  
2177 21 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 2 19 21
2178 2 require Math::Complex;
2179 2 my $inf = $Math::Complex::Inf;
2180 2 ▶ return $x -> is_negative() ? -$inf : $inf;
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative ? : 1 1 2
2181 }
2182  
2183 # Finite number.
2184  
2185 my $abs = $LIB->_is_one($x->{_d})
2186 ? $LIB->_num($x->{_n})
2187 : Math::BigFloat -> new($LIB->_str($x->{_n}))
2188 19 ▶ -> bdiv($LIB->_str($x->{_d}))
Branch: 1 branch
BranchTrueFalseTotal
$LIB->_is_one($x->{'_d'}) ? : 10 9 19
2189 -> bstr();
2190 19 ▶ return $x->{sign} eq '-' ? 0 - $abs : 0 + $abs;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 3 16 19
2191 }
2192  
2193 sub as_int {
2194 881 ▶ my ($class, $x) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_int: called CC=6 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 881 0 881
2195  
2196 881 ▶ return $x -> copy() if $x -> isa("Math::BigInt");
Branch: 1 branch
BranchTrueFalseTotal
if $x->isa("Math::BigInt") 0 881 881
2197  
2198 # disable upgrading and downgrading
2199  
2200 881 require Math::BigInt;
2201 881 my $upg = Math::BigInt -> upgrade();
2202 881 my $dng = Math::BigInt -> downgrade();
2203 881 Math::BigInt -> upgrade(undef);
2204 881 Math::BigInt -> downgrade(undef);
2205  
2206 881 my $y;
2207 881 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 4 877 881
elsif ($x->is_nan) { } 2 875 877
2208 4 $y = Math::BigInt -> binf($x->sign());
2209 } elsif ($x -> is_nan()) {
2210 2 $y = Math::BigInt -> bnan();
2211 } else {
2212 875 my $int = $LIB -> _div($LIB -> _copy($x->{_n}), $x->{_d}); # 22/7 => 3
2213 875 $y = Math::BigInt -> new($LIB -> _str($int));
2214 875 ▶ $y = $y -> bneg() if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 4 871 875
2215 }
2216  
2217 # reset upgrading and downgrading
2218  
2219 881 Math::BigInt -> upgrade($upg);
2220 881 Math::BigInt -> downgrade($dng);
2221  
2222 881 return $y;
2223 }
2224  
2225 sub as_float {
2226 5 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_float: called CC=6 SCAR=18.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 0 5
2227  
2228 5 ▶ return $x -> copy() if $x -> isa("Math::BigFloat");
Branch: 1 branch
BranchTrueFalseTotal
if $x->isa("Math::BigFloat") 0 5 5
2229  
2230 # disable upgrading and downgrading
2231  
2232 5 require Math::BigFloat;
2233 5 my $upg = Math::BigFloat -> upgrade();
2234 5 my $dng = Math::BigFloat -> downgrade();
2235 5 Math::BigFloat -> upgrade(undef);
2236 5 Math::BigFloat -> downgrade(undef);
2237  
2238 5 my $y;
2239 5 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 0 5 5
elsif ($x->is_nan) { } 0 5 5
2240 0 $y = Math::BigFloat -> binf($x->sign());
2241 } elsif ($x -> is_nan()) {
2242 0 $y = Math::BigFloat -> bnan();
2243 } else {
2244 5 $y = Math::BigFloat -> new($LIB -> _str($x->{_n}));
2245 5 $y -> {sign} = $x -> {sign};
2246 5 ▶ unless ($LIB -> _is_one($x->{_d})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_one($x->{"_d"})) 2 3 5
2247 2 my $xd = Math::BigFloat -> new($LIB -> _str($x->{_d}));
2248 2 $y -> bdiv($xd, @r);
2249 }
2250 }
2251  
2252 # reset upgrading and downgrading
2253  
2254 5 Math::BigFloat -> upgrade($upg);
2255 5 Math::BigFloat -> downgrade($dng);
2256  
2257 5 return $y;
2258 }
2259  
2260 sub as_bin {
2261 2 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine as_bin: called CC=4 SCAR=14.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
2262  
2263 2 ▶ return $x unless $x->is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 2 2
2264  
2265 2 my $s = $x->{sign};
2266 2 ▶ $s = '' if $s eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "+" 2 0 2
2267 2 $s . $LIB->_as_bin($x->{_n});
2268 }
2269  
2270 sub as_hex {
2271 2 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine as_hex: called CC=4 SCAR=14.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
2272  
2273 2 ▶ return $x unless $x->is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 2 2
2274  
2275 2 ▶ my $s = $x->{sign}; $s = '' if $s eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "+" 2 0 2
2276 2 $s . $LIB->_as_hex($x->{_n});
2277 }
2278  
2279 sub as_oct {
2280 2 ▶ my ($class, $x) = ref($_[0]) ? (undef, $_[0]) : objectify(1, @_);
Subroutine as_oct: called CC=4 SCAR=14.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
2281  
2282 2 ▶ return $x unless $x->is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 0 2 2
2283  
2284 2 ▶ my $s = $x->{sign}; $s = '' if $s eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $s eq "+" 2 0 2
2285 2 $s . $LIB->_as_oct($x->{_n});
2286 }
2287  
2288 ##############################################################################
2289  
2290 sub from_hex {
2291 3 ▶ my $class = shift;
Subroutine from_hex: called CC=1 SCAR=0.0
Pod documented
2292  
2293 # The relationship should probably go the otherway, i.e, that new() calls
2294 # from_hex(). Fixme!
2295 3 my ($x, @r) = @_;
2296 3 $x =~ s|^\s*(?:0?[Xx]_*)?|0x|;
2297 3 $class->new($x, @r);
2298 }
2299  
2300 sub from_bin {
2301 3 ▶ my $class = shift;
Subroutine from_bin: called CC=1 SCAR=0.0
Pod documented
2302  
2303 # The relationship should probably go the otherway, i.e, that new() calls
2304 # from_bin(). Fixme!
2305 3 my ($x, @r) = @_;
2306 3 $x =~ s|^\s*(?:0?[Bb]_*)?|0b|;
2307 3 $class->new($x, @r);
2308 }
2309  
2310 sub from_oct {
2311 5 ▶ my $class = shift;
Subroutine from_oct: called CC=2 SCAR=6.9
Pod documented
2312  
2313 # Why is this different from from_hex() and from_bin()? Fixme!
2314 5 my @parts;
2315 5 for my $c (@_) {
2316 5 push @parts, Math::BigInt->from_oct($c);
2317 }
2318 5 $class->new (@parts);
2319 }
2320  
2321 ##############################################################################
2322 # import
2323  
2324 sub import {
2325 19 ▶ my $class = shift;
Subroutine import: called CC=11 SCAR=26.4
2326 19 my @a; # unrecognized arguments
2327 19 my $lib_param = '';
2328 19 my $lib_value = '';
2329  
2330 19 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 19 23
2331 4 my $param = shift;
2332  
2333 # Enable overloading of constants.
2334  
2335 4 ▶ if ($param eq ':constant') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq ":constant") 1 3 4
2336 overload::constant
2337  
2338 integer => sub {
2339 7 ▶ $class -> new(shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
2340 },
2341  
2342 float => sub {
2343 75 ▶ $class -> new(shift);
Subroutine __ANON__: called CC=1 SCAR=0.0
2344 },
2345  
2346 binary => sub {
2347 # E.g., a literal 0377 shall result in an object whose value
2348 # is decimal 255, but new("0377") returns decimal 377.
2349 10 ▶ return $class -> from_oct($_[0]) if $_[0] =~ /^0_*[0-7]/;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^0_*[0-7]/ 2 8 10
2350 8 $class -> new(shift);
2351 1 };
2352 1 next;
2353 }
2354  
2355 # Upgrading.
2356  
2357 3 ▶ if ($param eq 'upgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "upgrade") 0 3 3
2358 0 $class -> upgrade(shift);
2359 0 next;
2360 }
2361  
2362 # Downgrading.
2363  
2364 3 ▶ if ($param eq 'downgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "downgrade") 1 2 3
2365 1 $class -> downgrade(shift);
2366 1 next;
2367 }
2368  
2369 # Accuracy.
2370  
2371 2 ▶ if ($param eq 'accuracy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "accuracy") 0 2 2
2372 0 $class -> accuracy(shift);
2373 0 next;
2374 }
2375  
2376 # Precision.
2377  
2378 2 ▶ if ($param eq 'precision') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "precision") 0 2 2
2379 0 $class -> precision(shift);
2380 0 next;
2381 }
2382  
2383 # Rounding mode.
2384  
2385 2 ▶ if ($param eq 'round_mode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "round_mode") 0 2 2
2386 0 $class -> round_mode(shift);
2387 0 next;
2388 }
2389  
2390 # Backend library.
2391  
2392 2 ▶ if ($param =~ /^(lib|try|only)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(lib|try|only)\z/) 2 0 2
2393 # alternative library
2394 2 $lib_param = $param; # "lib", "try", or "only"
2395 2 $lib_value = shift;
2396 2 next;
2397 }
2398  
2399 0 ▶ if ($param eq 'with') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "with") 0 0 0
2400 # alternative class for our private parts()
2401 # XXX: no longer supported
2402 # $LIB = shift() || 'Calc';
2403 # carp "'with' is no longer supported, use 'lib', 'try', or 'only'";
2404 0 shift;
2405 0 next;
2406 }
2407  
2408 # Unrecognized parameter.
2409  
2410 0 push @a, $param;
2411 }
2412  
2413 19 require Math::BigInt;
2414  
2415 19 my @import = ('objectify');
2416 19 ▶ push @import, $lib_param, $lib_value if $lib_param ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $lib_param ne "" 2 17 19
2417 19 Math::BigInt -> import(@import);
2418  
2419 # find out which one was actually loaded
2420 19 $LIB = Math::BigInt -> config("lib");
2421  
2422 # any non :constant stuff is handled by Exporter (loaded by parent class)
2423 # even if @_ is empty, to give it a chance
2424 19 $class->SUPER::import(@a); # for subclasses
2425 19 $class->export_to_level(1, $class, @a); # need this, too
2426 }
2427  
2428 19 1;
2429  
2430 __END__
↑ Summary