BigFloat.pm

filter: Statementstmt 70.4% 2171 / 3082 Branchbran 57.9% 1350 / 2330 Conditioncond 58.6% 646 / 1101 MC/DCmcdc 36.2% 229 / 631 Subroutinesub 74.0% 131 / 177 Podpod 99.0% 103 / 104 totaltotal 62.3% 4630 / 7425 CC 1543 SCAR 49.0
CC
1543
Cov
64%
btdiv
1640.0
to_ieee754
1260.0
from_fp80
600.0
CRAP 133.7
« BigRat.pm ↑ Summary BigInt.pm »
1 package Math::BigFloat;
2  
3 #
4 # Mike grinned. 'Two down, infinity to go' - Mike Nostrus in 'Before and After'
5 #
6  
7 # The following hash values are used internally:
8 #
9 # sign : "+", "-", "+inf", "-inf", or "NaN"
10 # _m : absolute value of mantissa ($LIB thingy)
11 # _es : sign of exponent ("+" or "-")
12 # _e : absolute value of exponent ($LIB thingy)
13 # accuracy : accuracy (scalar)
14 # precision : precision (scalar)
15  
16 43 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 43 ▶use Carp qw< carp croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 43 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 43 ▶use Math::BigInt qw< >;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 43 our $VERSION = '2.005003';
25 43 $VERSION =~ tr/_//d;
26  
27 43 require Exporter;
28 43 our @ISA = qw< Math::BigInt >;
29 43 our @EXPORT_OK = qw< bpi >;
30  
31 use overload
32  
33 # overload key: with_assign
34  
35 33 ▶ '+' => sub { $_[0] -> copy() -> badd($_[1]); },
Subroutine BEGIN: called CC=1 SCAR=1.3
Subroutine __ANON__: called CC=1 SCAR=0.0
36  
37 22 ▶ '-' => sub { my $c = $_[0] -> copy();
Subroutine __ANON__: called CC=2 SCAR=7.2
38 22 ▶ $_[2] ? $c -> bneg() -> badd($_[1])
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 22 22
39 : $c -> bsub($_[1]); },
40  
41 143 ▶ '*' => sub { $_[0] -> copy() -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 22 ▶ '/' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bdiv($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 22 22
44 : $_[0] -> copy() -> bdiv($_[1]); },
45  
46 0 ▶ '%' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bmod($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
47 : $_[0] -> copy() -> bmod($_[1]); },
48  
49 7 ▶ '**' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bpow($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 7 7
50 : $_[0] -> copy() -> bpow($_[1]); },
51  
52 0 ▶ '<<' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bblsft($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
53 : $_[0] -> copy() -> bblsft($_[1]); },
54  
55 0 ▶ '>>' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bbrsft($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
56 : $_[0] -> copy() -> bbrsft($_[1]); },
57  
58 # overload key: assign
59  
60 29 ▶ '+=' => sub { $_[0] -> badd($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
61  
62 28 ▶ '-=' => sub { $_[0] -> bsub($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
63  
64 6472 ▶ '*=' => sub { $_[0] -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
65  
66 8 ▶ '/=' => sub { scalar $_[0] -> bdiv($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
67  
68 8 ▶ '%=' => sub { $_[0] -> bmod($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
69  
70 4 ▶ '**=' => sub { $_[0] -> bpow($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
71  
72 8 ▶ '<<=' => sub { $_[0] -> bblsft($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
73  
74 8 ▶ '>>=' => sub { $_[0] -> bbrsft($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
75  
76 # 'x=' => sub { },
77  
78 # '.=' => sub { },
79  
80 # overload key: num_comparison
81  
82 671 ▶ '<' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> blt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 671 671
83 : $_[0] -> blt($_[1]); },
84  
85 845 ▶ '<=' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> ble($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 845 845
86 : $_[0] -> ble($_[1]); },
87  
88 875 ▶ '>' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bgt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 875 875
89 : $_[0] -> bgt($_[1]); },
90  
91 216 ▶ '>=' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bge($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 159 57 216
92 : $_[0] -> bge($_[1]); },
93  
94 216 ▶ '==' => sub { $_[0] -> beq($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
95  
96 9 ▶ '!=' => sub { $_[0] -> bne($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
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 5885 ▶ 'cmp' => sub { $_[2] ? "$_[1]" cmp $_[0] -> bstr()
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 5885 5885
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 0 ▶ '&' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> band($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
127 : $_[0] -> copy() -> band($_[1]); },
128  
129 0 ▶ '&=' => sub { $_[0] -> band($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
130  
131 0 ▶ '|' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bior($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
132 : $_[0] -> copy() -> bior($_[1]); },
133  
134 0 ▶ '|=' => sub { $_[0] -> bior($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
135  
136 0 ▶ '^' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bxor($_[0])
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 0 0
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 344 ▶ 'neg' => sub { $_[0] -> copy() -> bneg(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
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 7 ▶ '++' => sub { $_[0] -> binc() },
Subroutine __ANON__: called CC=1 SCAR=0.0
166  
167 0 ▶ '--' => sub { $_[0] -> bdec() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 10 ▶ 'abs' => sub { $_[0] -> copy() -> babs(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
181  
182 40 ▶ '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 178 ▶ 'int' => sub { $_[0] -> copy() -> bint(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
187  
188 # overload key: conversion
189  
190 168 ▶ 'bool' => sub { $_[0] -> is_zero() ? '' : 1; },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_zero ? : 0 168 168
191  
192 738 ▶ '""' => sub { $_[0] -> bstr(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
193  
194 8 ▶ '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 43 ;
199  
200 ##############################################################################
201 # global constants, flags and assorted stuff
202  
203 # the following are public, but their usage is not recommended. Use the
204 # accessor methods instead.
205  
206 # class constants, use Class->constant_name() to access
207 # one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'
208  
209 43 our $accuracy = undef;
210 43 our $precision = undef;
211 43 our $round_mode = 'even';
212 43 our $div_scale = 40;
213  
214 43 our $upgrade = undef;
215 43 our $downgrade = undef;
216  
217 43 our $_trap_nan = 0; # croak on NaNs?
218 43 our $_trap_inf = 0; # croak on Infs?
219  
220 43 my $nan = 'NaN'; # constant for easier life
221  
222 43 my $LIB = Math::BigInt -> config('lib'); # math backend library
223  
224 # Has import() been called yet? This variable is needed to make "require" work.
225  
226 43 my $IMPORT = 0;
227  
228 # some digits of accuracy for blog(undef, 10); which we use in blog() for speed
229 43 my $LOG_10 =
230 '2.3025850929940456840179914546843642076011014886287729760333279009675726097';
231 43 my $LOG_10_A = length($LOG_10)-1;
232 # ditto for log(2)
233 43 my $LOG_2 =
234 '0.6931471805599453094172321214581765680755001343602552541206800094933936220';
235 43 my $LOG_2_A = length($LOG_2)-1;
236 43 my $HALF = '0.5'; # made into an object if nec.
237  
238 ##############################################################################
239 # the old code had $rnd_mode, so we need to support it, too
240  
241 43 our $rnd_mode;
242 43 our $AUTOLOAD;
243  
244 43 ▶sub TIESCALAR {
Subroutine TIESCALAR: called CC=1 SCAR=0.0
245 43 my ($class) = @_;
246 43 bless \$round_mode, $class;
247 }
248  
249 1 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
250 1 return $round_mode;
251 }
252  
253 1 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
254 1 $rnd_mode = (ref $_[0]) -> round_mode($_[1]);
255 }
256  
257 43 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
258 43 *objectify = \&Math::BigInt::objectify;
259  
260 # when someone sets $rnd_mode, we catch this and check the value to see
261 # whether it is valid or not.
262 43 $rnd_mode = 'even';
263 43 tie $rnd_mode, 'Math::BigFloat';
264  
265 43 *as_number = \&as_int;
266 }
267  
268 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
269 # going through AUTOLOAD for every DESTROY is costly, avoid it by empty sub
270 }
271  
272 1820 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=6 SCAR=19.6
273  
274 # Make fxxx() work by mapping fxxx() to Math::BigFloat::bxxx().
275  
276 1820 my $name = $AUTOLOAD;
277 1820 $name =~ s/^(.*):://; # strip package name
278 1820 ▶ my $class = $1 || __PACKAGE__;
Condition50%1/2
$1 || __PACKAGE__
l!l
18200
MC/DC0%0/1
$1 || __PACKAGE__
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
279  
280 1820 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 1820 1820
281  
282 # E.g., "fabs" -> "babs", but "is_neg" -> "is_neg"
283  
284 1820 my $bname = $name;
285 1820 $bname =~ s/^f/b/;
286  
287 # Map, e.g., Math::BigFloat::fabs() to Math::BigFloat::babs()
288  
289 1820 ▶ if ($bname ne $name && Math::BigFloat -> can($bname)) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bname ne $name and 'Math::BigFloat'->can($bname)) { } 0 1820 1820
elsif ('Math::BigInt'->can($bname)) { } 1820 0 1820
Condition33%1/3
$bname ne $name and "Math::BigFloat"->can($bname)
!ll&&!rl&&r
182000
MC/DC0%0/2
$bname ne $name and "Math::BigFloat"->can($bname)
$bname ne $name "Math::BigFloat"->can($bname)
Truth table33%1/3
A and B
A= $bname ne $nameB= "Math::BigFloat"->can($bname)
ABresult
0X0
100
111
290 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
291 0 return &{"Math::BigFloat::$bname"}(@_);
292 }
293  
294 # Map, e.g., Math::BigFloat::babs() to Math::BigInt::babs()
295  
296 elsif (Math::BigInt -> can($bname)) {
297 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
298 1820 return &{"Math::BigInt::$bname"}(@_);
299 }
300  
301 else {
302 0 croak("Can't call $class->$name(), not a valid method");
303 }
304 }
305  
306 ##############################################################################
307  
308 # Compare the following function with @ISA above. This inheritance mess needs a
309 # clean up. When doing so, also consider the BEGIN block and the AUTOLOAD code.
310 # Fixme!
311  
312 28987 ▶sub isa {
Subroutine isa: called CC=2 SCAR=6.9
Pod undocumented
313 28987 my ($self, $class) = @_;
314 28987 ▶ return if $class =~ /^Math::BigInt/; # we aren't one of these
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /^Math::BigInt/ 1119 27868 28987
315 27868 UNIVERSAL::isa($self, $class);
316 }
317  
318 435 ▶sub config {
Subroutine config: called CC=8 SCAR=20.8
Pod documented
319 435 my $self = shift;
320 435 ▶ my $class = ref($self) || $self || __PACKAGE__;
Condition60%3/5
ref $self || $self || __PACKAGE__
l!l
4350
ref $self or $self
l!l&&r!l&&!r
623730
MC/DC0%0/2
ref $self || $self || __PACKAGE__
ref $self $self
Truth table66%2/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
321  
322 # Getter/accessor.
323  
324 435 ▶ if (@_ == 1 && ref($_[0]) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] ne "HASH") 316 119 435
Condition100%3/3
@_ == 1 and ref $_[0] ne "HASH"
!ll&&!rl&&r
8534316
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
325 316 my $param = shift;
326 316 ▶ return $class if $param eq 'class';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "class" 2 314 316
327 314 ▶ return $LIB if $param eq 'with';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "with" 3 311 314
328 311 return $self -> SUPER::config($param);
329 }
330  
331 # Setter.
332  
333 119 my $cfg = $self -> SUPER::config(@_);
334  
335 # We need only to override the ones that are different from our parent.
336  
337 115 ▶ unless (ref($self)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 91 24 115
338 91 $cfg->{class} = $class;
339 91 $cfg->{with} = $LIB;
340 }
341  
342 115 $cfg;
343 }
344  
345 ###############################################################################
346 # Constructor methods
347 ###############################################################################
348  
349 20371 ▶sub new {
Subroutine new: called CC=35 SCAR=36.8
Pod documented
350 # Create a new Math::BigFloat object from a string or another Math::BigInt,
351 # Math::BigFloat, or Math::BigRat object. See hash keys documented at top.
352  
353 20371 my $self = shift;
354 20371 my $selfref = ref $self;
355 20371 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
0203710
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
356  
357 # Make "require" work.
358  
359 20371 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 3 20368 20371
360  
361 # Calling new() with no input arguments has been discouraged for more than
362 # 10 years, but people apparently still use it, so we still support it.
363  
364 20371 ▶ return $class -> bzero() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 8 20363 20371
365  
366 20363 my ($wanted, @r) = @_;
367  
368 20363 ▶ if (!defined($wanted)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wanted) 0 20363 20363
369 #if (warnings::enabled("uninitialized")) {
370 # warnings::warn("uninitialized",
371 # "Use of uninitialized value in new()");
372 #}
373 0 return $class -> bzero(@r);
374 }
375  
376 20363 ▶ if (!ref($wanted) && $wanted eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $wanted and $wanted eq "") 0 20363 20363
Condition66%2/3
not ref $wanted and $wanted eq ""
!ll&&!rl&&r
168201950
MC/DC0%0/2
not ref $wanted and $wanted eq ""
not ref $wanted $wanted eq ""
Truth table66%2/3
not(A) and B
A= not ref $wantedB= $wanted eq ""
ABresult
0X0
100
111
377 #if (warnings::enabled("numeric")) {
378 # warnings::warn("numeric",
379 # q|Argument "" isn't numeric in new()|);
380 #}
381 #return $class -> bzero(@r);
382 0 return $class -> bnan(@r);
383 }
384  
385 # Initialize a new object.
386  
387 20363 $self = bless {}, $class;
388  
389 # See if $wanted is an object that is a Math::BigFloat or can convert
390 # itself to a Math::BigFloat.
391  
392 20363 ▶ if (defined(blessed($wanted)) && $wanted -> can('as_float')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($wanted) and $wanted->can("as_float")) 167 20196 20363
Condition100%3/3
defined builtin::blessed($wanted) and $wanted->can("as_float")
!ll&&!rl&&r
201951167
MC/DC100%2/2
defined builtin::blessed($wanted) and $wanted->can("as_float")
defined builtin::blessed($wanted) $wanted->can("as_float")
Truth table100%3/3
A and B
A= defined builtin::blessed($wanted)B= $wanted->can("as_float")
ABresult
0X0
100
111
393 167 my $tmp = $wanted -> as_float(@r);
394 167 for my $attr ('sign', '_m', '_es', '_e') {
395 668 $self -> {$attr} = $tmp -> {$attr};
396 }
397 167 return $self -> round(@r);
398 }
399  
400 # From now on we only work on the stringified version of $wanted, so
401 # stringify it once and for all.
402  
403 20196 $wanted = "$wanted";
404  
405 # Shortcut for simple forms like '123' that have no trailing zeros.
406 # Trailing zeros would require a non-zero exponent.
407  
408 20196 ▶ if ($wanted =~
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ \s* # optional leading whitespace ( [+-]? ) # optional sign 0* # optional leading zeros ( [1-9] (?: [0-9]* [1-9] )? ) # significand \s* # optional trailing whitespace $ /x) 8794 11402 20196
409 / ^
410 \s* # optional leading whitespace
411 ( [+-]? ) # optional sign
412 0* # optional leading zeros
413 ( [1-9] (?: [0-9]* [1-9] )? ) # significand
414 \s* # optional trailing whitespace
415 $
416 /x)
417 {
418 8794 my $dng = $class -> downgrade();
419 8794 ▶ return $dng -> new($1 . $2) if $dng && $dng ne $class;
Branch: 1 branch
BranchTrueFalseTotal
if $dng and $dng ne $class 8 8786 8794
Condition66%2/3
$dng and $dng ne $class
!ll&&!rl&&r
878608
MC/DC50%1/2
$dng and $dng ne $class
$dng $dng ne $class
Truth table66%2/3
A and B
A= $dngB= $dng ne $class
ABresult
0X0
100
111
420 8786 ▶ $self->{sign} = $1 || '+';
Condition100%2/2
$1 || '+'
l!l
30595727
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
421 8786 $self->{_m} = $LIB -> _new($2);
422 8786 $self->{_es} = '+';
423 8786 $self->{_e} = $LIB -> _zero();
424 8786 ▶ $self -> round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 8724 62 8786
Condition100%6/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
87121262
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
8709374
MC/DC100%3/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table100%4/4
A and not(B) and not(C)
A= @r >= 2B= not defined $r[0]C= not defined $r[1]
ABCresult
0XX0
10X0
1100
1111
425 unless @r >= 2 && !defined $r[0] && !defined $r[1];
426 8786 return $self;
427 }
428  
429 # Handle Infs.
430  
431 11402 ▶ if ($wanted =~ / ^
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ \s* ( [+-]? ) inf (?: inity )? \s* \z /xi) 2605 8797 11402
432 \s*
433 ( [+-]? )
434 inf (?: inity )?
435 \s*
436 \z
437 /ix)
438 {
439 2605 ▶ my $sgn = $1 || '+';
Condition100%2/2
$1 || '+'
l!l
1969636
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
440 2605 return $class -> binf($sgn, @r);
441 }
442  
443 # Handle explicit NaNs (not the ones returned due to invalid input).
444  
445 8797 ▶ if ($wanted =~ / ^
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ \s* ( [+-]? ) nan \s* \z /xi) 795 8002 8797
446 \s*
447 ( [+-]? )
448 nan
449 \s*
450 \z
451 /ix)
452 {
453 795 return $class -> bnan(@r);
454 }
455  
456 8002 my @parts;
457  
458 8002 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted) or @parts = $class->_dec_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)) 7214 788 8002
Condition70%17/24
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted) or @parts = $class->_dec_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
l!l&&r!l&&!r
72140788
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted) or @parts = $class->_dec_str_to_flt_lib_parts($wanted)
l!l&&r!l&&!r
427172788
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted)
l!l&&r!l&&!r
26167960
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
l!l&&r!l&&!r
2607976
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
7976026
$wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
797600
$wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
79243616
$wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
78800
MC/DC44%4/9
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted) or @parts = $class->_dec_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
$wanted =~ /^\s*[+-]?0?[Xx]/ @parts = $class->_hex_str_to_flt_lib_parts($wanted) $wanted =~ /^\s*[+-]?0?[Oo]/ @parts = $class->_oct_str_to_flt_lib_parts($wanted) $wanted =~ /^\s*[+-]?0?[Bb]/ @parts = $class->_bin_str_to_flt_lib_parts($wanted) @parts = $class->_dec_str_to_flt_lib_parts($wanted) $wanted =~ /^\s*[+-]?0_*\d/ @parts = $class->_oct_str_to_flt_lib_parts($wanted)
Truth table12%5/39
A and B or C and D or E and F or G or H and I
A= $wanted =~ /^\s*[+-]?0?[Xx]/B= @parts = $class->_hex_str_to_flt_lib_parts($wanted)C= $wanted =~ /^\s*[+-]?0?[Oo]/D= @parts = $class->_oct_str_to_flt_lib_parts($wanted)E= $wanted =~ /^\s*[+-]?0?[Bb]/F= @parts = $class->_bin_str_to_flt_lib_parts($wanted)G= @parts = $class->_dec_str_to_flt_lib_parts($wanted)H= $wanted =~ /^\s*[+-]?0_*\d/I= @parts = $class->_oct_str_to_flt_lib_parts($wanted)
ABCDEFGHIresult
11XXXXXXX1
0X11XXXXX1
1011XXXXX1
0X0X11XXX1
0X1011XXX1
100X11XXX1
101011XXX1
0X0X0X1XX1
0X0X101XX1
0X100X1XX1
0X10101XX1
100X0X1XX1
100X101XX1
10100X1XX1
1010101XX1
0X0X0X0111
0X0X100111
0X100X0111
0X10100111
100X0X0111
100X100111
10100X0111
1010100111
0X0X0X00X0
0X0X0X0100
0X0X1000X0
0X0X100100
0X100X00X0
0X100X0100
0X101000X0
0X10100100
100X0X00X0
100X0X0100
100X1000X0
100X100100
10100X00X0
10100X0100
10101000X0
1010100100
459 # Handle hexadecimal numbers. We auto-detect hexadecimal numbers if
460 # they have a "0x", "0X", "x", or "X" prefix, cf. CORE::oct().
461  
462 $wanted =~ /^\s*[+-]?0?[Xx]/ and
463 @parts = $class -> _hex_str_to_flt_lib_parts($wanted)
464  
465 or
466  
467 # Handle octal numbers. We auto-detect octal numbers if they have a
468 # "0o", "0O", "o", "O" prefix, cf. CORE::oct().
469  
470 $wanted =~ /^\s*[+-]?0?[Oo]/ and
471 @parts = $class -> _oct_str_to_flt_lib_parts($wanted)
472  
473 or
474  
475 # Handle binary numbers. We auto-detect binary numbers if they have a
476 # "0b", "0B", "b", or "B" prefix, cf. CORE::oct().
477  
478 $wanted =~ /^\s*[+-]?0?[Bb]/ and
479 @parts = $class -> _bin_str_to_flt_lib_parts($wanted)
480  
481 or
482  
483 # At this point, what is left are decimal numbers that aren't handled
484 # above and octal floating point numbers that don't have any of the
485 # "0o", "0O", "o", or "O" prefixes. First see if it is a decimal
486 # number.
487  
488 @parts = $class -> _dec_str_to_flt_lib_parts($wanted)
489 or
490  
491 # See if it is an octal floating point number. The extra check is
492 # included because _oct_str_to_flt_lib_parts() accepts octal numbers
493 # that don't have a prefix (this is needed to make it work with, e.g.,
494 # from_oct() that don't require a prefix). However, Perl requires a
495 # prefix for octal floating point literals. For example, "1p+0" is not
496 # valid, but "01p+0" and "0__1p+0" are.
497  
498 $wanted =~ /^\s*[+-]?0_*\d/ and
499 @parts = $class -> _oct_str_to_flt_lib_parts($wanted))
500 {
501 7214 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
502  
503 7214 ▶ $self -> round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 6126 1088 7214
Condition66%4/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
612601088
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
612601088
MC/DC33%1/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table50%2/4
A and not(B) and not(C)
A= @r >= 2B= not defined $r[0]C= not defined $r[1]
ABCresult
0XX0
10X0
1100
1111
504 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
505  
506 7214 ▶ $self -> _dng() if ($self -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int or $self->is_inf or $self->is_nan 3567 3647 7214
Condition66%4/6
$self->is_int or $self->is_inf or $self->is_nan
l!l&&r!l&&!r
356703647
$self->is_int or $self->is_inf
l!l&&r!l&&!r
356703647
MC/DC33%1/3
$self->is_int or $self->is_inf or $self->is_nan
$self->is_int $self->is_inf $self->is_nan
Truth table50%2/4
A or B or C
A= $self->is_intB= $self->is_infC= $self->is_nan
ABCresult
1XX1
01X1
0011
0000
507 $self -> is_inf() ||
508 $self -> is_nan());
509  
510 7214 return $self;
511 }
512  
513 # If we get here, the value is neither a valid decimal, binary, octal, or
514 # hexadecimal number. It is not an explicit Inf or a NaN either.
515  
516 788 return $class -> bnan(@r);
517 }
518  
519 1 ▶sub from_dec {
Subroutine from_dec: called CC=13 SCAR=32.5
Pod documented
520 1 my $self = shift;
521 1 my $selfref = ref $self;
522 1 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
523  
524 # Make "require" work.
525  
526 1 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 1 1
527  
528 # Don't modify constant (read-only) objects.
529  
530 1 ▶ return $self if $selfref && $self -> modify('from_dec');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_dec") 0 1 1
Condition33%1/3
$selfref and $self->modify("from_dec")
!ll&&!rl&&r
100
MC/DC0%0/2
$selfref and $self->modify("from_dec")
$selfref $self->modify("from_dec")
Truth table33%1/3
A and B
A= $selfrefB= $self->modify("from_dec")
ABresult
0X0
100
111
531  
532 1 my $str = shift;
533 1 my @r = @_;
534  
535 1 ▶ if (my @parts = $class -> _dec_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_dec_str_to_flt_lib_parts($str)) 1 0 1
536  
537 # If called as a class method, initialize a new object.
538  
539 1 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1 0 1
540 1 $self = bless {}, $class;
541 #$self -> _init();
542 }
543  
544 1 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
545  
546 1 ▶ $self -> round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 1 0 1
Condition33%2/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
100
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
100
MC/DC0%0/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table25%1/4
A and not(B) and not(C)
A= @r >= 2B= not defined $r[0]C= not defined $r[1]
ABCresult
0XX0
10X0
1100
1111
547 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
548  
549 1 ▶ $self -> _dng() if ($self -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int or $self->is_inf or $self->is_nan 1 0 1
Condition33%2/6
$self->is_int or $self->is_inf or $self->is_nan
l!l&&r!l&&!r
100
$self->is_int or $self->is_inf
l!l&&r!l&&!r
100
MC/DC0%0/3
$self->is_int or $self->is_inf or $self->is_nan
$self->is_int $self->is_inf $self->is_nan
Truth table25%1/4
A or B or C
A= $self->is_intB= $self->is_infC= $self->is_nan
ABCresult
1XX1
01X1
0011
0000
550 $self -> is_inf() ||
551 $self -> is_nan());
552  
553 1 return $self;
554 }
555  
556 0 return $self -> bnan(@r);
557 }
558  
559 1 ▶sub from_hex {
Subroutine from_hex: called CC=13 SCAR=32.5
Pod documented
560 1 my $self = shift;
561 1 my $selfref = ref $self;
562 1 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
563  
564 # Make "require" work.
565  
566 1 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 1 1
567  
568 # Don't modify constant (read-only) objects.
569  
570 1 ▶ return $self if $selfref && $self -> modify('from_hex');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_hex") 0 1 1
Condition33%1/3
$selfref and $self->modify("from_hex")
!ll&&!rl&&r
100
MC/DC0%0/2
$selfref and $self->modify("from_hex")
$selfref $self->modify("from_hex")
Truth table33%1/3
A and B
A= $selfrefB= $self->modify("from_hex")
ABresult
0X0
100
111
571  
572 1 my $str = shift;
573 1 my @r = @_;
574  
575 1 ▶ if (my @parts = $class -> _hex_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_hex_str_to_flt_lib_parts($str)) 1 0 1
576  
577 # If called as a class method, initialize a new object.
578  
579 1 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1 0 1
580 1 $self = bless {}, $class;
581 #$self -> _init();
582 }
583  
584 1 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
585  
586 1 ▶ $self -> round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 1 0 1
Condition33%2/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
100
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
100
MC/DC0%0/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table25%1/4
A and not(B) and not(C)
A= @r >= 2B= not defined $r[0]C= not defined $r[1]
ABCresult
0XX0
10X0
1100
1111
587 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
588  
589 1 ▶ $self -> _dng() if ($self -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int or $self->is_inf or $self->is_nan 1 0 1
Condition33%2/6
$self->is_int or $self->is_inf or $self->is_nan
l!l&&r!l&&!r
100
$self->is_int or $self->is_inf
l!l&&r!l&&!r
100
MC/DC0%0/3
$self->is_int or $self->is_inf or $self->is_nan
$self->is_int $self->is_inf $self->is_nan
Truth table25%1/4
A or B or C
A= $self->is_intB= $self->is_infC= $self->is_nan
ABCresult
1XX1
01X1
0011
0000
590 $self -> is_inf() ||
591 $self -> is_nan());
592 1 return $self;
593 }
594  
595 0 return $self -> bnan(@r);
596 }
597  
598 1 ▶sub from_oct {
Subroutine from_oct: called CC=13 SCAR=32.5
Pod documented
599 1 my $self = shift;
600 1 my $selfref = ref $self;
601 1 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
602  
603 # Make "require" work.
604  
605 1 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 1 1
606  
607 # Don't modify constant (read-only) objects.
608  
609 1 ▶ return $self if $selfref && $self -> modify('from_oct');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_oct") 0 1 1
Condition33%1/3
$selfref and $self->modify("from_oct")
!ll&&!rl&&r
100
MC/DC0%0/2
$selfref and $self->modify("from_oct")
$selfref $self->modify("from_oct")
Truth table33%1/3
A and B
A= $selfrefB= $self->modify("from_oct")
ABresult
0X0
100
111
610  
611 1 my $str = shift;
612 1 my @r = @_;
613  
614 1 ▶ if (my @parts = $class -> _oct_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_oct_str_to_flt_lib_parts($str)) 1 0 1
615  
616 # If called as a class method, initialize a new object.
617  
618 1 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1 0 1
619 1 $self = bless {}, $class;
620 #$self -> _init();
621 }
622  
623 1 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
624  
625 1 ▶ $self -> round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 1 0 1
Condition33%2/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
100
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
100
MC/DC0%0/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table25%1/4
A and not(B) and not(C)
A= @r >= 2B= not defined $r[0]C= not defined $r[1]
ABCresult
0XX0
10X0
1100
1111
626 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
627  
628 1 ▶ $self -> _dng() if ($self -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int or $self->is_inf or $self->is_nan 1 0 1
Condition33%2/6
$self->is_int or $self->is_inf or $self->is_nan
l!l&&r!l&&!r
100
$self->is_int or $self->is_inf
l!l&&r!l&&!r
100
MC/DC0%0/3
$self->is_int or $self->is_inf or $self->is_nan
$self->is_int $self->is_inf $self->is_nan
Truth table25%1/4
A or B or C
A= $self->is_intB= $self->is_infC= $self->is_nan
ABCresult
1XX1
01X1
0011
0000
629 $self -> is_inf() ||
630 $self -> is_nan());
631 1 return $self;
632 }
633  
634 0 return $self -> bnan(@r);
635 }
636  
637 3 ▶sub from_bin {
Subroutine from_bin: called CC=13 SCAR=32.5
Pod documented
638 3 my $self = shift;
639 3 my $selfref = ref $self;
640 3 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
030
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
641  
642 # Make "require" work.
643  
644 3 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 3 3
645  
646 # Don't modify constant (read-only) objects.
647  
648 3 ▶ return $self if $selfref && $self -> modify('from_bin');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_bin") 0 3 3
Condition33%1/3
$selfref and $self->modify("from_bin")
!ll&&!rl&&r
300
MC/DC0%0/2
$selfref and $self->modify("from_bin")
$selfref $self->modify("from_bin")
Truth table33%1/3
A and B
A= $selfrefB= $self->modify("from_bin")
ABresult
0X0
100
111
649  
650 3 my $str = shift;
651 3 my @r = @_;
652  
653 3 ▶ if (my @parts = $class -> _bin_str_to_flt_lib_parts($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@parts) = $class->_bin_str_to_flt_lib_parts($str)) 3 0 3
654  
655 # If called as a class method, initialize a new object.
656  
657 3 ▶ unless ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 3 0 3
658 3 $self = bless {}, $class;
659 #$self -> _init();
660 }
661  
662 3 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
663  
664 3 ▶ $self -> round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 3 0 3
Condition33%2/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
300
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
300
MC/DC0%0/3
@r >= 2 and not defined $r[0] and not defined $r[1]
@r >= 2 not defined $r[0] not defined $r[1]
Truth table25%1/4
A and not(B) and not(C)
A= @r >= 2B= not defined $r[0]C= not defined $r[1]
ABCresult
0XX0
10X0
1100
1111
665 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
666  
667 3 ▶ $self -> _dng() if ($self -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int or $self->is_inf or $self->is_nan 3 0 3
Condition33%2/6
$self->is_int or $self->is_inf or $self->is_nan
l!l&&r!l&&!r
300
$self->is_int or $self->is_inf
l!l&&r!l&&!r
300
MC/DC0%0/3
$self->is_int or $self->is_inf or $self->is_nan
$self->is_int $self->is_inf $self->is_nan
Truth table25%1/4
A or B or C
A= $self->is_intB= $self->is_infC= $self->is_nan
ABCresult
1XX1
01X1
0011
0000
668 $self -> is_inf() ||
669 $self -> is_nan());
670 3 return $self;
671 }
672  
673 0 return $self -> bnan(@r);
674 }
675  
676 0 ▶sub from_bytes {
Subroutine from_bytes: not called CC=6 SCAR=37.4
Pod documented
677 0 my $self = shift;
678 0 my $selfref = ref $self;
679 0 ▶ my $class = $selfref || $self;
Condition0%0/3
$selfref || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table0%0/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
680  
681 # Make "require" work.
682  
683 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
684  
685 # Don't modify constant (read-only) objects.
686  
687 0 ▶ return $self if $selfref && $self -> modify('from_bytes');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_bytes") 0 0 0
Condition0%0/3
$selfref and $self->modify("from_bytes")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("from_bytes")
$selfref $self->modify("from_bytes")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("from_bytes")
ABresult
0X0
100
111
688  
689 0 my $str = shift;
690 0 my @r = @_;
691  
692 # If called as a class method, initialize a new object.
693  
694 0 ▶ $self = $class -> bzero(@r) unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
695  
696 0 $self -> {sign} = "+";
697 0 $self -> {_m} = $LIB -> _from_bytes($str);
698 0 $self -> {_es} = "+";
699 0 $self -> {_e} = $LIB -> _zero();
700 0 $self -> bnorm();
701  
702 0 $self -> _dng();
703 0 return $self;
704 }
705  
706 1 ▶sub from_ieee754 {
Subroutine from_ieee754: called CC=38 SCAR=60.2
Pod documented
707 1 my $self = shift;
708 1 my $selfref = ref $self;
709 1 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
010
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
710  
711 # Make "require" work.
712  
713 1 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 1 1
714  
715 # Don't modify constant (read-only) objects.
716  
717 1 ▶ return $self if $selfref && $self -> modify('from_ieee754');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_ieee754") 0 1 1
Condition33%1/3
$selfref and $self->modify("from_ieee754")
!ll&&!rl&&r
100
MC/DC0%0/2
$selfref and $self->modify("from_ieee754")
$selfref $self->modify("from_ieee754")
Truth table33%1/3
A and B
A= $selfrefB= $self->modify("from_ieee754")
ABresult
0X0
100
111
718  
719 1 my $in = shift; # input string (or raw bytes)
720 1 my $format = shift; # format ("binary32", "decimal64" etc.)
721 1 my $enc; # significand encoding (applies only to decimal)
722 my $k; # storage width in bits
723 1 my $b; # base
724 1 my @r = @_; # rounding parameters, if any
725  
726 1 ▶ if ($format =~ /^binary(\d+)\z/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($format =~ /^binary(\d+)\z/) { } 1 0 1
elsif ($format =~ /^decimal(\d+)(dpd|bcd)?\z/) { } 0 0 0
elsif ($format eq 'half') { } 0 0 0
elsif ($format eq 'single') { } 0 0 0
elsif ($format eq 'double') { } 0 0 0
elsif ($format eq 'quadruple') { } 0 0 0
elsif ($format eq 'octuple') { } 0 0 0
elsif ($format eq 'sexdecuple') { } 0 0 0
727 1 $k = $1;
728 1 $b = 2;
729 } elsif ($format =~ /^decimal(\d+)(dpd|bcd)?\z/) {
730 0 $k = $1;
731 0 $b = 10;
732 0 ▶ $enc = $2 || 'dpd'; # default is dencely-packed decimals (DPD)
Condition0%0/2
$2 || 'dpd'
l!l
00
MC/DC0%0/1
$2 || 'dpd'
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
733 } elsif ($format eq 'half') {
734 0 $k = 16;
735 0 $b = 2;
736 } elsif ($format eq 'single') {
737 0 $k = 32;
738 0 $b = 2;
739 } elsif ($format eq 'double') {
740 0 $k = 64;
741 0 $b = 2;
742 } elsif ($format eq 'quadruple') {
743 0 $k = 128;
744 0 $b = 2;
745 } elsif ($format eq 'octuple') {
746 0 $k = 256;
747 0 $b = 2;
748 } elsif ($format eq 'sexdecuple') {
749 0 $k = 512;
750 0 $b = 2;
751 }
752  
753 1 ▶ if ($b == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 2) 1 0 1
754  
755 # Get the parameters for this format.
756  
757 1 my $p; # precision (in bits)
758 my $t; # number of bits in significand
759 1 my $w; # number of bits in exponent
760  
761 1 ▶ if ($k == 16) { # binary16 (half-precision)
Branch: 3 branches
BranchTrueFalseTotal
if ($k == 16) { } 0 1 1
elsif ($k == 32) { } 1 0 1
elsif ($k == 64) { } 0 0 0
762 0 $p = 11;
763 0 $t = 10;
764 0 $w = 5;
765 } elsif ($k == 32) { # binary32 (single-precision)
766 1 $p = 24;
767 1 $t = 23;
768 1 $w = 8;
769 } elsif ($k == 64) { # binary64 (double-precision)
770 0 $p = 53;
771 0 $t = 52;
772 0 $w = 11;
773 } else { # binaryN (quadruple-precision and above)
774 0 ▶ if ($k < 128 || $k != 32 * sprintf('%.0f', $k / 32)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < 128 or $k != 32 * sprintf("%.0f", $k / 32)) 0 0 0
Condition0%0/3
$k < 128 or $k != 32 * sprintf("%.0f", $k / 32)
l!l&&r!l&&!r
000
MC/DC0%0/2
$k < 128 or $k != 32 * sprintf("%.0f", $k / 32)
$k < 128 $k != 32 * sprintf("%.0f", $k / 32)
Truth table0%0/3
A or B
A= $k < 128B= $k != 32 * sprintf("%.0f", $k / 32)
ABresult
1X1
011
000
775 0 croak "Number of bits must be 16, 32, 64, or >= 128 and",
776 " a multiple of 32";
777 }
778 0 $p = $k - sprintf('%.0f', 4 * log($k) / log(2)) + 13;
779 0 $t = $p - 1;
780 0 $w = $k - $t - 1;
781 }
782  
783 # The maximum exponent, minimum exponent, and exponent bias.
784  
785 1 my $emax = $class -> new(2) -> bpow($w - 1) -> bdec();
786 1 my $emin = 1 - $emax;
787 1 my $bias = $emax;
788  
789 # Undefined input.
790  
791 1 ▶ unless (defined $in) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $in) 0 1 1
792 0 carp("Input is undefined");
793 0 return $self -> bzero(@r);
794 }
795  
796 # Make sure input string is a string of zeros and ones.
797  
798 1 my $len = CORE::length $in;
799 1 ▶ if (8 * $len == $k) { # bytes
Branch: 3 branches
BranchTrueFalseTotal
if (8 * $len == $k) { } 1 0 1
elsif (4 * $len == $k) { } 0 0 0
elsif ($len == $k) { } 0 0 0
800 1 $in = unpack "B*", $in;
801 } elsif (4 * $len == $k) { # hexadecimal
802 0 ▶ if ($in =~ /([^\da-f])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /([^\da-f])/i) 0 0 0
803 0 croak "Illegal hexadecimal digit '$1'";
804 }
805 0 $in = unpack "B*", pack "H*", $in;
806 } elsif ($len == $k) { # bits
807 0 ▶ if ($in =~ /([^01])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /([^01])/) 0 0 0
808 0 croak "Illegal binary digit '$1'";
809 }
810 } else {
811 0 croak "Unknown input -- $in";
812 }
813  
814 # Split bit string into sign, exponent, and mantissa/significand.
815  
816 1 ▶ my $sign = substr($in, 0, 1) eq '1' ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
substr($in, 0, 1) eq '1' ? : 0 1 1
817 1 my $expo = $class -> from_bin(substr($in, 1, $w));
818 1 my $mant = $class -> from_bin(substr($in, $w + 1));
819  
820 1 my $x;
821  
822 1 $expo -> bsub($bias); # subtract bias
823  
824 1 ▶ if ($expo < $emin) { # zero and subnormals
Branch: 2 branches
BranchTrueFalseTotal
if ($expo < $emin) { } 0 1 1
elsif ($expo > $emax) { } 0 1 1
825 0 ▶ if ($mant == 0) { # zero
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == 0) { } 0 0 0
826 0 $x = $class -> bzero();
827 } else { # subnormals
828 # compute (1/$b)**(N) rather than ($b)**(-N)
829 0 $x = $class -> new("0.5"); # 1/$b
830 0 $x -> bpow($bias + $t - 1) -> bmul($mant);
831 0 ▶ $x -> bneg() if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 0 0 0
832 }
833 }
834  
835 elsif ($expo > $emax) { # inf and nan
836 0 ▶ if ($mant == 0) { # inf
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == 0) { } 0 0 0
837 0 $x = $class -> binf($sign);
838 } else { # nan
839 0 $x = $class -> bnan(@r);
840 }
841 }
842  
843 else { # normals
844 1 $mant = $class -> new(2) -> bpow($t) -> badd($mant);
845 1 ▶ if ($expo < $t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expo < $t) { } 1 0 1
846 # compute (1/$b)**(N) rather than ($b)**(-N)
847 1 $x = $class -> new("0.5"); # 1/$b
848 1 $x -> bpow($t - $expo) -> bmul($mant);
849 } else {
850 0 $x = $class -> new(2);
851 0 $x -> bpow($expo - $t) -> bmul($mant);
852 }
853 1 ▶ $x -> bneg() if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 0 1 1
854 }
855  
856 1 ▶ if ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selfref) { } 0 1 1
857 0 $self -> {sign} = $x -> {sign};
858 0 $self -> {_m} = $x -> {_m};
859 0 $self -> {_es} = $x -> {_es};
860 0 $self -> {_e} = $x -> {_e};
861 } else {
862 1 $self = $x;
863 }
864  
865 1 $self -> round(@r);
866 1 ▶ $self -> _dng() if ($self -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int or $self->is_inf or $self->is_nan 1 0 1
Condition33%2/6
$self->is_int or $self->is_inf or $self->is_nan
l!l&&r!l&&!r
100
$self->is_int or $self->is_inf
l!l&&r!l&&!r
100
MC/DC0%0/3
$self->is_int or $self->is_inf or $self->is_nan
$self->is_int $self->is_inf $self->is_nan
Truth table25%1/4
A or B or C
A= $self->is_intB= $self->is_infC= $self->is_nan
ABCresult
1XX1
01X1
0011
0000
867 $self -> is_inf() ||
868 $self -> is_nan());
869 1 return $self;
870 }
871  
872 0 croak("The format '$format' is not yet supported.");
873 }
874  
875 0 ▶sub from_fp80 {
Subroutine from_fp80: not called CC=24 SCAR=64.0
Pod documented
876 0 my $self = shift;
877 0 my $selfref = ref $self;
878 0 ▶ my $class = $selfref || $self;
Condition0%0/3
$selfref || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table0%0/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
879  
880 # Make "require" work.
881  
882 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
883  
884 # Don't modify constant (read-only) objects.
885  
886 0 ▶ return $self if $selfref && $self -> modify('from_fp80');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_fp80") 0 0 0
Condition0%0/3
$selfref and $self->modify("from_fp80")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("from_fp80")
$selfref $self->modify("from_fp80")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("from_fp80")
ABresult
0X0
100
111
887  
888 0 my $in = shift; # input string (or raw bytes)
889 0 my @r = @_; # rounding parameters, if any
890  
891 # Undefined input.
892  
893 0 ▶ unless (defined $in) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $in) 0 0 0
894 0 carp("Input is undefined");
895 0 return $self -> bzero(@r);
896 }
897  
898 # The parameters for this format.
899  
900 0 my $p = 64; # precision (in bits)
901 0 my $w = 15; # number of bits in exponent
902  
903 # The maximum exponent, minimum exponent, and exponent bias.
904  
905 0 my $emax = $class -> new(2) -> bpow($w - 1) -> bdec(); # = 16383
906 0 my $emin = 1 - $emax; # = -16382
907 0 my $bias = $emax; # = -16383
908  
909 # Make sure input string is a string of zeros and ones.
910  
911 0 my $len = CORE::length $in;
912 0 ▶ if (8 * $len == 80) { # bytes
Branch: 3 branches
BranchTrueFalseTotal
if (8 * $len == 80) { } 0 0 0
elsif (4 * $len == 80) { } 0 0 0
elsif ($len == 80) { } 0 0 0
913 0 $in = unpack "B*", $in;
914 } elsif (4 * $len == 80) { # hexadecimal
915 0 ▶ if ($in =~ /([^\da-f])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /([^\da-f])/i) 0 0 0
916 0 croak "Illegal hexadecimal digit '$1'";
917 }
918 0 $in = unpack "B*", pack "H*", $in;
919 } elsif ($len == 80) { # bits
920 0 ▶ if ($in =~ /([^01])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /([^01])/) 0 0 0
921 0 croak "Illegal binary digit '$1'";
922 }
923 } else {
924 0 croak "Unknown input -- $in";
925 }
926  
927 # Split bit string into sign, exponent, and mantissa/significand.
928  
929 0 ▶ my $sign = substr($in, 0, 1) eq '1' ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
substr($in, 0, 1) eq '1' ? : 0 0 0
930 0 my $expo = $class -> from_bin(substr($in, 1, $w));
931 0 my $mant = $class -> from_bin(substr($in, $w + 1));
932  
933 0 my $x;
934  
935 0 $expo -> bsub($bias); # subtract bias
936  
937 # zero and subnormal numbers
938  
939 0 ▶ if ($expo < $emin) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo < $emin) { } 0 0 0
elsif ($expo > $emax) { } 0 0 0
940 0 ▶ if ($mant == 0) { # zero
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == 0) { } 0 0 0
941 0 $x = $class -> bzero();
942 } else { # subnormals
943 # compute (1/2)**N rather than 2**(-N)
944 0 $x = $class -> new("0.5");
945 0 $x -> bpow(-$emin - 1 + $p) -> bmul($mant);
946 0 ▶ $x -> bneg() if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 0 0 0
947 }
948 }
949  
950 # inf and nan
951  
952 elsif ($expo > $emax) {
953  
954 # if fraction of mantissa is zero, i.e., if mantissa is
955 # 0.000... or 1.000...
956  
957 0 ▶ if (substr($in, 16) =~ /^[01]0+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($in, 16) =~ /^[01]0+$/) { } 0 0 0
958 0 $x = $class -> binf($sign);
959 } else {
960 0 $x = $class -> bnan();
961 }
962 }
963  
964 # normal numbers
965  
966 else {
967  
968 # downscale mantissa
969 0 $mant -> blsft($p - 1, "0.5"); # brsft($p - 1, 2) does division
970  
971 0 ▶ if ($expo < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo < 0) { } 0 0 0
elsif ($expo > 0) { } 0 0 0
972 # compute (1/2)**N rather than 2**(-N)
973 0 $x = $mant -> blsft(-$expo, "0.5");
974 } elsif ($expo > 0) {
975 0 $x = $mant -> blsft($expo, "2");
976 } else {
977 0 $x = $mant;
978 }
979  
980 0 ▶ $x -> bneg() if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 0 0 0
981 }
982  
983 0 ▶ if ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selfref) { } 0 0 0
984 0 $self -> {sign} = $x -> {sign};
985 0 $self -> {_m} = $x -> {_m};
986 0 $self -> {_es} = $x -> {_es};
987 0 $self -> {_e} = $x -> {_e};
988 } else {
989 0 $self = $x;
990 }
991  
992 0 $self -> round(@r);
993 0 ▶ $self -> _dng() if ($self -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_int or $self->is_inf or $self->is_nan 0 0 0
Condition0%0/6
$self->is_int or $self->is_inf or $self->is_nan
l!l&&r!l&&!r
000
$self->is_int or $self->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
$self->is_int or $self->is_inf or $self->is_nan
$self->is_int $self->is_inf $self->is_nan
Truth table0%0/4
A or B or C
A= $self->is_intB= $self->is_infC= $self->is_nan
ABCresult
1XX1
01X1
0011
0000
994 $self -> is_inf() ||
995 $self -> is_nan());
996 0 return $self;
997 }
998  
999 0 ▶sub from_base {
Subroutine from_base: not called CC=16 SCAR=56.1
Pod documented
1000 0 my $self = shift;
1001 0 my $selfref = ref $self;
1002 0 ▶ my $class = $selfref || $self;
Condition0%0/3
$selfref || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table0%0/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1003  
1004 # Make "require" work.
1005  
1006 0 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 0 0
1007  
1008 # Don't modify constant (read-only) objects.
1009  
1010 0 ▶ return $self if $selfref && $self -> modify('from_base');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("from_base") 0 0 0
Condition0%0/3
$selfref and $self->modify("from_base")
!ll&&!rl&&r
000
MC/DC0%0/2
$selfref and $self->modify("from_base")
$selfref $self->modify("from_base")
Truth table0%0/3
A and B
A= $selfrefB= $self->modify("from_base")
ABresult
0X0
100
111
1011  
1012 0 my ($str, $base, $cs, @r) = @_; # $cs is the collation sequence
1013  
1014 0 ▶ $base = $class -> new($base) unless ref($base);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $base 0 0 0
1015  
1016 0 ▶ croak("the base must be a finite integer >= 2")
Branch: 1 branch
BranchTrueFalseTotal
if $base < 2 or not $base->is_int 0 0 0
Condition0%0/3
$base < 2 or not $base->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$base < 2 or not $base->is_int
$base < 2 not $base->is_int
Truth table0%0/3
A or not(B)
A= $base < 2B= not $base->is_int
ABresult
1X1
011
000
1017 if $base < 2 || ! $base -> is_int();
1018  
1019 # If called as a class method, initialize a new object.
1020  
1021 0 ▶ $self = $class -> bzero() unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 0 0 0
1022  
1023 # If no collating sequence is given, pass some of the conversions to
1024 # methods optimized for those cases.
1025  
1026 0 ▶ unless (defined $cs) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cs) 0 0 0
1027 0 ▶ return $self -> from_bin($str, @r) if $base == 2;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 2 0 0 0
1028 0 ▶ return $self -> from_oct($str, @r) if $base == 8;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 8 0 0 0
1029 0 ▶ return $self -> from_hex($str, @r) if $base == 16;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 16 0 0 0
1030 0 ▶ return $self -> from_dec($str, @r) if $base == 10;
Branch: 1 branch
BranchTrueFalseTotal
if $base == 10 0 0 0
1031 }
1032  
1033 0 ▶ croak("from_base() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_from_base") 0 0 0
1034 unless $LIB -> can('_from_base');
1035  
1036 0 my $base_lib = $LIB -> _lsft($LIB -> _copy($base->{_m}), $base->{_e}, 10);
1037 0 $self -> {sign} = '+';
1038 0 ▶ $self -> {_m} = $LIB->_from_base($str, $base_lib,
Branch: 1 branch
BranchTrueFalseTotal
defined $cs ? : 0 0 0
1039 defined($cs) ? $cs : ());
1040 0 $self -> {_es} = "+";
1041 0 $self -> {_e} = $LIB->_zero();
1042 0 $self -> bnorm();
1043  
1044 0 $self -> bround(@r);
1045 0 $self -> _dng();
1046 0 return $self;
1047 }
1048  
1049 599 ▶sub bzero {
Subroutine bzero: called CC=18 SCAR=30.9
Pod documented
1050 # create/assign '+0'
1051  
1052 # Class::method(...) -> Class->method(...)
1053 599 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 599 599
Condition55%5/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
00599
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
4941050
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
1050494
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1054 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1055 {
1056 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1057 # " use is as a method instead";
1058 0 unshift @_, __PACKAGE__;
1059 }
1060  
1061 599 my $self = shift;
1062 599 my $selfref = ref $self;
1063 599 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
4941050
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1064  
1065 # Make "require" work.
1066  
1067 599 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 599 599
1068  
1069 # Don't modify constant (read-only) objects.
1070  
1071 599 ▶ return $self if $selfref && $self -> modify('bzero');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bzero") 0 599 599
Condition66%2/3
$selfref and $self->modify("bzero")
!ll&&!rl&&r
1054940
MC/DC0%0/2
$selfref and $self->modify("bzero")
$selfref $self->modify("bzero")
Truth table66%2/3
A and B
A= $selfrefB= $self->modify("bzero")
ABresult
0X0
100
111
1072  
1073 599 my $dng = $class -> downgrade();
1074 599 ▶ if ($dng && $dng ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dng and $dng ne $class) 1 598 599
Condition66%2/3
$dng and $dng ne $class
!ll&&!rl&&r
59801
MC/DC50%1/2
$dng and $dng ne $class
$dng $dng ne $class
Truth table66%2/3
A and B
A= $dngB= $dng ne $class
ABresult
0X0
100
111
1075 1 ▶ return $self -> _dng() -> bzero(@_) if $selfref;
Branch: 1 branch
BranchTrueFalseTotal
if $selfref 0 1 1
1076 1 return $dng -> bzero(@_);
1077 }
1078  
1079 # Get the rounding parameters, if any.
1080  
1081 598 my @r = @_;
1082  
1083 # If called as a class method, initialize a new object.
1084  
1085 598 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 104 494 598
1086  
1087 598 $self -> {sign} = '+';
1088 598 $self -> {_m} = $LIB -> _zero();
1089 598 $self -> {_es} = '+';
1090 598 $self -> {_e} = $LIB -> _zero();
1091  
1092 # If rounding parameters are given as arguments, use them. If no rounding
1093 # parameters are given, and if called as a class method initialize the new
1094 # instance with the class variables.
1095  
1096 #return $self -> round(@r); # this should work, but doesnt; fixme!
1097  
1098 598 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 78 520 598
1099 78 ▶ if (@r >= 2 && defined($r[0]) && defined($r[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r >= 2 and defined $r[0] and defined $r[1]) 0 78 78
Condition83%5/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
55230
@r >= 2 and defined $r[0]
!ll&&!rl&&r
49623
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table75%3/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
1100 0 carp "can't specify both accuracy and precision";
1101 0 return $self -> bnan();
1102 }
1103 78 $self->{accuracy} = $r[0];
1104 78 $self->{precision} = $r[1];
1105 } else {
1106 520 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 98 422 520
1107 98 $self->{accuracy} = $class -> accuracy();
1108 98 $self->{precision} = $class -> precision();
1109 }
1110 }
1111  
1112 598 return $self;
1113 }
1114  
1115 1720 ▶sub bone {
Subroutine bone: called CC=20 SCAR=31.5
Pod documented
1116 # Create or assign '+1' (or -1 if given sign '-').
1117  
1118 # Class::method(...) -> Class->method(...)
1119 1720 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 1720 1720
Condition55%5/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
001720
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
58411360
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
11360584
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1120 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1121 {
1122 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1123 # " use is as a method instead";
1124 0 unshift @_, __PACKAGE__;
1125 }
1126  
1127 1720 my $self = shift;
1128 1720 my $selfref = ref $self;
1129 1720 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
58411360
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1130  
1131 # Make "require" work.
1132  
1133 1720 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 1720 1720
1134  
1135 # Don't modify constant (read-only) objects.
1136  
1137 1720 ▶ return $self if $selfref && $self -> modify('bone');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bone") 0 1720 1720
Condition66%2/3
$selfref and $self->modify("bone")
!ll&&!rl&&r
11365840
MC/DC0%0/2
$selfref and $self->modify("bone")
$selfref $self->modify("bone")
Truth table66%2/3
A and B
A= $selfrefB= $self->modify("bone")
ABresult
0X0
100
111
1138  
1139 1720 my $dng = $class -> downgrade();
1140 1720 ▶ if ($dng && $dng ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dng and $dng ne $class) 1 1719 1720
Condition66%2/3
$dng and $dng ne $class
!ll&&!rl&&r
171901
MC/DC50%1/2
$dng and $dng ne $class
$dng $dng ne $class
Truth table66%2/3
A and B
A= $dngB= $dng ne $class
ABresult
0X0
100
111
1141 1 ▶ return $self -> _dng() -> bone(@_) if $selfref;
Branch: 1 branch
BranchTrueFalseTotal
if $selfref 0 1 1
1142 1 return $dng -> bone(@_);
1143 }
1144  
1145 # Get the sign.
1146  
1147 1719 my $sign = '+'; # default is to return +1
1148 1719 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/) 170 1549 1719
Condition100%3/3
defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/
!ll&&!rl&&r
153811170
MC/DC100%2/2
defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/
defined $_[0] $_[0] =~ /^\s*([+-])\s*$/
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] =~ /^\s*([+-])\s*$/
ABresult
0X0
100
111
1149 170 $sign = $1;
1150 170 shift;
1151 }
1152  
1153 # Get the rounding parameters, if any.
1154  
1155 1719 my @r = @_;
1156  
1157 # If called as a class method, initialize a new object.
1158  
1159 1719 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1135 584 1719
1160  
1161 1719 $self -> {sign} = $sign;
1162 1719 $self -> {_m} = $LIB -> _one();
1163 1719 $self -> {_es} = '+';
1164 1719 $self -> {_e} = $LIB -> _zero();
1165  
1166 # If rounding parameters are given as arguments, use them. If no rounding
1167 # parameters are given, and if called as a class method initialize the new
1168 # instance with the class variables.
1169  
1170 #return $self -> round(@r); # this should work, but doesnt; fixme!
1171  
1172 1719 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 29 1690 1719
1173 29 ▶ if (@r >= 2 && defined($r[0]) && defined($r[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r >= 2 and defined $r[0] and defined $r[1]) 0 29 29
Condition83%5/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
2360
@r >= 2 and defined $r[0]
!ll&&!rl&&r
1496
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table75%3/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
1174 0 carp "can't specify both accuracy and precision";
1175 0 return $self -> bnan();
1176 }
1177 29 $self->{accuracy} = $_[0];
1178 29 $self->{precision} = $_[1];
1179 } else {
1180 1690 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1129 561 1690
1181 1129 $self->{accuracy} = $class -> accuracy();
1182 1129 $self->{precision} = $class -> precision();
1183 }
1184 }
1185  
1186 1719 return $self;
1187 }
1188  
1189 3027 ▶sub binf {
Subroutine binf: called CC=21 SCAR=31.9
Pod documented
1190 # create/assign a '+inf' or '-inf'
1191  
1192 # Class::method(...) -> Class->method(...)
1193 3027 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 3027 3027
Condition55%5/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
003027
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
39026370
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
26370390
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1194 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1195 {
1196 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1197 # " use is as a method instead";
1198 0 unshift @_, __PACKAGE__;
1199 }
1200  
1201 3027 my $self = shift;
1202 3027 my $selfref = ref $self;
1203 3027 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
39026370
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1204  
1205 {
1206 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1207 3027 ▶ if (${"${class}::_trap_inf"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_inf";}) 16 3011 3027
1208 16 croak("Tried to create +-inf in $class->binf()");
1209 }
1210 }
1211  
1212 # Make "require" work.
1213  
1214 3011 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 3011 3011
1215  
1216 # Don't modify constant (read-only) objects.
1217  
1218 3011 ▶ return $self if $selfref && $self -> modify('binf');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("binf") 0 3011 3011
Condition66%2/3
$selfref and $self->modify("binf")
!ll&&!rl&&r
26243870
MC/DC0%0/2
$selfref and $self->modify("binf")
$selfref $self->modify("binf")
Truth table66%2/3
A and B
A= $selfrefB= $self->modify("binf")
ABresult
0X0
100
111
1219  
1220 # Get the sign.
1221  
1222 3011 my $sign = '+'; # default is to return positive infinity
1223 3011 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])(inf|$)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i) 2927 84 3011
Condition100%3/3
defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i
!ll&&!rl&&r
8042927
MC/DC100%2/2
defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i
defined $_[0] $_[0] =~ /^\s*([+-])(inf|$)/i
Truth table100%3/3
A and B
A= defined $_[0]B= $_[0] =~ /^\s*([+-])(inf|$)/i
ABresult
0X0
100
111
1224 2927 $sign = $1;
1225 2927 shift;
1226 }
1227  
1228 # Get the rounding parameters, if any.
1229  
1230 3011 my @r = @_;
1231  
1232 # Downgrade?
1233  
1234 3011 my $dng = $class -> downgrade();
1235 3011 ▶ if ($dng && $dng ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dng and $dng ne $class) 10 3001 3011
Condition66%2/3
$dng and $dng ne $class
!ll&&!rl&&r
3001010
MC/DC50%1/2
$dng and $dng ne $class
$dng $dng ne $class
Truth table66%2/3
A and B
A= $dngB= $dng ne $class
ABresult
0X0
100
111
1236 10 ▶ return $self -> _dng() -> binf($sign, @r) if $selfref;
Branch: 1 branch
BranchTrueFalseTotal
if $selfref 8 2 10
1237 2 return $dng -> binf($sign, @r);
1238 }
1239  
1240 # If called as a class method, initialize a new object.
1241  
1242 3001 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 2622 379 3001
1243  
1244 3001 $self -> {sign} = $sign . 'inf';
1245 3001 $self -> {_m} = $LIB -> _zero();
1246 3001 $self -> {_es} = '+';
1247 3001 $self -> {_e} = $LIB -> _zero();
1248  
1249 # If rounding parameters are given as arguments, use them. If no rounding
1250 # parameters are given, and if called as a class method initialize the new
1251 # instance with the class variables.
1252  
1253 #return $self -> round(@r); # this should work, but doesnt; fixme!
1254  
1255 3001 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 919 2082 3001
1256 919 ▶ if (@r >= 2 && defined($r[0]) && defined($r[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r >= 2 and defined $r[0] and defined $r[1]) 0 919 919
Condition50%3/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
91900
@r >= 2 and defined $r[0]
!ll&&!rl&&r
129070
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table50%2/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
1257 0 carp "can't specify both accuracy and precision";
1258 0 return $self -> bnan();
1259 }
1260 919 $self->{accuracy} = $r[0];
1261 919 $self->{precision} = $r[1];
1262 } else {
1263 2082 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1755 327 2082
1264 1755 $self->{accuracy} = $class -> accuracy();
1265 1755 $self->{precision} = $class -> precision();
1266 }
1267 }
1268  
1269 3001 return $self;
1270 }
1271  
1272 2751 ▶sub bnan {
Subroutine bnan: called CC=19 SCAR=31.2
Pod documented
1273 # create/assign a 'NaN'
1274  
1275 # Class::method(...) -> Class->method(...)
1276 2751 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 2751 2751
Condition55%5/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
002751
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
106116900
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
169001061
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1277 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1278 {
1279 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1280 # " use is as a method instead";
1281 0 unshift @_, __PACKAGE__;
1282 }
1283  
1284 2751 my $self = shift;
1285 2751 my $selfref = ref $self;
1286 2751 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
106116900
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1287  
1288 {
1289 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1290 2751 ▶ if (${"${class}::_trap_nan"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_nan";}) 7 2744 2751
1291 7 croak("Tried to create NaN in $class->bnan()");
1292 }
1293 }
1294  
1295 # Make "require" work.
1296  
1297 2744 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 2744 2744
1298  
1299 # Don't modify constant (read-only) objects.
1300  
1301 2744 ▶ return $self if $selfref && $self -> modify('bnan');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bnan") 0 2744 2744
Condition66%2/3
$selfref and $self->modify("bnan")
!ll&&!rl&&r
168610580
MC/DC0%0/2
$selfref and $self->modify("bnan")
$selfref $self->modify("bnan")
Truth table66%2/3
A and B
A= $selfrefB= $self->modify("bnan")
ABresult
0X0
100
111
1302  
1303 2744 my $dng = $class -> downgrade();
1304 2744 ▶ if ($dng && $dng ne $class) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dng and $dng ne $class) 11 2733 2744
Condition66%2/3
$dng and $dng ne $class
!ll&&!rl&&r
2733011
MC/DC50%1/2
$dng and $dng ne $class
$dng $dng ne $class
Truth table66%2/3
A and B
A= $dngB= $dng ne $class
ABresult
0X0
100
111
1305 11 ▶ return $self -> _dng() -> bnan(@_) if $selfref;
Branch: 1 branch
BranchTrueFalseTotal
if $selfref 9 2 11
1306 2 return $dng -> bnan(@_);
1307 }
1308  
1309 # Get the rounding parameters, if any.
1310  
1311 2733 my @r = @_;
1312  
1313 # If called as a class method, initialize a new object.
1314  
1315 2733 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1684 1049 2733
1316  
1317 2733 $self -> {sign} = $nan;
1318 2733 $self -> {_m} = $LIB -> _zero();
1319 2733 $self -> {_es} = '+';
1320 2733 $self -> {_e} = $LIB -> _zero();
1321  
1322 # If rounding parameters are given as arguments, use them. If no rounding
1323 # parameters are given, and if called as a class method initialize the new
1324 # instance with the class variables.
1325  
1326 #return $self -> round(@r); # this should work, but doesnt; fixme!
1327  
1328 2733 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 752 1981 2733
1329 752 ▶ if (@r >= 2 && defined($r[0]) && defined($r[1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r >= 2 and defined $r[0] and defined $r[1]) 0 752 752
Condition50%3/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
75200
@r >= 2 and defined $r[0]
!ll&&!rl&&r
227300
MC/DC0%0/3
@r >= 2 and defined $r[0] and defined $r[1]
@r >= 2 defined $r[0] defined $r[1]
Truth table50%2/4
A and B and C
A= @r >= 2B= defined $r[0]C= defined $r[1]
ABCresult
0XX0
10X0
1100
1111
1330 0 carp "can't specify both accuracy and precision";
1331 0 return $self -> bnan();
1332 }
1333 752 $self->{accuracy} = $r[0];
1334 752 $self->{precision} = $r[1];
1335 } else {
1336 1981 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1028 953 1981
1337 1028 $self->{accuracy} = $class -> accuracy();
1338 1028 $self->{precision} = $class -> precision();
1339 }
1340 }
1341  
1342 2733 return $self;
1343 }
1344  
1345 257 ▶sub bpi {
Subroutine bpi: called CC=25 SCAR=39.0
Pod documented
1346  
1347 # Class::method(...) -> Class->method(...)
1348 257 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 1 256 257
Condition66%6/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
10256
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
831730
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
173083
MC/DC25%1/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table50%3/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1349 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1350 {
1351 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1352 # " use is as a method instead";
1353 1 unshift @_, __PACKAGE__;
1354 }
1355  
1356 # Called as Argument list
1357 # --------- -------------
1358 # Math::BigFloat->bpi() ("Math::BigFloat")
1359 # Math::BigFloat->bpi(10) ("Math::BigFloat", 10)
1360 # $x->bpi() ($x)
1361 # $x->bpi(10) ($x, 10)
1362 # Math::BigFloat::bpi() ()
1363 # Math::BigFloat::bpi(10) (10)
1364 #
1365 # In ambiguous cases, we favour the OO-style, so the following case
1366 #
1367 # $n = Math::BigFloat->new("10");
1368 # $x = Math::BigFloat->bpi($n);
1369 #
1370 # which gives an argument list with the single element $n, is resolved as
1371 #
1372 # $n->bpi();
1373  
1374 257 my $self = shift;
1375 257 my $selfref = ref $self;
1376 257 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
831740
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1377 257 my @r = @_; # rounding paramters
1378  
1379 # Make "require" work.
1380  
1381 257 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 0 257 257
1382  
1383 257 ▶ if ($selfref) { # bpi() called as an instance method
Branch: 1 branch
BranchTrueFalseTotal
if ($selfref) { } 83 174 257
1384 83 ▶ return $self if $self -> modify('bpi');
Branch: 1 branch
BranchTrueFalseTotal
if $self->modify("bpi") 0 83 83
1385 } else { # bpi() called as a class method
1386 174 $self = bless {}, $class; # initialize new instance
1387 }
1388  
1389 257 ($self, @r) = $self -> _find_round_parameters(@r);
1390  
1391 # The accuracy, i.e., the number of digits. Pi has one digit before the
1392 # dot, so a precision of 4 digits is equivalent to an accuracy of 5 digits.
1393  
1394 257 ▶ my $n = defined $r[0] ? $r[0]
Branch: 2 branches
BranchTrueFalseTotal
defined $r[0] ? : 243 14 257
defined $r[1] ? : 0 14 14
1395 : defined $r[1] ? 1 - $r[1]
1396 : $self -> div_scale();
1397  
1398 257 ▶ my $rmode = defined $r[2] ? $r[2] : $self -> round_mode();
Branch: 1 branch
BranchTrueFalseTotal
defined $r[2] ? : 243 14 257
1399  
1400 257 my $pi;
1401  
1402 257 ▶ if ($n <= 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 1000) { } 257 0 257
1403  
1404 # 75 x 14 = 1050 digits
1405  
1406 257 my $all_digits = <<EOF;
1407 314159265358979323846264338327950288419716939937510582097494459230781640628
1408 620899862803482534211706798214808651328230664709384460955058223172535940812
1409 848111745028410270193852110555964462294895493038196442881097566593344612847
1410 564823378678316527120190914564856692346034861045432664821339360726024914127
1411 372458700660631558817488152092096282925409171536436789259036001133053054882
1412 046652138414695194151160943305727036575959195309218611738193261179310511854
1413 807446237996274956735188575272489122793818301194912983367336244065664308602
1414 139494639522473719070217986094370277053921717629317675238467481846766940513
1415 200056812714526356082778577134275778960917363717872146844090122495343014654
1416 958537105079227968925892354201995611212902196086403441815981362977477130996
1417 051870721134999999837297804995105973173281609631859502445945534690830264252
1418 230825334468503526193118817101000313783875288658753320838142061717766914730
1419 359825349042875546873115956286388235378759375195778185778053217122680661300
1420 192787661119590921642019893809525720106548586327886593615338182796823030195
1421 EOF
1422  
1423 # Should we round up?
1424  
1425 257 my $round_up;
1426  
1427 # From the string above, we need to extract the number of digits we
1428 # want plus extra characters for the newlines.
1429  
1430 257 my $nchrs = $n + int($n / 75);
1431  
1432 # Extract the digits we want.
1433  
1434 257 my $digits = substr($all_digits, 0, $nchrs);
1435  
1436 # Find out whether we should round up or down. Rounding is easy, since
1437 # pi is trancendental. With directed rounding, it doesn't matter what
1438 # the following digits are. With rounding to nearest, we only have to
1439 # look at one extra digit.
1440  
1441 257 ▶ if ($rmode eq 'trunc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rmode eq 'trunc') { } 0 257 257
1442 0 $round_up = 0;
1443 } else {
1444 257 my $next_digit = substr($all_digits, $nchrs, 1);
1445 257 ▶ $round_up = $next_digit lt '5' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$next_digit lt '5' ? : 98 159 257
1446 }
1447  
1448 # Remove the newlines.
1449  
1450 257 $digits =~ tr/0-9//cd;
1451  
1452 # Now do the rounding. We could easily make the regex substitution
1453 # handle all cases, but we avoid using the regex engine when it is
1454 # simple to avoid it.
1455  
1456 257 ▶ if ($round_up) {
Branch: 1 branch
BranchTrueFalseTotal
if ($round_up) 159 98 257
1457 159 my $last_digit = substr($digits, -1, 1);
1458 159 ▶ if ($last_digit lt '9') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_digit lt '9') { } 140 19 159
1459 140 substr($digits, -1, 1) = ++$last_digit;
1460 } else {
1461 19 $digits =~ s{([0-8])(9+)$}
1462 19 { ($1 + 1) . ("0" x CORE::length($2)) }e;
1463 }
1464 }
1465  
1466 # Convert to an object.
1467  
1468 257 ▶ $pi = bless {
Branch: 1 branch
BranchTrueFalseTotal
CORE::length $digits > 1 ? : 257 0 257
1469 sign => '+',
1470 _m => $LIB -> _new($digits),
1471 _es => CORE::length($digits) > 1 ? '-' : '+',
1472 _e => $LIB -> _new($n - 1),
1473 }, $class;
1474  
1475 } else {
1476  
1477 # For large accuracy, the arctan formulas become very inefficient with
1478 # Math::BigFloat, so use Brent-Salamin (aka AGM or Gauss-Legendre).
1479  
1480 # Use a few more digits in the intermediate computations.
1481 0 $n += 8;
1482  
1483 0 ▶ $HALF = $class -> new($HALF) unless ref($HALF);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $HALF 0 0 0
1484 0 my ($an, $bn, $tn, $pn)
1485 = ($class -> bone, $HALF -> copy() -> bsqrt($n),
1486 $HALF -> copy() -> bmul($HALF), $class -> bone);
1487 0 ▶ while ($pn < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pn < $n) 0 0 0
1488 0 my $prev_an = $an -> copy();
1489 0 $an -> badd($bn) -> bmul($HALF, $n);
1490 0 $bn -> bmul($prev_an) -> bsqrt($n);
1491 0 $prev_an -> bsub($an);
1492 0 $tn -> bsub($pn * $prev_an * $prev_an);
1493 0 $pn -> badd($pn);
1494 }
1495 0 $an -> badd($bn);
1496 0 $an -> bmul($an, $n) -> bdiv(4 * $tn, $n);
1497  
1498 0 $an -> round(@r);
1499 0 $pi = $an;
1500 }
1501  
1502 257 ▶ if (defined $r[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r[0]) { } 243 14 257
elsif (defined $r[1]) { } 0 14 14
1503 243 $pi -> accuracy($r[0]);
1504 } elsif (defined $r[1]) {
1505 0 $pi -> precision($r[1]);
1506 }
1507  
1508 257 ▶ $pi -> _dng() if ($pi -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $pi->is_int or $pi->is_inf or $pi->is_nan 0 257 257
Condition33%2/6
$pi->is_int or $pi->is_inf or $pi->is_nan
l!l&&r!l&&!r
00257
$pi->is_int or $pi->is_inf
l!l&&r!l&&!r
00257
MC/DC0%0/3
$pi->is_int or $pi->is_inf or $pi->is_nan
$pi->is_int $pi->is_inf $pi->is_nan
Truth table25%1/4
A or B or C
A= $pi->is_intB= $pi->is_infC= $pi->is_nan
ABCresult
1XX1
01X1
0011
0000
1509 $pi -> is_inf() ||
1510 $pi -> is_nan());
1511  
1512 257 %$self = %$pi;
1513 257 bless $self, ref($pi);
1514 257 return $self;
1515 }
1516  
1517 22644 ▶sub copy {
Subroutine copy: called CC=5 SCAR=16.4
Pod documented
1518 22644 my ($x, $class);
1519 22644 ▶ if (ref($_[0])) { # $y = $x -> copy()
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 22644 0 22644
1520 22644 $x = shift;
1521 22644 $class = ref($x);
1522 } else { # $y = Math::BigInt -> copy($y)
1523 0 $class = shift;
1524 0 $x = shift;
1525 }
1526  
1527 22644 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 22644 22644
1528  
1529 22644 my $copy = bless {}, $class;
1530  
1531 22644 $copy->{sign} = $x->{sign};
1532 22644 $copy->{_es} = $x->{_es};
1533 22644 $copy->{_m} = $LIB->_copy($x->{_m});
1534 22644 $copy->{_e} = $LIB->_copy($x->{_e});
1535  
1536 22644 ▶ $copy->{accuracy} = $x->{accuracy} if exists $x->{accuracy};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{"accuracy"} 17708 4936 22644
1537 22644 ▶ $copy->{precision} = $x->{precision} if exists $x->{precision};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{"precision"} 17666 4978 22644
1538  
1539 22644 return $copy;
1540 }
1541  
1542 781 ▶sub as_int {
Subroutine as_int: called CC=5 SCAR=16.2
Pod documented
1543 781 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 781 0 781
1544  
1545 # Temporarily disable upgrading and downgrading.
1546  
1547 781 my $upg = Math::BigInt -> upgrade();
1548 781 my $dng = Math::BigInt -> downgrade();
1549 781 Math::BigInt -> upgrade(undef);
1550 781 Math::BigInt -> downgrade(undef);
1551  
1552 781 my $y;
1553 781 ▶ if ($x -> isa("Math::BigInt")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigInt')) { } 0 781 781
1554 0 $y = $x -> copy();
1555 } else {
1556 781 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 12 769 781
elsif ($x->is_nan) { } 4 765 769
1557 12 $y = Math::BigInt -> binf($x -> sign());
1558 } elsif ($x -> is_nan()) {
1559 4 $y = Math::BigInt -> bnan();
1560 } else {
1561 765 $y = Math::BigInt -> new($x -> copy() -> bint() -> bdstr());
1562 }
1563  
1564 # Copy the remaining instance variables.
1565  
1566 781 ($y->{accuracy}, $y->{precision}) = ($x->{accuracy}, $x->{precision});
1567 }
1568  
1569 781 $y -> round(@r);
1570  
1571 # Restore upgrading and downgrading.
1572  
1573 781 Math::BigInt -> upgrade($upg);
1574 781 Math::BigInt -> downgrade($dng);
1575  
1576 781 return $y;
1577 }
1578  
1579 2655 ▶sub as_rat {
Subroutine as_rat: called CC=5 SCAR=16.2
Pod documented
1580 2655 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2655 0 2655
1581  
1582 # Temporarily disable upgrading and downgrading.
1583  
1584 2655 require Math::BigRat;
1585 2655 my $upg = Math::BigRat -> upgrade();
1586 2655 my $dng = Math::BigRat -> downgrade();
1587 2655 Math::BigRat -> upgrade(undef);
1588 2655 Math::BigRat -> downgrade(undef);
1589  
1590 2655 my $y;
1591 2655 ▶ if ($x -> isa("Math::BigRat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigRat')) { } 0 2655 2655
1592 0 $y = $x -> copy();
1593 } else {
1594  
1595 2655 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 867 1788 2655
elsif ($x->is_nan) { } 654 1134 1788
1596 867 $y = Math::BigRat -> binf($x -> sign());
1597 } elsif ($x -> is_nan()) {
1598 654 $y = Math::BigRat -> bnan();
1599 } else {
1600 1134 $y = Math::BigRat -> new($x -> bfstr());
1601 }
1602  
1603 # Copy the remaining instance variables.
1604  
1605 2655 ($y->{accuracy}, $y->{precision}) = ($x->{accuracy}, $x->{precision});
1606 }
1607  
1608 2655 $y -> round(@r);
1609  
1610 # Restore upgrading and downgrading.
1611  
1612 2655 Math::BigRat -> upgrade($upg);
1613 2655 Math::BigRat -> downgrade($dng);
1614  
1615 2655 return $y;
1616 }
1617  
1618 46 ▶sub as_float {
Subroutine as_float: called CC=7 SCAR=28.2
Pod documented
1619 46 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 46 0 46
1620  
1621 # Disable upgrading and downgrading.
1622  
1623 46 require Math::BigFloat;
1624 46 my $upg = Math::BigFloat -> upgrade();
1625 46 my $dng = Math::BigFloat -> downgrade();
1626 46 Math::BigFloat -> upgrade(undef);
1627 46 Math::BigFloat -> downgrade(undef);
1628  
1629 46 my $y;
1630 46 ▶ if ($x -> isa("Math::BigFloat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigFloat')) { } 46 0 46
1631 46 $y = $x -> copy();
1632 } else {
1633 0 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 0 0 0
elsif ($x->is_nan) { } 0 0 0
1634 0 $y = Math::BigFloat -> binf($x -> sign());
1635 } elsif ($x -> is_nan()) {
1636 0 $y = Math::BigFloat -> bnan();
1637 } else {
1638 0 ▶ if ($x -> isa("Math::BigRat")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->isa('Math::BigRat')) { } 0 0 0
1639 0 ▶ if ($x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_int) { } 0 0 0
1640 0 $y = Math::BigFloat -> new($x -> bdstr());
1641 } else {
1642 0 my ($num, $den) = $x -> fparts();
1643 0 my $str = $num -> as_float() -> bdiv($den, @r) -> bdstr();
1644 0 $y = Math::BigFloat -> new($str);
1645 }
1646 } else {
1647 0 $y = Math::BigFloat -> new($x -> bdstr());
1648 }
1649 }
1650  
1651 # Copy the remaining instance variables.
1652  
1653 0 ($y->{accuracy}, $y->{precision}) = ($x->{accuracy}, $x->{precision});
1654 }
1655  
1656 46 $y -> round(@r);
1657  
1658 # Restore upgrading and downgrading.
1659  
1660 46 Math::BigFloat -> upgrade($upg);
1661 46 Math::BigFloat -> downgrade($dng);
1662  
1663 46 return $y;
1664 }
1665  
1666 ###############################################################################
1667 # Boolean methods
1668 ###############################################################################
1669  
1670 110816 ▶sub is_zero {
Subroutine is_zero: called CC=4 SCAR=13.9
Pod documented
1671 # return true if arg (BFLOAT or num_str) is zero
1672 110816 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 110812 4 110816
1673  
1674 110816 ▶ return 0 if $x->{sign} ne '+';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne "+" 8705 102111 110816
1675 102111 ▶ return 1 if $LIB->_is_zero($x->{_m});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 1852 100259 102111
1676 100259 return 0;
1677 }
1678  
1679 3055 ▶sub is_one {
Subroutine is_one: called CC=8 SCAR=20.9
Pod documented
1680 # return true if arg (BFLOAT or num_str) is +1 or -1 if signis given
1681 3055 ▶ my (undef, $x, $sign) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3051 4 3055
1682  
1683 3055 ▶ if (defined($sign)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sign) { } 1274 1781 3055
1684 1274 ▶ croak 'is_one(): sign argument must be "+" or "-"'
Branch: 1 branch
BranchTrueFalseTotal
unless $sign eq "+" or $sign eq "-" 0 1274 1274
Condition66%2/3
$sign eq "+" or $sign eq "-"
l!l&&r!l&&!r
6815930
MC/DC0%0/2
$sign eq "+" or $sign eq "-"
$sign eq "+" $sign eq "-"
Truth table66%2/3
A or B
A= $sign eq "+"B= $sign eq "-"
ABresult
1X1
011
000
1685 unless $sign eq '+' || $sign eq '-';
1686 } else {
1687 1781 $sign = '+';
1688 }
1689  
1690 3055 ▶ return 0 if $x->{sign} ne $sign;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $sign 810 2245 3055
1691 2245 ▶ $LIB->_is_zero($x->{_e}) && $LIB->_is_one($x->{_m}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$LIB->_is_zero($x->{'_e'}) && $LIB->_is_one($x->{'_m'}) ? : 305 1940 2245
Condition100%3/3
$LIB->_is_zero($x->{"_e"}) and $LIB->_is_one($x->{"_m"})
!ll&&!rl&&r
942998305
MC/DC100%2/2
$LIB->_is_zero($x->{"_e"}) and $LIB->_is_one($x->{"_m"})
$LIB->_is_zero($x->{"_e"}) $LIB->_is_one($x->{"_m"})
Truth table100%3/3
A and B
A= $LIB->_is_zero($x->{"_e"})B= $LIB->_is_one($x->{"_m"})
ABresult
0X0
100
111
1692 }
1693  
1694 108 ▶sub is_odd {
Subroutine is_odd: called CC=5 SCAR=16.1
Pod documented
1695 # return true if arg (BFLOAT or num_str) is odd or false if even
1696 108 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 108 0 108
1697  
1698 108 ▶ return 0 unless $x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 16 92 108
1699 92 ▶ $LIB->_is_zero($x->{_e}) && $LIB->_is_odd($x->{_m}) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$LIB->_is_zero($x->{'_e'}) && $LIB->_is_odd($x->{'_m'}) ? : 44 48 92
Condition100%3/3
$LIB->_is_zero($x->{"_e"}) and $LIB->_is_odd($x->{"_m"})
!ll&&!rl&&r
202844
MC/DC100%2/2
$LIB->_is_zero($x->{"_e"}) and $LIB->_is_odd($x->{"_m"})
$LIB->_is_zero($x->{"_e"}) $LIB->_is_odd($x->{"_m"})
Truth table100%3/3
A and B
A= $LIB->_is_zero($x->{"_e"})B= $LIB->_is_odd($x->{"_m"})
ABresult
0X0
100
111
1700 }
1701  
1702 72 ▶sub is_even {
Subroutine is_even: called CC=5 SCAR=16.1
Pod documented
1703 # return true if arg (BINT or num_str) is even or false if odd
1704 72 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 72 0 72
1705  
1706 72 ▶ return 0 unless $x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 12 60 72
1707 ($x->{_es} eq '+') && # 123.45 isn't
1708 60 ▶ ($LIB->_is_even($x->{_m})) ? 1 : 0; # but 1200 is
Branch: 1 branch
BranchTrueFalseTotal
$x->{'_es'} eq '+' && $LIB->_is_even($x->{'_m'}) ? : 24 36 60
Condition100%3/3
$x->{"_es"} eq "+" and $LIB->_is_even($x->{"_m"})
!ll&&!rl&&r
162024
MC/DC100%2/2
$x->{"_es"} eq "+" and $LIB->_is_even($x->{"_m"})
$x->{"_es"} eq "+" $LIB->_is_even($x->{"_m"})
Truth table100%3/3
A and B
A= $x->{"_es"} eq "+"B= $LIB->_is_even($x->{"_m"})
ABresult
0X0
100
111
1709 }
1710  
1711 157351 ▶sub is_int {
Subroutine is_int: called CC=4 SCAR=13.9
Pod documented
1712 # return true if arg (BFLOAT or num_str) is an integer
1713 157351 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 157351 0 157351
1714  
1715 157351 ▶ return 0 unless $x -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_finite 113 157238 157351
1716 157238 ▶ return $x->{_es} eq '+' ? 1 : 0; # 1e-1 => no integer
Branch: 1 branch
BranchTrueFalseTotal
$x->{'_es'} eq '+' ? : 42572 114666 157238
1717 }
1718  
1719 ###############################################################################
1720 # Comparison methods
1721 ###############################################################################
1722  
1723 4037 ▶sub bcmp {
Subroutine bcmp: called CC=40 SCAR=37.0
Pod documented
1724 # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
1725  
1726 # set up parameters
1727 4037 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 3884 153 4037
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
01533884
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1728 ? (ref($_[0]), @_)
1729 : objectify(2, @_);
1730  
1731 4037 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 4037 4037
1732  
1733 # Handle all 'nan' cases.
1734  
1735 4037 ▶ return if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 58 3979 4037
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
32263979
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
1736  
1737 # Handle all '+inf' and '-inf' cases.
1738  
1739 3979 ▶ return 0 if ($x -> is_inf("+") && $y -> is_inf("+") ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") and $y->is_inf("+") or $x->is_inf("-") and $y->is_inf("-") 8 3971 3979
Condition100%9/9
$x->is_inf("+") and $y->is_inf("+") or $x->is_inf("-") and $y->is_inf("-")
l!l&&r!l&&!r
443971
$x->is_inf("+") and $y->is_inf("+")
!ll&&!rl&&r
3919564
$x->is_inf("-") and $y->is_inf("-")
!ll&&!rl&&r
3911604
MC/DC100%4/4
$x->is_inf("+") and $y->is_inf("+") or $x->is_inf("-") and $y->is_inf("-")
$x->is_inf("+") $y->is_inf("+") $x->is_inf("-") $y->is_inf("-")
Truth table71%5/7
A and B or C and D
A= $x->is_inf("+")B= $y->is_inf("+")C= $x->is_inf("-")D= $y->is_inf("-")
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1740 $x -> is_inf("-") && $y -> is_inf("-"));
1741 3971 ▶ return +1 if $x -> is_inf("+"); # x = +inf and y < +inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 56 3915 3971
1742 3915 ▶ return -1 if $x -> is_inf("-"); # x = -inf and y > -inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("-") 60 3855 3915
1743 3855 ▶ return -1 if $y -> is_inf("+"); # x < +inf and y = +inf
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_inf("+") 0 3855 3855
1744 3855 ▶ return +1 if $y -> is_inf("-"); # x > -inf and y = -inf
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_inf("-") 0 3855 3855
1745  
1746 # Handle all cases with opposite signs.
1747  
1748 3855 ▶ return +1 if $x->{sign} eq '+' && $y->{sign} eq '-'; # also does 0 <=> -y
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+" and $y->{"sign"} eq "-" 452 3403 3855
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "-"
!ll&&!rl&&r
4582945452
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
1749 3403 ▶ return -1 if $x->{sign} eq '-' && $y->{sign} eq '+'; # also does -x <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" and $y->{"sign"} eq "+" 306 3097 3403
Condition100%3/3
$x->{"sign"} eq "-" and $y->{"sign"} eq "+"
!ll&&!rl&&r
2945152306
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
1750  
1751 # Handle all remaining zero cases.
1752  
1753 3097 my $xz = $x -> is_zero();
1754 3097 my $yz = $y -> is_zero();
1755 3097 ▶ return 0 if $xz && $yz; # 0 <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $yz 134 2963 3097
Condition100%3/3
$xz and $yz
!ll&&!rl&&r
2843120134
MC/DC100%2/2
$xz and $yz
$xz $yz
Truth table100%3/3
A and B
A= $xzB= $yz
ABresult
0X0
100
111
1756 2963 ▶ return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $y->{"sign"} eq "+" 120 2843 2963
Condition66%2/3
$xz and $y->{"sign"} eq "+"
!ll&&!rl&&r
28430120
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
1757 2843 ▶ return +1 if $yz && $x->{sign} eq '+'; # +x <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $yz and $x->{"sign"} eq "+" 588 2255 2843
Condition66%2/3
$yz and $x->{"sign"} eq "+"
!ll&&!rl&&r
22550588
MC/DC50%1/2
$yz and $x->{"sign"} eq "+"
$yz $x->{"sign"} eq "+"
Truth table66%2/3
A and B
A= $yzB= $x->{"sign"} eq "+"
ABresult
0X0
100
111
1758  
1759 # Both arguments are now finite, non-zero numbers with the same sign.
1760  
1761 2255 my $cmp;
1762  
1763 # The next step is to compare the exponents, but since each mantissa is an
1764 # integer of arbitrary value, the exponents must be normalized by the
1765 # length of the mantissas before we can compare them.
1766  
1767 2255 my $mxl = $LIB->_len($x->{_m});
1768 2255 my $myl = $LIB->_len($y->{_m});
1769  
1770 # If the mantissas have the same length, there is no point in normalizing
1771 # the exponents by the length of the mantissas, so treat that as a special
1772 # case.
1773  
1774 2255 ▶ if ($mxl == $myl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mxl == $myl) 1475 780 2255
1775  
1776 # First handle the two cases where the exponents have different signs.
1777  
1778 1475 ▶ if ($x->{_es} eq '+' && $y->{_es} eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'_es'} eq '+' and $y->{'_es'} eq '-') { } 0 1475 1475
elsif ($x->{'_es'} eq '-' and $y->{'_es'} eq '+') { } 20 1455 1475
Condition66%2/3
$x->{"_es"} eq "+" and $y->{"_es"} eq "-"
!ll&&!rl&&r
12613490
MC/DC0%0/2
$x->{"_es"} eq "+" and $y->{"_es"} eq "-"
$x->{"_es"} eq "+" $y->{"_es"} eq "-"
Truth table66%2/3
A and B
A= $x->{"_es"} eq "+"B= $y->{"_es"} eq "-"
ABresult
0X0
100
111
1779 0 $cmp = +1;
1780 ▶ } elsif ($x->{_es} eq '-' && $y->{_es} eq '+') {
Condition100%3/3
$x->{"_es"} eq "-" and $y->{"_es"} eq "+"
!ll&&!rl&&r
134910620
MC/DC100%2/2
$x->{"_es"} eq "-" and $y->{"_es"} eq "+"
$x->{"_es"} eq "-" $y->{"_es"} eq "+"
Truth table100%3/3
A and B
A= $x->{"_es"} eq "-"B= $y->{"_es"} eq "+"
ABresult
0X0
100
111
1781 20 $cmp = -1;
1782 }
1783  
1784 # Then handle the case where the exponents have the same sign.
1785  
1786 else {
1787 1455 $cmp = $LIB->_acmp($x->{_e}, $y->{_e});
1788 1455 ▶ $cmp = -$cmp if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 106 1349 1455
1789 }
1790  
1791 # Adjust for the sign, which is the same for x and y, and bail out if
1792 # we're done.
1793  
1794 1475 ▶ $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 136 1339 1475
1795 1475 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 76 1399 1475
1796  
1797 }
1798  
1799 # We must normalize each exponent by the length of the corresponding
1800 # mantissa. Life is a lot easier if we first make both exponents
1801 # non-negative. We do this by adding the same positive value to both
1802 # exponent. This is safe, because when comparing the exponents, only the
1803 # relative difference is important.
1804  
1805 2179 my $ex;
1806 my $ey;
1807  
1808 2179 ▶ if ($x->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'_es'} eq '+') { } 1400 779 2179
1809  
1810 # If the exponent of x is >= 0 and the exponent of y is >= 0, there is
1811 # no need to do anything special.
1812  
1813 1400 ▶ if ($y->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{'_es'} eq '+') { } 1367 33 1400
1814 1367 $ex = $LIB->_copy($x->{_e});
1815 1367 $ey = $LIB->_copy($y->{_e});
1816 }
1817  
1818 # If the exponent of x is >= 0 and the exponent of y is < 0, add the
1819 # absolute value of the exponent of y to both.
1820  
1821 else {
1822 33 $ex = $LIB->_copy($x->{_e});
1823 33 $ex = $LIB->_add($ex, $y->{_e}); # ex + |ey|
1824 33 $ey = $LIB->_zero(); # -ex + |ey| = 0
1825 }
1826  
1827 } else {
1828  
1829 # If the exponent of x is < 0 and the exponent of y is >= 0, add the
1830 # absolute value of the exponent of x to both.
1831  
1832 779 ▶ if ($y->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{'_es'} eq '+') { } 49 730 779
1833 49 $ex = $LIB->_zero(); # -ex + |ex| = 0
1834 49 $ey = $LIB->_copy($y->{_e});
1835 49 $ey = $LIB->_add($ey, $x->{_e}); # ey + |ex|
1836 }
1837  
1838 # If the exponent of x is < 0 and the exponent of y is < 0, add the
1839 # absolute values of both exponents to both exponents.
1840  
1841 else {
1842 730 $ex = $LIB->_copy($y->{_e}); # -ex + |ey| + |ex| = |ey|
1843 730 $ey = $LIB->_copy($x->{_e}); # -ey + |ex| + |ey| = |ex|
1844 }
1845  
1846 }
1847  
1848 # Now we can normalize the exponents by adding lengths of the mantissas.
1849  
1850 2179 $ex = $LIB->_add($ex, $LIB->_new($mxl));
1851 2179 $ey = $LIB->_add($ey, $LIB->_new($myl));
1852  
1853 # We're done if the exponents are different.
1854  
1855 2179 $cmp = $LIB->_acmp($ex, $ey);
1856 2179 ▶ $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 144 2035 2179
1857 2179 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 650 1529 2179
1858  
1859 # Compare the mantissas, but first normalize them by padding the shorter
1860 # mantissa with zeros (shift left) until it has the same length as the
1861 # longer mantissa.
1862  
1863 1529 my $mx = $x->{_m};
1864 1529 my $my = $y->{_m};
1865  
1866 1529 ▶ if ($mxl > $myl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mxl > $myl) { } 49 1480 1529
elsif ($mxl < $myl) { } 81 1399 1480
1867 49 $my = $LIB->_lsft($LIB->_copy($my), $LIB->_new($mxl - $myl), 10);
1868 } elsif ($mxl < $myl) {
1869 81 $mx = $LIB->_lsft($LIB->_copy($mx), $LIB->_new($myl - $mxl), 10);
1870 }
1871  
1872 1529 $cmp = $LIB->_acmp($mx, $my);
1873 1529 ▶ $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 136 1393 1529
1874 1529 return $cmp;
1875  
1876 }
1877  
1878 9028 ▶sub bacmp {
Subroutine bacmp: called CC=23 SCAR=31.5
Pod documented
1879 # Compares 2 values, ignoring their signs.
1880 # Returns one of undef, <0, =0, >0. (suitable for sort)
1881  
1882 # set up parameters
1883 9028 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 9028 0 9028
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
009028
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
1884 ? (ref($_[0]), @_)
1885 : objectify(2, @_);
1886  
1887 9028 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 9028 9028
1888  
1889 # handle +-inf and NaN
1890 9028 ▶ if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/) 92 8936 9028
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
72208936
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
1891 92 ▶ return if ($x -> is_nan() || $y -> is_nan());
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 28 64 92
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
161264
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
1892 64 ▶ return 0 if ($x -> is_inf() && $y -> is_inf());
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf and $y->is_inf 16 48 64
Condition100%3/3
$x->is_inf and $y->is_inf
!ll&&!rl&&r
163216
MC/DC100%2/2
$x->is_inf and $y->is_inf
$x->is_inf $y->is_inf
Truth table100%3/3
A and B
A= $x->is_infB= $y->is_inf
ABresult
0X0
100
111
1893 48 ▶ return 1 if ($x -> is_inf() && !$y -> is_inf());
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf and not $y->is_inf 32 16 48
Condition66%2/3
$x->is_inf and not $y->is_inf
!ll&&!rl&&r
16032
MC/DC50%1/2
$x->is_inf and not $y->is_inf
$x->is_inf not $y->is_inf
Truth table66%2/3
A and not(B)
A= $x->is_infB= not $y->is_inf
ABresult
0X0
100
111
1894 16 return -1;
1895 }
1896  
1897 # shortcut
1898 8936 my $xz = $x -> is_zero();
1899 8936 my $yz = $y -> is_zero();
1900 8936 ▶ return 0 if $xz && $yz; # 0 <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $yz 4 8932 8936
Condition100%3/3
$xz and $yz
!ll&&!rl&&r
8892404
MC/DC100%2/2
$xz and $yz
$xz $yz
Truth table100%3/3
A and B
A= $xzB= $yz
ABresult
0X0
100
111
1901 8932 ▶ return -1 if $xz && !$yz; # 0 <=> +y
Branch: 1 branch
BranchTrueFalseTotal
if $xz and not $yz 40 8892 8932
Condition66%2/3
$xz and not $yz
!ll&&!rl&&r
8892040
MC/DC50%1/2
$xz and not $yz
$xz not $yz
Truth table66%2/3
A and not(B)
A= $xzB= not $yz
ABresult
0X0
100
111
1902 8892 ▶ return 1 if $yz && !$xz; # +x <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $yz and not $xz 40 8852 8892
Condition66%2/3
$yz and not $xz
!ll&&!rl&&r
8852040
MC/DC50%1/2
$yz and not $xz
$yz not $xz
Truth table66%2/3
A and not(B)
A= $yzB= not $xz
ABresult
0X0
100
111
1903  
1904 # adjust so that exponents are equal
1905 8852 my $lxm = $LIB->_len($x->{_m});
1906 8852 my $lym = $LIB->_len($y->{_m});
1907 8852 my ($xes, $yes) = (1, 1);
1908 8852 ▶ $xes = -1 if $x->{_es} ne '+';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} ne "+" 8219 633 8852
1909 8852 ▶ $yes = -1 if $y->{_es} ne '+';
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"_es"} ne "+" 8033 819 8852
1910 # the numify somewhat limits our length, but makes it much faster
1911 8852 my $lx = $lxm + $xes * $LIB->_num($x->{_e});
1912 8852 my $ly = $lym + $yes * $LIB->_num($y->{_e});
1913 8852 my $l = $lx - $ly;
1914 8852 ▶ return $l <=> 0 if $l != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $l != 0 4819 4033 8852
1915  
1916 # lengths (corrected by exponent) are equal
1917 # so make mantissa equal-length by padding with zero (shift left)
1918 4033 my $diff = $lxm - $lym;
1919 4033 my $xm = $x->{_m}; # not yet copy it
1920 4033 my $ym = $y->{_m};
1921 4033 ▶ if ($diff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 597 3436 4033
elsif ($diff < 0) { } 309 3127 3436
1922 597 $ym = $LIB->_copy($y->{_m});
1923 597 $ym = $LIB->_lsft($ym, $LIB->_new($diff), 10);
1924 } elsif ($diff < 0) {
1925 309 $xm = $LIB->_copy($x->{_m});
1926 309 $xm = $LIB->_lsft($xm, $LIB->_new(-$diff), 10);
1927 }
1928 4033 $LIB->_acmp($xm, $ym);
1929 }
1930  
1931 ###############################################################################
1932 # Arithmetic methods
1933 ###############################################################################
1934  
1935 1952 ▶sub bneg {
Subroutine bneg: called CC=8 SCAR=20.8
Pod documented
1936 # (BINT or num_str) return BINT
1937 # negate number or make a negated number from string
1938 1952 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1952 0 1952
1939  
1940 # Don't modify constant (read-only) objects.
1941  
1942 1952 ▶ return $x if $x -> modify('bneg');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bneg") 0 1952 1952
1943  
1944 # For +0 do not negate (to have always normalized +0).
1945 $x->{sign} =~ tr/+-/-+/
1946 1952 ▶ unless $x->{sign} eq '+' && $LIB->_is_zero($x->{_m});
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_m"}) 1915 37 1952
Condition100%3/3
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_m"})
!ll&&!rl&&r
277163837
MC/DC100%2/2
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_m"})
$x->{"sign"} eq "+" $LIB->_is_zero($x->{"_m"})
Truth table100%3/3
A and B
A= $x->{"sign"} eq "+"B= $LIB->_is_zero($x->{"_m"})
ABresult
0X0
100
111
1947  
1948 1952 $x -> round(@r);
1949 1952 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 1208 744 1952
Condition100%6/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
11999744
$x->is_int or $x->is_inf
l!l&&r!l&&!r
117029753
MC/DC100%3/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table100%4/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
1950 $x -> is_inf() ||
1951 $x -> is_nan());
1952 1952 return $x;
1953 }
1954  
1955 78586 ▶sub bnorm {
Subroutine bnorm: called CC=10 SCAR=23.0
Pod documented
1956 # bnorm() can't support rounding, because bround() and bfround() call
1957 # bnorm(), which would recurse indefinitely.
1958  
1959 # adjust m and e so that m is smallest possible
1960 78586 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 78586 0 78586
1961  
1962 78586 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 78586 78586
1963  
1964 # inf and nan
1965 78586 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 2 78584 78586
1966 2 $x -> round(@r);
1967 2 $x -> _dng();
1968 2 return $x;
1969 }
1970  
1971 78584 my $zeros = $LIB->_zeros($x->{_m}); # correct for trailing zeros
1972 78584 ▶ if ($zeros != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zeros != 0) { } 33451 45133 78584
1973 33451 my $z = $LIB->_new($zeros);
1974 33451 $x->{_m} = $LIB->_rsft($x->{_m}, $z, 10);
1975 33451 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 31708 1743 33451
1976 31708 ▶ if ($LIB->_acmp($x->{_e}, $z) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($x->{'_e'}, $z) >= 0) { } 31350 358 31708
1977 31350 $x->{_e} = $LIB->_sub($x->{_e}, $z);
1978 31350 ▶ $x->{_es} = '+' if $LIB->_is_zero($x->{_e});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_e"}) 355 30995 31350
1979 } else {
1980 358 $x->{_e} = $LIB->_sub($LIB->_copy($z), $x->{_e});
1981 358 $x->{_es} = '+';
1982 }
1983 } else {
1984 1743 $x->{_e} = $LIB->_add($x->{_e}, $z);
1985 }
1986 } else {
1987 # $x can only be 0Ey if there are no trailing zeros ('0' has 0 trailing
1988 # zeros). So, for something like 0Ey, set y to 0, and -0 => +0
1989 45133 ▶ if ($LIB->_is_zero($x->{_m})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{"_m"})) 423 44710 45133
1990 423 $x->{sign} = '+';
1991 423 $x->{_es} = '+';
1992 423 $x->{_e} = $LIB->_zero();
1993 }
1994 }
1995  
1996 # Inf and NaN was handled above, so no need to check for this.
1997  
1998 78584 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 17541 61043 78584
1999 78584 return $x;
2000 }
2001  
2002 5045 ▶sub binc {
Subroutine binc: called CC=13 SCAR=28.1
Pod documented
2003 # increment arg by one
2004 5045 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5045 0 5045
2005  
2006 # Don't modify constant (read-only) objects.
2007  
2008 5045 ▶ return $x if $x -> modify('binc');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("binc") 0 5045 5045
2009  
2010 # Inf and NaN
2011  
2012 5045 ▶ if ($x -> is_inf() || $x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf or $x->is_nan) 14 5031 5045
Condition100%3/3
$x->is_inf or $x->is_nan
l!l&&r!l&&!r
955031
MC/DC100%2/2
$x->is_inf or $x->is_nan
$x->is_inf $x->is_nan
Truth table100%3/3
A or B
A= $x->is_infB= $x->is_nan
ABresult
1X1
011
000
2013 14 $x -> round(@r);
2014 14 $x -> _dng();
2015 14 return $x
2016 }
2017  
2018 # Non-integer
2019  
2020 5031 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 468 4563 5031
2021 468 return $x -> badd($class -> bone(), @r);
2022 }
2023  
2024 # If the exponent is non-zero, convert the internal representation, so
2025 # that, e.g., 12e+3 becomes 12000e+0 and we can easily increment the
2026 # mantissa.
2027  
2028 4563 ▶ if (!$LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 404 4159 4563
2029 404 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10); # 1e2 => 100
2030 404 $x->{_e} = $LIB->_zero(); # normalize
2031 404 $x->{_es} = '+';
2032 # we know that the last digit of $x will be '1' or '9', depending on
2033 # the sign
2034 }
2035  
2036 # now $x->{_e} == 0
2037 4563 ▶ if ($x->{sign} eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '+') { } 4547 16 4563
elsif ($x->{'sign'} eq '-') { } 16 0 16
2038 4547 $x->{_m} = $LIB->_inc($x->{_m});
2039 4547 return $x -> bnorm() -> bround(@r);
2040 } elsif ($x->{sign} eq '-') {
2041 16 $x->{_m} = $LIB->_dec($x->{_m});
2042 16 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{_m}); # -1 +1 => -0 => +0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 4 12 16
2043 16 return $x -> bnorm() -> bround(@r);
2044 }
2045  
2046 0 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 0 0 0
Condition0%0/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
000
$x->is_int or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table0%0/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
2047 $x -> is_inf() ||
2048 $x -> is_nan());
2049 0 return $x;
2050 }
2051  
2052 174 ▶sub bdec {
Subroutine bdec: called CC=15 SCAR=27.7
Pod documented
2053 # decrement arg by one
2054 174 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 174 0 174
2055  
2056 # Don't modify constant (read-only) objects.
2057  
2058 174 ▶ return $x if $x -> modify('bdec');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdec") 0 174 174
2059  
2060 # Inf and NaN
2061  
2062 174 ▶ if ($x -> is_inf() || $x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf or $x->is_nan) 14 160 174
Condition100%3/3
$x->is_inf or $x->is_nan
l!l&&r!l&&!r
95160
MC/DC100%2/2
$x->is_inf or $x->is_nan
$x->is_inf $x->is_nan
Truth table100%3/3
A or B
A= $x->is_infB= $x->is_nan
ABresult
1X1
011
000
2063 14 $x -> round(@r);
2064 14 $x -> _dng();
2065 14 return $x
2066 }
2067  
2068 # Non-integer
2069  
2070 160 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 120 40 160
2071 120 return $x -> badd($class -> bone('-'), @r);
2072 }
2073  
2074 # If the exponent is non-zero, convert the internal representation, so
2075 # that, e.g., 12e+3 becomes 12000e+0 and we can easily increment the
2076 # mantissa.
2077  
2078 40 ▶ if (!$LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 8 32 40
2079 8 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10); # 1e2 => 100
2080 8 $x->{_e} = $LIB->_zero(); # normalize
2081 8 $x->{_es} = '+';
2082 }
2083  
2084 # now $x->{_e} == 0
2085 40 my $zero = $x -> is_zero();
2086 40 ▶ if (($x->{sign} eq '-') || $zero) { # x <= 0
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '-' or $zero) { } 21 19 40
elsif ($x->{'sign'} eq '+') { } 19 0 19
Condition100%3/3
$x->{"sign"} eq "-" or $zero
l!l&&r!l&&!r
16519
MC/DC100%2/2
$x->{"sign"} eq "-" or $zero
$x->{"sign"} eq "-" $zero
Truth table100%3/3
A or B
A= $x->{"sign"} eq "-"B= $zero
ABresult
1X1
011
000
2087 21 $x->{_m} = $LIB->_inc($x->{_m});
2088 21 ▶ $x->{sign} = '-' if $zero; # 0 => 1 => -1
Branch: 1 branch
BranchTrueFalseTotal
if $zero 5 16 21
2089 21 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{_m}); # -1 +1 => -0 => +0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 0 21 21
2090 21 $x -> bnorm();
2091 }
2092 elsif ($x->{sign} eq '+') { # x > 0
2093 19 $x->{_m} = $LIB->_dec($x->{_m});
2094 19 $x -> bnorm();
2095 }
2096  
2097 40 $x -> round(@r);
2098 40 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 40 0 40
Condition33%2/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
4000
$x->is_int or $x->is_inf
l!l&&r!l&&!r
4000
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table25%1/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
2099 $x -> is_inf() ||
2100 $x -> is_nan());
2101 40 return $x;
2102 }
2103  
2104 14129 ▶sub badd {
Subroutine badd: called CC=25 SCAR=32.3
Pod documented
2105 # set up parameters
2106 14129 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 14064 65 14129
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
65914064
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2107 ? (ref($_[0]), @_)
2108 : objectify(2, @_);
2109  
2110 # Don't modify constant (read-only) objects.
2111  
2112 14129 ▶ return $x if $x -> modify('badd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("badd") 0 14129 14129
2113  
2114 14129 ▶ unless ($x -> is_finite() && $y -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 120 14009 14129
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
903014009
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
2115  
2116 120 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 58 62 120
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
332562
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
2117  
2118 62 ▶ return $x -> is_inf("+") ? ($y -> is_inf("-") ? $x -> bnan(@r)
Branch: 5 branches
BranchTrueFalseTotal
$x->is_inf('+') ? : 23 39 62
$y->is_inf('-') ? : 6 17 23
$x->is_inf('-') ? : 22 17 39
$y->is_inf('+') ? : 6 16 22
$y->is_inf('+') ? : 9 8 17
2119 : $x -> binf("+", @r))
2120 : $x -> is_inf("-") ? ($y -> is_inf("+") ? $x -> bnan(@r)
2121 : $x -> binf("-", @r))
2122 : ($y -> is_inf("+") ? $x -> binf("+", @r)
2123 : $x -> binf("-", @r));
2124 }
2125  
2126 14009 ▶ return $x -> _upg() -> badd($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 1 14008 14009
2127  
2128 14008 $r[3] = $y; # no push!
2129  
2130 # for speed: no add for $x + 0
2131 14008 ▶ if ($y -> is_zero()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_zero) { } 56 13952 14008
elsif ($x->is_zero) { } 104 13848 13952
2132 56 $x -> round(@r);
2133 }
2134  
2135 # for speed: no add for 0 + $y
2136 elsif ($x -> is_zero()) {
2137 # make copy, clobbering up x (modify in place!)
2138 104 $x->{_e} = $LIB->_copy($y->{_e});
2139 104 $x->{_es} = $y->{_es};
2140 104 $x->{_m} = $LIB->_copy($y->{_m});
2141 104 ▶ $x->{sign} = $y->{sign} || $nan;
Condition33%1/3
$y->{'sign'} || $nan
l!l&&r!l&&!r
10400
MC/DC0%0/2
$y->{'sign'} || $nan
$y->{'sign'} $nan
Truth table33%1/3
A || B
A= $y->{'sign'}B= $nan
ABresult
1X1
011
000
2142 104 $x -> round(@r);
2143 }
2144  
2145 # both $x and $y are non-zero
2146 else {
2147  
2148 # take lower of the two e's and adapt m1 to it to match m2
2149 13848 my $e = $y->{_e};
2150 13848 ▶ $e = $LIB->_zero() if !defined $e; # if no BFLOAT?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 0 13848 13848
2151 13848 $e = $LIB->_copy($e); # make copy (didn't do it yet)
2152  
2153 13848 my $es;
2154  
2155 13848 ▶ ($e, $es) = $LIB -> _ssub($e, $y->{_es} || '+', $x->{_e}, $x->{_es});
Condition50%1/2
$y->{'_es'} || '+'
l!l
138480
MC/DC0%0/1
$y->{'_es'} || '+'
$y->{'_es'}
Truth table50%1/2
A
A= $y->{'_es'}
Aresult
11
00
2156  
2157 13848 my $add = $LIB->_copy($y->{_m});
2158  
2159 13848 ▶ if ($es eq '-') { # < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($es eq '-') { } 8035 5813 13848
elsif (not $LIB->_is_zero($e)) { } 821 4992 5813
2160 8035 $x->{_m} = $LIB->_lsft($x->{_m}, $e, 10);
2161 8035 ($x->{_e}, $x->{_es}) = $LIB -> _sadd($x->{_e}, $x->{_es}, $e, $es);
2162 } elsif (!$LIB->_is_zero($e)) { # > 0
2163 821 $add = $LIB->_lsft($add, $e, 10);
2164 }
2165  
2166 # else: both e are the same, so just leave them
2167  
2168 13848 ▶ if ($x->{sign} eq $y->{sign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq $y->{'sign'}) { } 12078 1770 13848
2169 12078 $x->{_m} = $LIB->_add($x->{_m}, $add);
2170 } else {
2171 ($x->{_m}, $x->{sign}) =
2172 1770 $LIB -> _sadd($x->{_m}, $x->{sign}, $add, $y->{sign});
2173 }
2174  
2175 # delete trailing zeros, then round
2176 13848 $x -> bnorm() -> round(@r);
2177 }
2178  
2179 14008 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 5063 8945 14008
Condition83%5/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
505768945
$x->is_int or $x->is_inf
l!l&&r!l&&!r
505708951
MC/DC66%2/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table75%3/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
2180 $x -> is_inf() ||
2181 $x -> is_nan());
2182 14008 return $x;
2183 }
2184  
2185 1528 ▶sub bsub {
Subroutine bsub: called CC=13 SCAR=25.7
Pod documented
2186 # set up parameters
2187 1528 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1495 33 1528
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
6271495
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2188 ? (ref($_[0]), @_)
2189 : objectify(2, @_);
2190  
2191 # Don't modify constant (read-only) objects.
2192  
2193 1528 ▶ return $x if $x -> modify('bsub');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsub") 0 1528 1528
2194  
2195 1528 $r[3] = $y; # no push!
2196  
2197 1528 ▶ unless ($x -> is_finite() && $y -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->is_finite and $y->is_finite) 120 1408 1528
Condition100%3/3
$x->is_finite and $y->is_finite
!ll&&!rl&&r
90301408
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
2198  
2199 120 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 58 62 120
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
332562
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
2200  
2201 62 ▶ return $x -> is_inf("+") ? ($y -> is_inf("+") ? $x -> bnan(@r)
Branch: 5 branches
BranchTrueFalseTotal
$x->is_inf('+') ? : 23 39 62
$y->is_inf('+') ? : 10 13 23
$x->is_inf('-') ? : 22 17 39
$y->is_inf('-') ? : 10 12 22
$y->is_inf('+') ? : 9 8 17
2202 : $x -> binf("+", @r))
2203 : $x -> is_inf("-") ? ($y -> is_inf("-") ? $x -> bnan(@r)
2204 : $x -> binf("-", @r))
2205 : ($y -> is_inf("+") ? $x -> binf("-", @r)
2206 : $x -> binf("+", @r));
2207 }
2208  
2209 1408 $x -> badd($y -> copy() -> bneg(), @r);
2210 1408 return $x;
2211 }
2212  
2213 20716 ▶sub bmul {
Subroutine bmul: called CC=19 SCAR=29.5
Pod documented
2214 # multiply two numbers
2215  
2216 # set up parameters
2217 20716 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 20304 412 20716
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
640620304
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2218 ? (ref($_[0]), @_)
2219 : objectify(2, @_);
2220  
2221 # Don't modify constant (read-only) objects.
2222  
2223 20716 ▶ return $x if $x -> modify('bmul');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmul") 0 20716 20716
2224  
2225 20716 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 53 20663 20716
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
292420663
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
2226  
2227 # inf handling
2228 20663 ▶ if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/) 85 20578 20663
Condition100%3/3
$x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/
l!l&&r!l&&!r
533220578
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
2229 85 ▶ return $x -> bnan(@r) if $x -> is_zero() || $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $y->is_zero 12 73 85
Condition100%3/3
$x->is_zero or $y->is_zero
l!l&&r!l&&!r
8473
MC/DC100%2/2
$x->is_zero or $y->is_zero
$x->is_zero $y->is_zero
Truth table100%3/3
A or B
A= $x->is_zeroB= $y->is_zero
ABresult
1X1
011
000
2230 # result will always be +-inf:
2231 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
2232 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
2233 73 ▶ return $x -> binf(@r) if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/ 23 50 73
Condition100%3/3
$x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/
!ll&&!rl&&r
282223
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
2234 50 ▶ return $x -> binf(@r) if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/ 14 36 50
Condition100%3/3
$x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/
!ll&&!rl&&r
221414
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
2235 36 return $x -> binf('-', @r);
2236 }
2237  
2238 20578 ▶ return $x -> _upg() -> bmul($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 1 20577 20578
2239  
2240 # aEb * cEd = (a*c)E(b+d)
2241 20577 $x->{_m} = $LIB->_mul($x->{_m}, $y->{_m});
2242 ($x->{_e}, $x->{_es})
2243 20577 = $LIB -> _sadd($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
2244  
2245 20577 $r[3] = $y; # no push!
2246  
2247 # adjust sign:
2248 20577 ▶ $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} ne $y->{'sign'} ? : 880 19697 20577
2249 20577 $x -> bnorm -> round(@r);
2250  
2251 20577 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 5861 14716 20577
Condition83%5/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
5858314716
$x->is_int or $x->is_inf
l!l&&r!l&&!r
5858014719
MC/DC66%2/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table75%3/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
2252 $x -> is_inf() ||
2253 $x -> is_nan());
2254 20577 return $x;
2255 }
2256  
2257 43 *bdiv = \&bfdiv;
2258 43 *bmod = \&bfmod;
2259  
2260 9639 ▶sub bfdiv {
Subroutine bfdiv: called CC=43 SCAR=37.7
Pod documented
2261 # This does floored division (or floor division) where the quotient is
2262 # rounded towards minus infinity.
2263 #
2264 # ($q, $r) = $x -> btdiv($y) returns $q and $r so that $q is floor($x / $y)
2265 # and $q * $y + $r = $x.
2266  
2267 # Set up parameters.
2268 9639 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 9488 151 9639
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
55969488
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2269 ? (ref($_[0]), @_)
2270 : objectify(2, @_);
2271  
2272 ###########################################################################
2273 # Code for all classes that share the common interface.
2274 ###########################################################################
2275  
2276 # Don't modify constant (read-only) objects.
2277  
2278 9639 ▶ return $x if $x -> modify('bfdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfdiv") 0 9639 9639
2279  
2280 9639 my $wantarray = wantarray; # call only once
2281  
2282 # At least one argument is NaN. This is handled the same way as in
2283 # Math::BigInt -> bdiv(). See the comment in the code for Math::BigInt ->
2284 # bdiv() for further details.
2285  
2286 9639 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 68 9571 9639
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
32369571
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
2287 68 ▶ return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 24 44 68
2288 : $x -> bnan(@r);
2289 }
2290  
2291 # Divide by zero and modulo zero. This is handled the same way as in
2292 # Math::BigInt -> bdiv(). See the comment in the code for Math::BigInt ->
2293 # bdiv() for further details.
2294  
2295 9571 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 52 9519 9571
2296 52 my $rem;
2297 52 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 16 36 52
2298 16 $rem = $x -> copy() -> round(@r);
2299 16 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 12 4 16
2300 }
2301 52 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 18 34 52
2302 18 $x -> bnan(@r);
2303 } else {
2304 34 $x -> binf($x->{sign}, @r);
2305 }
2306 48 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 16 32 48
2307 }
2308  
2309 # Numerator (dividend) is +/-inf. This is handled the same way as in
2310 # Math::BigInt -> bdiv(). See the comment in the code for Math::BigInt ->
2311 # bdiv() for further details.
2312  
2313 9519 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 40 9479 9519
2314 40 my $rem;
2315 40 ▶ $rem = $class -> bnan(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 16 24 40
2316 40 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 16 24 40
2317 16 $x -> bnan(@r);
2318 } else {
2319 24 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 12 12 24
2320 24 $x -> binf($sign, @r);
2321 }
2322 40 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 16 24 40
2323 }
2324  
2325 # Denominator (divisor) is +/-inf. This is handled the same way as in
2326 # Math::BigInt -> bdiv(), with one exception: In scalar context,
2327 # Math::BigFloat does true division (although rounded), not floored
2328 # division (F-division), so a finite number divided by +/-inf is always
2329 # zero. See the comment in the code for Math::BigInt -> bdiv() for further
2330 # details.
2331  
2332 9479 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 40 9439 9479
2333 40 my $rem;
2334 40 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 16 24 40
2335 16 ▶ 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)) { } 12 4 16
Condition100%3/3
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
l!l&&r!l&&!r
844
MC/DC100%2/2
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
$x->is_zero $x->bcmp(0) == $y->bcmp(0)
Truth table100%3/3
A or B
A= $x->is_zeroB= $x->bcmp(0) == $y->bcmp(0)
ABresult
1X1
011
000
2336 12 $rem = $x -> copy() -> round(@r);
2337 12 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 12 0 12
2338 12 $x -> bzero(@r);
2339 } else {
2340 4 $rem = $class -> binf($y -> {sign}, @r);
2341 4 $x -> bone('-', @r);
2342 }
2343 } else {
2344 24 $x -> bzero(@r);
2345 }
2346 40 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 16 24 40
2347 }
2348  
2349 # At this point, both the numerator and denominator are finite, non-zero
2350 # numbers.
2351  
2352 # we need to limit the accuracy to protect against overflow
2353 9439 my $fallback = 0;
2354 9439 my (@params, $scale);
2355 9439 ($x, @params) = $x->_find_round_parameters($r[0], $r[1], $r[2], $y);
2356  
2357 9439 ▶ if ($x -> is_nan()) { # error in _find_round_parameters?
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 9439 9439
2358 0 $x -> round(@r);
2359 0 ▶ return $wantarray ? ($x, $class -> bnan(@r)) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
2360 }
2361  
2362 # no rounding at all, so must use fallback
2363 9439 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 594 8845 9439
2364 # simulate old behaviour
2365 594 $params[0] = $class -> div_scale(); # and round to it as accuracy
2366 594 $scale = $params[0]+4; # at least four more for proper round
2367 594 $params[2] = $r[2]; # round mode by caller or undef
2368 594 $fallback = 1; # to clear a/p afterwards
2369 } else {
2370 # the 4 below is empirical, and there might be cases where it is not
2371 # enough...
2372 8845 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition66%2/3
$params[0] || $params[1]
l!l&&r!l&&!r
8812330
MC/DC0%0/2
$params[0] || $params[1]
$params[0] $params[1]
Truth table66%2/3
A || B
A= $params[0]B= $params[1]
ABresult
1X1
011
000
2373 }
2374  
2375 # Temporarily disable downgrading
2376  
2377 9439 my $dng = Math::BigFloat -> downgrade();
2378 9439 Math::BigFloat -> downgrade(undef);
2379  
2380 9439 my $rem;
2381 9439 ▶ $rem = $class -> bzero() if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 47 9392 9439
2382  
2383 9439 ▶ $y = $class -> new($y) unless $y -> isa('Math::BigFloat');
Branch: 1 branch
BranchTrueFalseTotal
unless $y->isa("Math::BigFloat") 0 9439 9439
2384  
2385 9439 my $lx = $LIB -> _len($x->{_m});
2386 9439 my $ly = $LIB -> _len($y->{_m});
2387 9439 ▶ $scale = $lx if $lx > $scale;
Branch: 1 branch
BranchTrueFalseTotal
if $lx > $scale 71 9368 9439
2388 9439 ▶ $scale = $ly if $ly > $scale;
Branch: 1 branch
BranchTrueFalseTotal
if $ly > $scale 617 8822 9439
2389 9439 my $diff = $ly - $lx;
2390 9439 ▶ $scale += $diff if $diff > 0; # if lx << ly, but not if ly << lx!
Branch: 1 branch
BranchTrueFalseTotal
if $diff > 0 3039 6400 9439
2391  
2392 # Are both operands the same object, i.e., like $x -> bdiv($x)? If so,
2393 # flipping the sign of $y also flips the sign of $x.
2394  
2395 9439 my $xsign = $x -> {sign};
2396 9439 my $ysign = $y -> {sign};
2397  
2398 9439 $y -> {sign} =~ tr/+-/-+/; # Flip the sign of $y, and see ...
2399 9439 my $same = $xsign ne $x -> {sign}; # ... if that changed the sign of $x.
2400 9439 $y -> {sign} = $ysign; # Re-insert the original sign.
2401  
2402 9439 ▶ if ($same) { # $x -> bdiv($x)
Branch: 1 branch
BranchTrueFalseTotal
if ($same) { } 8 9431 9439
2403 8 $x -> bone();
2404 } else {
2405 # make copy of $x in case of list context for later remainder
2406 # calculation
2407 9431 ▶ $rem = $x -> copy() if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 47 9384 9431
2408  
2409 9431 ▶ $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} ne $y->{'sign'} ? : 896 8535 9431
2410  
2411 # promote Math::BigInt and its subclasses (except when already a
2412 # Math::BigFloat)
2413 9431 ▶ $y = $class -> new($y) unless $y -> isa('Math::BigFloat');
Branch: 1 branch
BranchTrueFalseTotal
unless $y->isa("Math::BigFloat") 0 9431 9431
2414  
2415 # calculate the result to $scale digits and then round it
2416 # (a * 10 ** b) / (c * 10 ** d) => (a/c) * 10 ** (b-d)
2417 9431 $x->{_m} = $LIB->_lsft($x->{_m}, $LIB->_new($scale), 10); # scale up
2418 9431 $x->{_m} = $LIB->_div($x->{_m}, $y->{_m}); # divide
2419  
2420 # correct exponent of $x
2421 ($x->{_e}, $x->{_es})
2422 9431 = $LIB -> _ssub($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
2423  
2424 # correct for 10**scale
2425 ($x->{_e}, $x->{_es})
2426 9431 = $LIB -> _ssub($x->{_e}, $x->{_es}, $LIB->_new($scale), '+');
2427  
2428 9431 $x -> bnorm(); # remove trailing zeros
2429 }
2430  
2431 # shortcut to not run through _find_round_parameters again
2432 9439 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 9406 33 9439
2433 9406 $x->{accuracy} = undef; # clear before round
2434 9406 $x -> bround($params[0], $params[2]); # then round accordingly
2435 } else {
2436 33 $x->{precision} = undef; # clear before round
2437 33 $x -> bfround($params[1], $params[2]); # then round accordingly
2438 }
2439 9439 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 594 8845 9439
2440 # clear a/p after round, since user did not request it
2441 594 $x->{accuracy} = undef;
2442 594 $x->{precision} = undef;
2443 }
2444  
2445 # Restore downgrading
2446  
2447 9439 Math::BigFloat -> downgrade($dng);
2448  
2449 9439 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 47 9392 9439
2450 47 $x -> bfloor();
2451 47 $rem -> bfmod($y, @params); # copy already done
2452 47 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 47 0 47
2453 # clear a/p after round, since user did not request it
2454 47 $rem->{accuracy} = undef;
2455 47 $rem->{precision} = undef;
2456 }
2457 47 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 47 0 47
2458 47 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 28 19 47
2459 47 return $x, $rem;
2460 }
2461  
2462 9392 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 290 9102 9392
2463 9392 $x; # rounding already done above
2464 }
2465  
2466 813 ▶sub bfmod {
Subroutine bfmod: called CC=21 SCAR=30.4
Pod documented
2467 # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return
2468 # remainder
2469  
2470 # set up parameters
2471 813 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 804 9 813
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
63804
MC/DC100%2/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table100%3/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2472 ? (ref($_[0]), @_)
2473 : objectify(2, @_);
2474  
2475 # Don't modify constant (read-only) objects.
2476  
2477 813 ▶ return $x if $x -> modify('bfmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfmod") 0 813 813
2478  
2479 # At least one argument is NaN. This is handled the same way as in
2480 # Math::BigInt -> bfmod().
2481  
2482 813 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 36 777 813
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
2016777
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
2483  
2484 # Modulo zero. This is handled the same way as in Math::BigInt -> bfmod().
2485  
2486 777 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 40 737 777
2487 40 return $x -> round(@r);
2488 }
2489  
2490 # Numerator (dividend) is +/-inf. This is handled the same way as in
2491 # Math::BigInt -> bfmod().
2492  
2493 737 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 48 689 737
2494 48 return $x -> bnan(@r);
2495 }
2496  
2497 # Denominator (divisor) is +/-inf. This is handled the same way as in
2498 # Math::BigInt -> bfmod().
2499  
2500 689 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 40 649 689
2501 40 ▶ if ($x -> is_zero() || $x -> bcmp(0) == $y -> bcmp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero or $x->bcmp(0) == $y->bcmp(0)) { } 28 12 40
Condition100%3/3
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
l!l&&r!l&&!r
161212
MC/DC100%2/2
$x->is_zero or $x->bcmp(0) == $y->bcmp(0)
$x->is_zero $x->bcmp(0) == $y->bcmp(0)
Truth table100%3/3
A or B
A= $x->is_zeroB= $x->bcmp(0) == $y->bcmp(0)
ABresult
1X1
011
000
2502 28 return $x -> round(@r);
2503 } else {
2504 12 return $x -> binf($y -> sign(), @r);
2505 }
2506 }
2507  
2508 # Modulo is zero if $x is zero or if $x is an integer and $y is +/-1.
2509  
2510 return $x -> bzero(@r) if $x -> is_zero()
2511 || ($x -> is_int() &&
2512 # check that $y == +1 or $y == -1:
2513 649 ▶ ($LIB->_is_zero($y->{_e}) && $LIB->_is_one($y->{_m})));
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_int and ($LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"})) 112 537 649
Condition100%9/9
$x->is_zero or $x->is_int and ($LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"}))
l!l&&r!l&&!r
3280537
$x->is_int and ($LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"}))
!ll&&!rl&&r
9144680
$LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"})
!ll&&!rl&&r
6038680
MC/DC100%4/4
$x->is_zero or $x->is_int and ($LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"}))
$x->is_zero $x->is_int $LIB->_is_zero($y->{"_e"}) $LIB->_is_one($y->{"_m"})
Truth table100%5/5
A or B and C and D
A= $x->is_zeroB= $x->is_intC= $LIB->_is_zero($y->{"_e"})D= $LIB->_is_one($y->{"_m"})
ABCDresult
1XXX1
01111
00XX0
010X0
01100
2514  
2515 # Numerator (dividend) and denominator (divisor) are identical. Return
2516 # zero.
2517  
2518 537 my $cmp = $x -> bacmp($y); # $x <=> $y
2519 537 ▶ if ($cmp == 0) { # $x == $y => result 0
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp == 0) 24 513 537
2520 24 return $x -> bzero(@r);
2521 }
2522  
2523 # Compare the exponents of $x and $y.
2524  
2525 513 my $ecmp = $LIB->_scmp($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
2526  
2527 513 my $ym = $y->{_m}; # mantissa of y, scaled if necessary
2528  
2529 513 ▶ if ($ecmp > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ecmp > 0) { } 176 337 513
elsif ($ecmp < 0) { } 79 258 337
2530  
2531 # $x has a larger exponent than $y, so shift the mantissa of $x by the
2532 # difference between the exponents of $x and $y.
2533 #
2534 # 123e+2 % 456e+1 => 1230 % 456 (+2 - +1 = 1)
2535 # 123e+2 % 456e-1 => 123000 % 456 (+2 - -1 = 3)
2536 # 456e-1 % 123e-3 => 12300 % 456 (-1 - -3 = 2)
2537  
2538 # get the difference between exponents; $ds is always "+" here
2539 my ($de, $ds) = $LIB->_ssub($LIB->_copy($x->{_e}), $x->{_es},
2540 176 $y->{_e}, $y->{_es});
2541  
2542 # adjust the mantissa of x by the difference between exponents
2543 176 $x->{_m} = $LIB->_lsft($x->{_m}, $de, 10);
2544  
2545 # compute the modulus
2546 176 $x->{_m} = $LIB->_mod($x->{_m}, $ym);
2547  
2548 # adjust the exponent of x to correct for the ajustment of the mantissa
2549 176 ($x->{_e}, $x->{_es}) = $LIB->_ssub($x->{_e}, $x->{_es}, $de, $ds);
2550  
2551 } elsif ($ecmp < 0) {
2552  
2553 # $x has a smaller exponent than $y, so shift the mantissa of $y by the
2554 # difference between the exponents of $x and $y.
2555 #
2556 # 123456e+1 % 78e+2 => 123456 % 780 (+2 - +1 = 1)
2557 # 123456e-2 % 78e+1 => 123456 % 78000 (+1 - -2 = 3)
2558  
2559 # get the difference between exponents; $ds is always "+" here
2560 my ($de, $ds) = $LIB->_ssub($LIB->_copy($y->{_e}), $y->{_es},
2561 79 $x->{_e}, $x->{_es});
2562  
2563 # adjust the mantissa of y by the difference between exponents
2564 79 $ym = $LIB->_lsft($LIB->_copy($ym), $de, 10);
2565  
2566 # compute the modulus
2567 79 $x->{_m} = $LIB->_mod($x->{_m}, $ym);
2568  
2569 } else {
2570  
2571 # $x has the same exponent as $y, so compute the modulus directly
2572  
2573 # compute the modulus
2574 258 $x->{_m} = $LIB->_mod($x->{_m}, $ym);
2575 }
2576  
2577 513 ▶ if ($LIB->_is_zero($x->{_m})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{'_m'})) { } 100 413 513
2578 100 $x->{sign} = '+';
2579 } else {
2580 # adjust for floored division/modulus
2581 $x->{_m} = $LIB->_sub($ym, $x->{_m}, 1)
2582 413 ▶ if $x->{sign} ne $y->{sign};
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $y->{"sign"} 57 356 413
2583 413 $x->{sign} = $y->{sign};
2584 }
2585  
2586 513 $x -> bnorm();
2587 513 $x -> round($r[0], $r[1], $r[2], $y);
2588 513 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 423 90 513
2589 513 return $x;
2590 }
2591  
2592 0 ▶sub btdiv {
Subroutine btdiv: not called CC=40 SCAR=74.0
Pod documented
2593 # This does truncated division, where the quotient is truncted, i.e.,
2594 # rounded towards zero.
2595 #
2596 # ($q, $r) = $x -> btdiv($y) returns $q and $r so that $q is int($x / $y)
2597 # and $q * $y + $r = $x.
2598  
2599 # Set up parameters
2600 0 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2601 ? (ref($_[0]), @_)
2602 : objectify(2, @_);
2603  
2604 ###########################################################################
2605 # Code for all classes that share the common interface.
2606 ###########################################################################
2607  
2608 # Don't modify constant (read-only) objects.
2609  
2610 0 ▶ return $x if $x -> modify('btdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btdiv") 0 0 0
2611  
2612 0 my $wantarray = wantarray; # call only once
2613  
2614 # At least one argument is NaN. Return NaN for both quotient and the
2615 # modulo/remainder.
2616  
2617 0 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 0 0 0
Condition0%0/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table0%0/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
2618 0 ▶ return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
2619 : $x -> bnan(@r);
2620 }
2621  
2622 # Divide by zero and modulo zero.
2623 #
2624 # Division: Use the common convention that x / 0 is inf with the same sign
2625 # as x, except when x = 0, where we return NaN. This is also what earlier
2626 # versions did.
2627 #
2628 # Modulo: In modular arithmetic, the congruence relation z = x (mod y)
2629 # means that there is some integer k such that z - x = k y. If y = 0, we
2630 # get z - x = 0 or z = x. This is also what earlier versions did, except
2631 # that 0 % 0 returned NaN.
2632 #
2633 # inf / 0 = inf inf % 0 = inf
2634 # 5 / 0 = inf 5 % 0 = 5
2635 # 0 / 0 = NaN 0 % 0 = 0
2636 # -5 / 0 = -inf -5 % 0 = -5
2637 # -inf / 0 = -inf -inf % 0 = -inf
2638  
2639 0 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 0 0 0
2640 0 my $rem;
2641 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 0 0 0
2642 0 $rem = $x -> copy(@r);
2643 }
2644 0 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 0 0 0
2645 0 $x -> bnan(@r);
2646 } else {
2647 0 $x -> binf($x -> {sign}, @r);
2648 }
2649 0 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
2650 }
2651  
2652 # Numerator (dividend) is +/-inf, and denominator is finite and non-zero.
2653 # The divide by zero cases are covered above. In all of the cases listed
2654 # below we return the same as core Perl.
2655 #
2656 # inf / -inf = NaN inf % -inf = NaN
2657 # inf / -5 = -inf inf % -5 = NaN
2658 # inf / 5 = inf inf % 5 = NaN
2659 # inf / inf = NaN inf % inf = NaN
2660 #
2661 # -inf / -inf = NaN -inf % -inf = NaN
2662 # -inf / -5 = inf -inf % -5 = NaN
2663 # -inf / 5 = -inf -inf % 5 = NaN
2664 # -inf / inf = NaN -inf % inf = NaN
2665  
2666 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
2667 0 my $rem;
2668 0 ▶ $rem = $class -> bnan(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 0 0 0
2669 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 0 0 0
2670 0 $x -> bnan(@r);
2671 } else {
2672 0 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 0 0 0
2673 0 $x -> binf($sign,@r );
2674 }
2675 0 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
2676 }
2677  
2678 # Denominator (divisor) is +/-inf. The cases when the numerator is +/-inf
2679 # are covered above. In the modulo cases (in the right column) we return
2680 # the same as core Perl, which does floored division, so for consistency we
2681 # also do floored division in the division cases (in the left column).
2682 #
2683 # -5 / inf = 0 -5 % inf = -5
2684 # 0 / inf = 0 0 % inf = 0
2685 # 5 / inf = 0 5 % inf = 5
2686 #
2687 # -5 / -inf = 0 -5 % -inf = -5
2688 # 0 / -inf = 0 0 % -inf = 0
2689 # 5 / -inf = 0 5 % -inf = 5
2690  
2691 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 0 0 0
2692 0 my $rem;
2693 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 0 0 0
2694 0 $rem = $x -> copy() -> round(@r);
2695 0 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 0 0 0
2696 }
2697 0 $x -> bzero(@r);
2698 0 ▶ return $wantarray ? ($x, $rem) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
2699 }
2700  
2701 # At this point, both the numerator and denominator are finite, non-zero
2702 # numbers.
2703  
2704 # we need to limit the accuracy to protect against overflow
2705 0 my $fallback = 0;
2706 0 my (@params, $scale);
2707 0 ($x, @params) = $x->_find_round_parameters($r[0], $r[1], $r[2], $y);
2708  
2709 0 ▶ if ($x -> is_nan()) { # error in _find_round_parameters?
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
2710 0 $x -> round(@r);
2711 0 ▶ return $wantarray ? ($x, $class -> bnan(@r)) : $x;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 0 0
2712 }
2713  
2714 # no rounding at all, so must use fallback
2715 0 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 0 0 0
2716 # simulate old behaviour
2717 0 $params[0] = $class -> div_scale(); # and round to it as accuracy
2718 0 $scale = $params[0]+4; # at least four more for proper round
2719 0 $params[2] = $r[2]; # round mode by caller or undef
2720 0 $fallback = 1; # to clear a/p afterwards
2721 } else {
2722 # the 4 below is empirical, and there might be cases where it is not
2723 # enough...
2724 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
2725 }
2726  
2727 # Temporarily disable downgrading
2728  
2729 0 my $dng = Math::BigFloat -> downgrade();
2730 0 Math::BigFloat -> downgrade(undef);
2731  
2732 0 my $rem;
2733 0 ▶ $rem = $class -> bzero() if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 0 0 0
2734  
2735 0 ▶ $y = $class -> new($y) unless $y -> isa('Math::BigFloat');
Branch: 1 branch
BranchTrueFalseTotal
unless $y->isa("Math::BigFloat") 0 0 0
2736  
2737 0 my $lx = $LIB -> _len($x->{_m});
2738 0 my $ly = $LIB -> _len($y->{_m});
2739 0 ▶ $scale = $lx if $lx > $scale;
Branch: 1 branch
BranchTrueFalseTotal
if $lx > $scale 0 0 0
2740 0 ▶ $scale = $ly if $ly > $scale;
Branch: 1 branch
BranchTrueFalseTotal
if $ly > $scale 0 0 0
2741 0 my $diff = $ly - $lx;
2742 0 ▶ $scale += $diff if $diff > 0; # if lx << ly, but not if ly << lx!
Branch: 1 branch
BranchTrueFalseTotal
if $diff > 0 0 0 0
2743  
2744 # Are both operands the same object, i.e., like $x -> bdiv($x)? If so,
2745 # flipping the sign of $y also flips the sign of $x.
2746  
2747 0 my $xsign = $x -> {sign};
2748 0 my $ysign = $y -> {sign};
2749  
2750 0 $y -> {sign} =~ tr/+-/-+/; # Flip the sign of $y, and see ...
2751 0 my $same = $xsign ne $x -> {sign}; # ... if that changed the sign of $x.
2752 0 $y -> {sign} = $ysign; # Re-insert the original sign.
2753  
2754 0 ▶ if ($same) { # $x -> bdiv($x)
Branch: 1 branch
BranchTrueFalseTotal
if ($same) { } 0 0 0
2755 0 $x -> bone();
2756 } else {
2757 # make copy of $x in case of list context for later remainder
2758 # calculation
2759 0 ▶ $rem = $x -> copy() if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 0 0 0
2760  
2761 0 ▶ $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} ne $y->{'sign'} ? : 0 0 0
2762  
2763 # promote Math::BigInt and its subclasses (except when already a
2764 # Math::BigFloat)
2765 0 ▶ $y = $class -> new($y) unless $y -> isa('Math::BigFloat');
Branch: 1 branch
BranchTrueFalseTotal
unless $y->isa("Math::BigFloat") 0 0 0
2766  
2767 # calculate the result to $scale digits and then round it
2768 # (a * 10 ** b) / (c * 10 ** d) => (a/c) * 10 ** (b-d)
2769 0 $x->{_m} = $LIB->_lsft($x->{_m}, $LIB->_new($scale), 10); # scale up
2770 0 $x->{_m} = $LIB->_div($x->{_m}, $y->{_m}); # divide
2771  
2772 # correct exponent of $x
2773 ($x->{_e}, $x->{_es})
2774 0 = $LIB -> _ssub($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
2775  
2776 # correct for 10**scale
2777 ($x->{_e}, $x->{_es})
2778 0 = $LIB -> _ssub($x->{_e}, $x->{_es}, $LIB->_new($scale), '+');
2779  
2780 0 $x -> bnorm(); # remove trailing zeros in mantissa
2781 }
2782  
2783 # shortcut to not run through _find_round_parameters again
2784 0 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 0 0 0
2785 0 $x->{accuracy} = undef; # clear before round
2786 0 $x -> bround($params[0], $params[2]); # then round accordingly
2787 } else {
2788 0 $x->{precision} = undef; # clear before round
2789 0 $x -> bfround($params[1], $params[2]); # then round accordingly
2790 }
2791 0 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 0 0 0
2792 # clear a/p after round, since user did not request it
2793 0 $x->{accuracy} = undef;
2794 0 $x->{precision} = undef;
2795 }
2796  
2797 # Restore downgrading
2798  
2799 0 Math::BigFloat -> downgrade($dng);
2800  
2801 0 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 0 0 0
2802 0 $x -> bint();
2803 0 $rem -> btmod($y, @params); # copy already done
2804  
2805 0 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 0 0 0
2806 # clear a/p after round, since user did not request it
2807 0 $rem->{accuracy} = undef;
2808 0 $rem->{precision} = undef;
2809 }
2810 0 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 0 0
2811 0 ▶ $rem -> _dng() if $rem -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $rem->is_int 0 0 0
2812 0 return $x, $rem;
2813 }
2814  
2815 0 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 0 0
2816 0 $x; # rounding already done above
2817 }
2818  
2819 0 ▶sub btmod {
Subroutine btmod: not called CC=18 SCAR=58.3
Pod documented
2820 # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return
2821 # remainder
2822  
2823 # set up parameters
2824 0 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
2825 ? (ref($_[0]), @_)
2826 : objectify(2, @_);
2827  
2828 # Don't modify constant (read-only) objects.
2829  
2830 0 ▶ return $x if $x -> modify('btmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btmod") 0 0 0
2831  
2832 # At least one argument is NaN. This is handled the same way as in
2833 # Math::BigInt -> btmod().
2834  
2835 0 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 0 0 0
Condition0%0/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table0%0/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
2836  
2837 # Modulo zero. This is handled the same way as in Math::BigInt -> btmod().
2838  
2839 0 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 0 0 0
2840 0 return $x -> round(@r);
2841 }
2842  
2843 # Numerator (dividend) is +/-inf. This is handled the same way as in
2844 # Math::BigInt -> btmod().
2845  
2846 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
2847 0 return $x -> bnan(@r);
2848 }
2849  
2850 # Denominator (divisor) is +/-inf. This is handled the same way as in
2851 # Math::BigInt -> btmod().
2852  
2853 0 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 0 0 0
2854 0 return $x -> round(@r);
2855 }
2856  
2857 # Modulo is zero if $x is zero or if $x is an integer and $y is +/-1.
2858  
2859 return $x -> bzero(@r) if $x -> is_zero()
2860 || ($x -> is_int() &&
2861 # check that $y == +1 or $y == -1:
2862 0 ▶ ($LIB->_is_zero($y->{_e}) && $LIB->_is_one($y->{_m})));
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_int and ($LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"})) 0 0 0
Condition0%0/9
$x->is_zero or $x->is_int and ($LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"}))
l!l&&r!l&&!r
000
$x->is_int and ($LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"}))
!ll&&!rl&&r
000
$LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"})
!ll&&!rl&&r
000
MC/DC0%0/4
$x->is_zero or $x->is_int and ($LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"}))
$x->is_zero $x->is_int $LIB->_is_zero($y->{"_e"}) $LIB->_is_one($y->{"_m"})
Truth table0%0/5
A or B and C and D
A= $x->is_zeroB= $x->is_intC= $LIB->_is_zero($y->{"_e"})D= $LIB->_is_one($y->{"_m"})
ABCDresult
1XXX1
01111
00XX0
010X0
01100
2863  
2864 # Numerator (dividend) and denominator (divisor) are identical. Return
2865 # zero.
2866  
2867 0 my $cmp = $x -> bacmp($y); # $x <=> $y
2868 0 ▶ if ($cmp == 0) { # $x == $y => result 0
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp == 0) 0 0 0
2869 0 return $x -> bzero(@r);
2870 }
2871  
2872 # Compare the exponents of $x and $y.
2873  
2874 0 my $ecmp = $LIB->_scmp($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
2875  
2876 0 ▶ if ($ecmp > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ecmp > 0) { } 0 0 0
elsif ($ecmp < 0) { } 0 0 0
2877  
2878 # $x has a larger exponent than $y, so shift the mantissa of $x by the
2879 # difference between the exponents of $x and $y.
2880 #
2881 # 123e+2 % 456e+1 => 1230 % 456 (+2 - +1 = 1)
2882 # 123e+2 % 456e-1 => 123000 % 456 (+2 - -1 = 3)
2883 # 456e-1 % 123e-3 => 12300 % 456 (-1 - -3 = 2)
2884  
2885 # get the difference between exponents; $ds is always "+" here
2886 my ($de, $ds) = $LIB->_ssub($LIB->_copy($x->{_e}), $x->{_es},
2887 0 $y->{_e}, $y->{_es});
2888  
2889 # adjust the mantissa of x by the difference between exponents
2890 0 $x->{_m} = $LIB->_lsft($x->{_m}, $de, 10);
2891  
2892 # compute the modulus
2893 0 $x->{_m} = $LIB->_mod($x->{_m}, $y->{_m});
2894  
2895 # adjust the exponent of x to correct for the ajustment of the mantissa
2896 0 ($x->{_e}, $x->{_es}) = $LIB->_ssub($x->{_e}, $x->{_es}, $de, $ds);
2897  
2898 } elsif ($ecmp < 0) {
2899  
2900 # $x has a smaller exponent than $y, so shift the mantissa of $y by the
2901 # difference between the exponents of $x and $y.
2902 #
2903 # 123456e+1 % 78e+2 => 123456 % 780 (+2 - +1 = 1)
2904 # 123456e-2 % 78e+1 => 123456 % 78000 (+1 - -2 = 3)
2905  
2906 # get the difference between exponents; $ds is always "+" here
2907 my ($de, $ds) = $LIB->_ssub($LIB->_copy($y->{_e}), $y->{_es},
2908 0 $x->{_e}, $x->{_es});
2909  
2910 # adjust the mantissa of y by the difference between exponents
2911 0 my $ym = $LIB->_lsft($LIB->_copy($y->{_m}), $de, 10);
2912  
2913 # compute the modulus
2914 0 $x->{_m} = $LIB->_mod($x->{_m}, $ym);
2915  
2916 } else {
2917  
2918 # $x has the same exponent as $y, so compute the modulus directly
2919  
2920 # compute the modulus
2921 0 $x->{_m} = $LIB->_mod($x->{_m}, $y->{_m});
2922 }
2923  
2924 0 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{_m}); # fix sign for -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 0 0 0
2925  
2926 0 $x -> bnorm();
2927 0 $x -> round($r[0], $r[1], $r[2], $y);
2928 0 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 0 0
2929 0 return $x;
2930 }
2931  
2932 0 ▶sub binv {
Subroutine binv: not called CC=6 SCAR=37.4
Pod documented
2933 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
2934  
2935 # Don't modify constant (read-only) objects.
2936  
2937 0 ▶ return $x if $x -> modify('binv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("binv") 0 0 0
2938  
2939 # bone() might perform downgrading, so temporarily disable downgrading
2940  
2941 0 my $dng = Math::BigFloat -> downgrade();
2942 0 Math::BigFloat -> downgrade(undef);
2943  
2944 0 my $inv = $class -> bone() -> bdiv($x, @r);
2945  
2946 # Restore downgrading
2947  
2948 0 Math::BigFloat -> downgrade($dng);
2949  
2950 0 %$x = %$inv;
2951  
2952 0 $x -> round(@r);
2953 0 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 0 0 0
Condition0%0/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
000
$x->is_int or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table0%0/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
2954 $x -> is_inf() ||
2955 $x -> is_nan());
2956 0 return $x;
2957 }
2958  
2959 445 ▶sub bsqrt {
Subroutine bsqrt: called CC=19 SCAR=29.5
Pod documented
2960 # calculate square root
2961 445 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 445 0 445
2962  
2963 # Don't modify constant (read-only) objects.
2964  
2965 445 ▶ return $x if $x -> modify('bsqrt');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsqrt") 0 445 445
2966  
2967 # Handle trivial cases.
2968  
2969 445 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 8 437 445
2970 437 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 4 433 437
2971 433 ▶ return $x -> round(@r) if $x -> is_zero() || $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one 8 425 433
Condition100%3/3
$x->is_zero or $x->is_one
l!l&&r!l&&!r
44425
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
2972  
2973 # We don't support complex numbers.
2974  
2975 425 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 20 405 425
2976 20 ▶ return $x -> _upg() -> bsqrt(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 20 20
2977 20 return $x -> bnan(@r);
2978 }
2979  
2980 # we need to limit the accuracy to protect against overflow
2981 405 my $fallback = 0;
2982 405 my (@params, $scale);
2983 405 ($x, @params) = $x->_find_round_parameters(@r);
2984  
2985 # error in _find_round_parameters?
2986 405 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 405 405
2987  
2988 # no rounding at all, so must use fallback
2989 405 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 125 280 405
2990 # simulate old behaviour
2991 125 $params[0] = $class -> div_scale(); # and round to it as accuracy
2992 125 $scale = $params[0]+4; # at least four more for proper round
2993 125 $params[2] = $r[2]; # round mode by caller or undef
2994 125 $fallback = 1; # to clear a/p afterwards
2995 } else {
2996 # the 4 below is empirical, and there might be cases where it is not
2997 # enough...
2998 280 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition100%3/3
$params[0] || $params[1]
l!l&&r!l&&!r
258184
MC/DC100%2/2
$params[0] || $params[1]
$params[0] $params[1]
Truth table100%3/3
A || B
A= $params[0]B= $params[1]
ABresult
1X1
011
000
2999 }
3000  
3001 # Shift the significand left or right to get the desired number of digits,
3002 # which is 2*$scale with possibly one extra digit to ensure that the
3003 # exponent is an even number.
3004  
3005 405 my $l = $LIB -> _len($x->{_m});
3006 405 my $n = 2 * $scale - $l; # how much should we shift?
3007 405 ▶ $n++ if ($l % 2 xor $LIB -> _is_odd($x->{_e}));
Branch: 1 branch
BranchTrueFalseTotal
if $l % 2 xor $LIB->_is_odd($x->{"_e"}) 304 101 405
Condition100%4/4
$l % 2 xor $LIB->_is_odd($x->{"_e"})
l&&rl&&!r!l&&r!l&&!r
2018412081
MC/DC100%2/2
$l % 2 xor $LIB->_is_odd($x->{"_e"})
$l % 2 $LIB->_is_odd($x->{"_e"})
Truth table100%4/4
A xor B
A= $l % 2B= $LIB->_is_odd($x->{"_e"})
ABresult
110
101
011
000
3008 405 ▶ my ($na, $ns) = $n < 0 ? (abs($n), "-") : ($n, "+");
Branch: 1 branch
BranchTrueFalseTotal
$n < 0 ? : 2 403 405
3009 405 $na = $LIB -> _new($na);
3010  
3011 $x->{_m} = $ns eq "+" ? $LIB -> _lsft($x->{_m}, $na, 10)
3012 405 ▶ : $LIB -> _rsft($x->{_m}, $na, 10);
Branch: 1 branch
BranchTrueFalseTotal
$ns eq '+' ? : 403 2 405
3013  
3014 405 $x->{_m} = $LIB -> _sqrt($x->{_m});
3015  
3016 # Adjust the exponent by the amount that we shifted the significand. The
3017 # square root of the exponent is simply half of it: sqrt(10^(2*a)) = 10^a.
3018  
3019 405 ($x->{_e}, $x->{_es}) = $LIB -> _ssub($x->{_e}, $x->{_es}, $na, $ns);
3020 405 $x->{_e} = $LIB -> _div($x->{_e}, $LIB -> _new("2"));
3021  
3022 # Normalize to get rid of any trailing zeros in the significand.
3023  
3024 405 $x -> bnorm();
3025  
3026 # shortcut to not run through _find_round_parameters again
3027 405 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 383 22 405
3028 383 $x -> bround($params[0], $params[2]); # then round accordingly
3029 } else {
3030 22 $x -> bfround($params[1], $params[2]); # then round accordingly
3031 }
3032  
3033 405 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 125 280 405
3034 # clear a/p after round, since user did not request it
3035 125 $x->{accuracy} = undef;
3036 125 $x->{precision} = undef;
3037 }
3038  
3039 405 $x -> round(@r);
3040 405 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 72 333 405
3041 405 $x;
3042 }
3043  
3044 1077 ▶sub bpow {
Subroutine bpow: called CC=40 SCAR=36.9
Pod documented
3045 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
3046 # compute power of two numbers, second arg is used as integer
3047 # modifies first argument
3048  
3049 # set up parameters
3050 1077 my ($class, $x, $y, $a, $p, $r) = (ref($_[0]), @_);
3051 # objectify is costly, so avoid it
3052 1077 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 98 979 1077
Condition100%3/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
791979
MC/DC100%2/2
not ref $_[0] or ref $_[0] ne ref $_[1]
not ref $_[0] ref $_[0] ne ref $_[1]
Truth table100%3/3
not(A) or B
A= not ref $_[0]B= ref $_[0] ne ref $_[1]
ABresult
1X1
011
000
3053 98 ($class, $x, $y, $a, $p, $r) = objectify(2, @_);
3054 }
3055  
3056 # Don't modify constant (read-only) objects.
3057  
3058 1077 ▶ return $x if $x -> modify('bpow');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bpow") 0 1077 1077
3059  
3060 # $x and/or $y is a NaN
3061 1077 ▶ return $x -> bnan() if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 116 961 1077
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
6056961
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
3062  
3063 # $x and/or $y is a +/-Inf
3064 961 ▶ if ($x -> is_inf("-")) {
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 60 901 961
elsif ($x->is_inf('+')) { } 60 841 901
elsif ($y->is_inf('-')) { } 44 797 841
elsif ($y->is_inf('+')) { } 44 753 797
3065 60 ▶ return $x -> bzero() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 28 32 60
3066 32 ▶ return $x -> bnan() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 28 32
3067 28 ▶ return $x if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 8 20 28
3068 20 return $x -> bneg();
3069 } elsif ($x -> is_inf("+")) {
3070 60 ▶ return $x -> bzero() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 28 32 60
3071 32 ▶ return $x -> bnan() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 28 32
3072 28 return $x;
3073 } elsif ($y -> is_inf("-")) {
3074 44 ▶ return $x -> bnan() if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 4 40 44
3075 40 ▶ return $x -> binf("+") if $x > -1 && $x < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x > -1 and $x < 1 12 28 40
Condition100%3/3
$x > -1 and $x < 1
!ll&&!rl&&r
121612
MC/DC100%2/2
$x > -1 and $x < 1
$x > -1 $x < 1
Truth table100%3/3
A and B
A= $x > -1B= $x < 1
ABresult
0X0
100
111
3076 28 ▶ return $x -> bone() if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 4 24 28
3077 24 return $x -> bzero();
3078 } elsif ($y -> is_inf("+")) {
3079 44 ▶ return $x -> bnan() if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 4 40 44
3080 40 ▶ return $x -> bzero() if $x > -1 && $x < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x > -1 and $x < 1 12 28 40
Condition100%3/3
$x > -1 and $x < 1
!ll&&!rl&&r
121612
MC/DC100%2/2
$x > -1 and $x < 1
$x > -1 $x < 1
Truth table100%3/3
A and B
A= $x > -1B= $x < 1
ABresult
0X0
100
111
3081 28 ▶ return $x -> bone() if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 4 24 28
3082 24 return $x -> binf("+");
3083 }
3084  
3085 753 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 48 705 753
3086 48 ▶ return $x -> bone() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 44 48
3087 44 ▶ return $x -> binf() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 20 24 44
3088 24 return $x;
3089 }
3090  
3091 # We don't support complex numbers, so upgrade or return NaN.
3092  
3093 705 ▶ if ($x -> is_negative() && !$y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_negative and not $y->is_int) 80 625 705
Condition100%3/3
$x->is_negative and not $y->is_int
!ll&&!rl&&r
47315280
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
3094 80 ▶ return $x -> _upg() -> bpow($y, $a, $p, $r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 80 80
3095 80 return $x -> bnan();
3096 }
3097  
3098 625 ▶ if ($x -> is_one("+") || $y -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("+") or $y->is_one) 148 477 625
Condition100%3/3
$x->is_one("+") or $y->is_one
l!l&&r!l&&!r
44104477
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
3099 148 return $x;
3100 }
3101  
3102 477 ▶ if ($x -> is_one("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("-")) 24 453 477
3103 24 ▶ return $x if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 12 12 24
3104 12 return $x -> bneg();
3105 }
3106  
3107 453 ▶ return $x -> _pow($y, $a, $p, $r) if !$y -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $y->is_int 115 338 453
3108  
3109 # We should NOT be looking at private variables of other objects. Fixme XXX
3110 338 my $y1 = $y -> as_int()->{value}; # make MBI part
3111  
3112 338 my $new_sign = '+';
3113 338 ▶ $new_sign = $LIB -> _is_odd($y1) ? '-' : '+' if $x->{sign} ne '+';
Branch: 2 branches
BranchTrueFalseTotal
if $x->{"sign"} ne "+" 108 230 338
$LIB->_is_odd($y1) ? : 54 54 108
3114  
3115 # calculate $x->{_m} ** $y and $x->{_e} * $y separately (faster)
3116 338 $x->{_m} = $LIB -> _pow($x->{_m}, $y1);
3117 338 $x->{_e} = $LIB -> _mul($x->{_e}, $y1);
3118  
3119 338 $x->{sign} = $new_sign;
3120 338 $x -> bnorm();
3121  
3122 # x ** (-y) = 1 / (x ** y)
3123  
3124 338 ▶ if ($y->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{"sign"} eq "-") 107 231 338
3125 # modify $x in place!
3126 107 my $z = $x -> copy();
3127 107 $x -> bone();
3128 # round in one go (might ignore y's A!)
3129 107 return scalar $x -> bdiv($z, $a, $p, $r);
3130 }
3131  
3132 231 $x -> round($a, $p, $r, $y);
3133  
3134 231 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 162 69 231
Condition83%5/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
159369
$x->is_int or $x->is_inf
l!l&&r!l&&!r
159072
MC/DC66%2/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table75%3/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
3135 $x -> is_inf() ||
3136 $x -> is_nan());
3137 231 return $x;
3138 }
3139  
3140 193 ▶sub broot {
Subroutine broot: called CC=40 SCAR=38.7
Pod documented
3141 # calculate $y'th root of $x
3142  
3143 # set up parameters
3144 193 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 183 10 193
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
010183
MC/DC50%1/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table66%2/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
3145 ? (ref($_[0]), @_)
3146 : objectify(2, @_);
3147  
3148 # Don't modify constant (read-only) objects.
3149  
3150 193 ▶ return $x if $x -> modify('broot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("broot") 0 193 193
3151  
3152 # Handle trivial cases.
3153  
3154 193 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 36 157 193
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1620157
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
3155  
3156 157 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 44 113 157
3157 # -27 ** (1/3) = -(27 ** (1/3)) = -3
3158 44 ▶ return $x -> broot($y -> copy() -> bneg(), @r) -> bneg()
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int and $y->is_int and $y->is_neg and $y->is_odd 4 40 44
Condition77%7/9
$x->is_int and $y->is_int and $y->is_neg and $y->is_odd
!ll&&!rl&&r
4004
$x->is_int and $y->is_int and $y->is_neg
!ll&&!rl&&r
12284
$x->is_int and $y->is_int
!ll&&!rl&&r
12032
MC/DC50%2/4
$x->is_int and $y->is_int and $y->is_neg and $y->is_odd
$x->is_int $y->is_int $y->is_neg $y->is_odd
Truth table60%3/5
A and B and C and D
A= $x->is_intB= $y->is_intC= $y->is_negD= $y->is_odd
ABCDresult
0XXX0
10XX0
110X0
11100
11111
3159 if ($x -> is_int() && $y -> is_int() &&
3160 $y -> is_neg() && $y -> is_odd());
3161 40 ▶ return $x -> _upg -> broot($y, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 40 40
3162 40 return $x -> bnan(@r);
3163 }
3164  
3165 # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0
3166 return $x -> bnan(@r) if ($x->{sign} !~ /^\+/ || $y -> is_zero() ||
3167 113 ▶ $y->{sign} !~ /^\+$/);
Branch: 1 branch
BranchTrueFalseTotal
if not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/ 24 89 113
Condition83%5/6
not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/
l!l&&r!l&&!r
121289
not $x->{"sign"} =~ /^\+/ or $y->is_zero
l!l&&r!l&&!r
012101
MC/DC66%2/3
not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/
not $x->{"sign"} =~ /^\+/ $y->is_zero not $y->{"sign"} =~ /^\+$/
Truth table75%3/4
not(A) or B or not(C)
A= not $x->{"sign"} =~ /^\+/B= $y->is_zeroC= not $y->{"sign"} =~ /^\+$/
ABCresult
1XX1
01X1
0011
0000
3168  
3169 # Trivial cases.
3170 89 ▶ return $x if ($x -> is_zero() || $x -> is_one() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one or $x->is_inf or $y->is_one 16 73 89
Condition100%9/9
$x->is_zero or $x->is_one or $x->is_inf or $y->is_one
l!l&&r!l&&!r
12473
$x->is_zero or $x->is_one or $x->is_inf
l!l&&r!l&&!r
8477
$x->is_zero or $x->is_one
l!l&&r!l&&!r
4481
MC/DC100%4/4
$x->is_zero or $x->is_one or $x->is_inf or $y->is_one
$x->is_zero $x->is_one $x->is_inf $y->is_one
Truth table100%5/5
A or B or C or D
A= $x->is_zeroB= $x->is_oneC= $x->is_infD= $y->is_one
ABCDresult
1XXX1
01XX1
001X1
00011
00000
3171 $x -> is_inf() || $y -> is_one());
3172  
3173 # we need to limit the accuracy to protect against overflow
3174 73 my $fallback = 0;
3175 73 my (@params, $scale);
3176 73 ($x, @params) = $x->_find_round_parameters(@r);
3177  
3178 73 ▶ return $x if $x -> is_nan(); # error in _find_round_parameters?
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 73 73
3179  
3180 # no rounding at all, so must use fallback
3181 73 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 73 0 73
3182 # simulate old behaviour
3183 73 $params[0] = $class -> div_scale(); # and round to it as accuracy
3184 73 $scale = $params[0]+4; # at least four more for proper round
3185 73 $params[2] = $r[2]; # round mode by caller or undef
3186 73 $fallback = 1; # to clear a/p afterwards
3187 } else {
3188 # the 4 below is empirical, and there might be cases where it is not
3189 # enough...
3190 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
3191 }
3192  
3193 # When user set globals, they would interfere with our calculation, so
3194 # disable them and later re-enable them.
3195  
3196 73 my $ab = $class -> accuracy();
3197 73 my $pb = $class -> precision();
3198 73 $class -> accuracy(undef);
3199 73 $class -> precision(undef);
3200  
3201 # Disabling upgrading and downgrading is no longer necessary to avoid an
3202 # infinite recursion, but it avoids unnecessary upgrading and downgrading
3203 # in the intermediate computations.
3204  
3205 73 my $upg = $class -> upgrade();
3206 73 my $dng = $class -> downgrade();
3207 73 $class -> upgrade(undef);
3208 73 $class -> downgrade(undef);
3209  
3210 # We also need to disable any set A or P on $x (_find_round_parameters took
3211 # them already into account), since these would interfere, too.
3212  
3213 73 $x->{accuracy} = undef;
3214 73 $x->{precision} = undef;
3215  
3216 # remember sign and make $x positive, since -4 ** (1/2) => -2
3217 73 my $sign = 0;
3218 73 ▶ $sign = 1 if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 0 73 73
3219 73 $x->{sign} = '+';
3220  
3221 73 my $is_two = 0;
3222 73 ▶ if ($y -> isa('Math::BigFloat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->isa('Math::BigFloat')) { } 73 0 73
3223 $is_two = $y->{sign} eq '+' && $LIB->_is_two($y->{_m})
3224 73 ▶ && $LIB->_is_zero($y->{_e});
Condition66%4/6
$y->{'sign'} eq '+' && $LIB->_is_two($y->{'_m'}) && $LIB->_is_zero($y->{'_e'})
!ll&&!rl&&r
24049
$y->{"sign"} eq "+" and $LIB->_is_two($y->{"_m"})
!ll&&!rl&&r
02449
MC/DC33%1/3
$y->{'sign'} eq '+' && $LIB->_is_two($y->{'_m'}) && $LIB->_is_zero($y->{'_e'})
$y->{"sign"} eq "+" $LIB->_is_two($y->{"_m"}) $LIB->_is_zero($y->{'_e'})
Truth table50%2/4
A and B && C
A= $y->{"sign"} eq "+"B= $LIB->_is_two($y->{"_m"})C= $LIB->_is_zero($y->{'_e'})
ABCresult
0XX0
10X0
1100
1111
3225 } else {
3226 0 $is_two = $y == 2;
3227 }
3228  
3229 # Normal square root if $y == 2
3230  
3231 73 ▶ if ($is_two) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_two) { } 49 24 73
elsif ($y->is_one('-')) { } 0 24 24
3232 49 $x -> bsqrt($scale + 4);
3233 }
3234  
3235 # Inverse: $x ** (-1) => 1 / $x
3236  
3237 elsif ($y -> is_one('-')) {
3238 0 $x -> binv($scale + 4);
3239 }
3240  
3241 # General case: calculate the broot() as integer result first, and if it
3242 # fits, return it rightaway (but only if $x and $y are integer).
3243 #
3244 # This code should be improved. XXX
3245  
3246 else {
3247  
3248 # Temporarily disable upgrading in Math::BigInt.
3249  
3250 24 my $mbi_upg = Math::BigInt -> upgrade();
3251 24 Math::BigInt -> upgrade(undef);
3252  
3253 24 my $done = 0; # not yet
3254 24 ▶ if ($y -> is_int() && $x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int and $x->is_int) 23 1 24
Condition66%2/3
$y->is_int and $x->is_int
!ll&&!rl&&r
0123
MC/DC50%1/2
$y->is_int and $x->is_int
$y->is_int $x->is_int
Truth table66%2/3
A and B
A= $y->is_intB= $x->is_int
ABresult
0X0
100
111
3255 23 my $i = $LIB->_copy($x->{_m});
3256 23 ▶ $i = $LIB->_lsft($i, $x->{_e}, 10) unless $LIB->_is_zero($x->{_e});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_zero($x->{"_e"}) 0 23 23
3257 23 my $int = Math::BigInt -> bzero();
3258 23 $int->{value} = $i;
3259 23 $int -> broot($y -> as_int());
3260 # if ($exact)
3261 23 ▶ if ($int -> copy() -> bpow($y -> as_int()) == $x -> as_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int->copy->bpow($y->as_int) == $x->as_int) 17 6 23
3262 # found result, return it
3263 17 $x->{_m} = $int->{value};
3264 17 $x->{_e} = $LIB->_zero();
3265 17 $x->{_es} = '+';
3266 17 $x -> bnorm();
3267 17 $done = 1;
3268 }
3269 }
3270  
3271 24 ▶ if ($done == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done == 0) 7 17 24
3272 7 my $u = $class -> bone() -> bdiv($y, $scale+4);
3273 7 $u->{accuracy} = undef;
3274 7 $u->{precision} = undef;
3275 7 $x -> bpow($u, $scale+4); # el cheapo
3276 }
3277  
3278 24 Math::BigInt -> upgrade($mbi_upg);
3279 }
3280  
3281 73 ▶ $x -> bneg() if $sign == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $sign == 1 0 73 73
3282  
3283 # shortcut to not run through _find_round_parameters again
3284 73 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 73 0 73
3285 73 $x -> bround($params[0], $params[2]); # then round accordingly
3286 } else {
3287 0 $x -> bfround($params[1], $params[2]); # then round accordingly
3288 }
3289 73 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 73 0 73
3290 # clear a/p after round, since user did not request it
3291 73 $x->{accuracy} = undef;
3292 73 $x->{precision} = undef;
3293 }
3294  
3295 # Restore globals. We need to do it like this, because setting one
3296 # undefines the other.
3297  
3298 73 ▶ if (defined $ab) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ab) { } 0 73 73
3299 0 $class -> accuracy($ab);
3300 } else {
3301 73 $class -> precision($pb);
3302 }
3303  
3304 73 $class -> upgrade($upg);
3305 73 $class -> downgrade($dng);
3306  
3307 73 $x -> round(@r);
3308 73 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 35 38 73
Condition66%4/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
35038
$x->is_int or $x->is_inf
l!l&&r!l&&!r
35038
MC/DC33%1/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table50%2/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
3309 $x -> is_inf() ||
3310 $x -> is_nan());
3311 73 return $x;
3312 }
3313  
3314 247 ▶sub bmuladd {
Subroutine bmuladd: called CC=48 SCAR=50.3
Pod documented
3315 # multiply two numbers and add the third to the result
3316  
3317 # set up parameters
3318 247 ▶ my ($class, $x, $y, $z, @r)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 246 1 247
Condition66%4/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
10246
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
01246
MC/DC33%1/3
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
ref $_[0] ref $_[0] eq ref $_[1] ref $_[1] eq ref $_[2]
Truth table50%2/4
A and B and C
A= ref $_[0]B= ref $_[0] eq ref $_[1]C= ref $_[1] eq ref $_[2]
ABCresult
0XX0
10X0
1100
1111
3319 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
3320 ? (ref($_[0]), @_)
3321 : objectify(3, @_);
3322  
3323 # Don't modify constant (read-only) objects.
3324  
3325 247 ▶ return $x if $x -> modify('bmuladd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmuladd") 0 247 247
3326  
3327 # At least one of x, y, and z is a NaN
3328  
3329 247 ▶ return $x -> bnan(@r) if ($x -> is_nan() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan or $z->is_nan 33 214 247
Condition100%6/6
$x->is_nan or $y->is_nan or $z->is_nan
l!l&&r!l&&!r
294214
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1712218
MC/DC100%3/3
$x->is_nan or $y->is_nan or $z->is_nan
$x->is_nan $y->is_nan $z->is_nan
Truth table100%4/4
A or B or C
A= $x->is_nanB= $y->is_nanC= $z->is_nan
ABCresult
1XX1
01X1
0011
0000
3330 $y -> is_nan() ||
3331 $z -> is_nan());
3332  
3333 # At least one of x, y, and z is an Inf
3334  
3335 214 ▶ if ($x -> is_inf("-")) {
Branch: 5 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 8 206 214
elsif ($x->{'sign'} eq '+inf') { } 10 196 206
elsif ($x->is_neg) { } 40 156 196
elsif ($x->is_zero) { } 17 139 156
elsif ($x->is_pos) { } 139 0 139
3336  
3337 8 ▶ if ($y -> is_neg()) { # x = -inf, y < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_neg) { } 4 4 8
elsif ($y->is_zero) { } 0 4 4
3338 4 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 4 4
3339 0 return $x -> bnan(@r);
3340 } else {
3341 4 return $x -> binf("+", @r);
3342 }
3343 } elsif ($y -> is_zero()) { # x = -inf, y = 0
3344 0 return $x -> bnan(@r);
3345 } else { # x = -inf, y > 0
3346 4 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 4 4
3347 0 return $x -> bnan(@r);
3348 } else {
3349 4 return $x -> binf("-", @r);
3350 }
3351 }
3352  
3353 } elsif ($x->{sign} eq "+inf") {
3354  
3355 10 ▶ if ($y -> is_neg()) { # x = +inf, y < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_neg) { } 4 6 10
elsif ($y->is_zero) { } 0 6 6
3356 4 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 4 4
3357 0 return $x -> bnan(@r);
3358 } else {
3359 4 return $x -> binf("-", @r);
3360 }
3361 } elsif ($y -> is_zero()) { # x = +inf, y = 0
3362 0 return $x -> bnan(@r);
3363 } else { # x = +inf, y > 0
3364 6 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 6 6
3365 0 return $x -> bnan(@r);
3366 } else {
3367 6 return $x -> binf("+", @r);
3368 }
3369 }
3370  
3371 } elsif ($x -> is_neg()) {
3372  
3373 40 ▶ if ($y -> is_inf("-")) { # -inf < x < 0, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('-')) { } 0 40 40
elsif ($y->{'sign'} eq '+inf') { } 0 40 40
3374 0 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
3375 0 return $x -> bnan(@r);
3376 } else {
3377 0 return $x -> binf("+", @r);
3378 }
3379 } elsif ($y->{sign} eq "+inf") { # -inf < x < 0, y = +inf
3380 0 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 0 0
3381 0 return $x -> bnan(@r);
3382 } else {
3383 0 return $x -> binf("-", @r);
3384 }
3385 } else { # -inf < x < 0, -inf < y < +inf
3386 40 ▶ if ($z -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 40 40
elsif ($z->{'sign'} eq '+inf') { } 0 40 40
3387 0 return $x -> binf("-", @r);
3388 } elsif ($z->{sign} eq "+inf") {
3389 0 return $x -> binf("+", @r);
3390 }
3391 }
3392  
3393 } elsif ($x -> is_zero()) {
3394  
3395 17 ▶ if ($y -> is_inf("-")) { # x = 0, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('-')) { } 0 17 17
elsif ($y->{'sign'} eq '+inf') { } 0 17 17
3396 0 return $x -> bnan(@r);
3397 } elsif ($y->{sign} eq "+inf") { # x = 0, y = +inf
3398 0 return $x -> bnan(@r);
3399 } else { # x = 0, -inf < y < +inf
3400 17 ▶ if ($z -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 17 17
elsif ($z->{'sign'} eq '+inf') { } 0 17 17
3401 0 return $x -> binf("-", @r);
3402 } elsif ($z->{sign} eq "+inf") {
3403 0 return $x -> binf("+", @r);
3404 }
3405 }
3406  
3407 } elsif ($x -> is_pos()) {
3408  
3409 139 ▶ if ($y -> is_inf("-")) { # 0 < x < +inf, y = -inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('-')) { } 0 139 139
elsif ($y->{'sign'} eq '+inf') { } 0 139 139
3410 0 ▶ if ($z->{sign} eq "+inf") {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{'sign'} eq '+inf') { } 0 0 0
3411 0 return $x -> bnan(@r);
3412 } else {
3413 0 return $x -> binf("-", @r);
3414 }
3415 } elsif ($y->{sign} eq "+inf") { # 0 < x < +inf, y = +inf
3416 0 ▶ if ($z -> is_inf("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 0 0
3417 0 return $x -> bnan(@r);
3418 } else {
3419 0 return $x -> binf("+", @r);
3420 }
3421 } else { # 0 < x < +inf, -inf < y < +inf
3422 139 ▶ if ($z -> is_inf("-")) {
Branch: 2 branches
BranchTrueFalseTotal
if ($z->is_inf('-')) { } 0 139 139
elsif ($z->{'sign'} eq '+inf') { } 1 138 139
3423 0 return $x -> binf("-", @r);
3424 } elsif ($z->{sign} eq "+inf") {
3425 1 return $x -> binf("+", @r);
3426 }
3427 }
3428 }
3429  
3430 # At this point, we know that x, y, and z are finite numbers
3431  
3432 # Rather than copying $y and/or $z, perhaps we should assign the output to
3433 # a temporary $x value, and assign the final result to $x? XXX
3434  
3435 195 ▶ $y = $y -> copy() if refaddr($y) eq refaddr($x);
Branch: 1 branch
BranchTrueFalseTotal
if refaddr($y) eq refaddr($x) 0 195 195
3436 195 ▶ $z = $z -> copy() if refaddr($z) eq refaddr($x);
Branch: 1 branch
BranchTrueFalseTotal
if refaddr($z) eq refaddr($x) 0 195 195
3437  
3438 # aEb * cEd = (a*c)E(b+d)
3439 195 $x->{_m} = $LIB->_mul($x->{_m}, $y->{_m});
3440 ($x->{_e}, $x->{_es})
3441 195 = $LIB -> _sadd($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
3442  
3443 195 $r[3] = $y; # no push!
3444  
3445 # adjust sign:
3446 195 ▶ $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} ne $y->{'sign'} ? : 44 151 195
3447  
3448 # take lower of the two e's and adapt m1 to it to match m2
3449 195 my $e = $z->{_e};
3450 195 ▶ $e = $LIB->_zero() if !defined $e; # if no BFLOAT?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 0 195 195
3451 195 $e = $LIB->_copy($e); # make copy (didn't do it yet)
3452  
3453 195 my $es;
3454  
3455 195 ▶ ($e, $es) = $LIB -> _ssub($e, $z->{_es} || '+', $x->{_e}, $x->{_es});
Condition50%1/2
$z->{'_es'} || '+'
l!l
1950
MC/DC0%0/1
$z->{'_es'} || '+'
$z->{'_es'}
Truth table50%1/2
A
A= $z->{'_es'}
Aresult
11
00
3456  
3457 195 my $add = $LIB->_copy($z->{_m});
3458  
3459 195 ▶ if ($es eq '-') # < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($es eq '-') { } 4 191 195
elsif (not $LIB->_is_zero($e)) { } 9 182 191
3460 {
3461 4 $x->{_m} = $LIB->_lsft($x->{_m}, $e, 10);
3462 4 ($x->{_e}, $x->{_es}) = $LIB -> _sadd($x->{_e}, $x->{_es}, $e, $es);
3463 } elsif (!$LIB->_is_zero($e)) # > 0
3464 {
3465 9 $add = $LIB->_lsft($add, $e, 10);
3466 }
3467 # else: both e are the same, so just leave them
3468  
3469 195 ▶ if ($x->{sign} eq $z->{sign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq $z->{'sign'}) { } 151 44 195
3470 # add
3471 151 $x->{_m} = $LIB->_add($x->{_m}, $add);
3472 } else {
3473 ($x->{_m}, $x->{sign}) =
3474 44 $LIB -> _sadd($x->{_m}, $x->{sign}, $add, $z->{sign});
3475 }
3476  
3477 # delete trailing zeros, then round
3478 195 $x -> bnorm() -> round(@r);
3479  
3480 195 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 187 8 195
Condition66%4/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
18708
$x->is_int or $x->is_inf
l!l&&r!l&&!r
18708
MC/DC33%1/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table50%2/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
3481 $x -> is_inf() ||
3482 $x -> is_nan());
3483 195 return $x;
3484 }
3485  
3486 20 ▶sub bmodpow {
Subroutine bmodpow: called CC=16 SCAR=35.1
Pod documented
3487 # takes a very large number to a very large exponent in a given very
3488 # large modulus, quickly, thanks to binary exponentiation. Supports
3489 # negative exponents.
3490 20 ▶ my ($class, $num, $exp, $mod, @r)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 20 0 20
Condition33%2/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
0020
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0020
MC/DC0%0/3
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
ref $_[0] ref $_[0] eq ref $_[1] ref $_[1] eq ref $_[2]
Truth table25%1/4
A and B and C
A= ref $_[0]B= ref $_[0] eq ref $_[1]C= ref $_[1] eq ref $_[2]
ABCresult
0XX0
10X0
1100
1111
3491 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
3492 ? (ref($_[0]), @_)
3493 : objectify(3, @_);
3494  
3495 # Don't modify constant (read-only) objects.
3496  
3497 20 ▶ return $num if $num -> modify('bmodpow');
Branch: 1 branch
BranchTrueFalseTotal
if $num->modify("bmodpow") 0 20 20
3498  
3499 20 ▶ return $num -> bnan(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $mod->is_nan or $exp->is_nan or $mod->is_nan 0 20 20
Condition33%2/6
$mod->is_nan or $exp->is_nan or $mod->is_nan
l!l&&r!l&&!r
0020
$mod->is_nan or $exp->is_nan
l!l&&r!l&&!r
0020
MC/DC0%0/3
$mod->is_nan or $exp->is_nan or $mod->is_nan
$mod->is_nan $exp->is_nan $mod->is_nan
Truth table25%1/4
A or B or C
A= $mod->is_nanB= $exp->is_nanC= $mod->is_nan
ABCresult
1XX1
01X1
0011
0000
3500 if $mod -> is_nan() || $exp -> is_nan() || $mod -> is_nan();
3501  
3502 # check modulus for valid values
3503 20 ▶ return $num -> bnan(@r) if $mod->{sign} ne '+' || $mod -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $mod->{"sign"} ne "+" or $mod->is_zero 0 20 20
Condition33%1/3
$mod->{"sign"} ne "+" or $mod->is_zero
l!l&&r!l&&!r
0020
MC/DC0%0/2
$mod->{"sign"} ne "+" or $mod->is_zero
$mod->{"sign"} ne "+" $mod->is_zero
Truth table33%1/3
A or B
A= $mod->{"sign"} ne "+"B= $mod->is_zero
ABresult
1X1
011
000
3504  
3505 # check exponent for valid values
3506 20 ▶ if ($exp->{sign} =~ /\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp->{"sign"} =~ /\w/) 0 20 20
3507 # i.e., if it's NaN, +inf, or -inf...
3508 0 return $num -> bnan(@r);
3509 }
3510  
3511 20 ▶ $num -> bmodinv($mod, @r) if $exp->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $exp->{"sign"} eq "-" 0 20 20
3512  
3513 # check num for valid values (also NaN if there was no inverse but $exp < 0)
3514 20 ▶ return $num -> bnan(@r) if $num->{sign} !~ /^[+-]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $num->{"sign"} =~ /^[+-]$/ 0 20 20
3515  
3516 # $mod is positive, sign on $exp is ignored, result also positive
3517  
3518 # XXX TODO: speed it up when all three numbers are integers
3519 20 $num -> bpow($exp) -> bmod($mod);
3520  
3521 20 $num -> round(@r);
3522 20 ▶ $num -> _dng() if ($num -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $num->is_int or $num->is_inf or $num->is_nan 16 4 20
Condition66%4/6
$num->is_int or $num->is_inf or $num->is_nan
l!l&&r!l&&!r
1604
$num->is_int or $num->is_inf
l!l&&r!l&&!r
1604
MC/DC33%1/3
$num->is_int or $num->is_inf or $num->is_nan
$num->is_int $num->is_inf $num->is_nan
Truth table50%2/4
A or B or C
A= $num->is_intB= $num->is_infC= $num->is_nan
ABCresult
1XX1
01X1
0011
0000
3523 $num -> is_inf() ||
3524 $num -> is_nan());
3525 20 return $num;
3526 }
3527  
3528 263 ▶sub blog {
Subroutine blog: called CC=42 SCAR=39.2
Pod documented
3529 # Return the logarithm of the operand. If a second operand is defined, that
3530 # value is used as the base, otherwise the base is assumed to be Euler's
3531 # constant.
3532  
3533 263 my ($class, $x, $base, @r);
3534  
3535 # Only objectify the base if it is defined, since an undefined base, as in
3536 # $x->blog() or $x->blog(undef) signals that the base is Euler's number =
3537 # 2.718281828...
3538  
3539 263 ▶ if (!ref($_[0]) && $_[0] =~ /^[A-Za-z]|::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/) { } 1 262 263
Condition66%2/3
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
!ll&&!rl&&r
26201
MC/DC50%1/2
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
not ref $_[0] $_[0] =~ /^[A-Za-z]|::/
Truth table66%2/3
not(A) and B
A= not ref $_[0]B= $_[0] =~ /^[A-Za-z]|::/
ABresult
0X0
100
111
3540 # E.g., Math::BigFloat->blog(256, 2)
3541 1 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 1 0 1
3542 defined $_[2] ? objectify(2, @_) : objectify(1, @_);
3543 } else {
3544 # E.g., $x->blog(2) or the deprecated Math::BigFloat::blog(256, 2)
3545 262 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 43 219 262
3546 defined $_[1] ? objectify(2, @_) : objectify(1, @_);
3547 }
3548  
3549 # Don't modify constant (read-only) objects.
3550  
3551 263 ▶ return $x if $x -> modify('blog');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blog") 0 263 263
3552  
3553 # Handle all exception cases and all trivial cases. I have used Wolfram
3554 # Alpha (http://www.wolframalpha.com) as the reference for these cases.
3555  
3556 263 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 259 263
3557  
3558 259 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 44 215 259
3559 44 ▶ $base = $class -> new($base)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($base) and $base->isa(__PACKAGE__) 0 44 44
Condition33%1/3
defined builtin::blessed($base) and $base->isa(__PACKAGE__)
!ll&&!rl&&r
0044
MC/DC0%0/2
defined builtin::blessed($base) and $base->isa(__PACKAGE__)
defined builtin::blessed($base) $base->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($base)B= $base->isa(__PACKAGE__)
ABresult
0X0
100
111
3560 unless defined(blessed($base)) && $base -> isa(__PACKAGE__);
3561 44 ▶ if ($base -> is_nan() || $base -> is_one()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($base->is_nan or $base->is_one) { } 8 36 44
elsif ($base->is_inf or $base->is_zero) { } 4 32 36
elsif ($base->is_negative) { } 4 28 32
Condition66%2/3
$base->is_nan or $base->is_one
l!l&&r!l&&!r
0836
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
3562 8 return $x -> bnan(@r);
3563 ▶ } elsif ($base -> is_inf() || $base -> is_zero()) {
Condition66%2/3
$base->is_inf or $base->is_zero
l!l&&r!l&&!r
0432
MC/DC50%1/2
$base->is_inf or $base->is_zero
$base->is_inf $base->is_zero
Truth table66%2/3
A or B
A= $base->is_infB= $base->is_zero
ABresult
1X1
011
000
3564 4 ▶ return $x -> bnan(@r) if $x -> is_inf() || $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf or $x->is_zero 0 4 4
Condition33%1/3
$x->is_inf or $x->is_zero
l!l&&r!l&&!r
004
MC/DC0%0/2
$x->is_inf or $x->is_zero
$x->is_inf $x->is_zero
Truth table33%1/3
A or B
A= $x->is_infB= $x->is_zero
ABresult
1X1
011
000
3565 4 return $x -> bzero(@r);
3566 } elsif ($base -> is_negative()) { # -inf < base < 0
3567 4 ▶ return $x -> bzero(@r) if $x -> is_one(); # x = 1
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 0 4 4
3568 4 ▶ return $x -> bone('+', @r) if $x == $base; # x = base
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 0 4 4
3569 # we can't handle these cases, so upgrade, if we can
3570 4 ▶ return $x -> _upg() -> blog($base, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 4 4
3571 4 return $x -> bnan(@r);
3572 }
3573 28 ▶ return $x -> bone(@r) if $x == $base; # 0 < base && 0 < x < inf
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 11 17 28
3574 }
3575  
3576 232 ▶ if ($x -> is_inf()) { # x = +/-inf
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 8 224 232
elsif ($x->is_neg) { } 16 208 224
elsif ($x->is_one) { } 16 192 208
elsif ($x->is_zero) { } 8 184 192
3577 8 ▶ my $sign = defined($base) && $base < 1 ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
defined $base && $base < 1 ? : 0 8 8
Condition33%1/3
defined $base and $base < 1
!ll&&!rl&&r
800
MC/DC0%0/2
defined $base and $base < 1
defined $base $base < 1
Truth table33%1/3
A and B
A= defined $baseB= $base < 1
ABresult
0X0
100
111
3578 8 return $x -> binf($sign, @r);
3579 } elsif ($x -> is_neg()) { # -inf < x < 0
3580 16 ▶ return $x -> _upg() -> blog($base, @r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 16 16
3581 16 return $x -> bnan(@r);
3582 } elsif ($x -> is_one()) { # x = 1
3583 16 return $x -> bzero(@r);
3584 } elsif ($x -> is_zero()) { # x = 0
3585 8 ▶ my $sign = defined($base) && $base < 1 ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
defined $base && $base < 1 ? : 0 8 8
Condition33%1/3
defined $base and $base < 1
!ll&&!rl&&r
800
MC/DC0%0/2
defined $base and $base < 1
defined $base $base < 1
Truth table33%1/3
A and B
A= defined $baseB= $base < 1
ABresult
0X0
100
111
3586 8 return $x -> binf($sign, @r);
3587 }
3588  
3589 # we need to limit the accuracy to protect against overflow
3590 184 my $fallback = 0;
3591 184 my ($scale, @params);
3592 184 ($x, @params) = $x->_find_round_parameters(@r);
3593  
3594 # no rounding at all, so must use fallback
3595 184 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 97 87 184
3596 # simulate old behaviour
3597 97 $params[0] = $class -> div_scale(); # and round to it as accuracy
3598 97 $params[1] = undef; # P = undef
3599 97 $scale = $params[0]+4; # at least four more for proper round
3600 97 $params[2] = $r[2]; # round mode by caller or undef
3601 97 $fallback = 1; # to clear a/p afterwards
3602 } else {
3603 # the 4 below is empirical, and there might be cases where it is not
3604 # enough...
3605 87 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition33%1/3
$params[0] || $params[1]
l!l&&r!l&&!r
8700
MC/DC0%0/2
$params[0] || $params[1]
$params[0] $params[1]
Truth table33%1/3
A || B
A= $params[0]B= $params[1]
ABresult
1X1
011
000
3606 }
3607  
3608 # When user set globals, they would interfere with our calculation, so
3609 # disable them and later re-enable them.
3610  
3611 184 my $ab = $class -> accuracy();
3612 184 my $pb = $class -> precision();
3613 184 $class -> accuracy(undef);
3614 184 $class -> precision(undef);
3615  
3616 # Disabling upgrading and downgrading is no longer necessary to avoid an
3617 # infinite recursion, but it avoids unnecessary upgrading and downgrading
3618 # in the intermediate computations.
3619  
3620 184 my $upg = $class -> upgrade();
3621 184 my $dng = $class -> downgrade();
3622 184 $class -> upgrade(undef);
3623 184 $class -> downgrade(undef);
3624  
3625 # We also need to disable any set A or P on $x (_find_round_parameters took
3626 # them already into account), since these would interfere, too.
3627  
3628 184 $x->{accuracy} = undef;
3629 184 $x->{precision} = undef;
3630  
3631 184 my $done = 0;
3632  
3633 # If both $x and $base are integers, try to calculate an integer result
3634 # first. This is very fast, and if the exact result was found, we are done.
3635  
3636 184 ▶ if (defined($base) && $base -> is_int() && $x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base and $base->is_int and $x->is_int) 11 173 184
Condition83%5/6
defined $base and $base->is_int and $x->is_int
!ll&&!rl&&r
171211
defined $base and $base->is_int
!ll&&!rl&&r
171013
MC/DC66%2/3
defined $base and $base->is_int and $x->is_int
defined $base $base->is_int $x->is_int
Truth table75%3/4
A and B and C
A= defined $baseB= $base->is_intC= $x->is_int
ABCresult
0XX0
10X0
1100
1111
3637 11 my $x_lib = $LIB -> _new($x -> bdstr());
3638 11 my $b_lib = $LIB -> _new($base -> bdstr());
3639 11 ($x_lib, my $exact) = $LIB -> _log_int($x_lib, $b_lib);
3640 11 ▶ if ($exact) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exact) 10 1 11
3641 10 $x->{_m} = $x_lib;
3642 10 $x->{_e} = $LIB -> _zero();
3643 10 $x -> bnorm();
3644 10 $done = 1;
3645 }
3646 }
3647  
3648 # If the integer result was not accurate, compute the natural logarithm
3649 # log($x) (using reduction by 10 and possibly also by 2), and if a
3650 # different base was requested, convert the result with log($x)/log($base).
3651  
3652 184 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 174 10 184
3653 174 $x -> _log_10($scale);
3654 174 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 3 171 174
3655 # log_b(x) = ln(x) / ln(b), so compute ln(b)
3656 3 my $base_log_e = $base -> copy() -> _log_10($scale);
3657 3 $x -> bdiv($base_log_e, $scale);
3658 }
3659 }
3660  
3661 # shortcut to not run through _find_round_parameters again
3662  
3663 184 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 184 0 184
3664 184 $x -> bround($params[0], $params[2]); # then round accordingly
3665 } else {
3666 0 $x -> bfround($params[1], $params[2]); # then round accordingly
3667 }
3668 184 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 97 87 184
3669 # clear a/p after round, since user did not request it
3670 97 $x->{accuracy} = undef;
3671 97 $x->{precision} = undef;
3672 }
3673  
3674 # Restore globals. We need to do it like this, because setting one
3675 # undefines the other.
3676  
3677 184 ▶ if (defined $ab) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ab) { } 0 184 184
3678 0 $class -> accuracy($ab);
3679 } else {
3680 184 $class -> precision($pb);
3681 }
3682  
3683 184 $class -> upgrade($upg);
3684 184 $class -> downgrade($dng);
3685  
3686 184 $x -> round(@r);
3687 184 ▶ return $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 15 169 184
3688 169 return $x;
3689 }
3690  
3691 20 ▶sub bexp {
Subroutine bexp: called CC=25 SCAR=34.0
Pod documented
3692 # Calculate e ** X (Euler's number to the power of X)
3693 20 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 11 9 20
3694  
3695 # Don't modify constant (read-only) objects.
3696  
3697 20 ▶ return $x if $x -> modify('bexp');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bexp") 0 20 20
3698  
3699 20 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 20 20
3700 20 ▶ return $x -> binf(@r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 20 20
3701 20 ▶ return $x -> bzero(@r) if $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("-") 0 20 20
3702  
3703 # Get the rounding parameters, if any.
3704  
3705 20 my $fallback = 0;
3706 20 my ($scale, @params);
3707 20 ($x, @params) = $x -> _find_round_parameters(@r);
3708  
3709 # Error in _find_round_parameters?
3710 20 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 20 20
3711  
3712 20 ▶ return $x -> bone(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 20 20
3713  
3714 # If no rounding parameters are give, use fallback.
3715  
3716 20 ▶ if (!@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @params) { } 11 9 20
3717 11 $params[0] = $class -> div_scale(); # fallback accuracy
3718 11 $params[1] = undef; # no precision
3719 11 $params[2] = $r[2]; # rounding mode
3720 11 $scale = $params[0];
3721 11 $fallback = 1; # to clear a/p afterwards
3722 } else {
3723 9 ▶ if (defined($params[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 9 0 9
3724 9 $scale = $params[0];
3725 } else {
3726 # We perform the computations below using accuracy only, not
3727 # precision, so when precision is given, we need to "convert" this
3728 # to accuracy. To do that, we need to know, at least approximately,
3729 # how many digits there will be in the final result.
3730 #
3731 # log10(exp($x)) = log(exp($x)) / log(10) = $x / log(10)
3732  
3733 #$scale = 1 + int(log($ms) / log(10) + $es) - $params[1];
3734 0 my $ndig = $x -> numify() / log(10);
3735 0 $scale = 1 + int($ndig) - $params[1];
3736 }
3737 }
3738  
3739 # Add extra digits to reduce the consequence of round-off errors in the
3740 # intermediate computations.
3741  
3742 20 $scale += 4;
3743  
3744 20 ▶ if (!$x -> isa('Math::BigFloat')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->isa("Math::BigFloat")) 0 20 20
3745 0 $x = Math::BigFloat -> new($x);
3746 0 $class = ref($x);
3747 }
3748  
3749 # When user set globals, they would interfere with our calculation, so
3750 # disable them and later re-enable them.
3751  
3752 20 my $ab = $class -> accuracy();
3753 20 my $pb = $class -> precision();
3754 20 $class -> accuracy(undef);
3755 20 $class -> precision(undef);
3756  
3757 # Disabling upgrading and downgrading is no longer necessary to avoid an
3758 # infinite recursion, but it avoids unnecessary upgrading and downgrading
3759 # in the intermediate computations.
3760  
3761 20 my $upg = $class -> upgrade();
3762 20 my $dng = $class -> downgrade();
3763 20 $class -> upgrade(undef);
3764 20 $class -> downgrade(undef);
3765  
3766 # We also need to disable any set A or P on $x (_find_round_parameters took
3767 # them already into account), since these would interfere, too.
3768  
3769 20 $x->{accuracy} = undef;
3770 20 $x->{precision} = undef;
3771  
3772 20 my $x_orig = $x -> copy();
3773  
3774 # We use the following Taylor series:
3775  
3776 # x x^2 x^3 x^4
3777 # e = 1 + --- + --- + --- + --- ...
3778 # 1! 2! 3! 4!
3779  
3780 # The difference for each term is X and N, which would result in:
3781 # 2 copy, 2 mul, 2 add, 1 inc, 1 div operations per term
3782  
3783 # But it is faster to compute exp(1) and then raising it to the
3784 # given power, esp. if $x is really big and an integer because:
3785  
3786 # * The numerator is always 1, making the computation faster
3787 # * the series converges faster in the case of x == 1
3788 # * We can also easily check when we have reached our limit: when the
3789 # term to be added is smaller than "1E$scale", we can stop - f.i.
3790 # scale == 5, and we have 1/40320, then we stop since 1/40320 < 1E-5.
3791 # * we can compute the *exact* result by simulating bigrat math:
3792  
3793 # 1 1 gcd(3, 4) = 1 1*24 + 1*6 5
3794 # - + - = ---------- = --
3795 # 6 24 6*24 24
3796  
3797 # We do not compute the gcd() here, but simple do:
3798 # 1 1 1*24 + 1*6 30
3799 # - + - = --------- = --
3800 # 6 24 6*24 144
3801  
3802 # In general:
3803 # a c a*d + c*b and note that c is always 1 and d = (b*f)
3804 # - + - = ---------
3805 # b d b*d
3806  
3807 # This leads to: which can be reduced by b to:
3808 # a 1 a*b*f + b a*f + 1
3809 # - + - = --------- = -------
3810 # b b*f b*b*f b*f
3811  
3812 # The first terms in the series are:
3813  
3814 # 1 1 1 1 1 1 1 1 13700
3815 # -- + -- + -- + -- + -- + --- + --- + ---- = -----
3816 # 1 1 2 6 24 120 720 5040 5040
3817  
3818 # Note that we cannot simply reduce 13700/5040 to 685/252, but must keep
3819 # the numerator and the denominator!
3820  
3821 20 ▶ if ($scale <= 75) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= 75) { } 17 3 20
3822 # set $x directly from a cached string form
3823 17 $x->{_m} = $LIB->_new("2718281828459045235360287471352662497757" .
3824 "2470936999595749669676277240766303535476");
3825 17 $x->{sign} = '+';
3826 17 $x->{_es} = '-';
3827 17 $x->{_e} = $LIB->_new(79);
3828 } else {
3829 # compute A and B so that e = A / B.
3830  
3831 # After some terms we end up with this, so we use it as a starting
3832 # point:
3833 3 my $A = $LIB->_new("9093339520860578540197197" .
3834 "0164779391644753259799242");
3835 3 my $F = $LIB->_new(42);
3836 3 my $step = 42;
3837  
3838 # Compute number of steps needed to get $A and $B sufficiently large.
3839  
3840 3 my $steps = _len_to_steps($scale - 4);
3841 # print STDERR "# Doing $steps steps for ", $scale-4, " digits\n";
3842  
3843 3 ▶ while ($step++ <= $steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step++ <= $steps) 79 3 82
3844 # calculate $a * $f + 1
3845 79 $A = $LIB -> _mul($A, $F);
3846 79 $A = $LIB -> _inc($A);
3847 # increment f
3848 79 $F = $LIB -> _inc($F);
3849 }
3850  
3851 # Compute $B as factorial of $steps (this is faster than doing it
3852 # manually)
3853 3 my $B = $LIB->_fac($LIB->_new($steps));
3854  
3855 # print "A ", $LIB->_str($A), "\nB ", $LIB->_str($B), "\n";
3856  
3857 # compute A/B with $scale digits in the result (truncate, not round)
3858 3 $A = $LIB->_lsft($A, $LIB->_new($scale), 10);
3859 3 $A = $LIB->_div($A, $B);
3860  
3861 3 $x->{_m} = $A;
3862 3 $x->{sign} = '+';
3863 3 $x->{_es} = '-';
3864 3 $x->{_e} = $LIB->_new($scale);
3865 }
3866  
3867 # Now $x contains now an estimate of e, with some additional digits.
3868  
3869 20 ▶ if ($x_orig -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x_orig->is_one) { } 10 10 20
3870  
3871 # else just round the already computed result
3872  
3873 10 $x->{accuracy} = undef;
3874 10 $x->{precision} = undef;
3875  
3876 # shortcut to not run through _find_round_parameters again
3877  
3878 10 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 10 0 10
3879 10 $x -> bround($params[0], $params[2]); # then round accordingly
3880 } else {
3881 0 $x -> bfround($params[1], $params[2]); # then round accordingly
3882 }
3883  
3884 } else {
3885  
3886 # Use the fact exp(x) = exp(x/n)**n. In our case, n = 2**i for some
3887 # integer i. We use this to compute exp(y) where y = x / (2**i) and
3888 # 1 <= |y| < 2.
3889 #
3890 # The code below is similar to the code found in to_ieee754().
3891  
3892 # We need to find the base 2 exponent. First make an estimate of the
3893 # base 2 exponent, before adjusting it below. We could skip this
3894 # estimation and go straight to the while-loops below, but the loops
3895 # are slow, especially when the final exponent is far from zero and
3896 # even more so if the number of digits is large. This initial
3897 # estimation speeds up the computation dramatically.
3898 #
3899 # log2($m * 10**$e) = log10($m + 10**$e) * log(10)/log(2)
3900 # = (log10($m) + $e) * log(10)/log(2)
3901 # = (log($m)/log(10) + $e) * log(10)/log(2)
3902  
3903 10 my ($m, $e) = $x_orig -> nparts();
3904 10 my $ms = $m -> numify();
3905 10 my $es = $e -> numify();
3906  
3907 # We start off by initializing the exponent to zero and the mantissa to
3908 # the input value. Then we increase the mantissa and decrease the
3909 # exponent, or vice versa, until the mantissa is in the desired range
3910 # or we hit one of the limits for the exponent.
3911  
3912 10 my $mant = $x_orig -> copy() -> babs();
3913 10 my $expo;
3914  
3915 10 my $one = $class -> bone();
3916 10 my $two = $class -> new("2");
3917 10 my $half = $class -> new("0.5");
3918  
3919 10 my $expo_est = (log(abs($ms))/log(10) + $es) * log(10)/log(2);
3920 10 $expo_est = int($expo_est);
3921  
3922 # Don't multiply by a number raised to a negative exponent. This will
3923 # cause a division, whose result is truncated to some fixed number of
3924 # digits. Instead, multiply by the inverse number raised to a positive
3925 # exponent.
3926  
3927 10 $expo = $class -> new($expo_est);
3928 10 ▶ if ($expo_est > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo_est > 0) { } 10 0 10
elsif ($expo_est < 0) { } 0 0 0
3929 10 $mant -> bmul($half -> copy() -> bpow($expo));
3930 } elsif ($expo_est < 0) {
3931 0 my $expo_abs = $expo -> copy() -> bneg();
3932 0 $mant -> bmul($two -> copy() -> bpow($expo_abs));
3933 }
3934  
3935 # Final adjustment of the estimate above.
3936  
3937 10 ▶ while ($mant -> bcmp($two) >= 0) { # $mant <= $two
Branch: 1 branch
BranchTrueFalseTotal
if ($mant->bcmp($two) >= 0) 0 10 10
3938 0 $mant -> bmul($half);
3939 0 $expo -> binc();
3940 }
3941  
3942 10 ▶ while ($mant -> bcmp($one) < 0) { # $mant > $one
Branch: 1 branch
BranchTrueFalseTotal
if ($mant->bcmp($one) < 0) 0 10 10
3943 0 $mant -> bmul($two);
3944 0 $expo -> bdec();
3945 }
3946  
3947 # Because of the upscaling, we need some additional digits.
3948  
3949 10 my $rescale = int($scale + abs($expo) * log(2) / log(10) + 1);
3950 10 ▶ $rescale = 4 if $rescale < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $rescale < 4 0 10 10
3951  
3952 10 $x -> bpow($mant, $rescale);
3953 10 my $pow2 = $two -> bpow($expo, $rescale);
3954 10 ▶ $pow2 -> bneg() if $x_orig -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $x_orig->is_negative 2 8 10
3955  
3956 # The bpow() below fails with the GMP and GMPz libraries if abs($pow2)
3957 # >= 2**30 = 1073741824. With the Pari library, it fails already when
3958 # abs($pow) >= 2**13 = 8192. With the Calc library, it is rediculously
3959 # slow when abs($pow2) is large. Fixme?
3960  
3961 10 ▶ croak "cannot compute bexp(); input value is too large"
Branch: 1 branch
BranchTrueFalseTotal
if $pow2->copy->babs->bcmp("1073741824") >= 0 0 10 10
3962 if $pow2 -> copy() -> babs() -> bcmp("1073741824") >= 0;
3963  
3964 10 $x -> bpow($pow2, $rescale);
3965  
3966 # Rounding parameters given as arguments currently don't override
3967 # instance variables, so accuracy (which is set in the computations
3968 # above) must be undefined before rounding. Fixme.
3969  
3970 10 $x->{accuracy} = undef;
3971 10 $x -> round(@params);
3972 }
3973  
3974 20 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 11 9 20
3975 # clear a/p after round, since user did not request it
3976 11 $x->{accuracy} = undef;
3977 11 $x->{precision} = undef;
3978 }
3979  
3980 # Restore globals. We need to do it like this, because setting one
3981 # undefines the other.
3982  
3983 20 ▶ if (defined $ab) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ab) { } 0 20 20
3984 0 $class -> accuracy($ab);
3985 } else {
3986 20 $class -> precision($pb);
3987 }
3988  
3989 20 $class -> upgrade($upg);
3990 20 $class -> downgrade($dng);
3991  
3992 # If downgrading, remember to preserve the relevant instance parameters.
3993 # There should be a more elegant way to do this. Fixme.
3994  
3995 20 $x -> round(@r);
3996 20 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 20 20
3997 20 $x;
3998 }
3999  
4000 0 ▶sub bilog2 {
Subroutine bilog2: not called CC=10 SCAR=47.0
Pod documented
4001 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4002  
4003 # Don't modify constant (read-only) objects.
4004  
4005 0 ▶ return $x if $x -> modify('bilog2');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bilog2") 0 0 0
4006  
4007 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4008 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
4009 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
4010  
4011 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
4012 0 ▶ return $x -> _upg() -> bilog2(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4013 0 return $x -> bnan(@r);
4014 }
4015  
4016 0 ▶ if ($x->{_es} eq '-') { # exponent < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 0 0 0
elsif (not $LIB->_is_zero($x->{'_e'})) { } 0 0 0
4017 0 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10);
4018 } elsif (! $LIB->_is_zero($x->{_e})) { # exponent > 0
4019 0 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10);
4020 }
4021  
4022 0 $x->{_m} = $LIB -> _ilog2($x->{_m});
4023 0 $x->{_e} = $LIB -> _zero();
4024 0 $x -> bnorm() -> round(@r);
4025 0 $x -> _dng();
4026 0 return $x;
4027 }
4028  
4029 0 ▶sub bilog10 {
Subroutine bilog10: not called CC=10 SCAR=47.0
Pod documented
4030 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4031  
4032 # Don't modify constant (read-only) objects.
4033  
4034 0 ▶ return $x if $x -> modify('bilog10');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bilog10") 0 0 0
4035  
4036 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4037 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
4038 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
4039  
4040 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
4041 0 ▶ return $x -> _upg() -> bilog10(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4042 0 return $x -> bnan(@r);
4043 }
4044  
4045 0 ▶ if ($x->{_es} eq '-') { # exponent < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 0 0 0
elsif (not $LIB->_is_zero($x->{'_e'})) { } 0 0 0
4046 0 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10);
4047 } elsif (! $LIB->_is_zero($x->{_e})) { # exponent > 0
4048 0 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10);
4049 }
4050  
4051 0 $x->{_m} = $LIB -> _ilog10($x->{_m});
4052 0 $x->{_e} = $LIB -> _zero();
4053 0 $x -> bnorm() -> round(@r);
4054 0 $x -> _dng();
4055 0 return $x;
4056 }
4057  
4058 0 ▶sub bclog2 {
Subroutine bclog2: not called CC=10 SCAR=47.0
Pod documented
4059 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4060  
4061 # Don't modify constant (read-only) objects.
4062  
4063 0 ▶ return $x if $x -> modify('bclog2');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bclog2") 0 0 0
4064  
4065 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4066 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
4067 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
4068  
4069 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
4070 0 ▶ return $x -> _upg() -> bclog2(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4071 0 return $x -> bnan(@r);
4072 }
4073  
4074 0 ▶ if ($x->{_es} eq '-') { # exponent < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 0 0 0
elsif (not $LIB->_is_zero($x->{'_e'})) { } 0 0 0
4075 0 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10);
4076 } elsif (! $LIB->_is_zero($x->{_e})) { # exponent > 0
4077 0 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10);
4078 }
4079  
4080 0 $x->{_m} = $LIB -> _clog2($x->{_m});
4081 0 $x->{_e} = $LIB -> _zero();
4082 0 $x -> bnorm() -> round(@r);
4083 0 $x -> _dng();
4084 0 return $x;
4085 }
4086  
4087 0 ▶sub bclog10 {
Subroutine bclog10: not called CC=10 SCAR=47.0
Pod documented
4088 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4089  
4090 # Don't modify constant (read-only) objects.
4091  
4092 0 ▶ return $x if $x -> modify('bclog10');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bclog10") 0 0 0
4093  
4094 0 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4095 0 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
4096 0 ▶ return $x -> binf("-", @r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
4097  
4098 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
4099 0 ▶ return $x -> _upg() -> bclog10(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4100 0 return $x -> bnan(@r);
4101 }
4102  
4103 0 ▶ if ($x->{_es} eq '-') { # exponent < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 0 0 0
elsif (not $LIB->_is_zero($x->{'_e'})) { } 0 0 0
4104 0 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10);
4105 } elsif (! $LIB->_is_zero($x->{_e})) { # exponent > 0
4106 0 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10);
4107 }
4108  
4109 0 $x->{_m} = $LIB -> _clog10($x->{_m});
4110 0 $x->{_e} = $LIB -> _zero();
4111 0 $x -> bnorm() -> round(@r);
4112 0 $x -> _dng();
4113 0 return $x;
4114 }
4115  
4116 60 ▶sub bnok {
Subroutine bnok: called CC=11 SCAR=26.4
Pod documented
4117 # Calculate n over k (binomial coefficient or "choose" function) as
4118 # integer. set up parameters
4119 60 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 60 0 60
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0060
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
4120 ? (ref($_[0]), @_)
4121 : objectify(2, @_);
4122  
4123 60 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 60 60
4124  
4125 # Don't modify constant (read-only) objects.
4126  
4127 60 ▶ return $x if $x -> modify('bnok');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bnok") 0 60 60
4128  
4129 60 ▶ return $x -> bnan() if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 12 48 60
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
8448
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
4130 48 ▶ 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 48 48
Condition44%4/9
$x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int
l!l&&r!l&&!r
0048
$x->is_finite and not $x->is_int
!ll&&!rl&&r
4440
$y->is_finite and not $y->is_int
!ll&&!rl&&r
0480
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
4131 ($y -> is_finite() && !$y -> is_int()));
4132  
4133 # This should be implemented without converting to Math::BigInt. XXX
4134  
4135 48 my $xint = $x -> as_int(); # to Math::BigInt
4136 48 my $yint = $y -> as_int(); # to Math::BigInt
4137  
4138 48 $xint -> bnok($yint);
4139 48 $xint -> round(@r);
4140  
4141 48 my $xflt = $xint -> as_float();
4142 48 $x -> {sign} = $xflt -> {sign};
4143 48 $x -> {_m} = $xflt -> {_m};
4144 48 $x -> {_es} = $xflt -> {_es};
4145 48 $x -> {_e} = $xflt -> {_e};
4146  
4147 48 return $x -> _dng();
4148 0 return $x;
4149 }
4150  
4151 0 ▶sub bperm {
Subroutine bperm: not called CC=11 SCAR=48.8
Pod documented
4152 # Calculate n over k (binomial coefficient or "choose" function) as
4153 # integer. set up parameters
4154 0 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 0 0 0
Condition0%0/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table0%0/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
4155 ? (ref($_[0]), @_)
4156 : objectify(2, @_);
4157  
4158 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4159  
4160 # Don't modify constant (read-only) objects.
4161  
4162 0 ▶ return $x if $x -> modify('bperm');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bperm") 0 0 0
4163  
4164 0 ▶ return $x -> bnan() if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 0 0 0
Condition0%0/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table0%0/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
4165 0 ▶ return $x -> bnan() if (($x -> is_finite() && !$x -> is_int()) ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int 0 0 0
Condition0%0/9
$x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int
l!l&&r!l&&!r
000
$x->is_finite and not $x->is_int
!ll&&!rl&&r
000
$y->is_finite and not $y->is_int
!ll&&!rl&&r
000
MC/DC0%0/4
$x->is_finite and not $x->is_int or $y->is_finite and not $y->is_int
$x->is_finite not $x->is_int $y->is_finite not $y->is_int
Truth table0%0/7
A and not(B) or C and not(D)
A= $x->is_finiteB= not $x->is_intC= $y->is_finiteD= not $y->is_int
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
4166 ($y -> is_finite() && !$y -> is_int()));
4167  
4168 # This should be implemented without converting to Math::BigInt. XXX
4169  
4170 0 my $xint = $x -> as_int(); # to Math::BigInt
4171 0 my $yint = $y -> as_int(); # to Math::BigInt
4172  
4173 0 $xint -> bperm($yint);
4174 0 $xint -> round(@r);
4175  
4176 0 my $xflt = $xint -> as_float();
4177 0 $x -> {sign} = $xflt -> {sign};
4178 0 $x -> {_m} = $xflt -> {_m};
4179 0 $x -> {_es} = $xflt -> {_es};
4180 0 $x -> {_e} = $xflt -> {_e};
4181  
4182 0 return $x -> _dng();
4183 0 return $x;
4184 }
4185  
4186 40 ▶sub bsin {
Subroutine bsin: called CC=21 SCAR=31.5
Pod documented
4187 # Calculate a sinus of x.
4188 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
4189  
4190 # First we apply range reduction to x. This is because if x is large, the
4191 # Taylor series converges slowly and requires higher accuracy in the
4192 # intermediate computation. The Taylor series is:
4193 #
4194 # x^3 x^5 x^7 x^9
4195 # sin(x) = x - --- + --- - --- + --- ...
4196 # 3! 5! 7! 9!
4197  
4198 # Don't modify constant (read-only) objects.
4199  
4200 40 ▶ return $x if $x -> modify('bsin');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsin") 0 40 40
4201  
4202 40 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 32 40
4203 32 ▶ return $x -> bnan(@r) if $x -> is_nan() || $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf 12 20 32
Condition100%3/3
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
4820
MC/DC100%2/2
$x->is_nan or $x->is_inf
$x->is_nan $x->is_inf
Truth table100%3/3
A or B
A= $x->is_nanB= $x->is_inf
ABresult
1X1
011
000
4204  
4205 # Get the rounding parameters, if any.
4206  
4207 20 my $fallback = 0;
4208 20 my ($scale, @params);
4209 20 ($x, @params) = $x -> _find_round_parameters(@r);
4210  
4211 # Error in _find_round_parameters?
4212  
4213 20 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 20 20
4214  
4215 # If no rounding parameters are given, use fallback.
4216  
4217 20 ▶ if (!@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @params) { } 0 20 20
4218 0 $params[0] = $class -> div_scale(); # fallback accuracy
4219 0 $params[1] = undef; # no precision
4220 0 $params[2] = $r[2]; # rounding mode
4221 0 $scale = $params[0];
4222 0 $fallback = 1; # to clear a/p afterwards
4223 } else {
4224 20 ▶ if (defined($params[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 20 0 20
4225 20 $scale = $params[0];
4226 } else {
4227 # We perform the computations below using accuracy only, not
4228 # precision, so when precision is given, we need to "convert" this
4229 # to accuracy.
4230 0 $scale = 1 - $params[1];
4231 }
4232 }
4233  
4234 # Add more digits to the scale if the magnitude of $x is large.
4235  
4236 20 my ($m, $e) = $x -> nparts();
4237 20 ▶ $scale += $e if $x >= 10;
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 10 0 20 20
4238 20 ▶ $scale = 4 if $scale < 4;
Branch: 1 branch
BranchTrueFalseTotal
if $scale < 4 0 20 20
4239  
4240 # When user set globals, they would interfere with our calculation, so
4241 # disable them and later re-enable them
4242  
4243 20 my $ab = $class -> accuracy();
4244 20 my $pb = $class -> precision();
4245 20 $class -> accuracy(undef);
4246 20 $class -> precision(undef);
4247  
4248 # Disabling upgrading and downgrading is no longer necessary to avoid an
4249 # infinite recursion, but it avoids unnecessary upgrading and downgrading
4250 # in the intermediate computations.
4251  
4252 20 my $upg = $class -> upgrade();
4253 20 my $dng = $class -> downgrade();
4254 20 $class -> upgrade(undef);
4255 20 $class -> downgrade(undef);
4256  
4257 # We also need to disable any set A or P on $x (_find_round_parameters took
4258 # them already into account), since these would interfere, too.
4259  
4260 20 $x->{accuracy} = undef;
4261 20 $x->{precision} = undef;
4262  
4263 20 my $sin_prev; # the previous approximation of sin(x)
4264 my $sin; # the current approximation of sin(x)
4265  
4266 20 while (1) {
4267  
4268 # Compute constants to the current scale.
4269  
4270 52 my $pi = $class -> bpi($scale); # 𝜋
4271 52 my $twopi = $pi -> copy() -> bmul("2"); # 2𝜋
4272 52 my $halfpi = $pi -> copy() -> bmul("0.5"); # 𝜋/2
4273  
4274 # Use the fact that sin(-x) = -sin(x) to reduce the range to the
4275 # interval to [0,∞).
4276  
4277 52 ▶ my $xsgn = $x < 0 ? -1 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$x < 0 ? : 0 52 52
4278 52 my $x = $x -> copy() -> babs();
4279  
4280 # Use the fact that sin(2𝜋x) = sin(x) to reduce the range to the
4281 # interval to [0, 2𝜋).
4282  
4283 52 $x -> bmod($twopi, $scale);
4284  
4285 # Use the fact that sin(x+𝜋) = -sin(x) to reduce the range to the
4286 # interval to [0,𝜋).
4287  
4288 52 ▶ if ($x -> bcmp($pi) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bcmp($pi) > 0) 12 40 52
4289 12 $xsgn = -$xsgn;
4290 12 $x -> bsub($pi);
4291 }
4292  
4293 # Use the fact that sin(𝜋-x) = sin(x) to reduce the range to the
4294 # interval [0,𝜋/2).
4295  
4296 52 ▶ if ($x -> bcmp($halfpi) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bcmp($halfpi) > 0) 12 40 52
4297 12 $x -> bsub($pi) -> bneg(); # 𝜋 - x
4298 }
4299  
4300 52 my $tol = $class -> new("1E-". ($scale-1));
4301  
4302 52 my $xsq = $x -> copy() -> bmul($x, $scale) -> bneg();
4303 52 my $term = $x -> copy();
4304 52 my $fac = $class -> bone();
4305 52 my $n = $class -> bone();
4306  
4307 52 $sin = $x -> copy(); # initialize sin(x) to the first term
4308  
4309 52 while (1) {
4310 504 $n -> binc();
4311 504 $fac = $n -> copy();
4312 504 $n -> binc();
4313 504 $fac -> bmul($n);
4314  
4315 504 $term -> bmul($xsq, $scale) -> bdiv($fac, $scale);
4316  
4317 504 $sin -> badd($term, $scale);
4318 504 ▶ last if $term -> copy() -> babs() -> bcmp($tol) < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $term->copy->babs->bcmp($tol) < 0 52 452 504
4319 }
4320  
4321 52 ▶ $sin -> bneg() if $xsgn < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $xsgn < 0 12 40 52
4322  
4323 # Rounding parameters given as arguments currently don't override
4324 # instance variables, so accuracy (which is set in the computations
4325 # above) must be undefined before rounding. Fixme.
4326  
4327 52 $sin->{accuracy} = undef;
4328 52 $sin -> round(@params);
4329  
4330 # Compare the current approximation of sin(x) with the previous one,
4331 # and if they are identical, we're done.
4332  
4333 52 ▶ if (defined $sin_prev) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sin_prev) 32 20 52
4334 32 ▶ last if $sin -> bcmp($sin_prev) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $sin->bcmp($sin_prev) == 0 20 12 32
4335 }
4336  
4337 # If the current approximation of sin(x) is different from the previous
4338 # approximation, double the scale (accuracy) and retry.
4339  
4340 32 $sin_prev = $sin;
4341 32 $scale *= 2;
4342 }
4343  
4344 # Assign the result to the invocand.
4345  
4346 20 %$x = %$sin;
4347  
4348 20 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 0 20 20
4349 # clear a/p after round, since user did not request it
4350 0 $x->{accuracy} = undef;
4351 0 $x->{precision} = undef;
4352 }
4353  
4354 # Restore globals. We need to do it like this, because setting one
4355 # undefines the other.
4356  
4357 20 ▶ if (defined $ab) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ab) { } 0 20 20
4358 0 $class -> accuracy($ab);
4359 } else {
4360 20 $class -> precision($pb);
4361 }
4362  
4363 20 $class -> upgrade($upg);
4364 20 $class -> downgrade($dng);
4365  
4366 # rounding has already been done
4367 20 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 20 20
4368 20 $x;
4369 }
4370  
4371 36 ▶sub bcos {
Subroutine bcos: called CC=14 SCAR=27.6
Pod documented
4372 # Calculate a cosinus of x.
4373 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
4374  
4375 # Taylor: x^2 x^4 x^6 x^8
4376 # cos = 1 - --- + --- - --- + --- ...
4377 # 2! 4! 6! 8!
4378  
4379 # Don't modify constant (read-only) objects.
4380  
4381 36 ▶ return $x if $x -> modify('bcos');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bcos") 0 36 36
4382  
4383 # we need to limit the accuracy to protect against overflow
4384 36 my $fallback = 0;
4385 36 my ($scale, @params);
4386 36 ($x, @params) = $x->_find_round_parameters(@r);
4387  
4388 # error in _find_round_parameters?
4389 36 ▶ return $x if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 32 36
4390 32 ▶ return $x -> bnan() if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 8 24 32
4391 24 ▶ return $x -> bone(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 16 24
4392  
4393 # no rounding at all, so must use fallback
4394 16 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 0 16 16
4395 # simulate old behaviour
4396 0 $params[0] = $class -> div_scale(); # and round to it as accuracy
4397 0 $params[1] = undef; # disable P
4398 0 $scale = $params[0] + 4; # at least four more for proper round
4399 0 $params[2] = $r[2]; # round mode by caller or undef
4400 0 $fallback = 1; # to clear a/p afterwards
4401 } else {
4402 # the 4 below is empirical, and there might be cases where it is not
4403 # enough...
4404 16 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition33%1/3
$params[0] || $params[1]
l!l&&r!l&&!r
1600
MC/DC0%0/2
$params[0] || $params[1]
$params[0] $params[1]
Truth table33%1/3
A || B
A= $params[0]B= $params[1]
ABresult
1X1
011
000
4405 }
4406  
4407 # When user set globals, they would interfere with our calculation, so
4408 # disable them and later re-enable them.
4409  
4410 16 my $ab = $class -> accuracy();
4411 16 my $pb = $class -> precision();
4412 16 $class -> accuracy(undef);
4413 16 $class -> precision(undef);
4414  
4415 # Disabling upgrading and downgrading is no longer necessary to avoid an
4416 # infinite recursion, but it avoids unnecessary upgrading and downgrading
4417 # in the intermediate computations.
4418  
4419 16 my $upg = $class -> upgrade();
4420 16 my $dng = $class -> downgrade();
4421 16 $class -> upgrade(undef);
4422 16 $class -> downgrade(undef);
4423  
4424 # We also need to disable any set A or P on $x (_find_round_parameters took
4425 # them already into account), since these would interfere, too.
4426  
4427 16 $x->{accuracy} = undef;
4428 16 $x->{precision} = undef;
4429  
4430 16 my $over = $x * $x; # X ^ 2
4431 16 my $x2 = $over -> copy(); # X ^ 2; difference between terms
4432 16 my $sign = 1; # start with -=
4433 16 my $below = $class -> new(2);
4434 16 my $factorial = $class -> new(3);
4435 16 $x -> bone();
4436 16 $x->{accuracy} = undef;
4437 16 $x->{precision} = undef;
4438  
4439 16 my $limit = $class -> new("1E-". ($scale-1));
4440 #my $steps = 0;
4441 16 while (3 < 5) {
4442 # we calculate the next term, and add it to the last
4443 # when the next term is below our limit, it won't affect the outcome
4444 # anymore, so we stop:
4445 156 my $next = $over -> copy() -> bdiv($below, $scale);
4446 156 ▶ last if $next -> bacmp($limit) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next->bacmp($limit) <= 0 16 140 156
4447  
4448 140 ▶ if ($sign == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign == 0) { } 68 72 140
4449 68 $x -> badd($next);
4450 } else {
4451 72 $x -> bsub($next);
4452 }
4453 140 $sign = 1-$sign; # alternate
4454 # calculate things for the next term
4455 140 $over -> bmul($x2); # $x*$x
4456 140 $below -> bmul($factorial); # n*(n+1)
4457 140 $factorial -> binc();
4458 140 $below -> bmul($factorial); # n*(n+1)
4459 140 $factorial -> binc();
4460 }
4461  
4462 # shortcut to not run through _find_round_parameters again
4463 16 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 16 0 16
4464 16 $x -> bround($params[0], $params[2]); # then round accordingly
4465 } else {
4466 0 $x -> bfround($params[1], $params[2]); # then round accordingly
4467 }
4468 16 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 0 16 16
4469 # clear a/p after round, since user did not request it
4470 0 $x->{accuracy} = undef;
4471 0 $x->{precision} = undef;
4472 }
4473  
4474 # Restore globals. We need to do it like this, because setting one
4475 # undefines the other.
4476  
4477 16 ▶ if (defined $ab) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ab) { } 0 16 16
4478 0 $class -> accuracy($ab);
4479 } else {
4480 16 $class -> precision($pb);
4481 }
4482  
4483 16 $class -> upgrade($upg);
4484 16 $class -> downgrade($dng);
4485  
4486 16 $x -> round(@r);
4487 16 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 0 16 16
4488 16 $x;
4489 }
4490  
4491 175 ▶sub batan {
Subroutine batan: called CC=21 SCAR=31.3
Pod documented
4492 # Calculate a arcus tangens of x.
4493 175 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 175 0 175
4494  
4495 # taylor: x^3 x^5 x^7 x^9
4496 # atan = x - --- + --- - --- + --- ...
4497 # 3 5 7 9
4498  
4499 # Don't modify constant (read-only) objects.
4500  
4501 175 ▶ return $x if $x -> modify('batan');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("batan") 0 175 175
4502  
4503 175 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 171 175
4504  
4505 # We need to limit the accuracy to protect against overflow.
4506  
4507 171 my $fallback = 0;
4508 171 my ($scale, @params);
4509 171 ($x, @params) = $x->_find_round_parameters(@r);
4510  
4511 # Error in _find_round_parameters?
4512  
4513 171 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 171 171
4514  
4515 171 ▶ if ($x->{sign} =~ /^[+-]inf\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]inf\z/) 16 155 171
4516 # +inf result is PI/2
4517 # -inf result is -PI/2
4518 # calculate PI/2
4519 16 my $pi = $class -> bpi(@r);
4520 # modify $x in place
4521 16 $x->{_m} = $pi->{_m};
4522 16 $x->{_e} = $pi->{_e};
4523 16 $x->{_es} = $pi->{_es};
4524 # -y => -PI/2, +y => PI/2
4525 16 $x->{sign} = substr($x->{sign}, 0, 1); # "+inf" => "+"
4526 16 $x -> {_m} = $LIB->_div($x->{_m}, $LIB->_new(2));
4527 16 return $x;
4528 }
4529  
4530 155 ▶ return $x -> bzero(@r) if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 26 129 155
4531  
4532 # no rounding at all, so must use fallback
4533 129 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 0 129 129
4534 # simulate old behaviour
4535 0 $params[0] = $class -> div_scale(); # and round to it as accuracy
4536 0 $params[1] = undef; # disable P
4537 0 $scale = $params[0]+4; # at least four more for proper round
4538 0 $params[2] = $r[2]; # round mode by caller or undef
4539 0 $fallback = 1; # to clear a/p afterwards
4540 } else {
4541 # the 4 below is empirical, and there might be cases where it is not
4542 # enough...
4543 129 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition33%1/3
$params[0] || $params[1]
l!l&&r!l&&!r
12900
MC/DC0%0/2
$params[0] || $params[1]
$params[0] $params[1]
Truth table33%1/3
A || B
A= $params[0]B= $params[1]
ABresult
1X1
011
000
4544 }
4545  
4546 # 1 or -1 => PI/4
4547 # inlined is_one() && is_one('-')
4548 129 ▶ if ($LIB->_is_one($x->{_m}) && $LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_one($x->{"_m"}) and $LIB->_is_zero($x->{"_e"})) 27 102 129
Condition100%3/3
$LIB->_is_one($x->{"_m"}) and $LIB->_is_zero($x->{"_e"})
!ll&&!rl&&r
98427
MC/DC100%2/2
$LIB->_is_one($x->{"_m"}) and $LIB->_is_zero($x->{"_e"})
$LIB->_is_one($x->{"_m"}) $LIB->_is_zero($x->{"_e"})
Truth table100%3/3
A and B
A= $LIB->_is_one($x->{"_m"})B= $LIB->_is_zero($x->{"_e"})
ABresult
0X0
100
111
4549 27 my $pi = $class -> bpi($scale - 3);
4550 # modify $x in place
4551 27 $x->{_m} = $pi->{_m};
4552 27 $x->{_e} = $pi->{_e};
4553 27 $x->{_es} = $pi->{_es};
4554 # leave the sign of $x alone (+1 => +PI/4, -1 => -PI/4)
4555 27 $x->{_m} = $LIB->_div($x->{_m}, $LIB->_new(4));
4556 27 return $x;
4557 }
4558  
4559 # When user set globals, they would interfere with our calculation, so
4560 # disable them and later re-enable them.
4561  
4562 102 my $ab = $class -> accuracy();
4563 102 my $pb = $class -> precision();
4564 102 $class -> accuracy(undef);
4565 102 $class -> precision(undef);
4566  
4567 # Disable upgrading and downgrading.
4568  
4569 102 my $upg = $class -> upgrade();
4570 102 my $dng = $class -> downgrade();
4571 102 $class -> upgrade(undef);
4572 102 $class -> downgrade(undef);
4573  
4574 # We also need to disable any set A or P on $x (_find_round_parameters took
4575 # them already into account), since these would interfere, too.
4576  
4577 102 $x->{accuracy} = undef;
4578 102 $x->{precision} = undef;
4579  
4580 # This series is only valid if -1 < x < 1, so for other x we need to
4581 # calculate PI/2 - atan(1/x):
4582 102 my $pi = undef;
4583 102 ▶ if ($x -> bacmp($x -> copy() -> bone) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bacmp($x->copy->bone) >= 0) 40 62 102
4584 # calculate PI/2
4585 40 $pi = $class -> bpi($scale - 3);
4586 40 $pi->{_m} = $LIB->_div($pi->{_m}, $LIB->_new(2));
4587 # calculate 1/$x:
4588 40 my $x_copy = $x -> copy();
4589 # modify $x in place
4590 40 $x -> bone();
4591 40 $x -> bdiv($x_copy, $scale);
4592 }
4593  
4594 102 my $fmul = 1;
4595 102 foreach (0 .. int($scale / 20)) {
4596 174 $fmul *= 2;
4597 174 $x -> bdiv($x -> copy() -> bmul($x) -> binc() -> bsqrt($scale + 4) -> binc(),
4598 $scale + 4);
4599 }
4600  
4601 102 my $over = $x * $x; # X ^ 2
4602 102 my $x2 = $over -> copy(); # X ^ 2; difference between terms
4603 102 $over -> bmul($x); # X ^ 3 as starting value
4604 102 my $sign = 1; # start with -=
4605 102 my $below = $class -> new(3);
4606 102 my $two = $class -> new(2);
4607 102 $x->{accuracy} = undef;
4608 102 $x->{precision} = undef;
4609  
4610 102 my $limit = $class -> new("1E-". ($scale-1));
4611 #my $steps = 0;
4612 102 while (1) {
4613 # We calculate the next term, and add it to the last. When the next
4614 # term is below our limit, it won't affect the outcome anymore, so we
4615 # stop:
4616 990 my $next = $over -> copy() -> bdiv($below, $scale);
4617 990 ▶ last if $next -> bacmp($limit) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next->bacmp($limit) <= 0 102 888 990
4618  
4619 888 ▶ if ($sign == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign == 0) { } 416 472 888
4620 416 $x -> badd($next);
4621 } else {
4622 472 $x -> bsub($next);
4623 }
4624 888 $sign = 1 - $sign; # alternatex
4625 # calculate things for the next term
4626 888 $over -> bmul($x2); # $x*$x
4627 888 $below -> badd($two); # n += 2
4628 }
4629 102 $x -> bmul($fmul);
4630  
4631 102 ▶ if (defined $pi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pi) 40 62 102
4632 40 my $x_copy = $x -> copy();
4633 # modify $x in place
4634 40 $x->{_m} = $pi->{_m};
4635 40 $x->{_e} = $pi->{_e};
4636 40 $x->{_es} = $pi->{_es};
4637 # PI/2 - $x
4638 40 $x -> bsub($x_copy);
4639 }
4640  
4641 # Shortcut to not run through _find_round_parameters again.
4642 102 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 102 0 102
4643 102 $x -> bround($params[0], $params[2]); # then round accordingly
4644 } else {
4645 0 $x -> bfround($params[1], $params[2]); # then round accordingly
4646 }
4647 102 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 0 102 102
4648 # Clear a/p after round, since user did not request it.
4649 0 $x->{accuracy} = undef;
4650 0 $x->{precision} = undef;
4651 }
4652  
4653 # Restore globals. We need to do it like this, because setting one
4654 # undefines the other.
4655  
4656 102 ▶ if (defined $ab) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ab) { } 0 102 102
4657 0 $class -> accuracy($ab);
4658 } else {
4659 102 $class -> precision($pb);
4660 }
4661  
4662 102 $class -> upgrade($upg);
4663 102 $class -> downgrade($dng);
4664  
4665 102 ▶ return $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf 0 102 102
Condition33%1/3
$x->is_int or $x->is_inf
l!l&&r!l&&!r
00102
MC/DC0%0/2
$x->is_int or $x->is_inf
$x->is_int $x->is_inf
Truth table33%1/3
A or B
A= $x->is_intB= $x->is_inf
ABresult
1X1
011
000
4666 $x -> is_inf());
4667 102 $x;
4668 }
4669  
4670 213 ▶sub batan2 {
Subroutine batan2: called CC=24 SCAR=31.9
Pod documented
4671 # $y -> batan2($x) returns the arcus tangens of $y / $x.
4672  
4673 # Set up parameters.
4674 213 ▶ my ($class, $y, $x, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 213 0 213
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00213
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
4675 ? (ref($_[0]), @_)
4676 : objectify(2, @_);
4677  
4678 # Don't modify constant (read-only) objects.
4679  
4680 213 ▶ return $y if $y -> modify('batan2');
Branch: 1 branch
BranchTrueFalseTotal
if $y->modify("batan2") 0 213 213
4681  
4682 # Handle all NaN cases.
4683 213 ▶ return $y -> bnan() if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 12 201 213
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
84201
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
4684  
4685 # We need to limit the accuracy to protect against overflow.
4686 201 my $fallback = 0;
4687 201 my ($scale, @params);
4688 201 ($y, @params) = $y -> _find_round_parameters(@r);
4689  
4690 # Error in _find_round_parameters?
4691 201 ▶ return $y if $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_nan 0 201 201
4692  
4693 # No rounding at all, so must use fallback.
4694 201 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 45 156 201
4695 # Simulate old behaviour
4696 45 $params[0] = $class -> div_scale(); # and round to it as accuracy
4697 45 $params[1] = undef; # disable P
4698 45 $scale = $params[0] + 4; # at least four more for proper round
4699 45 $params[2] = $r[2]; # round mode by caller or undef
4700 45 $fallback = 1; # to clear a/p afterwards
4701 } else {
4702 # The 4 below is empirical, and there might be cases where it is not
4703 # enough ...
4704 156 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition33%1/3
$params[0] || $params[1]
l!l&&r!l&&!r
15600
MC/DC0%0/2
$params[0] || $params[1]
$params[0] $params[1]
Truth table33%1/3
A || B
A= $params[0]B= $params[1]
ABresult
1X1
011
000
4705 }
4706  
4707 201 ▶ if ($x -> is_inf("+")) { # x = inf
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf('+')) { } 20 181 201
elsif ($x->is_inf('-')) { } 20 161 181
elsif ($x > 0) { } 87 74 161
elsif ($x < 0) { } 20 54 74
4708 20 ▶ if ($y -> is_inf("+")) { # y = inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('+')) { } 4 16 20
elsif ($y->is_inf('-')) { } 4 12 16
4709 4 $y -> bpi($scale) -> bmul("0.25"); # pi/4
4710 } elsif ($y -> is_inf("-")) { # y = -inf
4711 4 $y -> bpi($scale) -> bmul("-0.25"); # -pi/4
4712 } else { # -inf < y < inf
4713 12 return $y -> bzero(@r); # 0
4714 }
4715 } elsif ($x -> is_inf("-")) { # x = -inf
4716 20 ▶ if ($y -> is_inf("+")) { # y = inf
Branch: 3 branches
BranchTrueFalseTotal
if ($y->is_inf('+')) { } 4 16 20
elsif ($y->is_inf('-')) { } 4 12 16
elsif ($y >= 0) { } 8 4 12
4717 4 $y -> bpi($scale) -> bmul("0.75"); # 3/4 pi
4718 } elsif ($y -> is_inf("-")) { # y = -inf
4719 4 $y -> bpi($scale) -> bmul("-0.75"); # -3/4 pi
4720 } elsif ($y >= 0) { # y >= 0
4721 8 $y -> bpi($scale); # pi
4722 } else { # y < 0
4723 4 $y -> bpi($scale) -> bneg(); # -pi
4724 }
4725 } elsif ($x > 0) { # 0 < x < inf
4726 87 ▶ if ($y -> is_inf("+")) { # y = inf
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_inf('+')) { } 4 83 87
elsif ($y->is_inf('-')) { } 4 79 83
4727 4 $y -> bpi($scale) -> bmul("0.5"); # pi/2
4728 } elsif ($y -> is_inf("-")) { # y = -inf
4729 4 $y -> bpi($scale) -> bmul("-0.5"); # -pi/2
4730 } else { # -inf < y < inf
4731 79 $y -> bdiv($x, $scale) -> batan($scale); # atan(y/x)
4732 }
4733 } elsif ($x < 0) { # -inf < x < 0
4734 20 my $pi = $class -> bpi($scale);
4735 20 ▶ if ($y >= 0) { # y >= 0
Branch: 1 branch
BranchTrueFalseTotal
if ($y >= 0) { } 12 8 20
4736 12 $y -> bdiv($x, $scale) -> batan() # atan(y/x) + pi
4737 -> badd($pi);
4738 } else { # y < 0
4739 8 $y -> bdiv($x, $scale) -> batan() # atan(y/x) - pi
4740 -> bsub($pi);
4741 }
4742 } else { # x = 0
4743 54 ▶ if ($y > 0) { # y > 0
Branch: 2 branches
BranchTrueFalseTotal
if ($y > 0) { } 25 29 54
elsif ($y < 0) { } 22 7 29
4744 25 $y -> bpi($scale) -> bmul("0.5"); # pi/2
4745 } elsif ($y < 0) { # y < 0
4746 22 $y -> bpi($scale) -> bmul("-0.5"); # -pi/2
4747 } else { # y = 0
4748 7 return $y -> bzero(@r); # 0
4749 }
4750 }
4751  
4752 182 $y -> round(@r);
4753  
4754 182 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 42 140 182
4755 42 $y->{accuracy} = undef;
4756 42 $y->{precision} = undef;
4757 }
4758  
4759 182 return $y;
4760 }
4761  
4762 80 ▶sub bfac {
Subroutine bfac: called CC=14 SCAR=27.6
Pod documented
4763 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
4764 # compute factorial number, modifies first argument
4765  
4766 # set up parameters
4767 80 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 80 0 80
4768  
4769 # Don't modify constant (read-only) objects.
4770  
4771 80 ▶ return $x if $x -> modify('bfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfac") 0 80 80
4772  
4773 80 ▶ return $x -> bnan(@r) if $x -> is_nan() || $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 8 72 80
Condition100%3/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
4472
MC/DC100%2/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table100%3/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
4774 72 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 4 68 72
4775 68 ▶ return $x -> bnan(@r) if $x -> is_neg() || !$x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg or not $x->is_int 4 64 68
Condition66%2/3
$x->is_neg or not $x->is_int
l!l&&r!l&&!r
4064
MC/DC50%1/2
$x->is_neg or not $x->is_int
$x->is_neg not $x->is_int
Truth table66%2/3
A or not(B)
A= $x->is_negB= not $x->is_int
ABresult
1X1
011
000
4776 64 ▶ return $x -> bone(@r) if $x -> is_zero() || $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one 8 56 64
Condition100%3/3
$x->is_zero or $x->is_one
l!l&&r!l&&!r
4456
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
4777  
4778 56 ▶ if ($x -> is_neg() || !$x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg or not $x->is_int) 0 56 56
Condition33%1/3
$x->is_neg or not $x->is_int
l!l&&r!l&&!r
0056
MC/DC0%0/2
$x->is_neg or not $x->is_int
$x->is_neg not $x->is_int
Truth table33%1/3
A or not(B)
A= $x->is_negB= not $x->is_int
ABresult
1X1
011
000
4779 0 ▶ return $x -> _upg() -> bfac(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 0 0
4780 0 return $x -> bnan(@r);
4781 }
4782  
4783 56 ▶ if (! $LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 8 48 56
4784 8 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10); # change 12e1 to 120e0
4785 8 $x->{_e} = $LIB->_zero(); # normalize
4786 8 $x->{_es} = '+';
4787 }
4788 56 $x->{_m} = $LIB->_fac($x->{_m}); # calculate factorial
4789  
4790 56 $x -> bnorm(); # norm again
4791 56 $x -> round(@r);
4792 56 $x -> _dng();
4793 56 return $x;
4794 }
4795  
4796 72 ▶sub bdfac {
Subroutine bdfac: called CC=11 SCAR=24.1
Pod documented
4797 # compute double factorial, modify $x in place
4798 72 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 72 0 72
4799  
4800 # Don't modify constant (read-only) objects.
4801  
4802 72 ▶ return $x if $x -> modify('bdfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdfac") 0 72 72
4803  
4804 72 ▶ return $x -> bnan(@r) if $x -> is_nan() || $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 8 64 72
Condition100%3/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
4464
MC/DC100%2/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table100%3/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
4805 64 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 4 60 64
4806 60 ▶ return $x -> bnan(@r) if $x <= -2 || !$x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x <= -2 or not $x->is_int 4 56 60
Condition66%2/3
$x <= -2 or not $x->is_int
l!l&&r!l&&!r
4056
MC/DC50%1/2
$x <= -2 or not $x->is_int
$x <= -2 not $x->is_int
Truth table66%2/3
A or not(B)
A= $x <= -2B= not $x->is_int
ABresult
1X1
011
000
4807 56 ▶ return $x -> bone(@r) if $x <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= 1 12 44 56
4808  
4809 44 ▶ croak("bdfac() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_dfac") 0 44 44
4810 unless $LIB -> can('_dfac');
4811  
4812 44 ▶ if (! $LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 4 40 44
4813 4 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10); # change 12e1 to 120e0
4814 4 $x->{_e} = $LIB->_zero(); # normalize
4815 4 $x->{_es} = '+';
4816 }
4817 44 $x->{_m} = $LIB->_dfac($x->{_m}); # calculate factorial
4818  
4819 44 $x -> bnorm(); # norm again
4820 44 $x -> round(@r);
4821 44 $x -> _dng();
4822 44 return $x;
4823 }
4824  
4825 76 ▶sub btfac {
Subroutine btfac: called CC=12 SCAR=25.1
Pod documented
4826 # compute triple factorial
4827  
4828 # set up parameters
4829 76 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 76 0 76
4830  
4831 # Don't modify constant (read-only) objects.
4832  
4833 76 ▶ return $x if $x -> modify('btfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btfac") 0 76 76
4834  
4835 76 ▶ return $x -> bnan(@r) if $x -> is_nan() || $x -> is_inf("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 8 68 76
Condition100%3/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
4468
MC/DC100%2/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table100%3/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
4836 68 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 4 64 68
4837  
4838 64 ▶ if ($x <= -3 || !$x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= -3 or not $x->is_int) 4 60 64
Condition66%2/3
$x <= -3 or not $x->is_int
l!l&&r!l&&!r
4060
MC/DC50%1/2
$x <= -3 or not $x->is_int
$x <= -3 not $x->is_int
Truth table66%2/3
A or not(B)
A= $x <= -3B= not $x->is_int
ABresult
1X1
011
000
4839 4 ▶ return $x -> _upg() -> btfac(@r) if $class -> upgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade 0 4 4
4840 4 return $x -> bnan(@r);
4841 }
4842  
4843 60 my $k = $class -> new("3");
4844 60 ▶ return $x -> bnan(@r) if $x <= -$k;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= -$k 0 60 60
4845  
4846 60 my $one = $class -> bone();
4847 60 ▶ return $x -> bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 16 44 60
4848  
4849 44 my $f = $x -> copy();
4850 44 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 60 44 104
4851 60 $x = $x -> bmul($f);
4852 }
4853  
4854 44 $x -> round(@r);
4855 44 $x -> _dng();
4856 44 return $x;
4857 }
4858  
4859 364 ▶sub bmfac {
Subroutine bmfac: called CC=15 SCAR=27.4
Pod documented
4860 364 ▶ my ($class, $x, $k, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 364 0 364
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
00364
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
4861 ? (ref($_[0]), @_)
4862 : objectify(2, @_);
4863  
4864 # Don't modify constant (read-only) objects.
4865  
4866 364 ▶ return $x if $x -> modify('bmfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmfac") 0 364 364
4867  
4868 364 ▶ return $x -> bnan(@r) if $x -> is_nan() || $x -> is_inf("-") ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") or not $k->is_pos 56 308 364
Condition100%6/6
$x->is_nan or $x->is_inf("-") or not $k->is_pos
l!l&&r!l&&!r
4016308
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
2020324
MC/DC100%3/3
$x->is_nan or $x->is_inf("-") or not $k->is_pos
$x->is_nan $x->is_inf("-") not $k->is_pos
Truth table100%4/4
A or B or not(C)
A= $x->is_nanB= $x->is_inf("-")C= not $k->is_pos
ABCresult
1XX1
01X1
0011
0000
4869 !$k -> is_pos();
4870 308 ▶ return $x -> binf("+", @r) if $x -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 20 288 308
4871 288 ▶ return $x -> bround(@r) if $k -> is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $k->is_inf("+") 4 284 288
4872 284 ▶ return $x -> bnan(@r) if !$x -> is_int() || !$k -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int and $k->is_int 4 280 284
Condition66%2/3
$x->is_int and $k->is_int
!ll&&!rl&&r
04280
MC/DC50%1/2
$x->is_int and $k->is_int
$x->is_int $k->is_int
Truth table66%2/3
A and B
A= $x->is_intB= $k->is_int
ABresult
0X0
100
111
4873 280 ▶ return $x -> bnan(@r) if $k < 1 || $x <= -$k;
Branch: 1 branch
BranchTrueFalseTotal
if $k < 1 or $x <= -$k 20 260 280
Condition66%2/3
$k < 1 or $x <= -$k
l!l&&r!l&&!r
020260
MC/DC50%1/2
$k < 1 or $x <= -$k
$k < 1 $x <= -$k
Truth table66%2/3
A or B
A= $k < 1B= $x <= -$k
ABresult
1X1
011
000
4874  
4875 260 my $one = $class -> bone();
4876 260 ▶ return $x -> bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 80 180 260
4877  
4878 180 my $f = $x -> copy();
4879 180 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 284 180 464
4880 284 $x -> bmul($f);
4881 }
4882  
4883 180 $x -> round(@r);
4884 180 $x -> _dng();
4885 180 return $x;
4886 }
4887  
4888 0 ▶sub bfib {
Subroutine bfib: not called CC=20 SCAR=60.4
Pod documented
4889 # compute Fibonacci number(s)
4890 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4891  
4892 0 ▶ croak("bfib() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_fib") 0 0 0
4893 unless $LIB -> can('_fib');
4894  
4895 # Don't modify constant (read-only) objects.
4896  
4897 0 ▶ return $x if $x -> modify('bfib');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfib") 0 0 0
4898  
4899 # List context.
4900  
4901 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
4902 0 ▶ croak("bfib() can't return an infinitely long list of numbers")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4903 if $x -> is_inf();
4904  
4905 0 ▶ return if $x -> is_nan() || !$x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or not $x->is_int 0 0 0
Condition0%0/3
$x->is_nan or not $x->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or not $x->is_int
$x->is_nan not $x->is_int
Truth table0%0/3
A or not(B)
A= $x->is_nanB= not $x->is_int
ABresult
1X1
011
000
4906  
4907 # The following places a limit on how large $x can be. Should this
4908 # limit be removed? XXX
4909  
4910 0 my $n = $x -> numify();
4911  
4912 0 my @y;
4913 {
4914 0 $y[0] = $x -> copy() -> babs();
4915 0 $y[0]{_m} = $LIB -> _zero();
4916 0 $y[0]{_e} = $LIB -> _zero();
4917 0 ▶ last if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
4918  
4919 0 $y[1] = $y[0] -> copy();
4920 0 $y[1]{_m} = $LIB -> _one();
4921 0 $y[1]{_e} = $LIB -> _zero();
4922 0 ▶ last if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
4923  
4924 0 ▶ for (my $i = 2 ; $i <= abs($n) ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= abs $n) 0 0 0
4925 0 $y[$i] = $y[$i - 1] -> copy();
4926 $y[$i]{_m} = $LIB -> _add($LIB -> _copy($y[$i - 1]{_m}),
4927 0 $y[$i - 2]{_m});
4928 }
4929  
4930 # If negative, insert sign as appropriate.
4931  
4932 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
4933 0 ▶ for (my $i = 2 ; $i <= $#y ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#y) 0 0 0
4934 0 $y[$i]{sign} = '-';
4935 }
4936 }
4937  
4938 # The last element in the array is the invocand.
4939  
4940 0 $x->{sign} = $y[-1]{sign};
4941 0 $x->{_m} = $y[-1]{_m};
4942 0 $x->{_es} = $y[-1]{_es};
4943 0 $x->{_e} = $y[-1]{_e};
4944 0 $y[-1] = $x;
4945 }
4946  
4947 0 for (@y) {
4948 0 $_ -> bnorm();
4949 0 $_ -> round(@r);
4950 }
4951  
4952 0 return @y;
4953 }
4954  
4955 # Scalar context.
4956  
4957 else {
4958 0 ▶ return $x if $x -> is_inf('+');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
4959 0 ▶ return $x -> bnan() if $x -> is_nan() || $x -> is_inf('-');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 0 0 0
Condition0%0/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table0%0/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
4960  
4961 0 ▶ if ($x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_int) 0 0 0
4962  
4963 0 ▶ $x->{sign} = $x -> is_neg() && $x -> is_even() ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->is_neg && $x->is_even ? : 0 0 0
Condition0%0/3
$x->is_neg and $x->is_even
!ll&&!rl&&r
000
MC/DC0%0/2
$x->is_neg and $x->is_even
$x->is_neg $x->is_even
Truth table0%0/3
A and B
A= $x->is_negB= $x->is_even
ABresult
0X0
100
111
4964 0 $x->{_m} = $LIB -> _lsft($x->{_m}, $x -> {_e}, 10);
4965 0 $x->{_e} = $LIB -> _zero();
4966 0 $x->{_m} = $LIB -> _fib($x->{_m});
4967 0 $x -> bnorm();
4968 }
4969  
4970 0 return $x -> round(@r);
4971 }
4972 }
4973  
4974 0 ▶sub blucas {
Subroutine blucas: not called CC=20 SCAR=60.4
Pod documented
4975 # compute Lucas number(s)
4976 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4977  
4978 0 ▶ croak("blucas() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_lucas") 0 0 0
4979 unless $LIB -> can('_lucas');
4980  
4981 # Don't modify constant (read-only) objects.
4982  
4983 0 ▶ return $x if $x -> modify('blucas');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blucas") 0 0 0
4984  
4985 # List context.
4986  
4987 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
4988 0 ▶ croak("blucas() can't return an infinitely long list of numbers")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4989 if $x -> is_inf();
4990  
4991 0 ▶ return if $x -> is_nan() || !$x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or not $x->is_int 0 0 0
Condition0%0/3
$x->is_nan or not $x->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or not $x->is_int
$x->is_nan not $x->is_int
Truth table0%0/3
A or not(B)
A= $x->is_nanB= not $x->is_int
ABresult
1X1
011
000
4992  
4993 # The following places a limit on how large $x can be. Should this
4994 # limit be removed? XXX
4995  
4996 0 my $n = $x -> numify();
4997  
4998 0 my @y;
4999 {
5000 0 $y[0] = $x -> copy() -> babs();
5001 0 $y[0]{_m} = $LIB -> _two();
5002 0 $y[0]{_e} = $LIB -> _zero();
5003 0 ▶ last if $n == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 0 0 0 0
5004  
5005 0 $y[1] = $y[0] -> copy();
5006 0 $y[1]{_m} = $LIB -> _one();
5007 0 $y[1]{_e} = $LIB -> _zero();
5008 0 ▶ last if $n == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $n == 1 0 0 0
5009  
5010 0 ▶ for (my $i = 2 ; $i <= abs($n) ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= abs $n) 0 0 0
5011 0 $y[$i] = $y[$i - 1] -> copy();
5012 $y[$i]{_m} = $LIB -> _add($LIB -> _copy($y[$i - 1]{_m}),
5013 0 $y[$i - 2]{_m});
5014 }
5015  
5016 # If negative, insert sign as appropriate.
5017  
5018 0 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 0 0 0
5019 0 ▶ for (my $i = 2 ; $i <= $#y ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#y) 0 0 0
5020 0 $y[$i]{sign} = '-';
5021 }
5022 }
5023  
5024 # The last element in the array is the invocand.
5025  
5026 0 $x->{sign} = $y[-1]{sign};
5027 0 $x->{_m} = $y[-1]{_m};
5028 0 $x->{_es} = $y[-1]{_es};
5029 0 $x->{_e} = $y[-1]{_e};
5030 0 $y[-1] = $x;
5031 }
5032  
5033 0 for (@y) {
5034 0 $_ -> bnorm();
5035 0 $_ -> round(@r);
5036 }
5037  
5038 0 return @y;
5039 }
5040  
5041 # Scalar context.
5042  
5043 else {
5044 0 ▶ return $x if $x -> is_inf('+');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 0 0 0
5045 0 ▶ return $x -> bnan() if $x -> is_nan() || $x -> is_inf('-');
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") 0 0 0
Condition0%0/3
$x->is_nan or $x->is_inf("-")
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $x->is_inf("-")
$x->is_nan $x->is_inf("-")
Truth table0%0/3
A or B
A= $x->is_nanB= $x->is_inf("-")
ABresult
1X1
011
000
5046  
5047 0 ▶ if ($x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_int) 0 0 0
5048  
5049 0 ▶ $x->{sign} = $x -> is_neg() && $x -> is_even() ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->is_neg && $x->is_even ? : 0 0 0
Condition0%0/3
$x->is_neg and $x->is_even
!ll&&!rl&&r
000
MC/DC0%0/2
$x->is_neg and $x->is_even
$x->is_neg $x->is_even
Truth table0%0/3
A and B
A= $x->is_negB= $x->is_even
ABresult
0X0
100
111
5050 0 $x->{_m} = $LIB -> _lsft($x->{_m}, $x -> {_e}, 10);
5051 0 $x->{_e} = $LIB -> _zero();
5052 0 $x->{_m} = $LIB -> _lucas($x->{_m});
5053 0 $x -> bnorm();
5054 }
5055  
5056 0 return $x -> round(@r);
5057 }
5058 }
5059  
5060 33 ▶sub blsft {
Subroutine blsft: called CC=15 SCAR=31.5
Pod documented
5061 # shift left by $y in base $b, i.e., multiply by $b ** $y
5062  
5063 # set up parameters
5064 33 ▶ my ($class, $x, $y, $b, @r)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 0 33 33
Condition83%5/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
9240
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
6324
MC/DC0%0/3
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
ref $_[0] ref $_[0] eq ref $_[1] ref $_[1] eq ref $_[2]
Truth table75%3/4
A and B and C
A= ref $_[0]B= ref $_[0] eq ref $_[1]C= ref $_[1] eq ref $_[2]
ABCresult
0XX0
10X0
1100
1111
5065 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
5066 ? (ref($_[0]), @_)
5067 : objectify(2, @_);
5068  
5069 # Don't modify constant (read-only) objects.
5070  
5071 33 ▶ return $x if $x -> modify('blsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blsft") 0 33 33
5072  
5073 33 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 4 29 33
Condition66%2/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
4029
MC/DC50%1/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table66%2/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
5074  
5075 29 ▶ $b = 2 if !defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 29 0 29
5076 29 ▶ $b = $class -> new($b)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($b) and $b->isa(__PACKAGE__) 29 0 29
Condition33%1/3
defined builtin::blessed($b) and $b->isa(__PACKAGE__)
!ll&&!rl&&r
2900
MC/DC0%0/2
defined builtin::blessed($b) and $b->isa(__PACKAGE__)
defined builtin::blessed($b) $b->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($b)B= $b->isa(__PACKAGE__)
ABresult
0X0
100
111
5077 unless defined(blessed($b)) && $b -> isa(__PACKAGE__);
5078 29 ▶ return $x -> bnan(@r) if $b -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $b->is_nan 0 29 29
5079  
5080 # There needs to be more checking for special cases here. Fixme!
5081  
5082 # shift by a negative amount?
5083 29 ▶ return $x -> brsft($y -> copy() -> babs(), $b) if $y -> {sign} =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} =~ /^-/ 0 29 29
5084  
5085 29 $x = $x -> bmul($b -> bpow($y), $r[0], $r[1], $r[2], $y);
5086  
5087 29 $x -> round(@r);
5088 29 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 29 0 29
Condition33%2/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
2900
$x->is_int or $x->is_inf
l!l&&r!l&&!r
2900
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table25%1/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5089 $x -> is_inf() ||
5090 $x -> is_nan());
5091 29 return $x;
5092 }
5093  
5094 64 ▶sub brsft {
Subroutine brsft: called CC=15 SCAR=28.6
Pod documented
5095 # shift right by $y in base $b, i.e., divide by $b ** $y
5096  
5097 # set up parameters
5098 64 ▶ my ($class, $x, $y, $b, @r)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 0 64 64
Condition83%5/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
36280
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
63028
MC/DC0%0/3
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
ref $_[0] ref $_[0] eq ref $_[1] ref $_[1] eq ref $_[2]
Truth table75%3/4
A and B and C
A= ref $_[0]B= ref $_[0] eq ref $_[1]C= ref $_[1] eq ref $_[2]
ABCresult
0XX0
10X0
1100
1111
5099 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
5100 ? (ref($_[0]), @_)
5101 : objectify(2, @_);
5102  
5103 # Don't modify constant (read-only) objects.
5104  
5105 64 ▶ return $x if $x -> modify('brsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("brsft") 0 64 64
5106  
5107 64 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 4 60 64
Condition66%2/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
4060
MC/DC50%1/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table66%2/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
5108  
5109 # There needs to be more checking for special cases here. Fixme!
5110  
5111 60 ▶ $b = 2 if !defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 33 27 60
5112 60 ▶ $b = $class -> new($b)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($b) and $b->isa(__PACKAGE__) 60 0 60
Condition66%2/3
defined builtin::blessed($b) and $b->isa(__PACKAGE__)
!ll&&!rl&&r
49110
MC/DC0%0/2
defined builtin::blessed($b) and $b->isa(__PACKAGE__)
defined builtin::blessed($b) $b->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= defined builtin::blessed($b)B= $b->isa(__PACKAGE__)
ABresult
0X0
100
111
5113 unless defined(blessed($b)) && $b -> isa(__PACKAGE__);
5114 60 ▶ return $x -> bnan(@r) if $b -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $b->is_nan 0 60 60
5115  
5116 # shift by a negative amount?
5117 60 ▶ return $x -> blsft($y -> copy() -> babs(), $b) if $y -> {sign} =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} =~ /^-/ 0 60 60
5118  
5119 # call bdiv()
5120 60 $x = $x -> bdiv($b -> bpow($y), $r[0], $r[1], $r[2], $y);
5121  
5122 60 $x -> round(@r);
5123 60 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 31 29 60
Condition66%4/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
31029
$x->is_int or $x->is_inf
l!l&&r!l&&!r
31029
MC/DC33%1/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table50%2/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5124 $x -> is_inf() ||
5125 $x -> is_nan());
5126 60 return $x;
5127 }
5128  
5129 ###############################################################################
5130 # Bitwise methods
5131 ###############################################################################
5132  
5133 # Bitwise left shift.
5134  
5135 8 ▶sub bblsft {
Subroutine bblsft: called CC=6 SCAR=19.2
Pod documented
5136 # We don't call objectify(), because the bitwise methods should not
5137 # upgrade, even when upgrading is enabled.
5138  
5139 8 ▶ my ($class, $x, $y, @r) = ref($_[0]) ? (ref($_[0]), @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 8 0 8
5140  
5141 # Don't modify constant (read-only) objects.
5142  
5143 8 ▶ return $x if ref($x) && $x -> modify('bblsft');
Branch: 1 branch
BranchTrueFalseTotal
if ref $x and $x->modify("bblsft") 0 8 8
Condition33%1/3
ref $x and $x->modify("bblsft")
!ll&&!rl&&r
080
MC/DC0%0/2
ref $x and $x->modify("bblsft")
ref $x $x->modify("bblsft")
Truth table33%1/3
A and B
A= ref $xB= $x->modify("bblsft")
ABresult
0X0
100
111
5144  
5145 # Let Math::BigInt do the job.
5146  
5147 8 my $xint = Math::BigInt -> bblsft($x, $y, @r);
5148  
5149 # Temporarily disable downgrading.
5150  
5151 8 my $dng = $class -> downgrade();
5152 8 $class -> downgrade(undef);
5153  
5154 # convert to our class without downgrading.
5155  
5156 8 my $xflt = $class -> new($xint);
5157  
5158 # Reset downgrading.
5159  
5160 8 $class -> downgrade($dng);
5161  
5162 # If we are called as a class method, the first operand might not be an
5163 # object of this class, so check.
5164  
5165 8 ▶ if (defined(blessed($x)) && $x -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($x) and $x->isa(__PACKAGE__)) { } 8 0 8
Condition33%1/3
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
!ll&&!rl&&r
008
MC/DC0%0/2
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
defined builtin::blessed($x) $x->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($x)B= $x->isa(__PACKAGE__)
ABresult
0X0
100
111
5166 8 $x -> {sign} = $xflt -> {sign};
5167 8 $x -> {_m} = $xflt -> {_m};
5168 8 $x -> {_es} = $xflt -> {_es};
5169 8 $x -> {_e} = $xflt -> {_e};
5170 } else {
5171 0 $x = $xflt;
5172 }
5173  
5174 # Now we might downgrade.
5175  
5176 8 $x -> round(@r);
5177 8 $x -> _dng();
5178 8 return $x;
5179 }
5180  
5181 # Bitwise right shift.
5182  
5183 8 ▶sub bbrsft {
Subroutine bbrsft: called CC=6 SCAR=19.2
Pod documented
5184 # We don't call objectify(), because the bitwise methods should not
5185 # upgrade, even when upgrading is enabled.
5186  
5187 8 ▶ my ($class, $x, $y, @r) = ref($_[0]) ? (ref($_[0]), @_) : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 8 0 8
5188  
5189 # Don't modify constant (read-only) objects.
5190  
5191 8 ▶ return $x if ref($x) && $x -> modify('bbrsft');
Branch: 1 branch
BranchTrueFalseTotal
if ref $x and $x->modify("bbrsft") 0 8 8
Condition33%1/3
ref $x and $x->modify("bbrsft")
!ll&&!rl&&r
080
MC/DC0%0/2
ref $x and $x->modify("bbrsft")
ref $x $x->modify("bbrsft")
Truth table33%1/3
A and B
A= ref $xB= $x->modify("bbrsft")
ABresult
0X0
100
111
5192  
5193 # Let Math::BigInt do the job.
5194  
5195 8 my $xint = Math::BigInt -> bbrsft($x, $y, @r);
5196  
5197 # Temporarily disable downgrading.
5198  
5199 8 my $dng = $class -> downgrade();
5200 8 $class -> downgrade(undef);
5201  
5202 # Convert to our class without downgrading.
5203  
5204 8 my $xflt = $class -> new($xint);
5205  
5206 # Reset downgrading.
5207  
5208 8 $class -> downgrade($dng);
5209  
5210 # If we are called as a class method, the first operand might not be an
5211 # object of this class, so check.
5212  
5213 8 ▶ if (defined(blessed($x)) && $x -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($x) and $x->isa(__PACKAGE__)) { } 8 0 8
Condition33%1/3
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
!ll&&!rl&&r
008
MC/DC0%0/2
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
defined builtin::blessed($x) $x->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($x)B= $x->isa(__PACKAGE__)
ABresult
0X0
100
111
5214 8 $x -> {sign} = $xflt -> {sign};
5215 8 $x -> {_m} = $xflt -> {_m};
5216 8 $x -> {_es} = $xflt -> {_es};
5217 8 $x -> {_e} = $xflt -> {_e};
5218 } else {
5219 0 $x = $xflt;
5220 }
5221  
5222 # Now we might downgrade.
5223  
5224 8 $x -> round(@r);
5225 8 $x -> _dng();
5226 8 return $x;
5227 }
5228  
5229 1 ▶sub band {
Subroutine band: called CC=8 SCAR=24.3
Pod documented
5230 1 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1 0 1
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
001
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
5231 ? (ref($_[0]), @_)
5232 : objectify(2, @_);
5233  
5234 # Don't modify constant (read-only) objects.
5235  
5236 1 ▶ return if $x -> modify('band');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("band") 0 1 1
5237  
5238 # If $x and/or $y is Inf or NaN, return NaN.
5239  
5240 1 ▶ return $x -> bnan(@r) if ($x -> is_nan() || $x -> is_inf() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf or $y->is_nan or $y->is_inf 0 1 1
Condition33%3/9
$x->is_nan or $x->is_inf or $y->is_nan or $y->is_inf
l!l&&r!l&&!r
001
$x->is_nan or $x->is_inf or $y->is_nan
l!l&&r!l&&!r
001
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
001
MC/DC0%0/4
$x->is_nan or $x->is_inf or $y->is_nan or $y->is_inf
$x->is_nan $x->is_inf $y->is_nan $y->is_inf
Truth table20%1/5
A or B or C or D
A= $x->is_nanB= $x->is_infC= $y->is_nanD= $y->is_inf
ABCDresult
1XXX1
01XX1
001X1
00011
00000
5241 $y -> is_nan() || $y -> is_inf());
5242  
5243 # This should be implemented without converting to Math::BigInt. XXX
5244  
5245 1 my $xint = $x -> as_int(); # to Math::BigInt
5246 1 my $yint = $y -> as_int(); # to Math::BigInt
5247  
5248 1 $xint -> band($yint);
5249 1 $xint -> round(@r);
5250  
5251 1 my $xflt = $xint -> as_float();
5252 1 $x -> {sign} = $xflt -> {sign};
5253 1 $x -> {_m} = $xflt -> {_m};
5254 1 $x -> {_es} = $xflt -> {_es};
5255 1 $x -> {_e} = $xflt -> {_e};
5256  
5257 1 return $x -> _dng();
5258 0 return $x;
5259 }
5260  
5261 1 ▶sub bior {
Subroutine bior: called CC=8 SCAR=24.3
Pod documented
5262 1 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1 0 1
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
001
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
5263 ? (ref($_[0]), @_)
5264 : objectify(2, @_);
5265  
5266 # Don't modify constant (read-only) objects.
5267  
5268 1 ▶ return if $x -> modify('bior');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bior") 0 1 1
5269  
5270 # If $x and/or $y is Inf or NaN, return NaN.
5271  
5272 1 ▶ return $x -> bnan(@r) if ($x -> is_nan() || $x -> is_inf() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf or $y->is_nan or $y->is_inf 0 1 1
Condition33%3/9
$x->is_nan or $x->is_inf or $y->is_nan or $y->is_inf
l!l&&r!l&&!r
001
$x->is_nan or $x->is_inf or $y->is_nan
l!l&&r!l&&!r
001
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
001
MC/DC0%0/4
$x->is_nan or $x->is_inf or $y->is_nan or $y->is_inf
$x->is_nan $x->is_inf $y->is_nan $y->is_inf
Truth table20%1/5
A or B or C or D
A= $x->is_nanB= $x->is_infC= $y->is_nanD= $y->is_inf
ABCDresult
1XXX1
01XX1
001X1
00011
00000
5273 $y -> is_nan() || $y -> is_inf());
5274  
5275 # This should be implemented without converting to Math::BigInt. XXX
5276  
5277 1 my $xint = $x -> as_int(); # to Math::BigInt
5278 1 my $yint = $y -> as_int(); # to Math::BigInt
5279  
5280 1 $xint -> bior($yint);
5281 1 $xint -> round(@r);
5282  
5283 1 my $xflt = $xint -> as_float();
5284 1 $x -> {sign} = $xflt -> {sign};
5285 1 $x -> {_m} = $xflt -> {_m};
5286 1 $x -> {_es} = $xflt -> {_es};
5287 1 $x -> {_e} = $xflt -> {_e};
5288  
5289 1 return $x -> _dng();
5290 0 return $x;
5291 }
5292  
5293 1 ▶sub bxor {
Subroutine bxor: called CC=8 SCAR=24.3
Pod documented
5294 1 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1 0 1
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
001
MC/DC0%0/2
ref $_[0] and ref $_[0] eq ref $_[1]
ref $_[0] ref $_[0] eq ref $_[1]
Truth table33%1/3
A and B
A= ref $_[0]B= ref $_[0] eq ref $_[1]
ABresult
0X0
100
111
5295 ? (ref($_[0]), @_)
5296 : objectify(2, @_);
5297  
5298 # Don't modify constant (read-only) objects.
5299  
5300 1 ▶ return if $x -> modify('bxor');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bxor") 0 1 1
5301  
5302 # If $x and/or $y is Inf or NaN, return NaN.
5303  
5304 1 ▶ return $x -> bnan(@r) if ($x -> is_nan() || $x -> is_inf() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf or $y->is_nan or $y->is_inf 0 1 1
Condition33%3/9
$x->is_nan or $x->is_inf or $y->is_nan or $y->is_inf
l!l&&r!l&&!r
001
$x->is_nan or $x->is_inf or $y->is_nan
l!l&&r!l&&!r
001
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
001
MC/DC0%0/4
$x->is_nan or $x->is_inf or $y->is_nan or $y->is_inf
$x->is_nan $x->is_inf $y->is_nan $y->is_inf
Truth table20%1/5
A or B or C or D
A= $x->is_nanB= $x->is_infC= $y->is_nanD= $y->is_inf
ABCDresult
1XXX1
01XX1
001X1
00011
00000
5305 $y -> is_nan() || $y -> is_inf());
5306  
5307 # This should be implemented without converting to Math::BigInt. XXX
5308  
5309 1 my $xint = $x -> as_int(); # to Math::BigInt
5310 1 my $yint = $y -> as_int(); # to Math::BigInt
5311  
5312 1 $xint -> bxor($yint);
5313 1 $xint -> round(@r);
5314  
5315 1 my $xflt = $xint -> as_float();
5316 1 $x -> {sign} = $xflt -> {sign};
5317 1 $x -> {_m} = $xflt -> {_m};
5318 1 $x -> {_es} = $xflt -> {_es};
5319 1 $x -> {_e} = $xflt -> {_e};
5320  
5321 1 return $x -> _dng();
5322 0 return $x;
5323 }
5324  
5325 4 ▶sub bnot {
Subroutine bnot: called CC=4 SCAR=14.2
Pod documented
5326 4 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4 0 4
5327  
5328 # Don't modify constant (read-only) objects.
5329  
5330 4 ▶ return if $x -> modify('bnot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bnot") 0 4 4
5331  
5332 4 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 4 4
5333  
5334 # This should be implemented without converting to Math::BigInt. XXX
5335  
5336 4 my $xint = $x -> as_int(); # to Math::BigInt
5337  
5338 4 $xint -> bnot();
5339 4 $xint -> round(@r);
5340  
5341 4 my $xflt = $xint -> as_float();
5342 4 $x -> {sign} = $xflt -> {sign};
5343 4 $x -> {_m} = $xflt -> {_m};
5344 4 $x -> {_es} = $xflt -> {_es};
5345 4 $x -> {_e} = $xflt -> {_e};
5346  
5347 4 return $x -> _dng();
5348 0 return $x;
5349 }
5350  
5351 ###############################################################################
5352 # Rounding methods
5353 ###############################################################################
5354  
5355 47907 ▶sub bround {
Subroutine bround: called CC=24 SCAR=32.0
Pod documented
5356 # accuracy: preserve $N digits, and overwrite the rest with 0's
5357  
5358 47907 ▶ my ($class, $x, @a) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 47907 0 47907
5359  
5360 47907 ▶ if (($a[0] || 0) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($a[0] || 0) < 0) 3 47904 47907
Condition100%2/2
$a[0] || 0
l!l
433354572
MC/DC100%1/1
$a[0] || 0
$a[0]
Truth table100%2/2
A
A= $a[0]
Aresult
11
00
5361 3 croak('bround() needs positive accuracy');
5362 }
5363  
5364 # Don't modify constant (read-only) objects.
5365  
5366 47904 ▶ return $x if $x -> modify('bround');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bround") 0 47904 47904
5367  
5368 47904 my ($scale, $mode) = $x->_scale_a(@a);
5369 47904 ▶ if (!defined $scale) { # no-op
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scale) 4569 43335 47904
5370 4569 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 4566 3 4569
Condition100%6/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
456513
$x->is_int or $x->is_inf
l!l&&r!l&&!r
456414
MC/DC100%3/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table100%4/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5371 $x -> is_inf() ||
5372 $x -> is_nan());
5373 4569 return $x;
5374 }
5375  
5376 # Scale is now either $x->{accuracy}, $accuracy, or the input argument.
5377 # Test whether $x already has lower accuracy, do nothing in this case but
5378 # do round if the accuracy is the same, since a math operation might want
5379 # to round a number with A=5 to 5 digits afterwards again
5380  
5381 43335 ▶ if (defined $x->{accuracy} && $x->{accuracy} < $scale) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x->{"accuracy"} and $x->{"accuracy"} < $scale) 44 43291 43335
Condition100%3/3
defined $x->{"accuracy"} and $x->{"accuracy"} < $scale
!ll&&!rl&&r
112963199544
MC/DC100%2/2
defined $x->{"accuracy"} and $x->{"accuracy"} < $scale
defined $x->{"accuracy"} $x->{"accuracy"} < $scale
Truth table100%3/3
A and B
A= defined $x->{"accuracy"}B= $x->{"accuracy"} < $scale
ABresult
0X0
100
111
5382 44 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 4 40 44
Condition66%4/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
4040
$x->is_int or $x->is_inf
l!l&&r!l&&!r
4040
MC/DC33%1/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table50%2/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5383 $x -> is_inf() ||
5384 $x -> is_nan());
5385 44 return $x;
5386 }
5387  
5388 # scale < 0 makes no sense
5389 # scale == 0 => keep all digits
5390 # never round a +-inf, NaN
5391  
5392 43291 ▶ if ($scale <= 0 || $x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= 0 or not $x->{"sign"} =~ /^[+-]$/) 12 43279 43291
Condition66%2/3
$scale <= 0 or not $x->{"sign"} =~ /^[+-]$/
l!l&&r!l&&!r
01243279
MC/DC50%1/2
$scale <= 0 or not $x->{"sign"} =~ /^[+-]$/
$scale <= 0 not $x->{"sign"} =~ /^[+-]$/
Truth table66%2/3
A or not(B)
A= $scale <= 0B= not $x->{"sign"} =~ /^[+-]$/
ABresult
1X1
011
000
5393 12 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 12 0 12
Condition66%4/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
840
$x->is_int or $x->is_inf
l!l&&r!l&&!r
084
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table50%2/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5394 $x -> is_inf() ||
5395 $x -> is_nan());
5396 12 return $x;
5397 }
5398  
5399 # 1: never round a 0
5400 # 2: if we should keep more digits than the mantissa has, do nothing
5401 43279 ▶ if ($x -> is_zero() || $LIB->_len($x->{_m}) <= $scale) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero or $LIB->_len($x->{"_m"}) <= $scale) 15285 27994 43279
Condition100%3/3
$x->is_zero or $LIB->_len($x->{"_m"}) <= $scale
l!l&&r!l&&!r
971518827994
MC/DC100%2/2
$x->is_zero or $LIB->_len($x->{"_m"}) <= $scale
$x->is_zero $LIB->_len($x->{"_m"}) <= $scale
Truth table100%3/3
A or B
A= $x->is_zeroB= $LIB->_len($x->{"_m"}) <= $scale
ABresult
1X1
011
000
5402 15285 ▶ $x->{accuracy} = $scale if !defined $x->{accuracy} || $x->{accuracy} > $scale;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x->{"accuracy"} or $x->{"accuracy"} > $scale 2060 13225 15285
Condition100%3/3
not defined $x->{"accuracy"} or $x->{"accuracy"} > $scale
l!l&&r!l&&!r
20154513225
MC/DC100%2/2
not defined $x->{"accuracy"} or $x->{"accuracy"} > $scale
not defined $x->{"accuracy"} $x->{"accuracy"} > $scale
Truth table100%3/3
not(A) or B
A= not defined $x->{"accuracy"}B= $x->{"accuracy"} > $scale
ABresult
1X1
011
000
5403 15285 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 1027 14258 15285
5404 15285 return $x;
5405 }
5406  
5407 # pass sign to bround for '+inf' and '-inf' rounding modes
5408 27994 my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt';
5409  
5410 27994 $m = $m -> bround($scale, $mode); # round mantissa
5411 27994 $x->{_m} = $m->{value}; # get our mantissa back
5412 27994 $x->{accuracy} = $scale; # remember rounding
5413 27994 $x->{precision} = undef; # and clear P
5414  
5415 # bnorm() downgrades if necessary, so no need to check whether to
5416 # downgrade.
5417 27994 $x -> bnorm(); # del trailing zeros gen. by bround()
5418 }
5419  
5420 756 ▶sub bfround {
Subroutine bfround: called CC=48 SCAR=46.1
Pod documented
5421 # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.'
5422 # $n == 0 means round to integer
5423 # expects and returns normalized numbers!
5424  
5425 756 ▶ my ($class, $x, @p) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 756 0 756
5426  
5427 # Don't modify constant (read-only) objects.
5428  
5429 756 ▶ return $x if $x -> modify('bfround'); # no-op
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfround") 0 756 756
5430  
5431 756 my ($scale, $mode) = $x->_scale_p(@p);
5432 756 ▶ if (!defined $scale) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scale) 4 752 756
5433 4 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 4 0 4
Condition83%5/6
$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/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table75%3/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5434 $x -> is_inf() ||
5435 $x -> is_nan());
5436 4 return $x;
5437 }
5438  
5439 # never round a 0, +-inf, NaN
5440  
5441 752 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 21 731 752
5442 21 ▶ $x->{precision} = $scale if !defined $x->{precision} || $x->{precision} < $scale; # -3 < -2
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x->{"precision"} or $x->{"precision"} < $scale 21 0 21
Condition33%1/3
not defined $x->{"precision"} or $x->{"precision"} < $scale
l!l&&r!l&&!r
2100
MC/DC0%0/2
not defined $x->{"precision"} or $x->{"precision"} < $scale
not defined $x->{"precision"} $x->{"precision"} < $scale
Truth table33%1/3
not(A) or B
A= not defined $x->{"precision"}B= $x->{"precision"} < $scale
ABresult
1X1
011
000
5443 21 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 21 0 21
Condition33%2/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
2100
$x->is_int or $x->is_inf
l!l&&r!l&&!r
2100
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table25%1/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5444 $x -> is_inf() ||
5445 $x -> is_nan());
5446 21 return $x;
5447 }
5448  
5449 731 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 12 719 731
5450 12 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 12 0 12
Condition66%4/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
840
$x->is_int or $x->is_inf
l!l&&r!l&&!r
084
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table50%2/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5451 $x -> is_inf() ||
5452 $x -> is_nan());
5453 12 return $x;
5454 }
5455  
5456 # don't round if x already has lower precision
5457 719 ▶ if (defined $x->{precision} && $x->{precision} < 0 && $scale < $x->{precision}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x->{"precision"} and $x->{"precision"} < 0 and $scale < $x->{"precision"}) 0 719 719
Condition83%5/6
defined $x->{"precision"} and $x->{"precision"} < 0 and $scale < $x->{"precision"}
!ll&&!rl&&r
690290
defined $x->{"precision"} and $x->{"precision"} < 0
!ll&&!rl&&r
6801029
MC/DC0%0/3
defined $x->{"precision"} and $x->{"precision"} < 0 and $scale < $x->{"precision"}
defined $x->{"precision"} $x->{"precision"} < 0 $scale < $x->{"precision"}
Truth table75%3/4
A and B and C
A= defined $x->{"precision"}B= $x->{"precision"} < 0C= $scale < $x->{"precision"}
ABCresult
0XX0
10X0
1100
1111
5458 0 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 0 0 0
Condition0%0/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
000
$x->is_int or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table0%0/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5459 $x -> is_inf() ||
5460 $x -> is_nan());
5461 0 return $x;
5462 }
5463  
5464 719 $x->{precision} = $scale; # remember round in any case
5465 719 $x->{accuracy} = undef; # and clear A
5466 719 ▶ if ($scale < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < 0) { } 551 168 719
5467 # round right from the '.'
5468  
5469 551 ▶ if ($x->{_es} eq '+') { # e >= 0 => nothing to round
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "+") 37 514 551
5470 37 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 37 0 37
Condition33%2/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
3700
$x->is_int or $x->is_inf
l!l&&r!l&&!r
3700
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table25%1/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5471 $x -> is_inf() ||
5472 $x -> is_nan());
5473 37 return $x;
5474 }
5475  
5476 514 $scale = -$scale; # positive for simplicity
5477 514 my $len = $LIB->_len($x->{_m}); # length of mantissa
5478  
5479 # the following poses a restriction on _e, but if _e is bigger than a
5480 # scalar, you got other problems (memory etc) anyway
5481 514 my $dad = -(0+ ($x->{_es}.$LIB->_num($x->{_e}))); # digits after dot
5482 514 my $zad = 0; # zeros after dot
5483 514 ▶ $zad = $dad - $len if (-$dad < -$len); # for 0.00..00xxx style
Branch: 1 branch
BranchTrueFalseTotal
if -$dad < -$len 195 319 514
5484  
5485 # print "scale $scale dad $dad zad $zad len $len\n";
5486 # number bsstr len zad dad
5487 # 0.123 123e-3 3 0 3
5488 # 0.0123 123e-4 3 1 4
5489 # 0.001 1e-3 1 2 3
5490 # 1.23 123e-2 3 0 2
5491 # 1.2345 12345e-4 5 0 4
5492  
5493 # do not round after/right of the $dad
5494  
5495 514 ▶ if ($scale > $dad) { # 0.123, scale >= 3 => exit
Branch: 1 branch
BranchTrueFalseTotal
if ($scale > $dad) 47 467 514
5496 47 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 0 47 47
Condition33%2/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
0047
$x->is_int or $x->is_inf
l!l&&r!l&&!r
0047
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table25%1/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5497 $x -> is_inf() ||
5498 $x -> is_nan());
5499 47 return $x;
5500 }
5501  
5502 # round to zero if rounding inside the $zad, but not for last zero like:
5503 # 0.0065, scale -2, round last '0' with following '65' (scale == zad
5504 # case)
5505 467 ▶ if ($scale < $zad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < $zad) 40 427 467
5506 40 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 0 40 40
Condition33%2/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
0040
$x->is_int or $x->is_inf
l!l&&r!l&&!r
0040
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table25%1/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5507 $x -> is_inf() ||
5508 $x -> is_nan());
5509 40 return $x -> bzero();
5510 }
5511  
5512 427 ▶ if ($scale == $zad) { # for 0.006, scale -3 and trunc
Branch: 1 branch
BranchTrueFalseTotal
if ($scale == $zad) { } 41 386 427
5513 41 $scale = -$len;
5514 } else {
5515 # adjust round-point to be inside mantissa
5516 386 ▶ if ($zad != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zad != 0) { } 78 308 386
5517 78 $scale = $scale-$zad;
5518 } else {
5519 308 my $dbd = $len - $dad;
5520 308 ▶ $dbd = 0 if $dbd < 0; # digits before dot
Branch: 1 branch
BranchTrueFalseTotal
if $dbd < 0 0 308 308
5521 308 $scale = $dbd+$scale;
5522 }
5523 }
5524 } else {
5525 # round left from the '.'
5526  
5527 # 123 => 100 means length(123) = 3 - $scale (2) => 1
5528  
5529 168 my $dbt = $LIB->_len($x->{_m});
5530 # digits before dot
5531 168 my $dbd = $dbt + ($x->{_es} . $LIB->_num($x->{_e}));
5532 # should be the same, so treat it as this
5533 168 ▶ $scale = 1 if $scale == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $scale == 0 107 61 168
5534 # shortcut if already integer
5535 168 ▶ if ($scale == 1 && $dbt <= $dbd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale == 1 and $dbt <= $dbd) 14 154 168
Condition100%3/3
$scale == 1 and $dbt <= $dbd
!ll&&!rl&&r
4810614
MC/DC100%2/2
$scale == 1 and $dbt <= $dbd
$scale == 1 $dbt <= $dbd
Truth table100%3/3
A and B
A= $scale == 1B= $dbt <= $dbd
ABresult
0X0
100
111
5536 14 ▶ $x -> _dng() if ($x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int or $x->is_inf or $x->is_nan 14 0 14
Condition33%2/6
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
1400
$x->is_int or $x->is_inf
l!l&&r!l&&!r
1400
MC/DC0%0/3
$x->is_int or $x->is_inf or $x->is_nan
$x->is_int $x->is_inf $x->is_nan
Truth table25%1/4
A or B or C
A= $x->is_intB= $x->is_infC= $x->is_nan
ABCresult
1XX1
01X1
0011
0000
5537 $x -> is_inf() ||
5538 $x -> is_nan());
5539 14 return $x;
5540 }
5541 # maximum digits before dot
5542 154 ++$dbd;
5543  
5544 154 ▶ if ($scale > $dbd) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scale > $dbd) { } 30 124 154
elsif ($scale == $dbd) { } 72 52 124
5545 # not enough digits before dot, so round to zero
5546 30 return $x -> bzero;
5547 } elsif ($scale == $dbd) {
5548 # maximum
5549 72 $scale = -$dbt;
5550 } else {
5551 52 $scale = $dbd - $scale;
5552 }
5553 }
5554  
5555 # pass sign to bround for rounding modes '+inf' and '-inf'
5556 551 my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt';
5557 551 $m = $m -> bround($scale, $mode);
5558 551 $x->{_m} = $m->{value}; # get our mantissa back
5559  
5560 # bnorm() downgrades if necessary, so no need to check whether to
5561 # downgrade.
5562 551 $x -> bnorm();
5563 }
5564  
5565 106 ▶sub bfloor {
Subroutine bfloor: called CC=7 SCAR=19.5
Pod documented
5566 # round towards minus infinity
5567 106 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 106 0 106
5568  
5569 # Don't modify constant (read-only) objects.
5570  
5571 106 ▶ return $x if $x -> modify('bfloor');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfloor") 0 106 106
5572  
5573 106 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 101 106
5574  
5575 101 ▶ if ($x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_finite) 92 9 101
5576 # if $x has digits after dot, remove them
5577 92 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 56 36 92
5578 56 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10);
5579 56 $x->{_e} = $LIB->_zero();
5580 56 $x->{_es} = '+';
5581 # increment if negative
5582 56 ▶ $x->{_m} = $LIB->_inc($x->{_m}) if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 12 44 56
5583 }
5584 }
5585  
5586 101 $x -> round(@r);
5587 101 $x -> _dng();
5588 101 return $x;
5589 }
5590  
5591 43 ▶sub bceil {
Subroutine bceil: called CC=8 SCAR=20.8
Pod documented
5592 # round towards plus infinity
5593 43 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 43 0 43
5594  
5595 # Don't modify constant (read-only) objects.
5596  
5597 43 ▶ return $x if $x -> modify('bceil');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bceil") 0 43 43
5598  
5599 43 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 38 43
5600  
5601 38 ▶ if ($x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_finite) 29 9 38
5602 # if $x has digits after dot, remove them
5603 29 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 17 12 29
5604 17 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10);
5605 17 $x->{_e} = $LIB->_zero();
5606 17 $x->{_es} = '+';
5607 17 ▶ if ($x->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '+') { } 9 8 17
5608 9 $x->{_m} = $LIB->_inc($x->{_m}); # increment if positive
5609 } else {
5610 8 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{_m}); # avoid -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 4 4 8
5611 }
5612 }
5613 }
5614  
5615 38 $x -> round(@r);
5616 38 $x -> _dng();
5617 38 return $x;
5618 }
5619  
5620 991 ▶sub bint {
Subroutine bint: called CC=7 SCAR=19.5
Pod documented
5621 # round towards zero
5622 991 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 991 0 991
5623  
5624 # Don't modify constant (read-only) objects.
5625  
5626 991 ▶ return $x if $x -> modify('bint');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bint") 0 991 991
5627  
5628 991 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 986 991
5629  
5630 986 ▶ if ($x -> is_finite()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_finite) 977 9 986
5631 # if $x has digits after the decimal point
5632 977 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 206 771 977
5633 206 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10); # remove frac part
5634 206 $x->{_e} = $LIB->_zero(); # truncate/normalize
5635 206 $x->{_es} = '+'; # abs e
5636 206 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{_m}); # avoid -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 133 73 206
5637 }
5638 }
5639  
5640 986 $x -> round(@r);
5641 986 $x -> _dng();
5642 986 return $x;
5643 }
5644  
5645 ###############################################################################
5646 # Other mathematical methods
5647 ###############################################################################
5648  
5649 121 ▶sub bgcd {
Subroutine bgcd: called CC=12 SCAR=25.0
Pod documented
5650 # GCD -- Euclid's algorithm, variant C (Knuth Vol 3, pg 341 ff)
5651  
5652 # Class::method(...) -> Class->method(...)
5653 121 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)) 1 120 121
Condition75%9/12
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
!ll&&!rl&&r
01120
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
l!l&&r!l&&!r
151051
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
106015
$_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
!ll&&!rl&&r
10105
MC/DC40%2/5
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i not $_[0] =~ /^(inf|nan)/i
Truth table37%3/8
A and B and C or D and not(E)
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/iE= not $_[0] =~ /^(inf|nan)/i
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
5654 ($_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i &&
5655 $_[0] !~ /^(inf|nan)/i)))
5656 {
5657 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
5658 # " use is as a method instead";
5659 1 unshift @_, __PACKAGE__;
5660 }
5661  
5662 121 my ($class, @args) = objectify(0, @_);
5663  
5664 # Pre-process list of operands.
5665  
5666 121 for my $arg (@args) {
5667 225 ▶ return $class -> bnan() unless $arg -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->is_finite 40 185 225
5668 }
5669  
5670 # Temporarily disable downgrading.
5671  
5672 81 my $dng = $class -> downgrade();
5673 81 $class -> downgrade(undef);
5674  
5675 81 my $x = shift @args;
5676 81 $x = $x -> copy(); # bgcd() and blcm() never modify any operands
5677  
5678 81 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 92 49 141
5679 92 my $y = shift @args;
5680  
5681 # greatest common divisor
5682 92 ▶ while (! $y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->is_zero) 242 92 334
5683 242 ($x, $y) = ($y -> copy(), $x -> copy() -> bmod($y));
5684 }
5685  
5686 92 ▶ last if $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 32 60 92
5687 }
5688 81 $x -> babs();
5689  
5690 # Restore downgrading.
5691  
5692 81 $class -> downgrade($dng);
5693  
5694 81 ▶ $x -> _dng() if $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_int 81 0 81
5695 81 return $x;
5696 }
5697  
5698 35 ▶sub blcm {
Subroutine blcm: called CC=11 SCAR=24.0
Pod documented
5699 # Least Common Multiple
5700  
5701 # Class::method(...) -> Class->method(...)
5702 35 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)) 1 34 35
Condition75%9/12
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
!ll&&!rl&&r
0134
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
l!l&&r!l&&!r
1331
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
3401
$_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i and not $_[0] =~ /^(inf|nan)/i
!ll&&!rl&&r
1033
MC/DC40%2/5
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i && !($_[0] =~ /^(inf|nan)/i)
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i not $_[0] =~ /^(inf|nan)/i
Truth table37%3/8
A and B and C or D and not(E)
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/iE= not $_[0] =~ /^(inf|nan)/i
ABCDEresult
0XXXX0
10X0X0
10X100
1100X0
110100
111XX1
10X111
110111
5703 ($_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i &&
5704 $_[0] !~ /^(inf|nan)/i)))
5705 {
5706 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
5707 # " use is as a method instead";
5708 1 unshift @_, __PACKAGE__;
5709 }
5710  
5711 35 my ($class, @args) = objectify(0, @_);
5712  
5713 # Pre-process list of operands.
5714  
5715 35 for my $arg (@args) {
5716 65 ▶ return $class -> bnan() unless $arg -> is_finite();
Branch: 1 branch
BranchTrueFalseTotal
unless $arg->is_finite 12 53 65
5717 }
5718  
5719 23 for my $arg (@args) {
5720 41 ▶ return $class -> bzero() if $arg -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $arg->is_zero 12 29 41
5721 }
5722  
5723 11 my $x = shift @args;
5724 11 $x = $x -> copy(); # bgcd() and blcm() never modify any operands
5725  
5726 11 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 14 11 25
5727 14 my $y = shift @args;
5728 14 my $gcd = $x -> copy() -> bgcd($y);
5729 14 $x -> bdiv($gcd) -> bmul($y);
5730 }
5731  
5732 11 $x -> babs(); # might downgrade
5733 11 return $x;
5734 }
5735  
5736 ###############################################################################
5737 # Object property methods
5738 ###############################################################################
5739  
5740 54 ▶sub length {
Subroutine length: called CC=7 SCAR=19.9
Pod documented
5741 54 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 54 0 54
5742  
5743 54 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 54 54
5744  
5745 54 ▶ return 1 if $LIB->_is_zero($x->{_m});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 4 50 54
5746  
5747 50 my $len = $LIB->_len($x->{_m});
5748 50 ▶ $len += $LIB->_num($x->{_e}) if $x->{_es} eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "+" 50 0 50
5749 50 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 30 20 50
5750 30 my $t = 0;
5751 30 ▶ $t = $LIB->_num($x->{_e}) if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 0 30 30
5752 30 return $len, $t;
5753 }
5754 20 $len;
5755 }
5756  
5757 36 ▶sub mantissa {
Subroutine mantissa: called CC=5 SCAR=16.1
Pod documented
5758 # return a copy of the mantissa
5759 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
5760  
5761 # The following line causes a lot of noise in the test suits for
5762 # the Math-BigRat and bignum distributions. Fixme!
5763 #carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
5764  
5765 36 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 32 36
5766  
5767 32 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 8 24 32
5768 8 my $s = $x->{sign};
5769 8 $s =~ s/^\+//;
5770 8 return Math::BigInt -> new($s, undef, undef); # -inf, +inf => +inf
5771 }
5772 24 my $m = Math::BigInt -> new($LIB->_str($x->{_m}), undef, undef);
5773 24 ▶ $m = $m -> bneg() if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 8 16 24
5774 24 $m;
5775 }
5776  
5777 36 ▶sub exponent {
Subroutine exponent: called CC=4 SCAR=13.9
Pod documented
5778 # return a copy of the exponent
5779 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
5780  
5781 # The following line causes a lot of noise in the test suits for
5782 # the Math-BigRat and bignum distributions. Fixme!
5783 #carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
5784  
5785 36 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 32 36
5786  
5787 32 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 8 24 32
5788 8 my $s = $x->{sign};
5789 8 $s =~ s/^[+-]//;
5790 8 return Math::BigInt -> new($s, undef, undef); # -inf, +inf => +inf
5791 }
5792 24 Math::BigInt -> new($x->{_es} . $LIB->_str($x->{_e}), undef, undef);
5793 }
5794  
5795 32 ▶sub parts {
Subroutine parts: called CC=5 SCAR=16.1
Pod documented
5796 # return a copy of both the exponent and the mantissa
5797 32 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 32 0 32
5798  
5799 32 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 32 32
5800  
5801 32 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 12 20 32
5802 12 my $s = $x->{sign};
5803 12 $s =~ s/^\+//;
5804 12 my $se = $s;
5805 12 $se =~ s/^-//;
5806 # +inf => inf and -inf, +inf => inf
5807 12 return $class -> new($s), $class -> new($se);
5808 }
5809 20 my $m = Math::BigInt -> bzero();
5810 20 $m->{value} = $LIB->_copy($x->{_m});
5811 20 ▶ $m = $m -> bneg() if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 8 12 20
5812 20 ($m, Math::BigInt -> new($x->{_es} . $LIB->_num($x->{_e})));
5813 }
5814  
5815 # Parts used for scientific notation with significand/mantissa and exponent as
5816 # integers. E.g., "12345.6789" is returned as "123456789" (mantissa) and "-4"
5817 # (exponent).
5818  
5819 30 ▶sub sparts {
Subroutine sparts: called CC=8 SCAR=27.7
Pod documented
5820 30 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 30 0 30
5821  
5822 30 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 30 30
5823  
5824 # Not-a-number.
5825  
5826 30 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 30 30
5827 0 my $mant = $class -> bnan(); # mantissa
5828 0 ▶ return $mant unless wantarray; # scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5829 0 my $expo = $class -> bnan(); # exponent
5830 0 return $mant, $expo; # list context
5831 }
5832  
5833 # Infinity.
5834  
5835 30 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 30 30
5836 0 my $mant = $class -> binf($x->{sign}); # mantissa
5837 0 ▶ return $mant unless wantarray; # scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5838 0 my $expo = $class -> binf('+'); # exponent
5839 0 return $mant, $expo; # list context
5840 }
5841  
5842 # Finite number.
5843  
5844 30 my $mant = $class -> new($x);
5845 30 $mant->{_es} = '+';
5846 30 $mant->{_e} = $LIB->_zero();
5847 30 $mant -> _dng();
5848 30 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 30 30
5849  
5850 30 my $expo = $class -> new($x -> {_es} . $LIB->_str($x -> {_e}));
5851 30 $expo -> _dng();
5852 30 return $mant, $expo;
5853 }
5854  
5855 # Parts used for normalized notation with significand/mantissa as either 0 or a
5856 # number in the semi-open interval [1,10). E.g., "12345.6789" is returned as
5857 # "1.23456789" and "4".
5858  
5859 30 ▶sub nparts {
Subroutine nparts: called CC=9 SCAR=23.4
Pod documented
5860 30 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 30 0 30
5861  
5862 30 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 30 30
5863  
5864 # Not-a-number and Infinity.
5865  
5866 30 ▶ return $x -> sparts() if $x -> is_nan() || $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf 0 30 30
Condition33%1/3
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
0030
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
5867  
5868 # Finite number.
5869  
5870 30 my ($mant, $expo) = $x -> sparts();
5871  
5872 30 ▶ if ($mant -> bcmp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant->bcmp(0)) 30 0 30
5873 30 my ($ndigtot, $ndigfrac) = $mant -> length();
5874 30 my $expo10adj = $ndigtot - $ndigfrac - 1;
5875  
5876 30 ▶ if ($expo10adj > 0) { # if mantissa is not an integer
Branch: 1 branch
BranchTrueFalseTotal
if ($expo10adj > 0) 16 14 30
5877 16 $mant = $mant -> brsft($expo10adj, 10);
5878 16 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 16 16
5879 16 $expo = $expo -> badd($expo10adj);
5880 16 return $mant, $expo;
5881 }
5882 }
5883  
5884 14 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 14 14
5885 14 return $mant, $expo;
5886 }
5887  
5888 # Parts used for engineering notation with significand/mantissa as either 0 or
5889 # a number in the semi-open interval [1,1000) and the exponent is a multiple of
5890 # 3. E.g., "12345.6789" is returned as "12.3456789" and "3".
5891  
5892 0 ▶sub eparts {
Subroutine eparts: not called CC=6 SCAR=37.4
Pod documented
5893 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5894  
5895 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5896  
5897 # Not-a-number and Infinity.
5898  
5899 0 ▶ return $x -> sparts() if $x -> is_nan() || $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf 0 0 0
Condition0%0/3
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_nan or $x->is_inf
$x->is_nan $x->is_inf
Truth table0%0/3
A or B
A= $x->is_nanB= $x->is_inf
ABresult
1X1
011
000
5900  
5901 # Finite number.
5902  
5903 0 my ($mant, $expo) = $x -> nparts();
5904  
5905 0 my $c = $expo -> copy() -> bmod(3);
5906 0 $mant = $mant -> blsft($c, 10);
5907 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5908  
5909 0 $expo = $expo -> bsub($c);
5910 0 return $mant, $expo;
5911 }
5912  
5913 # Parts used for decimal notation, e.g., "12345.6789" is returned as "12345"
5914 # (integer part) and "0.6789" (fraction part).
5915  
5916 0 ▶sub dparts {
Subroutine dparts: not called CC=11 SCAR=48.8
Pod documented
5917 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5918  
5919 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5920  
5921 # Not-a-number.
5922  
5923 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
5924 0 my $int = $class -> bnan();
5925 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5926 0 my $frc = $class -> bzero(); # or NaN?
5927 0 return $int, $frc;
5928 }
5929  
5930 # Infinity.
5931  
5932 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
5933 0 my $int = $class -> binf($x->{sign});
5934 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5935 0 my $frc = $class -> bzero();
5936 0 return $int, $frc;
5937 }
5938  
5939 # Finite number.
5940  
5941 0 my $int = $x -> copy();
5942 0 my $frc;
5943  
5944 # If the input is an integer.
5945  
5946 0 ▶ if ($int->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($int->{'_es'} eq '+') { } 0 0 0
5947 0 $frc = $class -> bzero();
5948 }
5949  
5950 # If the input has a fraction part
5951  
5952 else {
5953 0 $int->{_m} = $LIB -> _rsft($int->{_m}, $int->{_e}, 10);
5954 0 $int->{_e} = $LIB -> _zero();
5955 0 $int->{_es} = '+';
5956 0 ▶ $int->{sign} = '+' if $LIB->_is_zero($int->{_m}); # avoid -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($int->{"_m"}) 0 0 0
5957 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5958 0 $frc = $x -> copy() -> bsub($int);
5959 0 return $int, $frc;
5960 }
5961  
5962 0 $int -> _dng();
5963 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5964 0 return $int, $frc;
5965 }
5966  
5967 # Fractional parts with the numerator and denominator as integers. E.g.,
5968 # "123.4375" is returned as "1975" and "16".
5969  
5970 0 ▶sub fparts {
Subroutine fparts: not called CC=10 SCAR=47.0
Pod documented
5971 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5972  
5973 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5974  
5975 # NaN => NaN/NaN
5976  
5977 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
5978 0 ▶ return $class -> bnan() unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5979 0 return $class -> bnan(), $class -> bnan();
5980 }
5981  
5982 # ±Inf => ±Inf/1
5983  
5984 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
5985 0 my $numer = $class -> binf($x->{sign});
5986 0 ▶ return $numer unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
5987 0 my $denom = $class -> bone();
5988 0 return $numer, $denom;
5989 }
5990  
5991 # Finite number.
5992  
5993 # If we get here, we know that the output is an integer.
5994  
5995 0 ▶ $class = $downgrade if $class -> downgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->downgrade 0 0 0
5996  
5997 0 my @flt_parts = ($x->{sign}, $x->{_m}, $x->{_es}, $x->{_e});
5998 0 my @rat_parts = $class -> _flt_lib_parts_to_rat_lib_parts(@flt_parts);
5999 0 my $numer = $class -> new($LIB -> _str($rat_parts[1]));
6000 0 ▶ $numer -> bneg() if $rat_parts[0] eq "-";
Branch: 1 branch
BranchTrueFalseTotal
if $rat_parts[0] eq "-" 0 0 0
6001 0 ▶ return $numer unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
6002  
6003 0 my $denom = $class -> new($LIB -> _str($rat_parts[2]));
6004 0 return $numer, $denom;
6005 }
6006  
6007 # Given "123.4375", returns "1975", since "123.4375" is "1975/16".
6008  
6009 0 ▶sub numerator {
Subroutine numerator: not called CC=9 SCAR=45.0
Pod documented
6010 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
6011  
6012 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
6013  
6014 0 ▶ return $class -> bnan() if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
6015 0 ▶ return $class -> binf($x -> sign()) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
6016 0 ▶ return $class -> bzero() if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
6017  
6018 # If we get here, we know that the output is an integer.
6019  
6020 0 ▶ $class = $downgrade if $class -> downgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->downgrade 0 0 0
6021  
6022 0 ▶ if ($x -> {_es} eq '-') { # exponent < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 0 0 0
elsif (not $LIB->_is_zero($x->{'_e'})) { } 0 0 0
6023 0 my $numer_lib = $LIB -> _copy($x -> {_m});
6024 0 my $denom_lib = $LIB -> _1ex($x -> {_e});
6025 0 my $gcd_lib = $LIB -> _gcd($LIB -> _copy($numer_lib), $denom_lib);
6026 0 $numer_lib = $LIB -> _div($numer_lib, $gcd_lib);
6027 0 return $class -> new($x -> {sign} . $LIB -> _str($numer_lib));
6028 }
6029  
6030 elsif (! $LIB -> _is_zero($x -> {_e})) { # exponent > 0
6031 0 my $numer_lib = $LIB -> _copy($x -> {_m});
6032 0 $numer_lib = $LIB -> _lsft($numer_lib, $x -> {_e}, 10);
6033 0 return $class -> new($x -> {sign} . $LIB -> _str($numer_lib));
6034 }
6035  
6036 else { # exponent = 0
6037 0 return $class -> new($x -> {sign} . $LIB -> _str($x -> {_m}));
6038 }
6039 }
6040  
6041 # Given "123.4375", returns "16", since "123.4375" is "1975/16".
6042  
6043 0 ▶sub denominator {
Subroutine denominator: not called CC=6 SCAR=37.4
Pod documented
6044 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
6045  
6046 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
6047  
6048 0 ▶ return $class -> bnan() if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
6049  
6050 # If we get here, we know that the output is an integer.
6051  
6052 0 ▶ $class = $downgrade if $class -> downgrade();
Branch: 1 branch
BranchTrueFalseTotal
if $class->downgrade 0 0 0
6053  
6054 0 ▶ if ($x -> {_es} eq '-') { # exponent < 0
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 0 0 0
6055 0 my $numer_lib = $LIB -> _copy($x -> {_m});
6056 0 my $denom_lib = $LIB -> _1ex($x -> {_e});
6057 0 my $gcd_lib = $LIB -> _gcd($LIB -> _copy($numer_lib), $denom_lib);
6058 0 $denom_lib = $LIB -> _div($denom_lib, $gcd_lib);
6059 0 return $class -> new($LIB -> _str($denom_lib));
6060 }
6061  
6062 else { # exponent >= 0
6063 0 return $class -> bone();
6064 }
6065 }
6066  
6067 ###############################################################################
6068 # String conversion methods
6069 ###############################################################################
6070  
6071 8459 ▶sub bstr {
Subroutine bstr: called CC=21 SCAR=30.5
Pod documented
6072 # (ref to BFLOAT or num_str) return num_str
6073 # Convert number from internal format to (non-scientific) string format.
6074 # internal format is always normalized (no leading zeros, "-0" => "+0")
6075 8459 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 8447 12 8459
6076  
6077 8459 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 8459 8459
6078  
6079 # Inf and NaN
6080  
6081 8459 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 2483 5976 8459
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
496110152483
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
6082 2483 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 1928 555 2483
6083 555 return 'inf'; # +inf
6084 }
6085  
6086 # Finite number
6087  
6088 5976 my $es = '0';
6089 5976 my $len = 1;
6090 5976 my $cad = 0;
6091 5976 my $dot = '.';
6092  
6093 # $x is zero?
6094 5976 ▶ my $not_zero = !($x->{sign} eq '+' && $LIB->_is_zero($x->{_m}));
Condition100%3/3
$x->{'sign'} eq '+' && $LIB->_is_zero($x->{'_m'})
!ll&&!rl&&r
101537441217
MC/DC100%2/2
$x->{'sign'} eq '+' && $LIB->_is_zero($x->{'_m'})
$x->{'sign'} eq '+' $LIB->_is_zero($x->{'_m'})
Truth table100%3/3
A && B
A= $x->{'sign'} eq '+'B= $LIB->_is_zero($x->{'_m'})
ABresult
0X0
100
111
6095 5976 ▶ if ($not_zero) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_zero) 4759 1217 5976
6096 4759 $es = $LIB->_str($x->{_m});
6097 4759 $len = CORE::length($es);
6098 4759 my $e = $LIB->_num($x->{_e});
6099 4759 ▶ $e = -$e if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 1629 3130 4759
6100 4759 ▶ if ($e < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e < 0) { } 1613 3146 4759
elsif ($e > 0) { } 682 2464 3146
6101 1613 $dot = '';
6102 # if _e is bigger than a scalar, the following will blow your memory
6103 1613 ▶ if ($e <= -$len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e <= -$len) { } 602 1011 1613
6104 602 my $r = abs($e) - $len;
6105 602 $es = '0.'. ('0' x $r) . $es;
6106 602 $cad = -($len+$r);
6107 } else {
6108 1011 substr($es, $e, 0) = '.';
6109 1011 $cad = $LIB->_num($x->{_e});
6110 1011 ▶ $cad = -$cad if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 1011 0 1011
6111 }
6112 } elsif ($e > 0) {
6113 # expand with zeros
6114 682 $es .= '0' x $e;
6115 682 $len += $e;
6116 682 $cad = 0;
6117 }
6118 } # if not zero
6119  
6120 5976 ▶ $es = '-'.$es if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 1015 4961 5976
6121 # if set accuracy or precision, pad with zeros on the right side
6122 5976 ▶ if ((defined $x->{accuracy}) && ($not_zero)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $x->{'accuracy'} and $not_zero) { } 695 5281 5976
elsif (($x->{'precision'} || 0) < 0) { } 502 4779 5281
Condition100%3/3
defined $x->{"accuracy"} and $not_zero
!ll&&!rl&&r
523348695
MC/DC100%2/2
defined $x->{"accuracy"} and $not_zero
defined $x->{"accuracy"} $not_zero
Truth table100%3/3
A and B
A= defined $x->{"accuracy"}B= $not_zero
ABresult
0X0
100
111
6123 # 123400 => 6, 0.1234 => 4, 0.001234 => 4
6124 695 my $zeros = $x->{accuracy} - $cad; # cad == 0 => 12340
6125 695 ▶ $zeros = $x->{accuracy} - $len if $cad != $len;
Branch: 1 branch
BranchTrueFalseTotal
if $cad != $len 695 0 695
6126 695 ▶ $es .= $dot.'0' x $zeros if $zeros > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $zeros > 0 35 660 695
6127 ▶ } elsif ((($x->{precision} || 0) < 0)) {
Condition100%2/2
$x->{'precision'} || 0
l!l
5394742
MC/DC100%1/1
$x->{'precision'} || 0
$x->{'precision'}
Truth table100%2/2
A
A= $x->{'precision'}
Aresult
11
00
6128 # 123400 => 6, 0.1234 => 4, 0.001234 => 6
6129 502 my $zeros = -$x->{precision} + $cad;
6130 502 ▶ $es .= $dot.'0' x $zeros if $zeros > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $zeros > 0 112 390 502
6131 }
6132 5976 $es;
6133 }
6134  
6135 # Scientific notation with significand/mantissa and exponent as integers, e.g.,
6136 # "12345.6789" is written as "123456789e-4".
6137  
6138 79 ▶sub bsstr {
Subroutine bsstr: called CC=8 SCAR=21.0
Pod documented
6139 79 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 63 16 79
6140  
6141 # Inf and NaN
6142  
6143 79 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 24 55 79
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
391624
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
6144 24 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 16 8 24
6145 8 return 'inf'; # +inf
6146 }
6147  
6148 # Upgrade?
6149  
6150 55 ▶ return $x -> _upg() -> bsstr(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 55 55
Condition33%1/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
5500
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6151 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6152  
6153 # Round according to arguments or global settings, if any.
6154  
6155 55 $x = $x -> copy() -> round(@r);
6156  
6157 # Finite number
6158  
6159 ($x->{sign} eq '-' ? '-' : '') . $LIB->_str($x->{_m})
6160 55 ▶ . 'e' . $x->{_es} . $LIB->_str($x->{_e});
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 16 39 55
6161 }
6162  
6163 # Normalized notation, e.g., "12345.6789" is written as "1.23456789e+4".
6164  
6165 538 ▶sub bnstr {
Subroutine bnstr: called CC=9 SCAR=24.2
Pod documented
6166 538 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 538 0 538
6167  
6168 # Inf and NaN
6169  
6170 538 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 0 538 538
Condition66%2/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
528100
MC/DC0%0/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table66%2/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
6171 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
6172 0 return 'inf'; # +inf
6173 }
6174  
6175 # Upgrade?
6176  
6177 538 ▶ return $x -> _upg() -> bnstr(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 538 538
Condition33%1/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
53800
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6178 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6179  
6180 # Finite number
6181  
6182 538 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 10 528 538
6183  
6184 # Round according to arguments or global settings, if any.
6185  
6186 538 $x = $x -> copy() -> round(@r);
6187  
6188 # Get the mantissa and the length of the mantissa.
6189  
6190 538 my $mant = $LIB->_str($x->{_m});
6191 538 my $mantlen = CORE::length($mant);
6192  
6193 538 ▶ if ($mantlen == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mantlen == 1) { } 89 449 538
6194  
6195 # Not decimal point when the mantissa has length one, i.e., return the
6196 # number 2 as the string "2", not "2.".
6197  
6198 89 $str .= $mant . 'e' . $x->{_es} . $LIB->_str($x->{_e});
6199  
6200 } else {
6201  
6202 # Compute new exponent where the original exponent is adjusted by the
6203 # length of the mantissa minus one (because the decimal point is after
6204 # one digit).
6205  
6206 my ($eabs, $esgn) = $LIB -> _sadd($LIB -> _copy($x->{_e}), $x->{_es},
6207 449 $LIB -> _new($mantlen - 1), "+");
6208 449 substr $mant, 1, 0, ".";
6209 449 $str .= $mant . 'e' . $esgn . $LIB->_str($eabs);
6210  
6211 }
6212  
6213 538 return $str;
6214 }
6215  
6216 # Engineering notation, e.g., "12345.6789" is written as "12.3456789e+3".
6217  
6218 0 ▶sub bestr {
Subroutine bestr: not called CC=12 SCAR=50.5
Pod documented
6219 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
6220  
6221 # Inf and NaN
6222  
6223 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
6224 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
6225 0 return 'inf'; # +inf
6226 }
6227  
6228 # Upgrade?
6229  
6230 0 ▶ return $x -> _upg() -> bestr(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6231 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6232  
6233 # Round according to arguments or global settings, if any.
6234  
6235 0 $x = $x -> copy() -> round(@r);
6236  
6237 # Finite number
6238  
6239 0 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
6240  
6241 # Get the mantissa, the length of the mantissa, and adjust the exponent by
6242 # the length of the mantissa minus 1 (because the dot is after one digit).
6243  
6244 0 my $mant = $LIB->_str($x->{_m});
6245 0 my $mantlen = CORE::length($mant);
6246 my ($eabs, $esgn) = $LIB -> _sadd($LIB -> _copy($x->{_e}), $x->{_es},
6247 0 $LIB -> _new($mantlen - 1), "+");
6248  
6249 0 my $dotpos = 1;
6250 0 my $mod = $LIB -> _mod($LIB -> _copy($eabs), $LIB -> _new("3"));
6251 0 ▶ unless ($LIB -> _is_zero($mod)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($mod)) 0 0 0
6252 0 ▶ if ($esgn eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($esgn eq '+') { } 0 0 0
6253 0 $eabs = $LIB -> _sub($eabs, $mod);
6254 0 $dotpos += $LIB -> _num($mod);
6255 } else {
6256 0 my $delta = $LIB -> _sub($LIB -> _new("3"), $mod);
6257 0 $eabs = $LIB -> _add($eabs, $delta);
6258 0 $dotpos += $LIB -> _num($delta);
6259 }
6260 }
6261  
6262 0 ▶ if ($dotpos < $mantlen) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dotpos < $mantlen) { } 0 0 0
elsif ($dotpos > $mantlen) { } 0 0 0
6263 0 substr $mant, $dotpos, 0, ".";
6264 } elsif ($dotpos > $mantlen) {
6265 0 $mant .= "0" x ($dotpos - $mantlen);
6266 }
6267  
6268 0 $str .= $mant . 'e' . $esgn . $LIB->_str($eabs);
6269  
6270 0 return $str;
6271 }
6272  
6273 # Decimal notation, e.g., "12345.6789" (no exponent).
6274  
6275 786 ▶sub bdstr {
Subroutine bdstr: called CC=12 SCAR=27.4
Pod documented
6276 786 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 786 0 786
6277  
6278 # Inf and NaN
6279  
6280 786 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 0 786 786
Condition66%2/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
6521340
MC/DC0%0/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table66%2/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
6281 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
6282 0 return 'inf'; # +inf
6283 }
6284  
6285 # Upgrade?
6286  
6287 786 ▶ return $x -> _upg() -> bdstr(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 786 786
Condition33%1/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
78600
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6288 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6289  
6290 # Round according to arguments or global settings, if any.
6291  
6292 786 $x = $x -> copy() -> round(@r);
6293  
6294 # Finite number
6295  
6296 786 my $mant = $LIB->_str($x->{_m});
6297 786 my $esgn = $x->{_es};
6298 786 my $eabs = $LIB -> _num($x->{_e});
6299  
6300 786 my $uintmax = ~0;
6301  
6302 786 my $str = $mant;
6303 786 ▶ if ($esgn eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($esgn eq '+') { } 784 2 786
6304  
6305 784 ▶ croak("The absolute value of the exponent is too large")
Branch: 1 branch
BranchTrueFalseTotal
if $eabs > $uintmax 0 784 784
6306 if $eabs > $uintmax;
6307  
6308 784 $str .= "0" x $eabs;
6309  
6310 } else {
6311 2 my $mlen = CORE::length($mant);
6312 2 my $c = $mlen - $eabs;
6313  
6314 2 my $intmax = ($uintmax - 1) / 2;
6315 2 ▶ croak("The absolute value of the exponent is too large")
Branch: 1 branch
BranchTrueFalseTotal
if 1 - $c > $intmax 0 2 2
6316 if (1 - $c) > $intmax;
6317  
6318 2 ▶ $str = "0" x (1 - $c) . $str if $c <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c <= 0 2 0 2
6319 2 substr($str, -$eabs, 0) = '.';
6320 }
6321  
6322 786 ▶ return $x->{sign} eq '-' ? '-' . $str : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 134 652 786
6323 }
6324  
6325 # Fractional notation, e.g., "123.4375" is written as "1975/16".
6326  
6327 1150 ▶sub bfstr {
Subroutine bfstr: called CC=11 SCAR=26.1
Pod documented
6328 1150 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1150 0 1150
6329  
6330 1150 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 1150 1150
6331  
6332 # Inf and NaN
6333  
6334 1150 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 0 1150 1150
Condition66%2/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
10501000
MC/DC0%0/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table66%2/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
6335 0 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 0 0 0
6336 0 return 'inf'; # +inf
6337 }
6338  
6339 # Upgrade?
6340  
6341 1150 ▶ return $x -> _upg() -> bfstr(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 1150 1150
Condition66%2/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
114820
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table66%2/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6342 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6343  
6344 # Finite number
6345  
6346 1150 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 100 1050 1150
6347  
6348 1150 ▶ if ($x->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'_es'} eq '+') { } 120 1030 1150
6349 120 $str .= $LIB -> _str($x->{_m}) . ("0" x $LIB -> _num($x->{_e}));
6350 } else {
6351 1030 my @flt_parts = ($x->{sign}, $x->{_m}, $x->{_es}, $x->{_e});
6352 1030 my @rat_parts = $class -> _flt_lib_parts_to_rat_lib_parts(@flt_parts);
6353 1030 $str = $LIB -> _str($rat_parts[1]) . "/" . $LIB -> _str($rat_parts[2]);
6354 1030 ▶ $str = "-" . $str if $rat_parts[0] eq "-";
Branch: 1 branch
BranchTrueFalseTotal
if $rat_parts[0] eq "-" 68 962 1030
6355 }
6356  
6357 1150 return $str;
6358 }
6359  
6360 36 ▶sub to_hex {
Subroutine to_hex: called CC=12 SCAR=26.0
Pod documented
6361 # return number as hexadecimal string (only for integers defined)
6362 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
6363  
6364 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
6365  
6366 # Inf and NaN
6367  
6368 36 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 12 24 36
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
20412
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
6369 12 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 8 4 12
6370 4 return 'inf'; # +inf
6371 }
6372  
6373 # Upgrade?
6374  
6375 24 ▶ return $x -> _upg() -> to_hex(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 24 24
Condition33%1/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2400
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6376 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6377  
6378 # Finite number
6379  
6380 24 ▶ return '0' if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 16 24
6381  
6382 16 ▶ return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex?
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} ne "+" 0 16 16
6383  
6384 16 my $z = $LIB->_copy($x->{_m});
6385 16 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 16 16
6386 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
6387 }
6388 16 my $str = $LIB->_to_hex($z);
6389 16 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 12 16
6390 }
6391  
6392 40 ▶sub to_oct {
Subroutine to_oct: called CC=12 SCAR=26.0
Pod documented
6393 # return number as octal digit string (only for integers defined)
6394 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
6395  
6396 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
6397  
6398 # Inf and NaN
6399  
6400 40 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 12 28 40
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
24412
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
6401 12 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 8 4 12
6402 4 return 'inf'; # +inf
6403 }
6404  
6405 # Upgrade?
6406  
6407 28 ▶ return $x -> _upg() -> to_oct(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 28 28
Condition33%1/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2800
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6408 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6409  
6410 # Finite number
6411  
6412 28 ▶ return '0' if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 20 28
6413  
6414 20 ▶ return $nan if $x->{_es} ne '+'; # how to do 1e-1 in octal?
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} ne "+" 0 20 20
6415  
6416 20 my $z = $LIB->_copy($x->{_m});
6417 20 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 20 20
6418 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
6419 }
6420 20 my $str = $LIB->_to_oct($z);
6421 20 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 16 20
6422 }
6423  
6424 40 ▶sub to_bin {
Subroutine to_bin: called CC=12 SCAR=26.0
Pod documented
6425 # return number as binary digit string (only for integers defined)
6426 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
6427  
6428 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
6429  
6430 # Inf and NaN
6431  
6432 40 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 12 28 40
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
24412
MC/DC100%2/2
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
$x->{"sign"} ne "+" $x->{"sign"} ne "-"
Truth table100%3/3
A and B
A= $x->{"sign"} ne "+"B= $x->{"sign"} ne "-"
ABresult
0X0
100
111
6433 12 ▶ return $x->{sign} unless $x -> is_inf("+"); # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_inf("+") 8 4 12
6434 4 return 'inf'; # +inf
6435 }
6436  
6437 # Upgrade?
6438  
6439 28 ▶ return $x -> _upg() -> to_bin(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 28 28
Condition33%1/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2800
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6440 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6441  
6442 # Finite number
6443  
6444 28 ▶ return '0' if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 20 28
6445  
6446 20 ▶ return $nan if $x->{_es} ne '+'; # how to do 1e-1 in binary?
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} ne "+" 0 20 20
6447  
6448 20 my $z = $LIB->_copy($x->{_m});
6449 20 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 20 20
6450 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
6451 }
6452 20 my $str = $LIB->_to_bin($z);
6453 20 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 16 20
6454 }
6455  
6456 0 ▶sub to_bytes {
Subroutine to_bytes: not called CC=8 SCAR=42.8
Pod documented
6457 # return a byte string
6458  
6459 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
6460  
6461 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
6462  
6463 0 ▶ croak("to_bytes() requires a finite, non-negative integer")
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg or not $x->is_int 0 0 0
Condition0%0/3
$x->is_neg or not $x->is_int
l!l&&r!l&&!r
000
MC/DC0%0/2
$x->is_neg or not $x->is_int
$x->is_neg not $x->is_int
Truth table0%0/3
A or not(B)
A= $x->is_negB= not $x->is_int
ABresult
1X1
011
000
6464 if $x -> is_neg() || ! $x -> is_int();
6465  
6466 0 ▶ return $x -> _upg() -> to_bytes(@r)
Branch: 1 branch
BranchTrueFalseTotal
if $class->upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
$class->upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
$class->upgrade and not $x->isa(__PACKAGE__)
$class->upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= $class->upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
6467 if $class -> upgrade() && !$x -> isa(__PACKAGE__);
6468  
6469 0 ▶ croak("to_bytes() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_to_bytes") 0 0 0
6470 unless $LIB -> can('_to_bytes');
6471  
6472 0 return $LIB->_to_bytes($LIB -> _lsft($x->{_m}, $x->{_e}, 10));
6473 }
6474  
6475 0 ▶sub to_ieee754 {
Subroutine to_ieee754: not called CC=35 SCAR=71.4
Pod documented
6476 0 ▶ my ($class, $x, $format, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
6477  
6478 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
6479  
6480 0 my $enc; # significand encoding (applies only to decimal)
6481 my $k; # storage width in bits
6482 0 my $b; # base
6483  
6484 0 ▶ if ($format =~ /^binary(\d+)\z/) {
Branch: 8 branches
BranchTrueFalseTotal
if ($format =~ /^binary(\d+)\z/) { } 0 0 0
elsif ($format =~ /^decimal(\d+)(dpd|bcd)?\z/) { } 0 0 0
elsif ($format eq 'half') { } 0 0 0
elsif ($format eq 'single') { } 0 0 0
elsif ($format eq 'double') { } 0 0 0
elsif ($format eq 'quadruple') { } 0 0 0
elsif ($format eq 'octuple') { } 0 0 0
elsif ($format eq 'sexdecuple') { } 0 0 0
6485 0 $k = $1;
6486 0 $b = 2;
6487 } elsif ($format =~ /^decimal(\d+)(dpd|bcd)?\z/) {
6488 0 $k = $1;
6489 0 $b = 10;
6490 0 ▶ $enc = $2 || 'dpd'; # default is dencely-packed decimals (DPD)
Condition0%0/2
$2 || 'dpd'
l!l
00
MC/DC0%0/1
$2 || 'dpd'
$2
Truth table0%0/2
A
A= $2
Aresult
11
00
6491 } elsif ($format eq 'half') {
6492 0 $k = 16;
6493 0 $b = 2;
6494 } elsif ($format eq 'single') {
6495 0 $k = 32;
6496 0 $b = 2;
6497 } elsif ($format eq 'double') {
6498 0 $k = 64;
6499 0 $b = 2;
6500 } elsif ($format eq 'quadruple') {
6501 0 $k = 128;
6502 0 $b = 2;
6503 } elsif ($format eq 'octuple') {
6504 0 $k = 256;
6505 0 $b = 2;
6506 } elsif ($format eq 'sexdecuple') {
6507 0 $k = 512;
6508 0 $b = 2;
6509 }
6510  
6511 0 ▶ if ($b == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 2) 0 0 0
6512  
6513 # Get the parameters for this format.
6514  
6515 0 my $p; # precision (in bits)
6516 my $t; # number of bits in significand
6517 0 my $w; # number of bits in exponent
6518  
6519 0 ▶ if ($k == 16) { # binary16 (half-precision)
Branch: 3 branches
BranchTrueFalseTotal
if ($k == 16) { } 0 0 0
elsif ($k == 32) { } 0 0 0
elsif ($k == 64) { } 0 0 0
6520 0 $p = 11;
6521 0 $t = 10;
6522 0 $w = 5;
6523 } elsif ($k == 32) { # binary32 (single-precision)
6524 0 $p = 24;
6525 0 $t = 23;
6526 0 $w = 8;
6527 } elsif ($k == 64) { # binary64 (double-precision)
6528 0 $p = 53;
6529 0 $t = 52;
6530 0 $w = 11;
6531 } else { # binaryN (quadruple-precition and above)
6532 0 ▶ if ($k < 128 || $k != 32 * sprintf('%.0f', $k / 32)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < 128 or $k != 32 * sprintf("%.0f", $k / 32)) 0 0 0
Condition0%0/3
$k < 128 or $k != 32 * sprintf("%.0f", $k / 32)
l!l&&r!l&&!r
000
MC/DC0%0/2
$k < 128 or $k != 32 * sprintf("%.0f", $k / 32)
$k < 128 $k != 32 * sprintf("%.0f", $k / 32)
Truth table0%0/3
A or B
A= $k < 128B= $k != 32 * sprintf("%.0f", $k / 32)
ABresult
1X1
011
000
6533 0 croak "Number of bits must be 16, 32, 64, or >= 128 and",
6534 " a multiple of 32";
6535 }
6536 0 $p = $k - sprintf('%.0f', 4 * log($k) / log(2)) + 13;
6537 0 $t = $p - 1;
6538 0 $w = $k - $t - 1;
6539 }
6540  
6541 # The maximum exponent, minimum exponent, and exponent bias.
6542  
6543 0 my $emax = $class -> new(2) -> bpow($w - 1) -> bdec();
6544 0 my $emin = 1 - $emax;
6545 0 my $bias = $emax;
6546  
6547 # Get numerical sign, exponent, and mantissa/significand for bit
6548 # string.
6549  
6550 0 my $sign = 0;
6551 0 my $expo;
6552 my $mant;
6553  
6554 0 ▶ if ($x -> is_nan()) { # nan
Branch: 3 branches
BranchTrueFalseTotal
if ($x->is_nan) { } 0 0 0
elsif ($x->is_inf) { } 0 0 0
elsif ($x->is_zero) { } 0 0 0
6555 0 $sign = 1;
6556 0 $expo = $emax -> copy() -> binc();
6557 0 $mant = $class -> new(2) -> bpow($t - 1);
6558 } elsif ($x -> is_inf()) { # inf
6559 0 ▶ $sign = 1 if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
6560 0 $expo = $emax -> copy() -> binc();
6561 0 $mant = $class -> bzero();
6562 } elsif ($x -> is_zero()) { # zero
6563 0 $expo = $emin -> copy() -> bdec();
6564 0 $mant = $class -> bzero();
6565 } else { # normal and subnormal
6566  
6567 0 ▶ $sign = 1 if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
6568  
6569 # Now we need to compute the mantissa and exponent in base $b.
6570  
6571 0 my $binv = $class -> new("0.5");
6572 0 my $b = $class -> new(2);
6573 0 my $one = $class -> bone();
6574  
6575 # We start off by initializing the exponent to zero and the
6576 # mantissa to the input value. Then we increase the mantissa and
6577 # decrease the exponent, or vice versa, until the mantissa is in
6578 # the desired range or we hit one of the limits for the exponent.
6579  
6580 0 $mant = $x -> copy() -> babs();
6581  
6582 # We need to find the base 2 exponent. First make an estimate of
6583 # the base 2 exponent, before adjusting it below. We could skip
6584 # this estimation and go straight to the while-loops below, but the
6585 # loops are slow, especially when the final exponent is far from
6586 # zero and even more so if the number of digits is large. This
6587 # initial estimation speeds up the computation dramatically.
6588 #
6589 # log2($m * 10**$e) = log10($m + 10**$e) * log(10)/log(2)
6590 # = (log10($m) + $e) * log(10)/log(2)
6591 # = (log($m)/log(10) + $e) * log(10)/log(2)
6592  
6593 0 my ($m, $e) = $x -> nparts();
6594 0 my $ms = $m -> numify();
6595 0 my $es = $e -> numify();
6596  
6597 0 my $expo_est = (log(abs($ms))/log(10) + $es) * log(10)/log(2);
6598 0 $expo_est = int($expo_est);
6599  
6600 # Limit the exponent.
6601  
6602 0 ▶ if ($expo_est > $emax) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo_est > $emax) { } 0 0 0
elsif ($expo_est < $emin) { } 0 0 0
6603 0 $expo_est = $emax;
6604 } elsif ($expo_est < $emin) {
6605 0 $expo_est = $emin;
6606 }
6607  
6608 # Don't multiply by a number raised to a negative exponent. This
6609 # will cause a division, whose result is truncated to some fixed
6610 # number of digits. Instead, multiply by the inverse number raised
6611 # to a positive exponent.
6612  
6613 0 $expo = $class -> new($expo_est);
6614 0 ▶ if ($expo_est > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo_est > 0) { } 0 0 0
elsif ($expo_est < 0) { } 0 0 0
6615 0 $mant = $mant -> bmul($binv -> copy() -> bpow($expo));
6616 } elsif ($expo_est < 0) {
6617 0 my $expo_abs = $expo -> copy() -> bneg();
6618 0 $mant = $mant -> bmul($b -> copy() -> bpow($expo_abs));
6619 }
6620  
6621 # Final adjustment of the estimate above.
6622  
6623 0 ▶ while ($mant >= $b && $expo <= $emax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant >= $b and $expo <= $emax) 0 0 0
Condition0%0/3
$mant >= $b and $expo <= $emax
!ll&&!rl&&r
000
MC/DC0%0/2
$mant >= $b and $expo <= $emax
$mant >= $b $expo <= $emax
Truth table0%0/3
A and B
A= $mant >= $bB= $expo <= $emax
ABresult
0X0
100
111
6624 0 $mant = $mant -> bmul($binv);
6625 0 $expo = $expo -> binc();
6626 }
6627  
6628 0 ▶ while ($mant < $one && $expo >= $emin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant < $one and $expo >= $emin) 0 0 0
Condition0%0/3
$mant < $one and $expo >= $emin
!ll&&!rl&&r
000
MC/DC0%0/2
$mant < $one and $expo >= $emin
$mant < $one $expo >= $emin
Truth table0%0/3
A and B
A= $mant < $oneB= $expo >= $emin
ABresult
0X0
100
111
6629 0 $mant = $mant -> bmul($b);
6630 0 $expo = $expo -> bdec();
6631 }
6632  
6633 # This is when the magnitude is larger than what can be represented
6634 # in this format. Encode as infinity.
6635  
6636 0 ▶ if ($expo > $emax) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo > $emax) { } 0 0 0
elsif ($expo < $emin) { } 0 0 0
6637 0 $mant = $class -> bzero();
6638 0 $expo = $emax -> copy() -> binc();
6639 }
6640  
6641 # This is when the magnitude is so small that the number is encoded
6642 # as a subnormal number.
6643 #
6644 # If the magnitude is smaller than that of the smallest subnormal
6645 # number, and rounded downwards, it is encoded as zero. This works
6646 # transparently and does not need to be treated as a special case.
6647 #
6648 # If the number is between the largest subnormal number and the
6649 # smallest normal number, and the value is rounded upwards, the
6650 # value must be encoded as a normal number. This must be treated as
6651 # a special case.
6652  
6653 elsif ($expo < $emin) {
6654  
6655 # Scale up the mantissa (significand), and round to integer.
6656  
6657 0 my $const = $class -> new($b) -> bpow($t - 1);
6658 0 $mant = $mant -> bmul($const);
6659 0 $mant = $mant -> bfround(0);
6660  
6661 # If the mantissa overflowed, encode as the smallest normal
6662 # number.
6663  
6664 0 ▶ if ($mant == $const -> bmul($b)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == $const->bmul($b)) 0 0 0
6665 0 $mant = $mant -> bzero();
6666 0 $expo = $expo -> binc();
6667 }
6668 }
6669  
6670 # This is when the magnitude is within the range of what can be
6671 # encoded as a normal number.
6672  
6673 else {
6674  
6675 # Remove implicit leading bit, scale up the mantissa
6676 # (significand) to an integer, and round.
6677  
6678 0 $mant = $mant -> bdec();
6679 0 my $const = $class -> new($b) -> bpow($t);
6680 0 $mant = $mant -> bmul($const) -> bfround(0);
6681  
6682 # If the mantissa overflowed, encode as the next larger value.
6683 # This works correctly also when the next larger value is
6684 # infinity.
6685  
6686 0 ▶ if ($mant == $const) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == $const) 0 0 0
6687 0 $mant = $mant -> bzero();
6688 0 $expo = $expo -> binc();
6689 }
6690 }
6691 }
6692  
6693 0 $expo = $expo -> badd($bias); # add bias
6694  
6695 0 my $signbit = "$sign";
6696  
6697 0 my $mantbits = $mant -> to_bin();
6698 0 $mantbits = ("0" x ($t - CORE::length($mantbits))) . $mantbits;
6699  
6700 0 my $expobits = $expo -> to_bin();
6701 0 $expobits = ("0" x ($w - CORE::length($expobits))) . $expobits;
6702  
6703 0 my $bin = $signbit . $expobits . $mantbits;
6704 0 return pack "B*", $bin;
6705 }
6706  
6707 0 croak("The format '$format' is not yet supported.");
6708 }
6709  
6710 0 ▶sub to_fp80 {
Subroutine to_fp80: not called CC=21 SCAR=61.4
Pod documented
6711 0 ▶ my ($class, $x, $format, @r) = ref($_[0]) ? (ref($_[0]), @_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
6712 : objectify(1, @_);
6713  
6714 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
6715  
6716 # The maximum exponent, minimum exponent, and exponent bias.
6717  
6718 0 my $emax = Math::BigFloat -> new("16383");
6719 0 my $emin = 1 - $emax;
6720 0 my $bias = $emax;
6721  
6722 # Get numerical sign, exponent, and mantissa/significand for bit string.
6723  
6724 0 my $sign = 0;
6725 0 my $expo;
6726 my $mant;
6727  
6728 0 ▶ if ($x -> is_nan()) { # nan
Branch: 3 branches
BranchTrueFalseTotal
if ($x->is_nan) { } 0 0 0
elsif ($x->is_inf) { } 0 0 0
elsif ($x->is_zero) { } 0 0 0
6729 0 $sign = 1;
6730 0 $expo = $emax -> copy() -> binc();
6731 0 $mant = $class -> new(2) -> bpow(64) -> bdec();
6732  
6733 } elsif ($x -> is_inf()) { # inf
6734 0 ▶ $sign = 1 if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
6735 0 $expo = $emax -> copy() -> binc();
6736 0 $mant = $class -> bzero();
6737  
6738 } elsif ($x -> is_zero()) { # zero
6739 0 $expo = $emin -> copy() -> bdec();
6740 0 $mant = $class -> bzero();
6741  
6742 } else { # normal and subnormal
6743  
6744 0 ▶ $sign = 1 if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
6745  
6746 # Now we need to compute the mantissa and exponent in base $b.
6747  
6748 0 my $binv = $class -> new("0.5");
6749 0 my $b = $class -> new("2");
6750 0 my $one = $class -> bone();
6751  
6752 # We start off by initializing the exponent to zero and the
6753 # mantissa to the input value. Then we increase the mantissa and
6754 # decrease the exponent, or vice versa, until the mantissa is in
6755 # the desired range or we hit one of the limits for the exponent.
6756  
6757 0 $mant = $x -> copy() -> babs();
6758  
6759 # We need to find the base 2 exponent. First make an estimate of
6760 # the base 2 exponent, before adjusting it below. We could skip
6761 # this estimation and go straight to the while-loops below, but the
6762 # loops are slow, especially when the final exponent is far from
6763 # zero and even more so if the number of digits is large. This
6764 # initial estimation speeds up the computation dramatically.
6765 #
6766 # log2($m * 10**$e) = log10($m + 10**$e) * log(10)/log(2)
6767 # = (log10($m) + $e) * log(10)/log(2)
6768 # = (log($m)/log(10) + $e) * log(10)/log(2)
6769  
6770 0 my ($m, $e) = $x -> nparts();
6771 0 my $ms = $m -> numify();
6772 0 my $es = $e -> numify();
6773  
6774 0 my $expo_est = (log(abs($ms))/log(10) + $es) * log(10)/log(2);
6775 0 $expo_est = int($expo_est);
6776  
6777 # Limit the exponent.
6778  
6779 0 ▶ if ($expo_est > $emax) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo_est > $emax) { } 0 0 0
elsif ($expo_est < $emin) { } 0 0 0
6780 0 $expo_est = $emax;
6781 } elsif ($expo_est < $emin) {
6782 0 $expo_est = $emin;
6783 }
6784  
6785 # Don't multiply by a number raised to a negative exponent. This
6786 # will cause a division, whose result is truncated to some fixed
6787 # number of digits. Instead, multiply by the inverse number raised
6788 # to a positive exponent.
6789  
6790 0 $expo = $class -> new($expo_est);
6791 0 ▶ if ($expo_est > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo_est > 0) { } 0 0 0
elsif ($expo_est < 0) { } 0 0 0
6792 0 $mant = $mant -> bmul($binv -> copy() -> bpow($expo));
6793 } elsif ($expo_est < 0) {
6794 0 my $expo_abs = $expo -> copy() -> bneg();
6795 0 $mant = $mant -> bmul($b -> copy() -> bpow($expo_abs));
6796 }
6797  
6798 # Final adjustment of the estimate above.
6799  
6800 0 ▶ while ($mant >= $b && $expo <= $emax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant >= $b and $expo <= $emax) 0 0 0
Condition0%0/3
$mant >= $b and $expo <= $emax
!ll&&!rl&&r
000
MC/DC0%0/2
$mant >= $b and $expo <= $emax
$mant >= $b $expo <= $emax
Truth table0%0/3
A and B
A= $mant >= $bB= $expo <= $emax
ABresult
0X0
100
111
6801 0 $mant = $mant -> bmul($binv);
6802 0 $expo = $expo -> binc();
6803 }
6804  
6805 0 ▶ while ($mant < $one && $expo >= $emin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant < $one and $expo >= $emin) 0 0 0
Condition0%0/3
$mant < $one and $expo >= $emin
!ll&&!rl&&r
000
MC/DC0%0/2
$mant < $one and $expo >= $emin
$mant < $one $expo >= $emin
Truth table0%0/3
A and B
A= $mant < $oneB= $expo >= $emin
ABresult
0X0
100
111
6806 0 $mant = $mant -> bmul($b);
6807 0 $expo = $expo -> bdec();
6808 }
6809  
6810 # This is when the magnitude is larger than what can be represented in
6811 # this format. Encode as infinity.
6812  
6813 0 ▶ if ($expo > $emax) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo > $emax) { } 0 0 0
elsif ($expo < $emin) { } 0 0 0
6814 0 $mant = $class -> bzero();
6815 0 $expo = $emax -> copy() -> binc();
6816 }
6817  
6818 # This is when the magnitude is so small that the number is encoded as
6819 # a subnormal number.
6820 #
6821 # If the magnitude is smaller than that of the smallest subnormal
6822 # number, and rounded downwards, it is encoded as zero. This works
6823 # transparently and does not need to be treated as a special case.
6824 #
6825 # If the number is between the largest subnormal number and the
6826 # smallest normal number, and the value is rounded upwards, the value
6827 # must be encoded as a normal number. This must be treated as a special
6828 # case.
6829  
6830 elsif ($expo < $emin) {
6831  
6832 # Scale up the mantissa (significand), and round to integer.
6833  
6834 0 my $const = $class -> new($b) -> bpow(62);
6835 0 $mant -> bmul($const) -> bfround(0);
6836  
6837 # If the mantissa overflowed, encode as the smallest normal number.
6838  
6839 0 ▶ if ($mant == $const -> bmul($b)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == $const->bmul($b)) 0 0 0
6840 0 $expo -> binc();
6841 }
6842 }
6843  
6844 # This is when the magnitude is within the range of what can be encoded
6845 # as a normal number.
6846  
6847 else {
6848  
6849 # Remove implicit leading bit, scale up the mantissa (significand)
6850 # to an integer, and round.
6851  
6852 0 my $const = $class -> new($b) -> bpow(63);
6853 0 $mant -> bmul($const) -> bfround(0);
6854  
6855 # If the mantissa overflowed, encode as the next larger value. If
6856 # this caused the exponent to overflow, encode as infinity.
6857  
6858 0 ▶ if ($mant == $const -> copy() -> bmul($b)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == $const->copy->bmul($b)) 0 0 0
6859 0 $expo -> binc();
6860 0 ▶ if ($expo > $emax) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expo > $emax) { } 0 0 0
6861 0 $mant = $class -> bzero();
6862 } else {
6863 0 $mant = $const;
6864 }
6865 }
6866 }
6867 }
6868  
6869 0 $expo = $expo -> badd($bias); # add bias
6870  
6871 0 my $signbit = "$sign";
6872  
6873 0 my $mantbits = $mant -> to_bin();
6874 0 $mantbits = ("0" x (64 - CORE::length($mantbits))) . $mantbits;
6875  
6876 0 my $expobits = $expo -> to_bin();
6877 0 $expobits = ("0" x (15 - CORE::length($expobits))) . $expobits;
6878  
6879 0 my $bin = $signbit . $expobits . $mantbits;
6880 0 return pack "B*", $bin;
6881 }
6882  
6883 36 ▶sub as_hex {
Subroutine as_hex: called CC=8 SCAR=21.5
Pod documented
6884 # return number as hexadecimal string (only for integers defined)
6885  
6886 36 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
6887  
6888 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
6889  
6890 36 ▶ return $x -> bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 24 36
6891 24 ▶ return '0x0' if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 16 24
6892  
6893 16 ▶ return $nan if $x->{_es} ne '+'; # how to do 1e-1 in hex?
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} ne "+" 0 16 16
6894  
6895 16 my $z = $LIB->_copy($x->{_m});
6896 16 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 16 16
6897 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
6898 }
6899 16 my $str = $LIB->_as_hex($z);
6900 16 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 12 16
6901 }
6902  
6903 40 ▶sub as_oct {
Subroutine as_oct: called CC=8 SCAR=21.5
Pod documented
6904 # return number as octal digit string (only for integers defined)
6905  
6906 40 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
6907  
6908 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
6909  
6910 40 ▶ return $x -> bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 28 40
6911 28 ▶ return '00' if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 20 28
6912  
6913 20 ▶ return $nan if $x->{_es} ne '+'; # how to do 1e-1 in octal?
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} ne "+" 0 20 20
6914  
6915 20 my $z = $LIB->_copy($x->{_m});
6916 20 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 20 20
6917 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
6918 }
6919 20 my $str = $LIB->_as_oct($z);
6920 20 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 16 20
6921 }
6922  
6923 40 ▶sub as_bin {
Subroutine as_bin: called CC=8 SCAR=21.5
Pod documented
6924 # return number as binary digit string (only for integers defined)
6925  
6926 40 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
6927  
6928 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
6929  
6930 40 ▶ return $x -> bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 28 40
6931 28 ▶ return '0b0' if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 20 28
6932  
6933 20 ▶ return $nan if $x->{_es} ne '+'; # how to do 1e-1 in binary?
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} ne "+" 0 20 20
6934  
6935 20 my $z = $LIB->_copy($x->{_m});
6936 20 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 20 20
6937 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
6938 }
6939 20 my $str = $LIB->_as_bin($z);
6940 20 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 16 20
6941 }
6942  
6943 538 ▶sub numify {
Subroutine numify: called CC=6 SCAR=25.4
Pod documented
6944 # Make a Perl scalar number from a Math::BigFloat object.
6945  
6946 538 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 538 0 538
6947  
6948 538 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 538 538
6949  
6950 538 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 538 538
6951 0 require Math::Complex;
6952 0 my $inf = $Math::Complex::Inf;
6953 0 return $inf - $inf;
6954 }
6955  
6956 538 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 538 538
6957 0 require Math::Complex;
6958 0 my $inf = $Math::Complex::Inf;
6959 0 ▶ return $x -> is_negative() ? -$inf : $inf;
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative ? : 0 0 0
6960 }
6961  
6962 # Create a string and let Perl's atoi()/atof() handle the rest.
6963  
6964 538 return 0 + $x -> bnstr();
6965 }
6966  
6967 ###############################################################################
6968 # Private methods and functions.
6969 ###############################################################################
6970  
6971 61 ▶sub import {
Subroutine import: called CC=13 SCAR=30.5
6972 61 my $class = shift;
6973 61 $IMPORT++; # remember we did import()
6974 61 my @a; # unrecognized arguments
6975  
6976 61 my @import = ();
6977  
6978 61 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 11 61 72
6979 11 my $param = shift;
6980  
6981 # Enable overloading of constants.
6982  
6983 11 ▶ if ($param eq ':constant') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq ":constant") 0 11 11
6984 overload::constant
6985  
6986 0 ▶ integer => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
6987 0 $class -> new(shift);
6988 },
6989  
6990 0 ▶ float => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
6991 0 $class -> new(shift);
6992 },
6993  
6994 0 ▶ binary => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
6995 # E.g., a literal 0377 shall result in an object whose
6996 # value is decimal 255, but new("0377") returns decimal
6997 # 377.
6998 0 ▶ return $class -> from_oct($_[0]) if $_[0] =~ /^0_*[0-7]/;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^0_*[0-7]/ 0 0 0
6999 0 $class -> new(shift);
7000 0 };
7001 0 next;
7002 }
7003  
7004 # Upgrading.
7005  
7006 11 ▶ if ($param eq 'upgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "upgrade") 2 9 11
7007 2 $class -> upgrade(shift);
7008 2 next;
7009 }
7010  
7011 # Downgrading.
7012  
7013 9 ▶ if ($param eq 'downgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "downgrade") 1 8 9
7014 1 $class -> downgrade(shift);
7015 1 next;
7016 }
7017  
7018 # Accuracy.
7019  
7020 8 ▶ if ($param eq 'accuracy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "accuracy") 0 8 8
7021 0 $class -> accuracy(shift);
7022 0 next;
7023 }
7024  
7025 # Precision.
7026  
7027 8 ▶ if ($param eq 'precision') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "precision") 0 8 8
7028 0 $class -> precision(shift);
7029 0 next;
7030 }
7031  
7032 # Rounding mode.
7033  
7034 8 ▶ if ($param eq 'round_mode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "round_mode") 0 8 8
7035 0 $class -> round_mode(shift);
7036 0 next;
7037 }
7038  
7039 # Fall-back accuracy.
7040  
7041 8 ▶ if ($param eq 'div_scale') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "div_scale") 0 8 8
7042 0 $class -> div_scale(shift);
7043 0 next;
7044 }
7045  
7046 # Backend library.
7047  
7048 8 ▶ if ($param =~ /^(lib|try|only)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(lib|try|only)\z/) 6 2 8
7049 6 push @import, $param;
7050 6 ▶ push @import, shift() if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 6 0 6
7051 6 next;
7052 }
7053  
7054 2 ▶ if ($param eq 'with') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "with") 2 0 2
7055 # alternative class for our private parts()
7056 # XXX: no longer supported
7057 # $LIB = shift() || 'Calc';
7058 # carp "'with' is no longer supported, use 'lib', 'try', or 'only'";
7059 2 shift;
7060 2 next;
7061 }
7062  
7063 # Unrecognized parameter.
7064  
7065 0 push @a, $param;
7066 }
7067  
7068 61 Math::BigInt -> import(@import);
7069  
7070 # find out which library was actually loaded
7071 61 $LIB = Math::BigInt -> config('lib');
7072  
7073 61 $class -> SUPER::import(@a); # for subclasses
7074 61 ▶ $class -> export_to_level(1, $class, @a) if @a; # need this, too
Branch: 1 branch
BranchTrueFalseTotal
if @a 0 61 61
7075 }
7076  
7077 3 ▶sub _len_to_steps {
Subroutine _len_to_steps: called CC=7 SCAR=19.7
7078 # Given D (digits in decimal), compute N so that N! (N factorial) is
7079 # at least D digits long. D should be at least 50.
7080 3 my $d = shift;
7081  
7082 # two constants for the Ramanujan estimate of ln(N!)
7083 3 my $lg2 = log(2 * 3.14159265) / 2;
7084 3 my $lg10 = log(10);
7085  
7086 # D = 50 => N => 42, so L = 40 and R = 50
7087 3 my $l = 40;
7088 3 my $r = $d;
7089  
7090 # Otherwise this does not work under -Mbignum and we do not yet have "no
7091 # bignum;" :(
7092 3 ▶ $l = $l -> numify if ref($l);
Branch: 1 branch
BranchTrueFalseTotal
if ref $l 0 3 3
7093 3 ▶ $r = $r -> numify if ref($r);
Branch: 1 branch
BranchTrueFalseTotal
if ref $r 0 3 3
7094 3 ▶ $lg2 = $lg2 -> numify if ref($lg2);
Branch: 1 branch
BranchTrueFalseTotal
if ref $lg2 0 3 3
7095 3 ▶ $lg10 = $lg10 -> numify if ref($lg10);
Branch: 1 branch
BranchTrueFalseTotal
if ref $lg10 0 3 3
7096  
7097 # binary search for the right value (could this be written as the reverse
7098 # of lg(n!)?)
7099 3 ▶ while ($r - $l > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r - $l > 1) 15 3 18
7100 15 my $n = int(($r - $l) / 2) + $l;
7101 15 my $ramanujan
7102 = int(($n * log($n) - $n + log($n * (1 + 4*$n*(1+2*$n))) / 6 + $lg2)
7103 / $lg10);
7104 15 ▶ $ramanujan > $d ? $r = $n : $l = $n;
Branch: 1 branch
BranchTrueFalseTotal
$ramanujan > $d ? : 12 3 15
7105 }
7106 3 $l;
7107 }
7108  
7109 137 ▶sub _log {
Subroutine _log: called CC=3 SCAR=11.0
7110 # internal log function to calculate ln() based on Taylor series.
7111 # Modifies $x in place.
7112 137 my ($x, $scale) = @_;
7113 137 my $class = ref $x;
7114  
7115 # in case of $x == 1, result is 0
7116 137 ▶ return $x -> bzero() if $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 23 114 137
7117  
7118 # XXX TODO: rewrite this in a similar manner to bexp()
7119  
7120 # http://www.efunda.com/math/taylor_series/logarithmic.cfm?search_string=log
7121  
7122 # u = x-1, v = x+1
7123 # _ _
7124 # Taylor: | u 1 u^3 1 u^5 |
7125 # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 0
7126 # |_ v 3 v^3 5 v^5 _|
7127  
7128 # This takes much more steps to calculate the result and is thus not used
7129 # u = x-1
7130 # _ _
7131 # Taylor: | u 1 u^2 1 u^3 |
7132 # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 1/2
7133 # |_ x 2 x^2 3 x^3 _|
7134  
7135 # scale used in intermediate computations
7136 114 my $scaleup = $scale + 4;
7137  
7138 114 my ($v, $u, $numer, $denom, $factor, $f);
7139  
7140 114 $v = $x -> copy();
7141 114 $v = $v -> binc(); # v = x+1
7142 114 $x = $x -> bdec();
7143 114 $u = $x -> copy(); # u = x-1; x = x-1
7144  
7145 114 $x = $x -> bdiv($v, $scaleup); # first term: u/v
7146  
7147 114 $numer = $u -> copy(); # numerator
7148 114 $denom = $v -> copy(); # denominator
7149  
7150 114 $u = $u -> bmul($u); # u^2
7151 114 $v = $v -> bmul($v); # v^2
7152  
7153 114 $numer = $numer -> bmul($u); # u^3
7154 114 $denom = $denom -> bmul($v); # v^3
7155  
7156 114 $factor = $class -> new(3);
7157 114 $f = $class -> new(2);
7158  
7159 114 while (1) {
7160 3289 my $next = $numer -> copy() -> bround($scaleup)
7161 -> bdiv($denom -> copy() -> bmul($factor) -> bround($scaleup), $scaleup);
7162  
7163 3289 $next->{accuracy} = undef;
7164 3289 $next->{precision} = undef;
7165 3289 my $x_prev = $x -> copy();
7166 3289 $x = $x -> badd($next);
7167  
7168 3289 ▶ last if $x -> bacmp($x_prev) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x->bacmp($x_prev) == 0 114 3175 3289
7169  
7170 # calculate things for the next term
7171 3175 $numer = $numer -> bmul($u);
7172 3175 $denom = $denom -> bmul($v);
7173 3175 $factor = $factor -> badd($f);
7174 }
7175  
7176 114 $x = $x -> bmul($f); # $x *= 2
7177 114 $x = $x -> bround($scale);
7178 }
7179  
7180 177 ▶sub _log_10 {
Subroutine _log_10: called CC=31 SCAR=34.4
7181 # Internal log function based on reducing input to the range of 0.1 .. 9.99
7182 # and then "correcting" the result to the proper one. Modifies $x in place.
7183 177 my ($x, $scale) = @_;
7184 177 my $class = ref $x;
7185  
7186 # Taking blog() from numbers greater than 10 takes a *very long* time, so
7187 # we break the computation down into parts based on the observation that:
7188 # blog(X*Y) = blog(X) + blog(Y)
7189 # We set Y here to multiples of 10 so that $x becomes below 1 - the smaller
7190 # $x is the faster it gets. Since 2*$x takes about 10 times as long, we
7191 # make it faster by about a factor of 100 by dividing $x by 10.
7192  
7193 # The same observation is valid for numbers smaller than 0.1, e.g.
7194 # computing log(1) is fastest, and the further away we get from 1, the
7195 # longer it takes. So we also 'break' this down by multiplying $x with 10
7196 # and subtract the log(10) afterwards to get the correct result.
7197  
7198 # To get $x even closer to 1, we also divide by 2 and then use log(2) to
7199 # correct for this. For instance if $x is 2.4, we use the formula:
7200 # blog(2.4 * 2) == blog(1.2) + blog(2)
7201 # and thus calculate only blog(1.2) and blog(2), which is faster in total
7202 # than calculating blog(2.4).
7203  
7204 # In addition, the values for blog(2) and blog(10) are cached.
7205  
7206 # Calculate the number of digits before the dot, i.e., 1 + floor(log10(x)):
7207 # x = 123 => dbd = 3
7208 # x = 1.23 => dbd = 1
7209 # x = 0.0123 => dbd = -1
7210 # x = 0.000123 => dbd = -3
7211 # etc.
7212  
7213 177 my $dbd = $LIB->_num($x->{_e});
7214 177 ▶ $dbd = -$dbd if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 69 108 177
7215 177 $dbd += $LIB->_len($x->{_m});
7216  
7217 # more than one digit (e.g. at least 10), but *not* exactly 10 to avoid
7218 # infinite recursion
7219  
7220 177 my $calc = 1; # do some calculation?
7221  
7222 # No upgrading or downgrading in the intermediate computations.
7223  
7224 177 my $upg = $class -> upgrade();
7225 177 my $dng = $class -> downgrade();
7226 177 $class -> upgrade(undef);
7227 177 $class -> downgrade(undef);
7228  
7229 # disable the shortcut for 10, since we need log(10) and this would recurse
7230 # infinitely deep
7231 177 ▶ if ($x->{_es} eq '+' && # $x == 10
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'_es'} eq '+' and ($LIB->_is_one($x->{'_e'}) and $LIB->_is_one($x->{'_m'}))) { } 7 170 177
Condition83%5/6
$x->{"_es"} eq "+" and ($LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"}))
!ll&&!rl&&r
691017
$LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"})
!ll&&!rl&&r
10107
MC/DC66%2/3
$x->{"_es"} eq "+" and ($LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"}))
$x->{"_es"} eq "+" $LIB->_is_one($x->{"_e"}) $LIB->_is_one($x->{"_m"})
Truth table75%3/4
A and B and C
A= $x->{"_es"} eq "+"B= $LIB->_is_one($x->{"_e"})C= $LIB->_is_one($x->{"_m"})
ABCresult
0XX0
10X0
1100
1111
7232 ($LIB->_is_one($x->{_e}) &&
7233 $LIB->_is_one($x->{_m})))
7234 {
7235 7 $dbd = 0; # disable shortcut
7236 # we can use the cached value in these cases
7237 7 ▶ if ($scale <= $LOG_10_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_10_A) 7 0 7
7238 7 $x = $x -> bzero();
7239 7 $x = $x -> badd($LOG_10); # modify $x in place
7240 7 $calc = 0; # no need to calc, but round
7241 }
7242 # if we can't use the shortcut, we continue normally
7243 } else {
7244 # disable the shortcut for 2, since we maybe have it cached
7245 170 ▶ if (($LIB->_is_zero($x->{_e}) && # $x == 2
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{"_e"}) and $LIB->_is_two($x->{"_m"})) 34 136 170
Condition100%3/3
$LIB->_is_zero($x->{"_e"}) and $LIB->_is_two($x->{"_m"})
!ll&&!rl&&r
785834
MC/DC100%2/2
$LIB->_is_zero($x->{"_e"}) and $LIB->_is_two($x->{"_m"})
$LIB->_is_zero($x->{"_e"}) $LIB->_is_two($x->{"_m"})
Truth table100%3/3
A and B
A= $LIB->_is_zero($x->{"_e"})B= $LIB->_is_two($x->{"_m"})
ABresult
0X0
100
111
7246 $LIB->_is_two($x->{_m})))
7247 {
7248 34 $dbd = 0; # disable shortcut
7249 # we can use the cached value in these cases
7250 34 ▶ if ($scale <= $LOG_2_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_2_A) 34 0 34
7251 34 $x = $x -> bzero();
7252 34 $x = $x -> badd($LOG_2); # modify $x in place
7253 34 $calc = 0; # no need to calc, but round
7254 }
7255 # if we can't use the shortcut, we continue normally
7256 }
7257 }
7258  
7259 # if $x = 0.1, we know the result must be 0-log(10)
7260 177 ▶ if ($calc != 0 &&
Branch: 1 branch
BranchTrueFalseTotal
if ($calc != 0 and ($x->{"_es"} eq "-" and ($LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"})))) 2 175 177
Condition100%9/9
$calc != 0 and ($x->{"_es"} eq "-" and ($LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"})))
!ll&&!rl&&r
411342
$x->{"_es"} eq "-" and ($LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"}))
!ll&&!rl&&r
67672
$LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"})
!ll&&!rl&&r
27402
MC/DC100%4/4
$calc != 0 and ($x->{"_es"} eq "-" and ($LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"})))
$calc != 0 $x->{"_es"} eq "-" $LIB->_is_one($x->{"_e"}) $LIB->_is_one($x->{"_m"})
Truth table100%5/5
A and B and C and D
A= $calc != 0B= $x->{"_es"} eq "-"C= $LIB->_is_one($x->{"_e"})D= $LIB->_is_one($x->{"_m"})
ABCDresult
0XXX0
10XX0
110X0
11100
11111
7261 ($x->{_es} eq '-' && # $x == 0.1
7262 ($LIB->_is_one($x->{_e}) &&
7263 $LIB->_is_one($x->{_m}))))
7264 {
7265 2 $dbd = 0; # disable shortcut
7266 # we can use the cached value in these cases
7267 2 ▶ if ($scale <= $LOG_10_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_10_A) 2 0 2
7268 2 $x = $x -> bzero();
7269 2 $x = $x -> bsub($LOG_10);
7270 2 $calc = 0; # no need to calc, but round
7271 }
7272 }
7273  
7274 177 ▶ return $x if $calc == 0; # already have the result
Branch: 1 branch
BranchTrueFalseTotal
if $calc == 0 43 134 177
7275  
7276 # default: these correction factors are undef and thus not used
7277 134 my $l_10; # value of ln(10) to A of $scale
7278 my $l_2; # value of ln(2) to A of $scale
7279  
7280 134 my $two = $class -> new(2);
7281  
7282 # $x == 2 => 1, $x == 13 => 2, $x == 0.1 => 0, $x == 0.01 => -1
7283 # so don't do this shortcut for 1 or 0
7284 134 ▶ if (($dbd > 1) || ($dbd < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbd > 1 or $dbd < 0) 54 80 134
Condition100%3/3
$dbd > 1 or $dbd < 0
l!l&&r!l&&!r
47780
MC/DC100%2/2
$dbd > 1 or $dbd < 0
$dbd > 1 $dbd < 0
Truth table100%3/3
A or B
A= $dbd > 1B= $dbd < 0
ABresult
1X1
011
000
7285 # convert our cached value to an object if not already (avoid doing
7286 # this at import() time, since not everybody needs this)
7287 54 ▶ $LOG_10 = $class -> new($LOG_10, undef, undef) unless ref $LOG_10;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $LOG_10 15 39 54
7288  
7289 # got more than one digit before the dot, or more than one zero after
7290 # the dot, so do:
7291 # log(123) == log(1.23) + log(10) * 2
7292 # log(0.0123) == log(1.23) - log(10) * 2
7293  
7294 54 ▶ if ($scale <= $LOG_10_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_10_A) { } 53 1 54
7295 # use cached value
7296 53 $l_10 = $LOG_10 -> copy(); # copy for mul
7297 } else {
7298 # else: slower, compute and cache result
7299  
7300 # shorten the time to calculate log(10) based on the following:
7301 # log(1.25 * 8) = log(1.25) + log(8)
7302 # = log(1.25) + log(2) + log(2) + log(2)
7303  
7304 # first get $l_2 (and possible compute and cache log(2))
7305 1 ▶ $LOG_2 = $class -> new($LOG_2, undef, undef) unless ref $LOG_2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $LOG_2 0 1 1
7306 1 ▶ if ($scale <= $LOG_2_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_2_A) { } 1 0 1
7307 # use cached value
7308 1 $l_2 = $LOG_2 -> copy(); # copy() for the mul below
7309 } else {
7310 # else: slower, compute and cache result
7311 0 $l_2 = $two -> copy();
7312 0 $l_2 = $l_2->_log($scale); # scale+4, actually
7313 0 $LOG_2 = $l_2 -> copy(); # cache the result for later
7314 # the copy() is for mul below
7315 0 $LOG_2_A = $scale;
7316 }
7317  
7318 # now calculate log(1.25):
7319 1 $l_10 = $class -> new('1.25');
7320 1 $l_10 = $l_10->_log($scale); # scale+4, actually
7321  
7322 # log(1.25) + log(2) + log(2) + log(2):
7323 1 $l_10 = $l_10 -> badd($l_2);
7324 1 $l_10 = $l_10 -> badd($l_2);
7325 1 $l_10 = $l_10 -> badd($l_2);
7326 1 $LOG_10 = $l_10 -> copy(); # cache the result for later
7327 # the copy() is for mul below
7328 1 $LOG_10_A = $scale;
7329 }
7330 54 ▶ $dbd-- if ($dbd > 1); # 20 => dbd=2, so make it dbd=1
Branch: 1 branch
BranchTrueFalseTotal
if $dbd > 1 47 7 54
7331 54 $l_10 = $l_10 -> bmul($class -> new($dbd)); # log(10) * (digits_before_dot-1)
7332 54 my $dbd_sign = '+';
7333 54 ▶ if ($dbd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbd < 0) 7 47 54
7334 7 $dbd = -$dbd;
7335 7 $dbd_sign = '-';
7336 }
7337 ($x->{_e}, $x->{_es}) =
7338 54 $LIB -> _ssub($x->{_e}, $x->{_es}, $LIB->_new($dbd), $dbd_sign);
7339 }
7340  
7341 # Now: 0.1 <= $x < 10 (and possible correction in l_10)
7342  
7343 ### Since $x in the range 0.5 .. 1.5 is MUCH faster, we do a repeated div
7344 ### or mul by 2 (maximum times 3, since x < 10 and x > 0.1)
7345  
7346 134 ▶ $HALF = $class -> new($HALF) unless ref($HALF);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $HALF 12 122 134
7347  
7348 134 my $twos = 0; # default: none (0 times)
7349 134 ▶ while ($x -> bacmp($HALF) <= 0) { # X <= 0.5
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bacmp($HALF) <= 0) 42 134 176
7350 42 $twos--;
7351 42 $x = $x -> bmul($two);
7352 }
7353 134 ▶ while ($x -> bacmp($two) >= 0) { # X >= 2
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bacmp($two) >= 0) 58 134 192
7354 58 $twos++;
7355 58 $x = $x -> bdiv($two, $scale+4); # keep all digits
7356 }
7357 134 $x = $x -> bround($scale+4);
7358 # $twos > 0 => did mul 2, < 0 => did div 2 (but we never did both)
7359 # So calculate correction factor based on ln(2):
7360 134 ▶ if ($twos != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($twos != 0) { } 79 55 134
7361 79 ▶ $LOG_2 = $class -> new($LOG_2, undef, undef) unless ref $LOG_2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $LOG_2 13 66 79
7362 79 ▶ if ($scale <= $LOG_2_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_2_A) { } 77 2 79
7363 # use cached value
7364 77 $l_2 = $LOG_2 -> copy(); # copy() for the mul below
7365 } else {
7366 # else: slower, compute and cache result
7367 2 $l_2 = $two -> copy();
7368 2 $l_2 = $l_2->_log($scale); # scale+4, actually
7369 2 $LOG_2 = $l_2 -> copy(); # cache the result for later
7370 # the copy() is for mul below
7371 2 $LOG_2_A = $scale;
7372 }
7373 79 $l_2 = $l_2 -> bmul($twos); # * -2 => subtract, * 2 => add
7374 } else {
7375 55 undef $l_2;
7376 }
7377  
7378 134 $x = $x->_log($scale); # need to do the "normal" way
7379 134 ▶ $x = $x -> badd($l_10) if defined $l_10; # correct it by ln(10)
Branch: 1 branch
BranchTrueFalseTotal
if defined $l_10 54 80 134
7380 134 ▶ $x = $x -> badd($l_2) if defined $l_2; # and maybe by ln(2)
Branch: 1 branch
BranchTrueFalseTotal
if defined $l_2 79 55 134
7381  
7382 # Restore globals
7383  
7384 134 $class -> upgrade($upg);
7385 134 $class -> downgrade($dng);
7386  
7387 # all done, $x contains now the result
7388 134 $x;
7389 }
7390  
7391 115 ▶sub _pow {
Subroutine _pow: called CC=13 SCAR=25.8
7392 # Calculate a power where $y is a non-integer, like 2 ** 0.3
7393 115 my ($x, $y, @r) = @_;
7394 115 my $class = ref($x);
7395  
7396 # if $y == 0.5, it is sqrt($x)
7397 115 ▶ $HALF = $class -> new($HALF) unless ref($HALF);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $HALF 6 109 115
7398 115 ▶ return $x -> bsqrt(@r, $y) if $y -> bcmp($HALF) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $y->bcmp($HALF) == 0 45 70 115
7399  
7400 # Using:
7401 # a ** x == e ** (x * ln a)
7402  
7403 # u = y * ln x
7404 # _ _
7405 # Taylor: | u u^2 u^3 |
7406 # x ** y = 1 + | --- + --- + ----- + ... |
7407 # |_ 1 1*2 1*2*3 _|
7408  
7409 # we need to limit the accuracy to protect against overflow
7410 70 my $fallback = 0;
7411 70 my ($scale, @params);
7412 70 ($x, @params) = $x->_find_round_parameters(@r);
7413  
7414 70 ▶ return $x if $x -> is_nan(); # error in _find_round_parameters?
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 70 70
7415  
7416 # no rounding at all, so must use fallback
7417 70 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 52 18 70
7418 # simulate old behaviour
7419 52 $params[0] = $class -> div_scale(); # and round to it as accuracy
7420 52 $params[1] = undef; # disable P
7421 52 $scale = $params[0]+4; # at least four more for proper round
7422 52 $params[2] = $r[2]; # round mode by caller or undef
7423 52 $fallback = 1; # to clear a/p afterwards
7424 } else {
7425 # the 4 below is empirical, and there might be cases where it is not
7426 # enough...
7427 18 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition33%1/3
$params[0] || $params[1]
l!l&&r!l&&!r
1800
MC/DC0%0/2
$params[0] || $params[1]
$params[0] $params[1]
Truth table33%1/3
A || B
A= $params[0]B= $params[1]
ABresult
1X1
011
000
7428 }
7429  
7430 # When user set globals, they would interfere with our calculation, so
7431 # disable them and later re-enable them.
7432  
7433 70 my $ab = $class -> accuracy();
7434 70 my $pb = $class -> precision();
7435 70 $class -> accuracy(undef);
7436 70 $class -> precision(undef);
7437  
7438 # Disabling upgrading and downgrading is no longer necessary to avoid an
7439 # infinite recursion, but it avoids unnecessary upgrading and downgrading
7440 # in the intermediate computations.
7441  
7442 70 my $upg = $class -> upgrade();
7443 70 my $dng = $class -> downgrade();
7444 70 $class -> upgrade(undef);
7445 70 $class -> downgrade(undef);
7446  
7447 # We also need to disable any set A or P on $x (_find_round_parameters took
7448 # them already into account), since these would interfere, too.
7449  
7450 70 $x->{accuracy} = undef;
7451 70 $x->{precision} = undef;
7452  
7453 70 my ($limit, $v, $u, $below, $factor, $next, $over);
7454  
7455 70 $u = $x -> copy() -> blog(undef, $scale) -> bmul($y);
7456 70 my $do_invert = ($u->{sign} eq '-');
7457 70 ▶ $u = $u -> bneg() if $do_invert;
Branch: 1 branch
BranchTrueFalseTotal
if $do_invert 31 39 70
7458 70 $v = $class -> bone(); # 1
7459 70 $factor = $class -> new(2); # 2
7460 70 $x = $x -> bone(); # first term: 1
7461  
7462 70 $below = $v -> copy();
7463 70 $over = $u -> copy();
7464  
7465 70 $limit = $class -> new("1E-". ($scale-1));
7466 70 while (3 < 5) {
7467 # we calculate the next term, and add it to the last
7468 # when the next term is below our limit, it won't affect the outcome
7469 # anymore, so we stop:
7470 3302 $next = $over -> copy() -> bdiv($below, $scale);
7471 3302 ▶ last if $next -> bacmp($limit) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next->bacmp($limit) <= 0 70 3232 3302
7472 3232 $x = $x -> badd($next);
7473 # calculate things for the next term
7474 3232 $over *= $u;
7475 3232 $below *= $factor;
7476 3232 $factor = $factor -> binc();
7477  
7478 3232 ▶ last if $x->{sign} !~ /^[-+]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[-+]$/ 0 3232 3232
7479 }
7480  
7481 70 ▶ if ($do_invert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_invert) 31 39 70
7482 31 my $x_copy = $x -> copy();
7483 31 $x = $x -> bone -> bdiv($x_copy, $scale);
7484 }
7485  
7486 # shortcut to not run through _find_round_parameters again
7487 70 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 70 0 70
7488 70 $x = $x -> bround($params[0], $params[2]); # then round accordingly
7489 } else {
7490 0 $x = $x -> bfround($params[1], $params[2]); # then round accordingly
7491 }
7492 70 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 52 18 70
7493 # clear a/p after round, since user did not request it
7494 52 $x->{accuracy} = undef;
7495 52 $x->{precision} = undef;
7496 }
7497  
7498 # Restore globals. We need to do it like this, because setting one
7499 # undefines the other.
7500  
7501 70 ▶ if (defined $ab) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ab) { } 0 70 70
7502 0 $class -> accuracy($ab);
7503 } else {
7504 70 $class -> precision($pb);
7505 }
7506  
7507 70 $class -> upgrade($upg);
7508 70 $class -> downgrade($dng);
7509  
7510 70 $x;
7511 }
7512  
7513 # These functions are only provided for backwards compabibility so that old
7514 # version of Math::BigRat etc. don't complain about missing them.
7515  
7516 0 ▶sub _e_add {
Subroutine _e_add: not called CC=1 SCAR=6.9
7517 0 my ($x, $y, $xs, $ys) = @_;
7518 0 return $LIB -> _sadd($x, $xs, $y, $ys);
7519 }
7520  
7521 0 ▶sub _e_sub {
Subroutine _e_sub: not called CC=1 SCAR=6.9
7522 0 my ($x, $y, $xs, $ys) = @_;
7523 0 return $LIB -> _ssub($x, $xs, $y, $ys);
7524 }
7525  
7526 43 1;
7527  
7528 __END__
« BigRat.pm ↑ Summary BigInt.pm »