BigFloat.pm

filter: Statementstmt 81.4% 2082 / 2556 Branchbran 67.5% 1304 / 1930 Conditioncond 59.7% 681 / 1140 MC/DCmcdc 34.9% 227 / 649 Subroutinesub 79.4% 135 / 170 Podpod 98.8% 87 / 88 totaltotal 69.1% 4516 / 6533 SCAR 106.0
CC
1352
Cov
72%
to_ieee754
1260.0
from_ieee754
395.9
bfround
286.3
CRAP 40246.9
« BigInt.pm ↑ Summary BigInt/Lib.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 # sign : "+", "-", "+inf", "-inf", or "NaN" if not a number
9 # _m : mantissa ($LIB thingy)
10 # _es : sign of _e
11 # _e : exponent ($LIB thingy)
12 # _a : accuracy
13 # _p : precision
14  
15 43 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 43 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 43 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 43 ▶use Carp qw< carp croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 43 ▶use Scalar::Util qw< blessed >;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 43 ▶use Math::BigInt qw< >;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 43 our $VERSION = '1.999842';
24 43 $VERSION =~ tr/_//d;
25  
26 43 require Exporter;
27 43 our @ISA = qw/Math::BigInt/;
28 43 our @EXPORT_OK = qw/bpi/;
29  
30 # $_trap_inf/$_trap_nan are internal and should never be accessed from outside
31 43 our ($AUTOLOAD, $accuracy, $precision, $div_scale, $round_mode, $rnd_mode,
32 $upgrade, $downgrade, $_trap_nan, $_trap_inf);
33  
34 use overload
35  
36 # overload key: with_assign
37  
38 13 ▶ '+' => sub { $_[0] -> copy() -> badd($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
39  
40 73 ▶ '-' => sub { my $c = $_[0] -> copy();
Subroutine __ANON__: called CC=2 SCAR=7.2
41 73 ▶ $_[2] ? $c -> bneg() -> badd($_[1])
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 73 73
42 : $c -> bsub($_[1]); },
43  
44 153 ▶ '*' => sub { $_[0] -> copy() -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
45  
46 12 ▶ '/' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bdiv($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 12 12
47 : $_[0] -> copy() -> bdiv($_[1]); },
48  
49 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
50 : $_[0] -> copy() -> bmod($_[1]); },
51  
52 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
53 : $_[0] -> copy() -> bpow($_[1]); },
54  
55 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
56 : $_[0] -> copy() -> bblsft($_[1]); },
57  
58 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
59 : $_[0] -> copy() -> bbrsft($_[1]); },
60  
61 # overload key: assign
62  
63 29 ▶ '+=' => sub { $_[0] -> badd($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
64  
65 28 ▶ '-=' => sub { $_[0] -> bsub($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
66  
67 6424 ▶ '*=' => sub { $_[0] -> bmul($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
68  
69 8 ▶ '/=' => sub { scalar $_[0] -> bdiv($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
70  
71 8 ▶ '%=' => sub { $_[0] -> bmod($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
72  
73 4 ▶ '**=' => sub { $_[0] -> bpow($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
74  
75 8 ▶ '<<=' => sub { $_[0] -> bblsft($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
76  
77 8 ▶ '>>=' => sub { $_[0] -> bbrsft($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
78  
79 # 'x=' => sub { },
80  
81 # '.=' => sub { },
82  
83 # overload key: num_comparison
84  
85 301 ▶ '<' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> blt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 301 301
86 : $_[0] -> blt($_[1]); },
87  
88 857 ▶ '<=' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> ble($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 857 857
89 : $_[0] -> ble($_[1]); },
90  
91 879 ▶ '>' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bgt($_[0])
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 879 879
92 : $_[0] -> bgt($_[1]); },
93  
94 177 ▶ '>=' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bge($_[0])
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 140 37 177
95 : $_[0] -> bge($_[1]); },
96  
97 186 ▶ '==' => sub { $_[0] -> beq($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
98  
99 9 ▶ '!=' => sub { $_[0] -> bne($_[1]); },
Subroutine __ANON__: called CC=1 SCAR=0.0
100  
101 # overload key: 3way_comparison
102  
103 0 ▶ '<=>' => sub { my $cmp = $_[0] -> bcmp($_[1]);
Subroutine __ANON__: not called CC=3 SCAR=24.8
104 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
105  
106 5923 ▶ 'cmp' => sub { $_[2] ? "$_[1]" cmp $_[0] -> bstr()
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[2] ? : 0 5923 5923
107 : $_[0] -> bstr() cmp "$_[1]"; },
108  
109 # overload key: str_comparison
110  
111 # 'lt' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrlt($_[0])
112 # : $_[0] -> bstrlt($_[1]); },
113 #
114 # 'le' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrle($_[0])
115 # : $_[0] -> bstrle($_[1]); },
116 #
117 # 'gt' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrgt($_[0])
118 # : $_[0] -> bstrgt($_[1]); },
119 #
120 # 'ge' => sub { $_[2] ? ref($_[0]) -> new($_[1]) -> bstrge($_[0])
121 # : $_[0] -> bstrge($_[1]); },
122 #
123 # 'eq' => sub { $_[0] -> bstreq($_[1]); },
124 #
125 # 'ne' => sub { $_[0] -> bstrne($_[1]); },
126  
127 # overload key: binary
128  
129 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
130 : $_[0] -> copy() -> band($_[1]); },
131  
132 0 ▶ '&=' => sub { $_[0] -> band($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
133  
134 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
135 : $_[0] -> copy() -> bior($_[1]); },
136  
137 0 ▶ '|=' => sub { $_[0] -> bior($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
138  
139 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
140 : $_[0] -> copy() -> bxor($_[1]); },
141  
142 0 ▶ '^=' => sub { $_[0] -> bxor($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
143  
144 # '&.' => sub { },
145  
146 # '&.=' => sub { },
147  
148 # '|.' => sub { },
149  
150 # '|.=' => sub { },
151  
152 # '^.' => sub { },
153  
154 # '^.=' => sub { },
155  
156 # overload key: unary
157  
158 352 ▶ 'neg' => sub { $_[0] -> copy() -> bneg(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
159  
160 # '!' => sub { },
161  
162 0 ▶ '~' => sub { $_[0] -> copy() -> bnot(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
163  
164 # '~.' => sub { },
165  
166 # overload key: mutators
167  
168 10 ▶ '++' => sub { $_[0] -> binc() },
Subroutine __ANON__: called CC=1 SCAR=0.0
169  
170 0 ▶ '--' => sub { $_[0] -> bdec() },
Subroutine __ANON__: not called CC=1 SCAR=6.9
171  
172 # overload key: func
173  
174 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
175 : $_[0] -> copy() -> batan2($_[1]); },
176  
177 0 ▶ 'cos' => sub { $_[0] -> copy() -> bcos(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
178  
179 0 ▶ 'sin' => sub { $_[0] -> copy() -> bsin(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
180  
181 0 ▶ 'exp' => sub { $_[0] -> copy() -> bexp($_[1]); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
182  
183 0 ▶ 'abs' => sub { $_[0] -> copy() -> babs(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
184  
185 40 ▶ 'log' => sub { $_[0] -> copy() -> blog(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
186  
187 0 ▶ 'sqrt' => sub { $_[0] -> copy() -> bsqrt(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
188  
189 140 ▶ 'int' => sub { $_[0] -> copy() -> bint(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
190  
191 # overload key: conversion
192  
193 280 ▶ 'bool' => sub { $_[0] -> is_zero() ? '' : 1; },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->is_zero ? : 0 280 280
194  
195 742 ▶ '""' => sub { $_[0] -> bstr(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
196  
197 8 ▶ '0+' => sub { $_[0] -> numify(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
198  
199 0 ▶ '=' => sub { $_[0] -> copy(); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
200  
201 43 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
202  
203 ##############################################################################
204 # global constants, flags and assorted stuff
205  
206 # the following are public, but their usage is not recommended. Use the
207 # accessor methods instead.
208  
209 # class constants, use Class->constant_name() to access
210 # one of 'even', 'odd', '+inf', '-inf', 'zero', 'trunc' or 'common'
211 43 $round_mode = 'even';
212 43 $accuracy = undef;
213 43 $precision = undef;
214 43 $div_scale = 40;
215  
216 43 $upgrade = undef;
217 43 $downgrade = undef;
218 # the package we are using for our private parts, defaults to:
219 # Math::BigInt->config('lib')
220 43 my $LIB = 'Math::BigInt::Calc';
221  
222 # are NaNs ok? (otherwise it dies when encountering an NaN) set w/ config()
223 43 $_trap_nan = 0;
224 # the same for infinity
225 43 $_trap_inf = 0;
226  
227 # constant for easier life
228 43 my $nan = 'NaN';
229  
230 43 my $IMPORT = 0; # was import() called yet? used to make require work
231  
232 # some digits of accuracy for blog(undef, 10); which we use in blog() for speed
233 43 my $LOG_10 =
234 '2.3025850929940456840179914546843642076011014886287729760333279009675726097';
235 43 my $LOG_10_A = length($LOG_10)-1;
236 # ditto for log(2)
237 43 my $LOG_2 =
238 '0.6931471805599453094172321214581765680755001343602552541206800094933936220';
239 43 my $LOG_2_A = length($LOG_2)-1;
240 43 my $HALF = '0.5'; # made into an object if nec.
241  
242 ##############################################################################
243 # the old code had $rnd_mode, so we need to support it, too
244  
245 sub TIESCALAR {
246 43 ▶ my ($class) = @_;
Subroutine TIESCALAR: called CC=1 SCAR=0.0
247 43 bless \$round_mode, $class;
248 }
249  
250 sub FETCH {
251 1 ▶ return $round_mode;
Subroutine FETCH: called CC=1 SCAR=0.0
252 }
253  
254 sub STORE {
255 1 ▶ $rnd_mode = $_[0]->round_mode($_[1]);
Subroutine STORE: called CC=1 SCAR=0.0
256 }
257  
258 BEGIN {
259 # when someone sets $rnd_mode, we catch this and check the value to see
260 # whether it is valid or not.
261 43 ▶ $rnd_mode = 'even';
Subroutine BEGIN: called CC=1 SCAR=0.0
262 43 tie $rnd_mode, 'Math::BigFloat';
263  
264 43 *as_number = \&as_int;
265 }
266  
267 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
268 # going through AUTOLOAD for every DESTROY is costly, avoid it by empty sub
269 }
270  
271 sub AUTOLOAD {
272 # make fxxx and bxxx both work by selectively mapping fxxx() to MBF::bxxx()
273 3 ▶ my $name = $AUTOLOAD;
Subroutine AUTOLOAD: called CC=6 SCAR=22.4
274 3 $name =~ s/(.*):://; # split package
275 3 ▶ my $c = $1 || __PACKAGE__;
Condition50%1/2
$1 || __PACKAGE__
l!l
30
MC/DC0%0/1
$1 || __PACKAGE__
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
276 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
277 3 ▶ $c->import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 3 0 3
278 3 ▶ if (!_method_alias($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_method_alias($name)) 3 0 3
279 3 ▶ if (!defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 3 3
280 # delayed load of Carp and avoid recursion
281 0 croak("$c: Can't call a method without name");
282 }
283 3 ▶ if (!_method_hand_up($name)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_method_hand_up($name)) 0 3 3
284 # delayed load of Carp and avoid recursion
285 0 croak("Can't call $c\-\>$name, not a valid method");
286 }
287 # try one level up, but subst. bxxx() for fxxx() since MBI only got
288 # bxxx()
289 3 $name =~ s/^f/b/;
290 3 return &{"Math::BigInt"."::$name"}(@_);
291 }
292 0 my $bname = $name;
293 0 $bname =~ s/^f/b/;
294 0 $c .= "::$name";
295 0 *{$c} = \&{$bname};
296 0 &{$c}; # uses @_
297 }
298  
299 ##############################################################################
300  
301 {
302 # valid method aliases for AUTOLOAD
303 43 my %methods = map { $_ => 1 }
304 qw / fadd fsub fmul fdiv fround ffround fsqrt fmod fstr fsstr fpow fnorm
305 fint facmp fcmp fzero fnan finf finc fdec ffac fneg
306 fceil ffloor frsft flsft fone flog froot fexp
307 /;
308 # valid methods that can be handed up (for AUTOLOAD)
309 43 my %hand_ups = map { $_ => 1 }
310 qw / is_nan is_inf is_negative is_positive is_pos is_neg
311 accuracy precision div_scale round_mode fabs fnot
312 objectify upgrade downgrade
313 bone binf bnan bzero
314 bsub
315 /;
316  
317 3 ▶ sub _method_alias { exists $methods{$_[0]||''}; }
Subroutine _method_alias: called CC=2 SCAR=7.6
Condition50%1/2
$_[0] || ''
l!l
30
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
318 3 ▶ sub _method_hand_up { exists $hand_ups{$_[0]||''}; }
Subroutine _method_hand_up: called CC=2 SCAR=7.6
Condition50%1/2
$_[0] || ''
l!l
30
MC/DC0%0/1
$_[0] || ''
$_[0]
Truth table50%1/2
A
A= $_[0]
Aresult
11
00
319 }
320  
321 43 sub isa {
322 24688 ▶ my ($self, $class) = @_;
Subroutine isa: called CC=2 SCAR=6.9
Pod undocumented
323 24688 ▶ return if $class =~ /^Math::BigInt/; # we aren't one of these
Branch: 1 branch
BranchTrueFalseTotal
if $class =~ /^Math::BigInt/ 1131 23557 24688
324 23557 UNIVERSAL::isa($self, $class);
325 }
326  
327 sub config {
328 # return (later set?) configuration data as hash ref
329 55 ▶ my $class = shift || 'Math::BigFloat';
Subroutine config: called CC=6 SCAR=17.9
Pod documented
Condition50%1/2
shift() || 'Math::BigFloat'
l!l
550
MC/DC0%0/1
shift() || 'Math::BigFloat'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
330  
331 # Getter/accessor.
332  
333 55 ▶ if (@_ == 1 && ref($_[0]) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] ne "HASH") 23 32 55
Condition100%3/3
@_ == 1 and ref $_[0] ne "HASH"
!ll&&!rl&&r
31123
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
334 23 my $param = shift;
335 23 ▶ return $class if $param eq 'class';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "class" 2 21 23
336 21 ▶ return $LIB if $param eq 'with';
Branch: 1 branch
BranchTrueFalseTotal
if $param eq "with" 5 16 21
337 16 return $class->SUPER::config($param);
338 }
339  
340 # Setter.
341  
342 32 my $cfg = $class->SUPER::config(@_);
343  
344 # now we need only to override the ones that are different from our parent
345 31 $cfg->{class} = $class;
346 31 $cfg->{with} = $LIB;
347 31 $cfg;
348 }
349  
350 ###############################################################################
351 # Constructor methods
352 ###############################################################################
353  
354 sub new {
355 # Create a new Math::BigFloat object from a string or another bigfloat
356 # object.
357 # _e: exponent
358 # _m: mantissa
359 # sign => ("+", "-", "+inf", "-inf", or "NaN")
360  
361 17120 ▶ my $self = shift;
Subroutine new: called CC=39 SCAR=38.5
Pod documented
362 17120 my $selfref = ref $self;
363 17120 ▶ my $class = $selfref || $self;
Condition33%1/3
$selfref || $self
l!l&&r!l&&!r
0171200
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table33%1/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
364  
365 # Make "require" work.
366  
367 17120 ▶ $class -> import() if $IMPORT == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 2 17118 17120
368  
369 # Although this use has been discouraged for more than 10 years, people
370 # apparently still use it, so we still support it.
371  
372 17120 ▶ return $class -> bzero() unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 8 17112 17120
373  
374 17112 my ($wanted, @r) = @_;
375  
376 17112 ▶ if (!defined($wanted)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wanted) 0 17112 17112
377 #if (warnings::enabled("uninitialized")) {
378 # warnings::warn("uninitialized",
379 # "Use of uninitialized value in new()");
380 #}
381 0 return $class -> bzero(@r);
382 }
383  
384 17112 ▶ if (!ref($wanted) && $wanted eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $wanted and $wanted eq "") 0 17112 17112
Condition66%2/3
not ref $wanted and $wanted eq ""
!ll&&!rl&&r
867162450
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
385 #if (warnings::enabled("numeric")) {
386 # warnings::warn("numeric",
387 # q|Argument "" isn't numeric in new()|);
388 #}
389 #return $class -> bzero(@r);
390 0 return $class -> bnan(@r);
391 }
392  
393 # Initialize a new object.
394  
395 17112 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 17112 0 17112
396  
397 # Math::BigFloat or subclass
398  
399 17112 ▶ if (defined(blessed($wanted)) && $wanted -> isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($wanted) and $wanted->isa(__PACKAGE__)) 344 16768 17112
Condition100%3/3
defined builtin::blessed($wanted) and $wanted->isa(__PACKAGE__)
!ll&&!rl&&r
16245523344
MC/DC100%2/2
defined builtin::blessed($wanted) and $wanted->isa(__PACKAGE__)
defined builtin::blessed($wanted) $wanted->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= defined builtin::blessed($wanted)B= $wanted->isa(__PACKAGE__)
ABresult
0X0
100
111
400  
401 # Don't copy the accuracy and precision, because a new object should get
402 # them from the global configuration.
403  
404 344 $self -> {sign} = $wanted -> {sign};
405 344 $self -> {_m} = $LIB -> _copy($wanted -> {_m});
406 344 $self -> {_es} = $wanted -> {_es};
407 344 $self -> {_e} = $LIB -> _copy($wanted -> {_e});
408 344 ▶ $self = $self->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 0 11 11
Condition66%4/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
333011
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
333011
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
409 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
410 344 return $self;
411 }
412  
413 # Shortcut for Math::BigInt and its subclasses. This should be improved.
414  
415 16768 ▶ if (defined(blessed($wanted))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($wanted)) 523 16245 16768
416 523 ▶ if ($wanted -> isa('Math::BigInt')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted->isa("Math::BigInt")) 522 1 523
417 522 $self->{sign} = $wanted -> {sign};
418 522 $self->{_m} = $LIB -> _copy($wanted -> {value});
419 522 $self->{_es} = '+';
420 522 $self->{_e} = $LIB -> _zero();
421 522 return $self -> bnorm();
422 }
423  
424 1 ▶ if ($wanted -> can("as_number")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted->can("as_number")) 0 1 1
425 0 $self->{sign} = $wanted -> sign();
426 0 $self->{_m} = $wanted -> as_number() -> {value};
427 0 $self->{_es} = '+';
428 0 $self->{_e} = $LIB -> _zero();
429 0 return $self -> bnorm();
430 }
431 }
432  
433 # Shortcut for simple forms like '123' that have no trailing zeros. Trailing
434 # zeros would require a non-zero exponent.
435  
436 16246 ▶ 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) 7885 8361 16246
437 / ^
438 \s* # optional leading whitespace
439 ( [+-]? ) # optional sign
440 0* # optional leading zeros
441 ( [1-9] (?: [0-9]* [1-9] )? ) # significand
442 \s* # optional trailing whitespace
443 $
444 /x)
445 {
446 7885 ▶ return $downgrade -> new($1 . $2) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 8 7877 7885
447 7877 ▶ $self->{sign} = $1 || '+';
Condition100%2/2
$1 || '+'
l!l
29574920
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
448 7877 $self->{_m} = $LIB -> _new($2);
449 7877 $self->{_es} = '+';
450 7877 $self->{_e} = $LIB -> _zero();
451 7877 ▶ $self = $self->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 24 1960 1984
Condition100%6/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
5903141960
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
5893101974
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
452 unless @r >= 2 && !defined $r[0] && !defined $r[1];
453 7877 return $self;
454 }
455  
456 # Handle Infs.
457  
458 8361 ▶ if ($wanted =~ / ^
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ \s* ( [+-]? ) inf (?: inity )? \s* \z /xi) 1727 6634 8361
459 \s*
460 ( [+-]? )
461 inf (?: inity )?
462 \s*
463 \z
464 /ix)
465 {
466 1727 ▶ my $sgn = $1 || '+';
Condition100%2/2
$1 || '+'
l!l
1299428
MC/DC100%1/1
$1 || '+'
$1
Truth table100%2/2
A
A= $1
Aresult
11
00
467 1727 return $class -> binf($sgn, @r);
468 }
469  
470 # Handle explicit NaNs (not the ones returned due to invalid input).
471  
472 6634 ▶ if ($wanted =~ / ^
Branch: 1 branch
BranchTrueFalseTotal
if ($wanted =~ / ^ \s* ( [+-]? ) nan \s* \z /xi) 475 6159 6634
473 \s*
474 ( [+-]? )
475 nan
476 \s*
477 \z
478 /ix)
479 {
480 475 return $class -> bnan(@r);
481 }
482  
483 6159 my @parts;
484  
485 6159 ▶ 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)) 5668 451 6119
Condition66%16/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
57080451
$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
405668451
$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
24166119
$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
2406135
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
6135024
$wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
613500
$wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
6119016
$wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
!ll&&!rl&&r
45100
MC/DC0%0/9
$wanted =~ /^\s*[+-]?0?[Xx]/ and @parts = $class->_hex_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Oo]/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0?[Bb]/ and @parts = $class->_bin_str_to_flt_lib_parts($wanted) or @parts = $class->_dec_str_to_flt_lib_parts($wanted) or $wanted =~ /^\s*[+-]?0_*\d/ and @parts = $class->_oct_str_to_flt_lib_parts($wanted)
$wanted =~ /^\s*[+-]?0?[Xx]/ @parts = $class->_hex_str_to_flt_lib_parts($wanted) $wanted =~ /^\s*[+-]?0?[Oo]/ @parts = $class->_oct_str_to_flt_lib_parts($wanted) $wanted =~ /^\s*[+-]?0?[Bb]/ @parts = $class->_bin_str_to_flt_lib_parts($wanted) @parts = $class->_dec_str_to_flt_lib_parts($wanted) $wanted =~ /^\s*[+-]?0_*\d/ @parts = $class->_oct_str_to_flt_lib_parts($wanted)
Truth table5%2/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
486 # Handle hexadecimal numbers. We auto-detect hexadecimal numbers if they
487 # have a "0x", "0X", "x", or "X" prefix, cf. CORE::oct().
488  
489 $wanted =~ /^\s*[+-]?0?[Xx]/ and
490 @parts = $class -> _hex_str_to_flt_lib_parts($wanted)
491  
492 or
493  
494 # Handle octal numbers. We auto-detect octal numbers if they have a
495 # "0o", "0O", "o", "O" prefix, cf. CORE::oct().
496  
497 $wanted =~ /^\s*[+-]?0?[Oo]/ and
498 @parts = $class -> _oct_str_to_flt_lib_parts($wanted)
499  
500 or
501  
502 # Handle binary numbers. We auto-detect binary numbers if they have a
503 # "0b", "0B", "b", or "B" prefix, cf. CORE::oct().
504  
505 $wanted =~ /^\s*[+-]?0?[Bb]/ and
506 @parts = $class -> _bin_str_to_flt_lib_parts($wanted)
507  
508 or
509  
510 # At this point, what is left are decimal numbers that aren't handled
511 # above and octal floating point numbers that don't have any of the
512 # "0o", "0O", "o", or "O" prefixes. First see if it is a decimal number.
513  
514 @parts = $class -> _dec_str_to_flt_lib_parts($wanted)
515 or
516  
517 # See if it is an octal floating point number. The extra check is
518 # included because _oct_str_to_flt_lib_parts() accepts octal numbers
519 # that don't have a prefix (this is needed to make it work with, e.g.,
520 # from_oct() that don't require a prefix). However, Perl requires a
521 # prefix for octal floating point literals. For example, "1p+0" is not
522 # valid, but "01p+0" and "0__1p+0" are.
523  
524 $wanted =~ /^\s*[+-]?0_*\d/ and
525 @parts = $class -> _oct_str_to_flt_lib_parts($wanted))
526 {
527 5708 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
528  
529 5708 ▶ $self = $self->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 4 1452 1456
Condition100%6/6
@r >= 2 and not defined $r[0] and not defined $r[1]
!ll&&!rl&&r
425511452
@r >= 2 and not defined $r[0]
!ll&&!rl&&r
425231453
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
530 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
531  
532 5708 ▶ return $downgrade -> new($self -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $self->is_int 2 5706 5708
Condition100%3/3
defined $Math::BigFloat::downgrade and $self->is_int
!ll&&!rl&&r
570242
MC/DC100%2/2
defined $Math::BigFloat::downgrade and $self->is_int
defined $Math::BigFloat::downgrade $self->is_int
Truth table100%3/3
A and B
A= defined $Math::BigFloat::downgradeB= $self->is_int
ABresult
0X0
100
111
533 if defined($downgrade) && $self -> is_int();
534 5706 return $self;
535 }
536  
537 # If we get here, the value is neither a valid decimal, binary, octal, or
538 # hexadecimal number. It is not an explicit Inf or a NaN either.
539  
540 451 return $class -> bnan(@r);
541 }
542  
543 sub from_dec {
544 1 ▶ my $self = shift;
Subroutine from_dec: called CC=11 SCAR=31.7
Pod documented
545 1 my $selfref = ref $self;
546 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
547  
548 # Don't modify constant (read-only) objects.
549  
550 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
551  
552 1 my $str = shift;
553 1 my @r = @_;
554  
555 # If called as a class method, initialize a new object.
556  
557 1 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1 0 1
558  
559 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
560 1 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
561  
562 1 ▶ $self = $self->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 0 0 0
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
563 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
564  
565 1 ▶ return $downgrade -> new($self -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $self->is_int 1 0 1
Condition33%1/3
defined $Math::BigFloat::downgrade and $self->is_int
!ll&&!rl&&r
001
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $self->is_int
defined $Math::BigFloat::downgrade $self->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $self->is_int
ABresult
0X0
100
111
566 if defined($downgrade) && $self -> is_int();
567 0 return $self;
568 }
569  
570 0 return $self -> bnan(@r);
571 }
572  
573 sub from_hex {
574 1 ▶ my $self = shift;
Subroutine from_hex: called CC=11 SCAR=31.7
Pod documented
575 1 my $selfref = ref $self;
576 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
577  
578 # Don't modify constant (read-only) objects.
579  
580 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
581  
582 1 my $str = shift;
583 1 my @r = @_;
584  
585 # If called as a class method, initialize a new object.
586  
587 1 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1 0 1
588  
589 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
590 1 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
591  
592 1 ▶ $self = $self->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 0 0 0
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
593 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
594  
595 1 ▶ return $downgrade -> new($self -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $self->is_int 1 0 1
Condition33%1/3
defined $Math::BigFloat::downgrade and $self->is_int
!ll&&!rl&&r
001
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $self->is_int
defined $Math::BigFloat::downgrade $self->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $self->is_int
ABresult
0X0
100
111
596 if defined($downgrade) && $self -> is_int();
597 0 return $self;
598 }
599  
600 0 return $self -> bnan(@r);
601 }
602  
603 sub from_oct {
604 1 ▶ my $self = shift;
Subroutine from_oct: called CC=11 SCAR=31.7
Pod documented
605 1 my $selfref = ref $self;
606 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
607  
608 # Don't modify constant (read-only) objects.
609  
610 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
611  
612 1 my $str = shift;
613 1 my @r = @_;
614  
615 # If called as a class method, initialize a new object.
616  
617 1 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1 0 1
618  
619 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
620 1 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
621  
622 1 ▶ $self = $self->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 0 0 0
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
623 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
624  
625 1 ▶ return $downgrade -> new($self -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $self->is_int 1 0 1
Condition33%1/3
defined $Math::BigFloat::downgrade and $self->is_int
!ll&&!rl&&r
001
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $self->is_int
defined $Math::BigFloat::downgrade $self->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $self->is_int
ABresult
0X0
100
111
626 if defined($downgrade) && $self -> is_int();
627 0 return $self;
628 }
629  
630 0 return $self -> bnan(@r);
631 }
632  
633 sub from_bin {
634 3 ▶ my $self = shift;
Subroutine from_bin: called CC=11 SCAR=31.7
Pod documented
635 3 my $selfref = ref $self;
636 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
637  
638 # Don't modify constant (read-only) objects.
639  
640 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
641  
642 3 my $str = shift;
643 3 my @r = @_;
644  
645 # If called as a class method, initialize a new object.
646  
647 3 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 3 0 3
648  
649 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
650 3 ($self->{sign}, $self->{_m}, $self->{_es}, $self->{_e}) = @parts;
651  
652 3 ▶ $self = $self->round(@r)
Branch: 1 branch
BranchTrueFalseTotal
unless @r >= 2 and not defined $r[0] and not defined $r[1] 0 0 0
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
653 unless @r >= 2 && !defined($r[0]) && !defined($r[1]);
654  
655 3 ▶ return $downgrade -> new($self -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $self->is_int 3 0 3
Condition33%1/3
defined $Math::BigFloat::downgrade and $self->is_int
!ll&&!rl&&r
003
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $self->is_int
defined $Math::BigFloat::downgrade $self->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $self->is_int
ABresult
0X0
100
111
656 if defined($downgrade) && $self -> is_int();
657 0 return $self;
658 }
659  
660 0 return $self -> bnan(@r);
661 }
662  
663 sub from_ieee754 {
664 1 ▶ my $self = shift;
Subroutine from_ieee754: called CC=36 SCAR=59.8
Pod documented
665 1 my $selfref = ref $self;
666 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
667  
668 # Don't modify constant (read-only) objects.
669  
670 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
671  
672 1 my $in = shift; # input string (or raw bytes)
673 1 my $format = shift; # format ("binary32", "decimal64" etc.)
674 1 my $enc; # significand encoding (applies only to decimal)
675 my $k; # storage width in bits
676 1 my $b; # base
677 1 my @r = @_; # rounding parameters, if any
678  
679 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
680 1 $k = $1;
681 1 $b = 2;
682 } elsif ($format =~ /^decimal(\d+)(dpd|bcd)?\z/) {
683 0 $k = $1;
684 0 $b = 10;
685 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
686 } elsif ($format eq 'half') {
687 0 $k = 16;
688 0 $b = 2;
689 } elsif ($format eq 'single') {
690 0 $k = 32;
691 0 $b = 2;
692 } elsif ($format eq 'double') {
693 0 $k = 64;
694 0 $b = 2;
695 } elsif ($format eq 'quadruple') {
696 0 $k = 128;
697 0 $b = 2;
698 } elsif ($format eq 'octuple') {
699 0 $k = 256;
700 0 $b = 2;
701 } elsif ($format eq 'sexdecuple') {
702 0 $k = 512;
703 0 $b = 2;
704 }
705  
706 1 ▶ if ($b == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 2) 1 0 1
707  
708 # Get the parameters for this format.
709  
710 1 my $p; # precision (in bits)
711 my $t; # number of bits in significand
712 1 my $w; # number of bits in exponent
713  
714 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
715 0 $p = 11;
716 0 $t = 10;
717 0 $w = 5;
718 } elsif ($k == 32) { # binary32 (single-precision)
719 1 $p = 24;
720 1 $t = 23;
721 1 $w = 8;
722 } elsif ($k == 64) { # binary64 (double-precision)
723 0 $p = 53;
724 0 $t = 52;
725 0 $w = 11;
726 } else { # binaryN (quadruple-precision and above)
727 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
728 0 croak "Number of bits must be 16, 32, 64, or >= 128 and",
729 " a multiple of 32";
730 }
731 0 $p = $k - sprintf('%.0f', 4 * log($k) / log(2)) + 13;
732 0 $t = $p - 1;
733 0 $w = $k - $t - 1;
734 }
735  
736 # The maximum exponent, minimum exponent, and exponent bias.
737  
738 1 my $emax = Math::BigFloat -> new(2) -> bpow($w - 1) -> bdec();
739 1 my $emin = 1 - $emax;
740 1 my $bias = $emax;
741  
742 # Undefined input.
743  
744 1 ▶ unless (defined $in) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $in) 0 1 1
745 0 carp("Input is undefined");
746 0 return $self -> bzero(@r);
747 }
748  
749 # Make sure input string is a string of zeros and ones.
750  
751 1 my $len = CORE::length $in;
752 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
753 1 $in = unpack "B*", $in;
754 } elsif (4 * $len == $k) { # hexadecimal
755 0 ▶ if ($in =~ /([^\da-f])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /([^\da-f])/i) 0 0 0
756 0 croak "Illegal hexadecimal digit '$1'";
757 }
758 0 $in = unpack "B*", pack "H*", $in;
759 } elsif ($len == $k) { # bits
760 0 ▶ if ($in =~ /([^01])/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in =~ /([^01])/) 0 0 0
761 0 croak "Illegal binary digit '$1'";
762 }
763 } else {
764 0 croak "Unknown input -- $in";
765 }
766  
767 # Split bit string into sign, exponent, and mantissa/significand.
768  
769 1 ▶ my $sign = substr($in, 0, 1) eq '1' ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
substr($in, 0, 1) eq '1' ? : 0 1 1
770 1 my $expo = $class -> from_bin(substr($in, 1, $w));
771 1 my $mant = $class -> from_bin(substr($in, $w + 1));
772  
773 1 my $x;
774  
775 1 $expo = $expo -> bsub($bias); # subtract bias
776  
777 1 ▶ if ($expo < $emin) { # zero and subnormals
Branch: 2 branches
BranchTrueFalseTotal
if ($expo < $emin) { } 0 1 1
elsif ($expo > $emax) { } 0 1 1
778 0 ▶ if ($mant == 0) { # zero
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == 0) { } 0 0 0
779 0 $x = $class -> bzero();
780 } else { # subnormals
781 # compute (1/$b)**(N) rather than ($b)**(-N)
782 0 $x = $class -> new("0.5"); # 1/$b
783 0 $x = $x -> bpow($bias + $t - 1) -> bmul($mant);
784 0 ▶ $x = $x -> bneg() if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 0 0 0
785 }
786 }
787  
788 elsif ($expo > $emax) { # inf and nan
789 0 ▶ if ($mant == 0) { # inf
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == 0) { } 0 0 0
790 0 $x = $class -> binf($sign);
791 } else { # nan
792 0 $x = $class -> bnan(@r);
793 }
794 }
795  
796 else { # normals
797 1 $mant = $class -> new(2) -> bpow($t) -> badd($mant);
798 1 ▶ if ($expo < $t) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expo < $t) { } 1 0 1
799 # compute (1/$b)**(N) rather than ($b)**(-N)
800 1 $x = $class -> new("0.5"); # 1/$b
801 1 $x = $x -> bpow($t - $expo) -> bmul($mant);
802 } else {
803 0 $x = $class -> new(2);
804 0 $x = $x -> bpow($expo - $t) -> bmul($mant);
805 }
806 1 ▶ $x = $x -> bneg() if $sign eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $sign eq "-" 0 1 1
807 }
808  
809 1 ▶ if ($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($selfref) { } 0 1 1
810 0 $self -> {sign} = $x -> {sign};
811 0 $self -> {_m} = $x -> {_m};
812 0 $self -> {_es} = $x -> {_es};
813 0 $self -> {_e} = $x -> {_e};
814 } else {
815 1 $self = $x;
816 }
817  
818 1 ▶ return $downgrade -> new($self -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $self->is_int 1 0 1
Condition33%1/3
defined $Math::BigFloat::downgrade and $self->is_int
!ll&&!rl&&r
001
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $self->is_int
defined $Math::BigFloat::downgrade $self->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $self->is_int
ABresult
0X0
100
111
819 if defined($downgrade) && $self -> is_int();
820 0 return $self -> round(@r);
821 }
822  
823 0 croak("The format '$format' is not yet supported.");
824 }
825  
826 sub bzero {
827 # create/assign '+0'
828  
829 # Class::method(...) -> Class->method(...)
830 603 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bzero: called CC=16 SCAR=28.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 603 603
Condition66%6/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
049554
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
4951080
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
1080495
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table16%1/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
831 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
832 {
833 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
834 # " use is as a method instead";
835 0 unshift @_, __PACKAGE__;
836 }
837  
838 603 my $self = shift;
839 603 my $selfref = ref $self;
840 603 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
4951080
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
841  
842 603 ▶ $self->import() if $IMPORT == 0; # make require work
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 1 602 603
843  
844 # Don't modify constant (read-only) objects.
845  
846 603 ▶ return $self if $selfref && $self->modify('bzero');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bzero") 0 603 603
Condition66%2/3
$selfref and $self->modify("bzero")
!ll&&!rl&&r
1084950
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
847  
848 # Get the rounding parameters, if any.
849  
850 603 my @r = @_;
851  
852 603 ▶ return $downgrade -> bzero(@r) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 1 602 603
853  
854 # If called as a class method, initialize a new object.
855  
856 602 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 107 495 602
857  
858 602 $self -> {sign} = '+';
859 602 $self -> {_m} = $LIB -> _zero();
860 602 $self -> {_es} = '+';
861 602 $self -> {_e} = $LIB -> _zero();
862  
863 # If rounding parameters are given as arguments, use them. If no rounding
864 # parameters are given, and if called as a class method initialize the new
865 # instance with the class variables.
866  
867 #return $self -> round(@r); # this should work, but doesnt; fixme!
868  
869 602 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 64 538 602
870 64 ▶ croak "can't specify both accuracy and precision"
Branch: 1 branch
BranchTrueFalseTotal
if @r >= 2 and defined $r[0] and defined $r[1] 0 64 64
Condition83%5/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
5590
@r >= 2 and defined $r[0]
!ll&&!rl&&r
4969
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
871 if @r >= 2 && defined($r[0]) && defined($r[1]);
872 64 $self->{_a} = $r[0];
873 64 $self->{_p} = $r[1];
874 } else {
875 538 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 101 437 538
876 101 $self->{_a} = $class -> accuracy();
877 101 $self->{_p} = $class -> precision();
878 }
879 }
880  
881 602 return $self;
882 }
883  
884 sub bone {
885 # Create or assign '+1' (or -1 if given sign '-').
886  
887 # Class::method(...) -> Class->method(...)
888 1589 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bone: called CC=18 SCAR=29.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 1589 1589
Condition66%6/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
0221567
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
58210070
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
10070582
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
889 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
890 {
891 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
892 # " use is as a method instead";
893 0 unshift @_, __PACKAGE__;
894 }
895  
896 1589 my $self = shift;
897 1589 my $selfref = ref $self;
898 1589 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
58210070
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
899  
900 1589 ▶ $self->import() if $IMPORT == 0; # make require work
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 1 1588 1589
901  
902 # Don't modify constant (read-only) objects.
903  
904 1589 ▶ return $self if $selfref && $self->modify('bone');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bone") 0 1589 1589
Condition66%2/3
$selfref and $self->modify("bone")
!ll&&!rl&&r
10075820
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
905  
906 1589 ▶ return $downgrade -> bone(@_) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 1 1588 1589
907  
908 # Get the sign.
909  
910 1588 my $sign = '+'; # default is to return +1
911 1588 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])\s*$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/) 163 1425 1588
Condition100%3/3
defined $_[0] and $_[0] =~ /^\s*([+-])\s*$/
!ll&&!rl&&r
141411163
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
912 163 $sign = $1;
913 163 shift;
914 }
915  
916 # Get the rounding parameters, if any.
917  
918 1588 my @r = @_;
919  
920 # If called as a class method, initialize a new object.
921  
922 1588 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1006 582 1588
923  
924 1588 $self -> {sign} = $sign;
925 1588 $self -> {_m} = $LIB -> _one();
926 1588 $self -> {_es} = '+';
927 1588 $self -> {_e} = $LIB -> _zero();
928  
929 # If rounding parameters are given as arguments, use them. If no rounding
930 # parameters are given, and if called as a class method initialize the new
931 # instance with the class variables.
932  
933 #return $self -> round(@r); # this should work, but doesnt; fixme!
934  
935 1588 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 29 1559 1588
936 29 ▶ croak "can't specify both accuracy and precision"
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
937 if @r >= 2 && defined($r[0]) && defined($r[1]);
938 29 $self->{_a} = $_[0];
939 29 $self->{_p} = $_[1];
940 } else {
941 1559 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1000 559 1559
942 1000 $self->{_a} = $class -> accuracy();
943 1000 $self->{_p} = $class -> precision();
944 }
945 }
946  
947 1588 return $self;
948 }
949  
950 sub binf {
951 # create/assign a '+inf' or '-inf'
952  
953 # Class::method(...) -> Class->method(...)
954 2071 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine binf: called CC=19 SCAR=30.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 2071 2071
Condition66%6/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
0182053
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
31517560
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
17560315
MC/DC0%0/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table16%1/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
955 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
956 {
957 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
958 # " use is as a method instead";
959 0 unshift @_, __PACKAGE__;
960 }
961  
962 2071 my $self = shift;
963 2071 my $selfref = ref $self;
964 2071 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
31517560
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
965  
966 {
967 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
968 2071 ▶ if (${"${class}::_trap_inf"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_inf";}) 5 2066 2071
969 5 croak("Tried to create +-inf in $class->binf()");
970 }
971 }
972  
973 2066 ▶ $self->import() if $IMPORT == 0; # make require work
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 1 2065 2066
974  
975 # Don't modify constant (read-only) objects.
976  
977 2066 ▶ return $self if $selfref && $self->modify('binf');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("binf") 0 2066 2066
Condition66%2/3
$selfref and $self->modify("binf")
!ll&&!rl&&r
17543120
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
978  
979 2066 ▶ return $downgrade -> binf(@_) if $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if $Math::BigFloat::downgrade 7 2059 2066
980  
981 # Get the sign.
982  
983 2059 my $sign = '+'; # default is to return positive infinity
984 2059 ▶ if (defined($_[0]) && $_[0] =~ /^\s*([+-])(inf|$)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i) 1968 91 2059
Condition100%3/3
defined $_[0] and $_[0] =~ /^\s*([+-])(inf|$)/i
!ll&&!rl&&r
8741968
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
985 1968 $sign = $1;
986 1968 shift;
987 }
988  
989 # Get the rounding parameters, if any.
990  
991 2059 my @r = @_;
992  
993 # If called as a class method, initialize a new object.
994  
995 2059 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1752 307 2059
996  
997 2059 $self -> {sign} = $sign . 'inf';
998 2059 $self -> {_m} = $LIB -> _zero();
999 2059 $self -> {_es} = '+';
1000 2059 $self -> {_e} = $LIB -> _zero();
1001  
1002 # If rounding parameters are given as arguments, use them. If no rounding
1003 # parameters are given, and if called as a class method initialize the new
1004 # instance with the class variables.
1005  
1006 #return $self -> round(@r); # this should work, but doesnt; fixme!
1007  
1008 2059 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 530 1529 2059
1009 530 ▶ croak "can't specify both accuracy and precision"
Branch: 1 branch
BranchTrueFalseTotal
if @r >= 2 and defined $r[0] and defined $r[1] 0 530 530
Condition50%3/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
53000
@r >= 2 and defined $r[0]
!ll&&!rl&&r
125180
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
1010 if @r >= 2 && defined($r[0]) && defined($r[1]);
1011 530 $self->{_a} = $r[0];
1012 530 $self->{_p} = $r[1];
1013 } else {
1014 1529 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 1234 295 1529
1015 1234 $self->{_a} = $class -> accuracy();
1016 1234 $self->{_p} = $class -> precision();
1017 }
1018 }
1019  
1020 2059 return $self;
1021 }
1022  
1023 sub bnan {
1024 # create/assign a 'NaN'
1025  
1026 # Class::method(...) -> Class->method(...)
1027 2115 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bnan: called CC=17 SCAR=29.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 0 2115 2115
Condition66%6/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
0542061
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
108510300
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
103001085
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
1028 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1029 {
1030 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1031 # " use is as a method instead";
1032 0 unshift @_, __PACKAGE__;
1033 }
1034  
1035 2115 my $self = shift;
1036 2115 my $selfref = ref $self;
1037 2115 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
108510300
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1038  
1039 {
1040 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1041 2115 ▶ if (${"${class}::_trap_nan"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (${"${class}::_trap_nan";}) 3 2112 2115
1042 3 croak("Tried to create NaN in $class->bnan()");
1043 }
1044 }
1045  
1046 2112 ▶ $self->import() if $IMPORT == 0; # make require work
Branch: 1 branch
BranchTrueFalseTotal
if $IMPORT == 0 1 2111 2112
1047  
1048 # Don't modify constant (read-only) objects.
1049  
1050 2112 ▶ return $self if $selfref && $self->modify('bnan');
Branch: 1 branch
BranchTrueFalseTotal
if $selfref and $self->modify("bnan") 0 2112 2112
Condition66%2/3
$selfref and $self->modify("bnan")
!ll&&!rl&&r
102910830
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
1051  
1052 2112 ▶ return $downgrade -> bnan(@_) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 14 2098 2112
1053  
1054 # Get the rounding parameters, if any.
1055  
1056 2098 my @r = @_;
1057  
1058 # If called as a class method, initialize a new object.
1059  
1060 2098 ▶ $self = bless {}, $class unless $selfref;
Branch: 1 branch
BranchTrueFalseTotal
unless $selfref 1027 1071 2098
1061  
1062 2098 $self -> {sign} = $nan;
1063 2098 $self -> {_m} = $LIB -> _zero();
1064 2098 $self -> {_es} = '+';
1065 2098 $self -> {_e} = $LIB -> _zero();
1066  
1067 # If rounding parameters are given as arguments, use them. If no rounding
1068 # parameters are given, and if called as a class method initialize the new
1069 # instance with the class variables.
1070  
1071 #return $self -> round(@r); # this should work, but doesnt; fixme!
1072  
1073 2098 ▶ if (@r) {
Branch: 1 branch
BranchTrueFalseTotal
if (@r) { } 296 1802 2098
1074 296 ▶ croak "can't specify both accuracy and precision"
Branch: 1 branch
BranchTrueFalseTotal
if @r >= 2 and defined $r[0] and defined $r[1] 0 296 296
Condition50%3/6
@r >= 2 and defined $r[0] and defined $r[1]
!ll&&!rl&&r
29600
@r >= 2 and defined $r[0]
!ll&&!rl&&r
232730
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
1075 if @r >= 2 && defined($r[0]) && defined($r[1]);
1076 296 $self->{_a} = $r[0];
1077 296 $self->{_p} = $r[1];
1078 } else {
1079 1802 ▶ unless($selfref) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($selfref) 751 1051 1802
1080 751 $self->{_a} = $class -> accuracy();
1081 751 $self->{_p} = $class -> precision();
1082 }
1083 }
1084  
1085 2098 return $self;
1086 }
1087  
1088 sub bpi {
1089  
1090 # Class::method(...) -> Class->method(...)
1091 205 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bpi: called CC=23 SCAR=37.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 1 204 205
Condition66%6/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
10204
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
831210
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
121083
MC/DC25%1/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1092 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
1093 {
1094 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
1095 # " use is as a method instead";
1096 1 unshift @_, __PACKAGE__;
1097 }
1098  
1099 # Called as Argument list
1100 # --------- -------------
1101 # Math::BigFloat->bpi() ("Math::BigFloat")
1102 # Math::BigFloat->bpi(10) ("Math::BigFloat", 10)
1103 # $x->bpi() ($x)
1104 # $x->bpi(10) ($x, 10)
1105 # Math::BigFloat::bpi() ()
1106 # Math::BigFloat::bpi(10) (10)
1107 #
1108 # In ambiguous cases, we favour the OO-style, so the following case
1109 #
1110 # $n = Math::BigFloat->new("10");
1111 # $x = Math::BigFloat->bpi($n);
1112 #
1113 # which gives an argument list with the single element $n, is resolved as
1114 #
1115 # $n->bpi();
1116  
1117 205 my $self = shift;
1118 205 my $selfref = ref $self;
1119 205 ▶ my $class = $selfref || $self;
Condition66%2/3
$selfref || $self
l!l&&r!l&&!r
831220
MC/DC0%0/2
$selfref || $self
$selfref $self
Truth table66%2/3
A || B
A= $selfrefB= $self
ABresult
1X1
011
000
1120 205 my @r = @_; # rounding paramters
1121  
1122 205 ▶ if ($selfref) { # bpi() called as an instance method
Branch: 1 branch
BranchTrueFalseTotal
if ($selfref) { } 83 122 205
1123 83 ▶ return $self if $self -> modify('bpi');
Branch: 1 branch
BranchTrueFalseTotal
if $self->modify("bpi") 0 83 83
1124 } else { # bpi() called as a class method
1125 122 $self = bless {}, $class; # initialize new instance
1126 }
1127  
1128 205 ($self, @r) = $self -> _find_round_parameters(@r);
1129  
1130 # The accuracy, i.e., the number of digits. Pi has one digit before the
1131 # dot, so a precision of 4 digits is equivalent to an accuracy of 5 digits.
1132  
1133 205 ▶ my $n = defined $r[0] ? $r[0]
Branch: 2 branches
BranchTrueFalseTotal
defined $r[0] ? : 191 14 205
defined $r[1] ? : 0 14 14
1134 : defined $r[1] ? 1 - $r[1]
1135 : $self -> div_scale();
1136  
1137 205 ▶ my $rmode = defined $r[2] ? $r[2] : $self -> round_mode();
Branch: 1 branch
BranchTrueFalseTotal
defined $r[2] ? : 191 14 205
1138  
1139 205 my $pi;
1140  
1141 205 ▶ if ($n <= 1000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n <= 1000) { } 205 0 205
1142  
1143 # 75 x 14 = 1050 digits
1144  
1145 205 my $all_digits = <<EOF;
1146 314159265358979323846264338327950288419716939937510582097494459230781640628
1147 620899862803482534211706798214808651328230664709384460955058223172535940812
1148 848111745028410270193852110555964462294895493038196442881097566593344612847
1149 564823378678316527120190914564856692346034861045432664821339360726024914127
1150 372458700660631558817488152092096282925409171536436789259036001133053054882
1151 046652138414695194151160943305727036575959195309218611738193261179310511854
1152 807446237996274956735188575272489122793818301194912983367336244065664308602
1153 139494639522473719070217986094370277053921717629317675238467481846766940513
1154 200056812714526356082778577134275778960917363717872146844090122495343014654
1155 958537105079227968925892354201995611212902196086403441815981362977477130996
1156 051870721134999999837297804995105973173281609631859502445945534690830264252
1157 230825334468503526193118817101000313783875288658753320838142061717766914730
1158 359825349042875546873115956286388235378759375195778185778053217122680661300
1159 192787661119590921642019893809525720106548586327886593615338182796823030195
1160 EOF
1161  
1162 # Should we round up?
1163  
1164 205 my $round_up;
1165  
1166 # From the string above, we need to extract the number of digits we
1167 # want plus extra characters for the newlines.
1168  
1169 205 my $nchrs = $n + int($n / 75);
1170  
1171 # Extract the digits we want.
1172  
1173 205 my $digits = substr($all_digits, 0, $nchrs);
1174  
1175 # Find out whether we should round up or down. Rounding is easy, since
1176 # pi is trancendental. With directed rounding, it doesn't matter what
1177 # the following digits are. With rounding to nearest, we only have to
1178 # look at one extra digit.
1179  
1180 205 ▶ if ($rmode eq 'trunc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rmode eq 'trunc') { } 0 205 205
1181 0 $round_up = 0;
1182 } else {
1183 205 my $next_digit = substr($all_digits, $nchrs, 1);
1184 205 ▶ $round_up = $next_digit lt '5' ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$next_digit lt '5' ? : 86 119 205
1185 }
1186  
1187 # Remove the newlines.
1188  
1189 205 $digits =~ tr/0-9//cd;
1190  
1191 # Now do the rounding. We could easily make the regex substitution
1192 # handle all cases, but we avoid using the regex engine when it is
1193 # simple to avoid it.
1194  
1195 205 ▶ if ($round_up) {
Branch: 1 branch
BranchTrueFalseTotal
if ($round_up) 119 86 205
1196 119 my $last_digit = substr($digits, -1, 1);
1197 119 ▶ if ($last_digit lt '9') {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_digit lt '9') { } 108 11 119
1198 108 substr($digits, -1, 1) = ++$last_digit;
1199 } else {
1200 11 $digits =~ s{([0-8])(9+)$}
1201 11 { ($1 + 1) . ("0" x CORE::length($2)) }e;
1202 }
1203 }
1204  
1205 # Convert to an object.
1206  
1207 205 $pi = bless {
1208 sign => '+',
1209 _m => $LIB -> _new($digits),
1210 _es => '-',
1211 _e => $LIB -> _new($n - 1),
1212 }, $class;
1213  
1214 } else {
1215  
1216 # For large accuracy, the arctan formulas become very inefficient with
1217 # Math::BigFloat, so use Brent-Salamin (aka AGM or Gauss-Legendre).
1218  
1219 # Use a few more digits in the intermediate computations.
1220 0 $n += 8;
1221  
1222 0 ▶ $HALF = $class -> new($HALF) unless ref($HALF);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $HALF 0 0 0
1223 0 my ($an, $bn, $tn, $pn)
1224 = ($class -> bone, $HALF -> copy() -> bsqrt($n),
1225 $HALF -> copy() -> bmul($HALF), $class -> bone);
1226 0 ▶ while ($pn < $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pn < $n) 0 0 0
1227 0 my $prev_an = $an -> copy();
1228 0 $an = $an -> badd($bn) -> bmul($HALF, $n);
1229 0 $bn = $bn -> bmul($prev_an) -> bsqrt($n);
1230 0 $prev_an = $prev_an -> bsub($an);
1231 0 $tn = $tn -> bsub($pn * $prev_an * $prev_an);
1232 0 $pn = $pn -> badd($pn);
1233 }
1234 0 $an = $an -> badd($bn);
1235 0 $an = $an -> bmul($an, $n) -> bdiv(4 * $tn, $n);
1236  
1237 0 $an = $an -> round(@r);
1238 0 $pi = $an;
1239 }
1240  
1241 205 ▶ if (defined $r[0]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $r[0]) { } 191 14 205
elsif (defined $r[1]) { } 0 14 14
1242 191 $pi -> accuracy($r[0]);
1243 } elsif (defined $r[1]) {
1244 0 $pi -> precision($r[1]);
1245 }
1246  
1247 205 for my $key (qw/ sign _m _es _e _a _p /) {
1248 1230 $self -> {$key} = $pi -> {$key};
1249 }
1250  
1251 205 ▶ return $downgrade -> new($self -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $self->is_int 0 205 205
Condition33%1/3
defined $Math::BigFloat::downgrade and $self->is_int
!ll&&!rl&&r
20500
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $self->is_int
defined $Math::BigFloat::downgrade $self->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $self->is_int
ABresult
0X0
100
111
1252 if defined($downgrade) && $self->is_int();
1253 205 return $self;
1254 }
1255  
1256 sub copy {
1257 17365 ▶ my ($x, $class);
Subroutine copy: called CC=5 SCAR=16.4
Pod documented
1258 17365 ▶ if (ref($_[0])) { # $y = $x -> copy()
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 17365 0 17365
1259 17365 $x = shift;
1260 17365 $class = ref($x);
1261 } else { # $y = Math::BigInt -> copy($y)
1262 0 $class = shift;
1263 0 $x = shift;
1264 }
1265  
1266 17365 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 17365 17365
1267  
1268 17365 my $copy = bless {}, $class;
1269  
1270 17365 $copy->{sign} = $x->{sign};
1271 17365 $copy->{_es} = $x->{_es};
1272 17365 $copy->{_m} = $LIB->_copy($x->{_m});
1273 17365 $copy->{_e} = $LIB->_copy($x->{_e});
1274 17365 ▶ $copy->{_a} = $x->{_a} if exists $x->{_a};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{"_a"} 14723 2642 17365
1275 17365 ▶ $copy->{_p} = $x->{_p} if exists $x->{_p};
Branch: 1 branch
BranchTrueFalseTotal
if exists $x->{"_p"} 14545 2820 17365
1276  
1277 17365 return $copy;
1278 }
1279  
1280 sub as_int {
1281 # return copy as a bigint representation of this Math::BigFloat number
1282 650 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_int: called CC=8 SCAR=20.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 650 0 650
1283 650 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 650 650
1284  
1285 650 ▶ return $x -> copy() if $x -> isa("Math::BigInt");
Branch: 1 branch
BranchTrueFalseTotal
if $x->isa("Math::BigInt") 0 650 650
1286  
1287 # disable upgrading and downgrading
1288  
1289 650 require Math::BigInt;
1290 650 my $upg = Math::BigInt -> upgrade();
1291 650 my $dng = Math::BigInt -> downgrade();
1292 650 Math::BigInt -> upgrade(undef);
1293 650 Math::BigInt -> downgrade(undef);
1294  
1295 650 my $y;
1296 650 ▶ if ($x -> is_inf()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 8 642 650
elsif ($x->is_nan) { } 4 638 642
1297 8 $y = Math::BigInt -> binf($x->sign());
1298 } elsif ($x -> is_nan()) {
1299 4 $y = Math::BigInt -> bnan();
1300 } else {
1301 638 $y = $LIB->_copy($x->{_m});
1302 638 ▶ if ($x->{_es} eq '-') { # < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 183 455 638
elsif (not $LIB->_is_zero($x->{'_e'})) { } 14 441 455
1303 183 $y = $LIB->_rsft($y, $x->{_e}, 10);
1304 } elsif (! $LIB->_is_zero($x->{_e})) { # > 0
1305 14 $y = $LIB->_lsft($y, $x->{_e}, 10);
1306 }
1307 638 $y = Math::BigInt->new($x->{sign} . $LIB->_str($y));
1308 }
1309  
1310 # reset upgrading and downgrading
1311  
1312 650 Math::BigInt -> upgrade($upg);
1313 650 Math::BigInt -> downgrade($dng);
1314  
1315 650 return $y;
1316 }
1317  
1318 sub as_float {
1319 2 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine as_float: called CC=4 SCAR=21.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
1320 2 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 2 2
1321  
1322 2 ▶ return $x -> copy() if $x -> isa("Math::BigFloat");
Branch: 1 branch
BranchTrueFalseTotal
if $x->isa("Math::BigFloat") 2 0 2
1323  
1324 # disable upgrading and downgrading
1325  
1326 0 require Math::BigFloat;
1327 0 my $upg = Math::BigFloat -> upgrade();
1328 0 my $dng = Math::BigFloat -> downgrade();
1329 0 Math::BigFloat -> upgrade(undef);
1330 0 Math::BigFloat -> downgrade(undef);
1331  
1332 0 my $y = Math::BigFloat -> new($x);
1333  
1334 # reset upgrading and downgrading
1335  
1336 0 Math::BigFloat -> upgrade($upg);
1337 0 Math::BigFloat -> downgrade($dng);
1338  
1339 0 return $y;
1340 }
1341  
1342 ###############################################################################
1343 # Boolean methods
1344 ###############################################################################
1345  
1346 sub is_zero {
1347 # return true if arg (BFLOAT or num_str) is zero
1348 110809 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_zero: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 110805 4 110809
1349  
1350 110809 ▶ ($x->{sign} eq '+' && $LIB->_is_zero($x->{_m})) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '+' && $LIB->_is_zero($x->{'_m'}) ? : 1725 109084 110809
Condition100%3/3
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_m"})
!ll&&!rl&&r
76281014561725
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
1351 }
1352  
1353 sub is_one {
1354 # return true if arg (BFLOAT or num_str) is +1 or -1 if signis given
1355 2958 ▶ my (undef, $x, $sign) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_one: called CC=7 SCAR=19.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2954 4 2958
1356  
1357 2958 ▶ $sign = '+' if !defined $sign || $sign ne '-';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $sign or $sign ne "-" 2375 583 2958
Condition100%3/3
not defined $sign or $sign ne "-"
l!l&&r!l&&!r
1729646583
MC/DC100%2/2
not defined $sign or $sign ne "-"
not defined $sign $sign ne "-"
Truth table100%3/3
not(A) or B
A= not defined $signB= $sign ne "-"
ABresult
1X1
011
000
1358  
1359 ($x->{sign} eq $sign &&
1360 $LIB->_is_zero($x->{_e}) &&
1361 2958 ▶ $LIB->_is_one($x->{_m})) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq $sign && $LIB->_is_zero($x->{'_e'}) && $LIB->_is_one($x->{'_m'}) ? : 285 2673 2958
Condition100%6/6
$x->{"sign"} eq $sign and $LIB->_is_zero($x->{"_e"}) and $LIB->_is_one($x->{"_m"})
!ll&&!rl&&r
1700973285
$x->{"sign"} eq $sign and $LIB->_is_zero($x->{"_e"})
!ll&&!rl&&r
8048961258
MC/DC100%3/3
$x->{"sign"} eq $sign and $LIB->_is_zero($x->{"_e"}) and $LIB->_is_one($x->{"_m"})
$x->{"sign"} eq $sign $LIB->_is_zero($x->{"_e"}) $LIB->_is_one($x->{"_m"})
Truth table100%4/4
A and B and C
A= $x->{"sign"} eq $signB= $LIB->_is_zero($x->{"_e"})C= $LIB->_is_one($x->{"_m"})
ABCresult
0XX0
10X0
1100
1111
1362 }
1363  
1364 sub is_odd {
1365 # return true if arg (BFLOAT or num_str) is odd or false if even
1366 104 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_odd: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 104 0 104
1367  
1368 (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
1369 ($LIB->_is_zero($x->{_e})) &&
1370 104 ▶ ($LIB->_is_odd($x->{_m}))) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^[+-]$/ && $LIB->_is_zero($x->{'_e'}) && $LIB->_is_odd($x->{'_m'}) ? : 40 64 104
Condition100%6/6
$x->{"sign"} =~ /^[+-]$/ and $LIB->_is_zero($x->{"_e"}) and $LIB->_is_odd($x->{"_m"})
!ll&&!rl&&r
362840
$x->{"sign"} =~ /^[+-]$/ and $LIB->_is_zero($x->{"_e"})
!ll&&!rl&&r
162068
MC/DC100%3/3
$x->{"sign"} =~ /^[+-]$/ and $LIB->_is_zero($x->{"_e"}) and $LIB->_is_odd($x->{"_m"})
$x->{"sign"} =~ /^[+-]$/ $LIB->_is_zero($x->{"_e"}) $LIB->_is_odd($x->{"_m"})
Truth table100%4/4
A and B and C
A= $x->{"sign"} =~ /^[+-]$/B= $LIB->_is_zero($x->{"_e"})C= $LIB->_is_odd($x->{"_m"})
ABCresult
0XX0
10X0
1100
1111
1371 }
1372  
1373 sub is_even {
1374 # return true if arg (BINT or num_str) is even or false if odd
1375 72 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_even: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 72 0 72
1376  
1377 (($x->{sign} =~ /^[+-]$/) && # NaN & +-inf aren't
1378 ($x->{_es} eq '+') && # 123.45 isn't
1379 72 ▶ ($LIB->_is_even($x->{_m}))) ? 1 : 0; # but 1200 is
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^[+-]$/ && $x->{'_es'} eq '+' && $LIB->_is_even($x->{'_m'}) ? : 24 48 72
Condition100%6/6
$x->{"sign"} =~ /^[+-]$/ and $x->{"_es"} eq "+" and $LIB->_is_even($x->{"_m"})
!ll&&!rl&&r
282024
$x->{"sign"} =~ /^[+-]$/ and $x->{"_es"} eq "+"
!ll&&!rl&&r
121644
MC/DC100%3/3
$x->{"sign"} =~ /^[+-]$/ and $x->{"_es"} eq "+" and $LIB->_is_even($x->{"_m"})
$x->{"sign"} =~ /^[+-]$/ $x->{"_es"} eq "+" $LIB->_is_even($x->{"_m"})
Truth table100%4/4
A and B and C
A= $x->{"sign"} =~ /^[+-]$/B= $x->{"_es"} eq "+"C= $LIB->_is_even($x->{"_m"})
ABCresult
0XX0
10X0
1100
1111
1380 }
1381  
1382 sub is_int {
1383 # return true if arg (BFLOAT or num_str) is an integer
1384 2548 ▶ my (undef, $x) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine is_int: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2548 0 2548
1385  
1386 (($x->{sign} =~ /^[+-]$/) && # NaN and +-inf aren't
1387 2548 ▶ ($x->{_es} eq '+')) ? 1 : 0; # 1e-1 => no integer
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} =~ /^[+-]$/ && $x->{'_es'} eq '+' ? : 2217 331 2548
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $x->{"_es"} eq "+"
!ll&&!rl&&r
652662217
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]$/ and $x->{"_es"} eq "+"
$x->{"sign"} =~ /^[+-]$/ $x->{"_es"} eq "+"
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]$/B= $x->{"_es"} eq "+"
ABresult
0X0
100
111
1388 }
1389  
1390 ###############################################################################
1391 # Comparison methods
1392 ###############################################################################
1393  
1394 sub bcmp {
1395 # Compares 2 values. Returns one of undef, <0, =0, >0. (suitable for sort)
1396  
1397 # set up parameters
1398 2913 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bcmp: called CC=40 SCAR=36.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 2799 114 2913
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
11132799
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
1399 ? (ref($_[0]), @_)
1400 : objectify(2, @_);
1401  
1402 2913 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 2913 2913
1403  
1404 # Handle all 'nan' cases.
1405  
1406 2913 ▶ return if ($x->{sign} eq $nan) || ($y->{sign} eq $nan);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 58 2855 2913
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
32262855
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
1407  
1408 # Handle all '+inf' and '-inf' cases.
1409  
1410 return 0 if ($x->{sign} eq '+inf' && $y->{sign} eq '+inf' ||
1411 2855 ▶ $x->{sign} eq '-inf' && $y->{sign} eq '-inf');
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" and $y->{"sign"} eq "+inf" or $x->{"sign"} eq "-inf" and $y->{"sign"} eq "-inf" 8 2847 2855
Condition100%9/9
$x->{"sign"} eq "+inf" and $y->{"sign"} eq "+inf" or $x->{"sign"} eq "-inf" and $y->{"sign"} eq "-inf"
l!l&&r!l&&!r
482843
$x->{"sign"} eq "+inf" and $y->{"sign"} eq "+inf"
!ll&&!rl&&r
2795564
$x->{"sign"} eq "-inf" and $y->{"sign"} eq "-inf"
!ll&&!rl&&r
2783644
MC/DC50%2/4
$x->{"sign"} eq "+inf" and $y->{"sign"} eq "+inf" or $x->{"sign"} eq "-inf" and $y->{"sign"} eq "-inf"
$x->{"sign"} eq "+inf" $y->{"sign"} eq "+inf" $x->{"sign"} eq "-inf" $y->{"sign"} eq "-inf"
Truth table42%3/7
A and B or C and D
A= $x->{"sign"} eq "+inf"B= $y->{"sign"} eq "+inf"C= $x->{"sign"} eq "-inf"D= $y->{"sign"} eq "-inf"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1412 2847 ▶ return +1 if $x->{sign} eq '+inf'; # x = +inf and y < +inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 56 2791 2847
1413 2791 ▶ return -1 if $x->{sign} eq '-inf'; # x = -inf and y > -inf
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-inf" 64 2727 2791
1414 2727 ▶ return -1 if $y->{sign} eq '+inf'; # x < +inf and y = +inf
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} eq "+inf" 0 2727 2727
1415 2727 ▶ return +1 if $y->{sign} eq '-inf'; # x > -inf and y = -inf
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} eq "-inf" 4 2723 2727
1416  
1417 # Handle all cases with opposite signs.
1418  
1419 2723 ▶ 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 "-" 456 2267 2723
Condition100%3/3
$x->{"sign"} eq "+" and $y->{"sign"} eq "-"
!ll&&!rl&&r
4501817456
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
1420 2267 ▶ 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 1961 2267
Condition100%3/3
$x->{"sign"} eq "-" and $y->{"sign"} eq "+"
!ll&&!rl&&r
1817144306
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
1421  
1422 # Handle all remaining zero cases.
1423  
1424 1961 my $xz = $x->is_zero();
1425 1961 my $yz = $y->is_zero();
1426 1961 ▶ return 0 if $xz && $yz; # 0 <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $yz 134 1827 1961
Condition100%3/3
$xz and $yz
!ll&&!rl&&r
1707120134
MC/DC100%2/2
$xz and $yz
$xz $yz
Truth table100%3/3
A and B
A= $xzB= $yz
ABresult
0X0
100
111
1427 1827 ▶ return -1 if $xz && $y->{sign} eq '+'; # 0 <=> +y
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $y->{"sign"} eq "+" 120 1707 1827
Condition66%2/3
$xz and $y->{"sign"} eq "+"
!ll&&!rl&&r
17070120
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
1428 1707 ▶ return +1 if $yz && $x->{sign} eq '+'; # +x <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $yz and $x->{"sign"} eq "+" 452 1255 1707
Condition66%2/3
$yz and $x->{"sign"} eq "+"
!ll&&!rl&&r
12550452
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
1429  
1430 # Both arguments are now finite, non-zero numbers with the same sign.
1431  
1432 1255 my $cmp;
1433  
1434 # The next step is to compare the exponents, but since each mantissa is an
1435 # integer of arbitrary value, the exponents must be normalized by the length
1436 # of the mantissas before we can compare them.
1437  
1438 1255 my $mxl = $LIB->_len($x->{_m});
1439 1255 my $myl = $LIB->_len($y->{_m});
1440  
1441 # If the mantissas have the same length, there is no point in normalizing
1442 # the exponents by the length of the mantissas, so treat that as a special
1443 # case.
1444  
1445 1255 ▶ if ($mxl == $myl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mxl == $myl) 1134 121 1255
1446  
1447 # First handle the two cases where the exponents have different signs.
1448  
1449 1134 ▶ if ($x->{_es} eq '+' && $y->{_es} eq '-') {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'_es'} eq '+' and $y->{'_es'} eq '-') { } 0 1134 1134
elsif ($x->{'_es'} eq '-' and $y->{'_es'} eq '+') { } 16 1118 1134
Condition66%2/3
$x->{"_es"} eq "+" and $y->{"_es"} eq "-"
!ll&&!rl&&r
9410400
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
1450 0 $cmp = +1;
1451 ▶ } elsif ($x->{_es} eq '-' && $y->{_es} eq '+') {
Condition100%3/3
$x->{"_es"} eq "-" and $y->{"_es"} eq "+"
!ll&&!rl&&r
10407816
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
1452 16 $cmp = -1;
1453 }
1454  
1455 # Then handle the case where the exponents have the same sign.
1456  
1457 else {
1458 1118 $cmp = $LIB->_acmp($x->{_e}, $y->{_e});
1459 1118 ▶ $cmp = -$cmp if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 78 1040 1118
1460 }
1461  
1462 # Adjust for the sign, which is the same for x and y, and bail out if
1463 # we're done.
1464  
1465 1134 ▶ $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 128 1006 1134
1466 1134 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 68 1066 1134
1467  
1468 }
1469  
1470 # We must normalize each exponent by the length of the corresponding
1471 # mantissa. Life is a lot easier if we first make both exponents
1472 # non-negative. We do this by adding the same positive value to both
1473 # exponent. This is safe, because when comparing the exponents, only the
1474 # relative difference is important.
1475  
1476 1187 my $ex;
1477 my $ey;
1478  
1479 1187 ▶ if ($x->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'_es'} eq '+') { } 1051 136 1187
1480  
1481 # If the exponent of x is >= 0 and the exponent of y is >= 0, there is
1482 # no need to do anything special.
1483  
1484 1051 ▶ if ($y->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{'_es'} eq '+') { } 1042 9 1051
1485 1042 $ex = $LIB->_copy($x->{_e});
1486 1042 $ey = $LIB->_copy($y->{_e});
1487 }
1488  
1489 # If the exponent of x is >= 0 and the exponent of y is < 0, add the
1490 # absolute value of the exponent of y to both.
1491  
1492 else {
1493 9 $ex = $LIB->_copy($x->{_e});
1494 9 $ex = $LIB->_add($ex, $y->{_e}); # ex + |ey|
1495 9 $ey = $LIB->_zero(); # -ex + |ey| = 0
1496 }
1497  
1498 } else {
1499  
1500 # If the exponent of x is < 0 and the exponent of y is >= 0, add the
1501 # absolute value of the exponent of x to both.
1502  
1503 136 ▶ if ($y->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{'_es'} eq '+') { } 25 111 136
1504 25 $ex = $LIB->_zero(); # -ex + |ex| = 0
1505 25 $ey = $LIB->_copy($y->{_e});
1506 25 $ey = $LIB->_add($ey, $x->{_e}); # ey + |ex|
1507 }
1508  
1509 # If the exponent of x is < 0 and the exponent of y is < 0, add the
1510 # absolute values of both exponents to both exponents.
1511  
1512 else {
1513 111 $ex = $LIB->_copy($y->{_e}); # -ex + |ey| + |ex| = |ey|
1514 111 $ey = $LIB->_copy($x->{_e}); # -ey + |ex| + |ey| = |ex|
1515 }
1516  
1517 }
1518  
1519 # Now we can normalize the exponents by adding lengths of the mantissas.
1520  
1521 1187 $ex = $LIB->_add($ex, $LIB->_new($mxl));
1522 1187 $ey = $LIB->_add($ey, $LIB->_new($myl));
1523  
1524 # We're done if the exponents are different.
1525  
1526 1187 $cmp = $LIB->_acmp($ex, $ey);
1527 1187 ▶ $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 136 1051 1187
1528 1187 ▶ return $cmp if $cmp;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp 90 1097 1187
1529  
1530 # Compare the mantissas, but first normalize them by padding the shorter
1531 # mantissa with zeros (shift left) until it has the same length as the
1532 # longer mantissa.
1533  
1534 1097 my $mx = $x->{_m};
1535 1097 my $my = $y->{_m};
1536  
1537 1097 ▶ if ($mxl > $myl) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mxl > $myl) { } 26 1071 1097
elsif ($mxl < $myl) { } 5 1066 1071
1538 26 $my = $LIB->_lsft($LIB->_copy($my), $LIB->_new($mxl - $myl), 10);
1539 } elsif ($mxl < $myl) {
1540 5 $mx = $LIB->_lsft($LIB->_copy($mx), $LIB->_new($myl - $mxl), 10);
1541 }
1542  
1543 1097 $cmp = $LIB->_acmp($mx, $my);
1544 1097 ▶ $cmp = -$cmp if $x->{sign} eq '-'; # 124 > 123, but -124 < -123
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 128 969 1097
1545 1097 return $cmp;
1546  
1547 }
1548  
1549 sub bacmp {
1550 # Compares 2 values, ignoring their signs.
1551 # Returns one of undef, <0, =0, >0. (suitable for sort)
1552  
1553 # set up parameters
1554 8979 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bacmp: called CC=23 SCAR=31.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 8948 31 8979
Condition66%2/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
0318948
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
1555 ? (ref($_[0]), @_)
1556 : objectify(2, @_);
1557  
1558 8979 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 8979 8979
1559  
1560 # handle +-inf and NaN's
1561 8979 ▶ if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/) 92 8887 8979
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
72208887
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
1562 92 ▶ return if (($x->{sign} eq $nan) || ($y->{sign} eq $nan));
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 28 64 92
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
161264
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
1563 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
1564 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
1565 16 return -1;
1566 }
1567  
1568 # shortcut
1569 8887 my $xz = $x->is_zero();
1570 8887 my $yz = $y->is_zero();
1571 8887 ▶ return 0 if $xz && $yz; # 0 <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $xz and $yz 4 8883 8887
Condition100%3/3
$xz and $yz
!ll&&!rl&&r
8843404
MC/DC100%2/2
$xz and $yz
$xz $yz
Truth table100%3/3
A and B
A= $xzB= $yz
ABresult
0X0
100
111
1572 8883 ▶ return -1 if $xz && !$yz; # 0 <=> +y
Branch: 1 branch
BranchTrueFalseTotal
if $xz and not $yz 40 8843 8883
Condition66%2/3
$xz and not $yz
!ll&&!rl&&r
8843040
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
1573 8843 ▶ return 1 if $yz && !$xz; # +x <=> 0
Branch: 1 branch
BranchTrueFalseTotal
if $yz and not $xz 40 8803 8843
Condition66%2/3
$yz and not $xz
!ll&&!rl&&r
8803040
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
1574  
1575 # adjust so that exponents are equal
1576 8803 my $lxm = $LIB->_len($x->{_m});
1577 8803 my $lym = $LIB->_len($y->{_m});
1578 8803 my ($xes, $yes) = (1, 1);
1579 8803 ▶ $xes = -1 if $x->{_es} ne '+';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} ne "+" 8161 642 8803
1580 8803 ▶ $yes = -1 if $y->{_es} ne '+';
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"_es"} ne "+" 7968 835 8803
1581 # the numify somewhat limits our length, but makes it much faster
1582 8803 my $lx = $lxm + $xes * $LIB->_num($x->{_e});
1583 8803 my $ly = $lym + $yes * $LIB->_num($y->{_e});
1584 8803 my $l = $lx - $ly;
1585 8803 ▶ return $l <=> 0 if $l != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $l != 0 4982 3821 8803
1586  
1587 # lengths (corrected by exponent) are equal
1588 # so make mantissa equal-length by padding with zero (shift left)
1589 3821 my $diff = $lxm - $lym;
1590 3821 my $xm = $x->{_m}; # not yet copy it
1591 3821 my $ym = $y->{_m};
1592 3821 ▶ if ($diff > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($diff > 0) { } 578 3243 3821
elsif ($diff < 0) { } 258 2985 3243
1593 578 $ym = $LIB->_copy($y->{_m});
1594 578 $ym = $LIB->_lsft($ym, $LIB->_new($diff), 10);
1595 } elsif ($diff < 0) {
1596 258 $xm = $LIB->_copy($x->{_m});
1597 258 $xm = $LIB->_lsft($xm, $LIB->_new(-$diff), 10);
1598 }
1599 3821 $LIB->_acmp($xm, $ym);
1600 }
1601  
1602 ###############################################################################
1603 # Arithmetic methods
1604 ###############################################################################
1605  
1606 sub bneg {
1607 # (BINT or num_str) return BINT
1608 # negate number or make a negated number from string
1609 475 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine bneg: called CC=10 SCAR=23.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 475 0 475
1610  
1611 475 ▶ return $x if $x->modify('bneg');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bneg") 0 475 475
1612  
1613 475 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 9 466 475
1614  
1615 # For +0 do not negate (to have always normalized +0).
1616 $x->{sign} =~ tr/+-/-+/
1617 466 ▶ unless $x->{sign} eq '+' && $LIB->_is_zero($x->{_m});
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_m"}) 461 5 466
Condition100%3/3
$x->{"sign"} eq "+" and $LIB->_is_zero($x->{"_m"})
!ll&&!rl&&r
933685
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
1618  
1619 466 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 1 463 464
Condition77%7/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
46150
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
302
$x->is_int or $x->is_inf
l!l&&r!l&&!r
212
MC/DC0%0/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table40%2/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1620 && ($x -> is_int() || $x -> is_inf() || $x -> is_nan());
1621 463 return $x -> round(@r);
1622 }
1623  
1624 sub bnorm {
1625 # bnorm() can't support rounding, because bround() and bfround() call
1626 # bnorm(), which would recurse indefinitely.
1627  
1628 # adjust m and e so that m is smallest possible
1629 72362 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine bnorm: called CC=12 SCAR=24.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 72362 0 72362
1630  
1631 72362 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 72362 72362
1632  
1633 # inf, nan etc
1634 72362 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 6 72356 72362
1635 6 ▶ return $downgrade -> new($x) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 2 4 6
1636 4 return $x;
1637 }
1638  
1639 72356 my $zeros = $LIB->_zeros($x->{_m}); # correct for trailing zeros
1640 72356 ▶ if ($zeros != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zeros != 0) { } 29435 42921 72356
1641 29435 my $z = $LIB->_new($zeros);
1642 29435 $x->{_m} = $LIB->_rsft($x->{_m}, $z, 10);
1643 29435 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 27727 1708 29435
1644 27727 ▶ if ($LIB->_acmp($x->{_e}, $z) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_acmp($x->{'_e'}, $z) >= 0) { } 27387 340 27727
1645 27387 $x->{_e} = $LIB->_sub($x->{_e}, $z);
1646 27387 ▶ $x->{_es} = '+' if $LIB->_is_zero($x->{_e});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_e"}) 226 27161 27387
1647 } else {
1648 340 $x->{_e} = $LIB->_sub($LIB->_copy($z), $x->{_e});
1649 340 $x->{_es} = '+';
1650 }
1651 } else {
1652 1708 $x->{_e} = $LIB->_add($x->{_e}, $z);
1653 }
1654 } else {
1655 # $x can only be 0Ey if there are no trailing zeros ('0' has 0 trailing
1656 # zeros). So, for something like 0Ey, set y to 0, and -0 => +0
1657 42921 ▶ if ($LIB->_is_zero($x->{_m})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{"_m"})) 399 42522 42921
1658 399 $x->{sign} = '+';
1659 399 $x->{_es} = '+';
1660 399 $x->{_e} = $LIB->_zero();
1661 }
1662 }
1663  
1664 72356 ▶ return $downgrade -> new($x)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 20 72336 72356
Condition100%3/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
72333320
MC/DC100%2/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table100%3/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
1665 if defined($downgrade) && $x->is_int();
1666 72336 return $x;
1667 }
1668  
1669 sub binc {
1670 # increment arg by one
1671 4362 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine binc: called CC=12 SCAR=26.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4362 0 4362
1672  
1673 4362 ▶ return $x if $x->modify('binc');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("binc") 0 4362 4362
1674  
1675 # Inf and NaN
1676  
1677 4362 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 4357 4362
1678 4357 ▶ return $x -> binf($x->{sign}, @r) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 9 4348 4357
1679  
1680 # Non-integer
1681  
1682 4348 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 461 3887 4348
1683 461 return $x->badd($class->bone(), @r);
1684 }
1685  
1686 # If the exponent is non-zero, convert the internal representation, so that,
1687 # e.g., 12e+3 becomes 12000e+0 and we can easily increment the mantissa.
1688  
1689 3887 ▶ if (!$LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 350 3537 3887
1690 350 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10); # 1e2 => 100
1691 350 $x->{_e} = $LIB->_zero(); # normalize
1692 350 $x->{_es} = '+';
1693 # we know that the last digit of $x will be '1' or '9', depending on the
1694 # sign
1695 }
1696  
1697 # now $x->{_e} == 0
1698 3887 ▶ if ($x->{sign} eq '+') {
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '+') { } 3871 16 3887
elsif ($x->{'sign'} eq '-') { } 16 0 16
1699 3871 $x->{_m} = $LIB->_inc($x->{_m});
1700 3871 return $x->bnorm()->bround(@r);
1701 } elsif ($x->{sign} eq '-') {
1702 16 $x->{_m} = $LIB->_dec($x->{_m});
1703 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
1704 16 return $x->bnorm()->bround(@r);
1705 }
1706  
1707 0 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 0 0
Condition0%0/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table0%0/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
1708 if defined($downgrade) && $x -> is_int();
1709 0 return $x;
1710 }
1711  
1712 sub bdec {
1713 # decrement arg by one
1714 168 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bdec: called CC=14 SCAR=27.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 168 0 168
1715  
1716 168 ▶ return $x if $x->modify('bdec');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdec") 0 168 168
1717  
1718 # Inf and NaN
1719  
1720 168 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 163 168
1721 163 ▶ return $x -> binf($x->{sign}, @r) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 9 154 163
1722  
1723 # Non-integer
1724  
1725 154 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 113 41 154
1726 113 return $x->badd($class->bone('-'), @r);
1727 }
1728  
1729 # If the exponent is non-zero, convert the internal representation, so that,
1730 # e.g., 12e+3 becomes 12000e+0 and we can easily increment the mantissa.
1731  
1732 41 ▶ if (!$LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 8 33 41
1733 8 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10); # 1e2 => 100
1734 8 $x->{_e} = $LIB->_zero(); # normalize
1735 8 $x->{_es} = '+';
1736 }
1737  
1738 # now $x->{_e} == 0
1739 41 my $zero = $x->is_zero();
1740 41 ▶ if (($x->{sign} eq '-') || $zero) { # x <= 0
Branch: 2 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq '-' or $zero) { } 21 20 41
elsif ($x->{'sign'} eq '+') { } 20 0 20
Condition100%3/3
$x->{"sign"} eq "-" or $zero
l!l&&r!l&&!r
16520
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
1741 21 $x->{_m} = $LIB->_inc($x->{_m});
1742 21 ▶ $x->{sign} = '-' if $zero; # 0 => 1 => -1
Branch: 1 branch
BranchTrueFalseTotal
if $zero 5 16 21
1743 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
1744 21 return $x->bnorm()->round(@r);
1745 }
1746 elsif ($x->{sign} eq '+') { # x > 0
1747 20 $x->{_m} = $LIB->_dec($x->{_m});
1748 20 return $x->bnorm()->round(@r);
1749 }
1750  
1751 0 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 0 0
Condition0%0/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table0%0/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
1752 if defined($downgrade) && $x -> is_int();
1753 0 return $x -> round(@r);
1754 }
1755  
1756 sub badd {
1757 # set up parameters
1758 13458 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine badd: called CC=24 SCAR=31.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 13424 34 13458
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
82613424
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
1759 ? (ref($_[0]), @_)
1760 : objectify(2, @_);
1761  
1762 13458 ▶ return $x if $x->modify('badd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("badd") 0 13458 13458
1763  
1764 # inf and NaN handling
1765 13458 ▶ if ($x->{sign} !~ /^[+-]$/ || $y->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/) 226 13232 13458
Condition100%3/3
$x->{"sign"} =~ /^[+-]$/ and $y->{"sign"} =~ /^[+-]$/
!ll&&!rl&&r
1626413232
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
1766  
1767 # $x is NaN and/or $y is NaN
1768 226 ▶ if ($x->{sign} eq $nan || $y->{sign} eq $nan) {
Branch: 3 branches
BranchTrueFalseTotal
if ($x->{'sign'} eq $nan or $y->{'sign'} eq $nan) { } 110 116 226
elsif ($x->{'sign'} =~ /^[+-]inf$/ and $y->{'sign'} =~ /^[+-]inf$/) { } 56 60 116
elsif ($y->{'sign'} =~ /^[+-]inf$/) { } 38 22 60
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
6050116
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
1769 110 $x = $x->bnan();
1770 }
1771  
1772 # $x is Inf and $y is Inf
1773 ▶ elsif ($x->{sign} =~ /^[+-]inf$/ && $y->{sign} =~ /^[+-]inf$/) {
Condition100%3/3
$x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/
!ll&&!rl&&r
382256
MC/DC100%2/2
$x->{"sign"} =~ /^[+-]inf$/ and $y->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} =~ /^[+-]inf$/ $y->{"sign"} =~ /^[+-]inf$/
Truth table100%3/3
A and B
A= $x->{"sign"} =~ /^[+-]inf$/B= $y->{"sign"} =~ /^[+-]inf$/
ABresult
0X0
100
111
1774 # +Inf + +Inf or -Inf + -Inf => same, rest is NaN
1775 56 ▶ $x = $x->bnan() if $x->{sign} ne $y->{sign};
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} ne $y->{"sign"} 24 32 56
1776 }
1777  
1778 # +-inf + something => +-inf; something +-inf => +-inf
1779 elsif ($y->{sign} =~ /^[+-]inf$/) {
1780 38 $x->{sign} = $y->{sign};
1781 }
1782  
1783 226 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 8 218 226
1784 218 return $x -> round(@r);
1785 }
1786  
1787 13232 ▶ return $upgrade->badd($x, $y, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 1 13231 13232
1788  
1789 13231 $r[3] = $y; # no push!
1790  
1791 # for speed: no add for $x + 0
1792 13231 ▶ if ($y->is_zero()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($y->is_zero) { } 24 13207 13231
elsif ($x->is_zero) { } 102 13105 13207
1793 24 $x = $x->round(@r);
1794 }
1795  
1796 # for speed: no add for 0 + $y
1797 elsif ($x->is_zero()) {
1798 # make copy, clobbering up x (modify in place!)
1799 102 $x->{_e} = $LIB->_copy($y->{_e});
1800 102 $x->{_es} = $y->{_es};
1801 102 $x->{_m} = $LIB->_copy($y->{_m});
1802 102 ▶ $x->{sign} = $y->{sign} || $nan;
Condition33%1/3
$y->{'sign'} || $nan
l!l&&r!l&&!r
10200
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
1803 102 $x = $x->round(@r);
1804 }
1805  
1806 # both $x and $y are non-zero
1807 else {
1808  
1809 # take lower of the two e's and adapt m1 to it to match m2
1810 13105 my $e = $y->{_e};
1811 13105 ▶ $e = $LIB->_zero() if !defined $e; # if no BFLOAT?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 0 13105 13105
1812 13105 $e = $LIB->_copy($e); # make copy (didn't do it yet)
1813  
1814 13105 my $es;
1815  
1816 13105 ▶ ($e, $es) = $LIB -> _ssub($e, $y->{_es} || '+', $x->{_e}, $x->{_es});
Condition50%1/2
$y->{'_es'} || '+'
l!l
131050
MC/DC0%0/1
$y->{'_es'} || '+'
$y->{'_es'}
Truth table50%1/2
A
A= $y->{'_es'}
Aresult
11
00
1817  
1818 13105 my $add = $LIB->_copy($y->{_m});
1819  
1820 13105 ▶ if ($es eq '-') { # < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($es eq '-') { } 7475 5630 13105
elsif (not $LIB->_is_zero($e)) { } 789 4841 5630
1821 7475 $x->{_m} = $LIB->_lsft($x->{_m}, $e, 10);
1822 7475 ($x->{_e}, $x->{_es}) = $LIB -> _sadd($x->{_e}, $x->{_es}, $e, $es);
1823 } elsif (!$LIB->_is_zero($e)) { # > 0
1824 789 $add = $LIB->_lsft($add, $e, 10);
1825 }
1826  
1827 # else: both e are the same, so just leave them
1828  
1829 13105 ▶ if ($x->{sign} eq $y->{sign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq $y->{'sign'}) { } 11511 1594 13105
1830 11511 $x->{_m} = $LIB->_add($x->{_m}, $add);
1831 } else {
1832 ($x->{_m}, $x->{sign}) =
1833 1594 $LIB -> _sadd($x->{_m}, $x->{sign}, $add, $y->{sign});
1834 }
1835  
1836 # delete trailing zeros, then round
1837 13105 $x = $x->bnorm()->round(@r);
1838 }
1839  
1840 13231 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 7 13224 13231
Condition66%2/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
1322407
MC/DC50%1/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table66%2/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
1841 if defined($downgrade) && $x -> is_int();
1842 13224 return $x; # rounding already done above
1843 }
1844  
1845 sub bsub {
1846 # set up parameters
1847 1655 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bsub: called CC=12 SCAR=25.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 1622 33 1655
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
6271622
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
1848 ? (ref($_[0]), @_)
1849 : objectify(2, @_);
1850  
1851 1655 ▶ return $x if $x -> modify('bsub');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsub") 0 1655 1655
1852  
1853 1655 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) { } 42 1613 1655
1854 42 $x = $x -> round(@r);
1855 } else {
1856  
1857 # To correctly handle the special case $x -> bsub($x), we note the sign
1858 # of $x, then flip the sign of $y, and if the sign of $x changed too,
1859 # then we know that $x and $y are the same object.
1860  
1861 1613 my $xsign = $x -> {sign};
1862 1613 $y -> {sign} =~ tr/+-/-+/; # does nothing for NaN
1863 1613 ▶ if ($xsign ne $x -> {sign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsign ne $x->{"sign"}) 24 1589 1613
1864 # special case of $x -> bsub($x) results in 0
1865 24 ▶ if ($xsign =~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsign =~ /^[+-]$/) { } 16 8 24
1866 16 $x = $x -> bzero(@r);
1867 } else {
1868 8 $x = $x -> bnan(); # NaN, -inf, +inf
1869 }
1870 24 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 24 24
1871 24 return $x -> round(@r);
1872 }
1873 1589 $x = $x -> badd($y, @r); # badd does not leave internal zeros
1874 1589 $y -> {sign} =~ tr/+-/-+/; # reset $y (does nothing for NaN)
1875 }
1876  
1877 1631 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 4 1623 1627
Condition77%7/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
162308
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
620
$x->is_int or $x->is_inf
l!l&&r!l&&!r
422
MC/DC25%1/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table40%2/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1878 if defined($downgrade) && ($x->is_int() || $x->is_inf() || $x->is_nan());
1879 1623 $x; # already rounded by badd() or no rounding
1880 }
1881  
1882 sub bmul {
1883 # multiply two numbers
1884  
1885 # set up parameters
1886 19512 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bmul: called CC=20 SCAR=30.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 19213 299 19512
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
1428519213
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
1887 ? (ref($_[0]), @_)
1888 : objectify(2, @_);
1889  
1890 19512 ▶ return $x if $x->modify('bmul');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmul") 0 19512 19512
1891  
1892 19512 ▶ return $x->bnan(@r) if ($x->{sign} eq $nan) || ($y->{sign} eq $nan);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 57 19455 19512
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
332419455
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
1893  
1894 # inf handling
1895 19455 ▶ if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/) 85 19370 19455
Condition100%3/3
$x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/
l!l&&r!l&&!r
533219370
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
1896 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
1897 # result will always be +-inf:
1898 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
1899 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
1900 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
1901 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
1902 36 return $x->binf('-', @r);
1903 }
1904  
1905 19370 ▶ return $upgrade->bmul($x, $y, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 1 19369 19370
1906  
1907 # aEb * cEd = (a*c)E(b+d)
1908 19369 $x->{_m} = $LIB->_mul($x->{_m}, $y->{_m});
1909 ($x->{_e}, $x->{_es})
1910 19369 = $LIB -> _sadd($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
1911  
1912 19369 $r[3] = $y; # no push!
1913  
1914 # adjust sign:
1915 19369 ▶ $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} ne $y->{'sign'} ? : 559 18810 19369
1916 19369 $x = $x->bnorm->round(@r);
1917  
1918 19369 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 19365 19365
Condition44%4/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
1936540
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
400
$x->is_int or $x->is_inf
l!l&&r!l&&!r
400
MC/DC0%0/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1919 if defined($downgrade) && ($x->is_int() || $x->is_inf() || $x->is_nan());
1920 19365 return $x;
1921 }
1922  
1923 sub bmuladd {
1924 # multiply two numbers and add the third to the result
1925  
1926 # set up parameters
1927 247 ▶ my ($class, $x, $y, $z, @r)
Subroutine bmuladd: called CC=28 SCAR=34.7
Pod documented
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
1928 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
1929 ? (ref($_[0]), @_)
1930 : objectify(3, @_);
1931  
1932 247 ▶ return $x if $x->modify('bmuladd');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmuladd") 0 247 247
1933  
1934 return $x->bnan(@r) if (($x->{sign} eq $nan) ||
1935 ($y->{sign} eq $nan) ||
1936 247 ▶ ($z->{sign} eq $nan));
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan or $z->{"sign"} eq $nan 16 214 230
Condition100%6/6
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan or $z->{"sign"} eq $nan
l!l&&r!l&&!r
294214
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
1712218
MC/DC100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan or $z->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan $z->{"sign"} eq $nan
Truth table100%4/4
A or B or C
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nanC= $z->{"sign"} eq $nan
ABCresult
1XX1
01X1
0011
0000
1937  
1938 # inf handling
1939 214 ▶ if (($x->{sign} =~ /^[+-]inf$/) || ($y->{sign} =~ /^[+-]inf$/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/) 18 196 214
Condition66%2/3
$x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/
l!l&&r!l&&!r
180196
MC/DC50%1/2
$x->{"sign"} =~ /^[+-]inf$/ or $y->{"sign"} =~ /^[+-]inf$/
$x->{"sign"} =~ /^[+-]inf$/ $y->{"sign"} =~ /^[+-]inf$/
Truth table66%2/3
A or B
A= $x->{"sign"} =~ /^[+-]inf$/B= $y->{"sign"} =~ /^[+-]inf$/
ABresult
1X1
011
000
1940 18 ▶ return $x->bnan(@r) if $x->is_zero() || $y->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $y->is_zero 0 18 18
Condition33%1/3
$x->is_zero or $y->is_zero
l!l&&r!l&&!r
0018
MC/DC0%0/2
$x->is_zero or $y->is_zero
$x->is_zero $y->is_zero
Truth table33%1/3
A or B
A= $x->is_zeroB= $y->is_zero
ABresult
1X1
011
000
1941 # result will always be +-inf:
1942 # +inf * +/+inf => +inf, -inf * -/-inf => +inf
1943 # +inf * -/-inf => -inf, -inf * +/+inf => -inf
1944 18 ▶ return $x->binf(@r) if ($x->{sign} =~ /^\+/ && $y->{sign} =~ /^\+/);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/ 6 12 18
Condition100%3/3
$x->{"sign"} =~ /^\+/ and $y->{"sign"} =~ /^\+/
!ll&&!rl&&r
846
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
1945 12 ▶ return $x->binf(@r) if ($x->{sign} =~ /^-/ && $y->{sign} =~ /^-/);
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/ 4 8 12
Condition100%3/3
$x->{"sign"} =~ /^-/ and $y->{"sign"} =~ /^-/
!ll&&!rl&&r
444
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
1946 8 return $x->binf('-', @r);
1947 }
1948  
1949 # aEb * cEd = (a*c)E(b+d)
1950 196 $x->{_m} = $LIB->_mul($x->{_m}, $y->{_m});
1951 ($x->{_e}, $x->{_es})
1952 196 = $LIB -> _sadd($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
1953  
1954 196 $r[3] = $y; # no push!
1955  
1956 # adjust sign:
1957 196 ▶ $x->{sign} = $x->{sign} ne $y->{sign} ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} ne $y->{'sign'} ? : 44 152 196
1958  
1959 # z=inf handling (z=NaN handled above)
1960 196 ▶ if ($z->{sign} =~ /^[+-]inf$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($z->{"sign"} =~ /^[+-]inf$/) 1 195 196
1961 1 $x->{sign} = $z->{sign};
1962 1 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 1 0 1
1963 0 return $x -> round(@r);
1964 }
1965  
1966 # take lower of the two e's and adapt m1 to it to match m2
1967 195 my $e = $z->{_e};
1968 195 ▶ $e = $LIB->_zero() if !defined $e; # if no BFLOAT?
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e 0 195 195
1969 195 $e = $LIB->_copy($e); # make copy (didn't do it yet)
1970  
1971 195 my $es;
1972  
1973 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
1974  
1975 195 my $add = $LIB->_copy($z->{_m});
1976  
1977 195 ▶ if ($es eq '-') # < 0
Branch: 2 branches
BranchTrueFalseTotal
if ($es eq '-') { } 4 191 195
elsif (not $LIB->_is_zero($e)) { } 9 182 191
1978 {
1979 4 $x->{_m} = $LIB->_lsft($x->{_m}, $e, 10);
1980 4 ($x->{_e}, $x->{_es}) = $LIB -> _sadd($x->{_e}, $x->{_es}, $e, $es);
1981 } elsif (!$LIB->_is_zero($e)) # > 0
1982 {
1983 9 $add = $LIB->_lsft($add, $e, 10);
1984 }
1985 # else: both e are the same, so just leave them
1986  
1987 195 ▶ if ($x->{sign} eq $z->{sign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq $z->{'sign'}) { } 151 44 195
1988 # add
1989 151 $x->{_m} = $LIB->_add($x->{_m}, $add);
1990 } else {
1991 ($x->{_m}, $x->{sign}) =
1992 44 $LIB -> _sadd($x->{_m}, $x->{sign}, $add, $z->{sign});
1993 }
1994  
1995 # delete trailing zeros, then round
1996 195 $x = $x->bnorm()->round(@r);
1997  
1998 195 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 192 192
Condition44%4/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
19230
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
300
$x->is_int or $x->is_inf
l!l&&r!l&&!r
300
MC/DC0%0/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
1999 if defined($downgrade) && ($x->is_int() || $x->is_inf() || $x->is_nan());
2000 192 return $x;
2001 }
2002  
2003 sub bdiv {
2004 # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return
2005 # (BFLOAT, BFLOAT) (quo, rem) or BFLOAT (only quo)
2006  
2007 # set up parameters
2008 9074 ▶ my ($class, $x, $y, @r) = (ref($_[0]), @_);
Subroutine bdiv: called CC=60 SCAR=42.2
Pod documented
2009 # objectify is costly, so avoid it
2010 9074 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 137 8937 9074
Condition100%3/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
70678937
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
2011 137 ($class, $x, $y, @r) = objectify(2, @_);
2012 }
2013  
2014 9074 ▶ return $x if $x->modify('bdiv');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdiv") 0 9074 9074
2015  
2016 9074 my $wantarray = wantarray; # call only once
2017  
2018 # At least one argument is NaN. This is handled the same way as in
2019 # Math::BigInt -> bdiv().
2020  
2021 9074 ▶ if ($x -> is_nan() || $y -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $y->is_nan) 68 9006 9074
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
32369006
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
2022 68 ▶ return $wantarray ? ($x -> bnan(@r), $class -> bnan(@r))
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 24 44 68
2023 : $x -> bnan(@r);
2024 }
2025  
2026 # Divide by zero and modulo zero. This is handled the same way as in
2027 # Math::BigInt -> bdiv(). See the comment in the code for Math::BigInt ->
2028 # bdiv() for further details.
2029  
2030 9006 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 55 8951 9006
2031 55 my ($quo, $rem);
2032 55 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 16 39 55
2033 16 $rem = $x -> copy() -> round(@r);
2034 16 ▶ $rem = $downgrade -> new($rem, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $rem->is_int 0 16 16
Condition33%1/3
defined $Math::BigFloat::downgrade and $rem->is_int
!ll&&!rl&&r
1600
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $rem->is_int
defined $Math::BigFloat::downgrade $rem->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $rem->is_int
ABresult
0X0
100
111
2035 if defined($downgrade) && $rem -> is_int();
2036 }
2037 55 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) { } 21 34 55
2038 21 $quo = $x -> bnan(@r);
2039 } else {
2040 34 $quo = $x -> binf($x -> {sign}, @r);
2041 }
2042 52 ▶ return $wantarray ? ($quo, $rem) : $quo;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 16 36 52
2043 }
2044  
2045 # Numerator (dividend) is +/-inf. This is handled the same way as in
2046 # Math::BigInt -> bdiv(). See the comment in the code for Math::BigInt ->
2047 # bdiv() for further details.
2048  
2049 8951 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 40 8911 8951
2050 40 my ($quo, $rem);
2051 40 ▶ $rem = $class -> bnan(@r) if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 16 24 40
2052 40 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) { } 16 24 40
2053 16 $quo = $x -> bnan(@r);
2054 } else {
2055 24 ▶ my $sign = $x -> bcmp(0) == $y -> bcmp(0) ? '+' : '-';
Branch: 1 branch
BranchTrueFalseTotal
$x->bcmp(0) == $y->bcmp(0) ? : 12 12 24
2056 24 $quo = $x -> binf($sign, @r);
2057 }
2058 40 ▶ return $wantarray ? ($quo, $rem) : $quo;
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 16 24 40
2059 }
2060  
2061 # Denominator (divisor) is +/-inf. This is handled the same way as in
2062 # Math::BigInt -> bdiv(), with one exception: In scalar context,
2063 # Math::BigFloat does true division (although rounded), not floored division
2064 # (F-division), so a finite number divided by +/-inf is always zero. See the
2065 # comment in the code for Math::BigInt -> bdiv() for further details.
2066  
2067 8911 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 40 8871 8911
2068 40 my ($quo, $rem);
2069 40 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) { } 16 24 40
2070 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
2071 12 $rem = $x -> copy() -> round(@r);
2072 12 ▶ $rem = $downgrade -> new($rem, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $rem->is_int 0 12 12
Condition33%1/3
defined $Math::BigFloat::downgrade and $rem->is_int
!ll&&!rl&&r
1200
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $rem->is_int
defined $Math::BigFloat::downgrade $rem->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $rem->is_int
ABresult
0X0
100
111
2073 if defined($downgrade) && $rem -> is_int();
2074 12 $quo = $x -> bzero(@r);
2075 } else {
2076 4 $rem = $class -> binf($y -> {sign}, @r);
2077 4 $quo = $x -> bone('-', @r);
2078 }
2079 16 return ($quo, $rem);
2080 } else {
2081 24 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 24 0 24
2082 24 ▶ if ($x -> is_nan() || $x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan or $x->is_inf) { } 0 24 24
Condition33%1/3
$x->is_nan or $x->is_inf
l!l&&r!l&&!r
0024
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
2083 0 return $x -> bnan(@r);
2084 } else {
2085 24 return $x -> bzero(@r);
2086 }
2087 }
2088 }
2089 }
2090  
2091 # At this point, both the numerator and denominator are finite numbers, and
2092 # the denominator (divisor) is non-zero.
2093  
2094 # x == 0?
2095 8871 ▶ if ($x->is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 58 8813 8871
2096 58 my ($quo, $rem);
2097 58 $quo = $x->round(@r);
2098 58 ▶ $quo = $downgrade -> new($quo, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $quo->is_int 1 57 58
Condition66%2/3
defined $Math::BigFloat::downgrade and $quo->is_int
!ll&&!rl&&r
5701
MC/DC50%1/2
defined $Math::BigFloat::downgrade and $quo->is_int
defined $Math::BigFloat::downgrade $quo->is_int
Truth table66%2/3
A and B
A= defined $Math::BigFloat::downgradeB= $quo->is_int
ABresult
0X0
100
111
2099 if defined($downgrade) && $quo -> is_int();
2100 58 ▶ if ($wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wantarray) 13 45 58
2101 13 $rem = $class -> bzero(@r);
2102 13 return $quo, $rem;
2103 }
2104 45 return $quo;
2105 }
2106  
2107 # Division might return a value that we can not represent exactly, so
2108 # upgrade, if upgrading is enabled.
2109  
2110 return $upgrade -> bdiv($x, $y, @r)
2111 8813 ▶ if defined($upgrade) && !wantarray && !$LIB -> _is_one($y -> {_m});
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade and not wantarray and not $LIB->_is_one($y->{"_m"}) 0 8813 8813
Condition33%2/6
defined $Math::BigFloat::upgrade and not wantarray and not $LIB->_is_one($y->{"_m"})
!ll&&!rl&&r
881300
defined $Math::BigFloat::upgrade and not wantarray
!ll&&!rl&&r
881300
MC/DC0%0/3
defined $Math::BigFloat::upgrade and not wantarray and not $LIB->_is_one($y->{"_m"})
defined $Math::BigFloat::upgrade not wantarray not $LIB->_is_one($y->{"_m"})
Truth table25%1/4
A and not(B) and not(C)
A= defined $Math::BigFloat::upgradeB= not wantarrayC= not $LIB->_is_one($y->{"_m"})
ABCresult
0XX0
10X0
1100
1111
2112  
2113 # we need to limit the accuracy to protect against overflow
2114 8813 my $fallback = 0;
2115 8813 my (@params, $scale);
2116 8813 ($x, @params) = $x->_find_round_parameters($r[0], $r[1], $r[2], $y);
2117  
2118 8813 ▶ return $x -> round(@r) if $x->is_nan(); # error in _find_round_parameters?
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 8813 8813
2119  
2120 # no rounding at all, so must use fallback
2121 8813 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 506 8307 8813
2122 # simulate old behaviour
2123 506 $params[0] = $class->div_scale(); # and round to it as accuracy
2124 506 $scale = $params[0]+4; # at least four more for proper round
2125 506 $params[2] = $r[2]; # round mode by caller or undef
2126 506 $fallback = 1; # to clear a/p afterwards
2127 } else {
2128 # the 4 below is empirical, and there might be cases where it is not
2129 # enough...
2130 8307 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition66%2/3
$params[0] || $params[1]
l!l&&r!l&&!r
8274330
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
2131 }
2132  
2133 8813 my $rem;
2134 8813 ▶ $rem = $class -> bzero() if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 49 8764 8813
2135  
2136 8813 ▶ $y = $class->new($y) unless $y->isa('Math::BigFloat');
Branch: 1 branch
BranchTrueFalseTotal
unless $y->isa("Math::BigFloat") 0 8813 8813
2137  
2138 8813 my $lx = $LIB -> _len($x->{_m});
2139 8813 my $ly = $LIB -> _len($y->{_m});
2140 8813 ▶ $scale = $lx if $lx > $scale;
Branch: 1 branch
BranchTrueFalseTotal
if $lx > $scale 1112 7701 8813
2141 8813 ▶ $scale = $ly if $ly > $scale;
Branch: 1 branch
BranchTrueFalseTotal
if $ly > $scale 613 8200 8813
2142 8813 my $diff = $ly - $lx;
2143 8813 ▶ $scale += $diff if $diff > 0; # if lx << ly, but not if ly << lx!
Branch: 1 branch
BranchTrueFalseTotal
if $diff > 0 3146 5667 8813
2144  
2145 # check that $y is not 1 nor -1 and cache the result:
2146 8813 ▶ my $y_not_one = !($LIB->_is_zero($y->{_e}) && $LIB->_is_one($y->{_m}));
Condition100%3/3
$LIB->_is_zero($y->{'_e'}) && $LIB->_is_one($y->{'_m'})
!ll&&!rl&&r
65572087169
MC/DC100%2/2
$LIB->_is_zero($y->{'_e'}) && $LIB->_is_one($y->{'_m'})
$LIB->_is_zero($y->{'_e'}) $LIB->_is_one($y->{'_m'})
Truth table100%3/3
A && B
A= $LIB->_is_zero($y->{'_e'})B= $LIB->_is_one($y->{'_m'})
ABresult
0X0
100
111
2147  
2148 # flipping the sign of $y will also flip the sign of $x for the special
2149 # case of $x->bsub($x); so we can catch it below:
2150 8813 my $xsign = $x->{sign};
2151 8813 $y->{sign} =~ tr/+-/-+/;
2152  
2153 8813 ▶ if ($xsign ne $x->{sign}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xsign ne $x->{'sign'}) { } 8 8805 8813
2154 # special case of $x /= $x results in 1
2155 8 $x = $x->bone(); # "fixes" also sign of $y, since $x is $y
2156 } else {
2157 # correct $y's sign again
2158 8805 $y->{sign} =~ tr/+-/-+/;
2159 # continue with normal div code:
2160  
2161 # make copy of $x in case of list context for later remainder
2162 # calculation
2163 8805 ▶ if (wantarray && $y_not_one) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray and $y_not_one) 25 8780 8805
Condition100%3/3
wantarray and $y_not_one
!ll&&!rl&&r
87562425
MC/DC100%2/2
wantarray and $y_not_one
wantarray $y_not_one
Truth table100%3/3
A and B
A= wantarrayB= $y_not_one
ABresult
0X0
100
111
2164 25 $rem = $x->copy();
2165 }
2166  
2167 8805 ▶ $x->{sign} = $x->{sign} ne $y->sign() ? '-' : '+';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} ne $y->sign ? : 561 8244 8805
2168  
2169 # check for / +-1 (+/- 1E0)
2170 8805 ▶ if ($y_not_one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y_not_one) 8636 169 8805
2171 # promote Math::BigInt and its subclasses (except when already a
2172 # Math::BigFloat)
2173 8636 ▶ $y = $class->new($y) unless $y->isa('Math::BigFloat');
Branch: 1 branch
BranchTrueFalseTotal
unless $y->isa("Math::BigFloat") 0 8636 8636
2174  
2175 # calculate the result to $scale digits and then round it
2176 # a * 10 ** b / c * 10 ** d => a/c * 10 ** (b-d)
2177 8636 $x->{_m} = $LIB->_lsft($x->{_m}, $LIB->_new($scale), 10);
2178 8636 $x->{_m} = $LIB->_div($x->{_m}, $y->{_m}); # a/c
2179  
2180 # correct exponent of $x
2181 ($x->{_e}, $x->{_es})
2182 8636 = $LIB -> _ssub($x->{_e}, $x->{_es}, $y->{_e}, $y->{_es});
2183 # correct for 10**scale
2184 ($x->{_e}, $x->{_es})
2185 8636 = $LIB -> _ssub($x->{_e}, $x->{_es}, $LIB->_new($scale), '+');
2186 8636 $x = $x->bnorm(); # remove trailing 0's
2187 }
2188 } # end else $x != $y
2189  
2190 # shortcut to not run through _find_round_parameters again
2191 8813 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 8780 33 8813
2192 8780 $x->{_a} = undef; # clear before round
2193 8780 $x = $x->bround($params[0], $params[2]); # then round accordingly
2194 } else {
2195 33 $x->{_p} = undef; # clear before round
2196 33 $x = $x->bfround($params[1], $params[2]); # then round accordingly
2197 }
2198 8813 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 506 8307 8813
2199 # clear a/p after round, since user did not request it
2200 506 $x->{_a} = undef;
2201 506 $x->{_p} = undef;
2202 }
2203  
2204 8813 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 49 8764 8813
2205 49 ▶ if ($y_not_one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y_not_one) 25 24 49
2206 25 $x = $x -> bfloor();
2207 25 $rem = $rem->bmod($y, @params); # copy already done
2208 }
2209 49 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 49 0 49
2210 # clear a/p after round, since user did not request it
2211 49 $rem->{_a} = undef;
2212 49 $rem->{_p} = undef;
2213 }
2214 49 ▶ $x = $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 49 49
Condition33%1/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
4900
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
2215 if defined($downgrade) && $x -> is_int();
2216 49 ▶ $rem = $downgrade -> new($rem -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $rem->is_int 0 49 49
Condition33%1/3
defined $Math::BigFloat::downgrade and $rem->is_int
!ll&&!rl&&r
4900
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $rem->is_int
defined $Math::BigFloat::downgrade $rem->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $rem->is_int
ABresult
0X0
100
111
2217 if defined($downgrade) && $rem -> is_int();
2218 49 return ($x, $rem);
2219 }
2220  
2221 8764 ▶ $x = $downgrade -> new($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 8764 8764
Condition66%2/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
876310
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table66%2/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
2222 if defined($downgrade) && $x -> is_int();
2223 8764 $x; # rounding already done above
2224 }
2225  
2226 sub bmod {
2227 # (dividend: BFLOAT or num_str, divisor: BFLOAT or num_str) return remainder
2228  
2229 # set up parameters
2230 743 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bmod: called CC=36 SCAR=36.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 734 9 743
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
63734
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
2231 ? (ref($_[0]), @_)
2232 : objectify(2, @_);
2233  
2234 743 ▶ return $x if $x->modify('bmod');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmod") 0 743 743
2235  
2236 # At least one argument is NaN. This is handled the same way as in
2237 # Math::BigInt -> bmod().
2238  
2239 743 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 36 707 743
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
2016707
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
2240  
2241 # Modulo zero. This is handled the same way as in Math::BigInt -> bmod().
2242  
2243 707 ▶ if ($y -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_zero) 40 667 707
2244 40 return $x -> round(@r);
2245 }
2246  
2247 # Numerator (dividend) is +/-inf. This is handled the same way as in
2248 # Math::BigInt -> bmod().
2249  
2250 667 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 48 619 667
2251 48 return $x -> bnan(@r);
2252 }
2253  
2254 # Denominator (divisor) is +/-inf. This is handled the same way as in
2255 # Math::BigInt -> bmod().
2256  
2257 619 ▶ if ($y -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_inf) 40 579 619
2258 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
2259 28 return $x -> round(@r);
2260 } else {
2261 12 return $x -> binf($y -> sign(), @r);
2262 }
2263 }
2264  
2265 return $x->bzero(@r) if $x->is_zero()
2266 || ($x->is_int() &&
2267 # check that $y == +1 or $y == -1:
2268 579 ▶ ($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"})) 96 483 579
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
2488467
$x->is_int and ($LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"}))
!ll&&!rl&&r
3544872
$LIB->_is_zero($y->{"_e"}) and $LIB->_is_one($y->{"_m"})
!ll&&!rl&&r
4939972
MC/DC50%2/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 table60%3/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
2269  
2270 483 my $cmp = $x->bacmp($y); # equal or $x < $y?
2271 483 ▶ if ($cmp == 0) { # $x == $y => result 0
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp == 0) 30 453 483
2272 30 return $x -> bzero(@r);
2273 }
2274  
2275 # only $y of the operands negative?
2276 453 ▶ my $neg = $x->{sign} ne $y->{sign} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} ne $y->{'sign'} ? : 51 402 453
2277  
2278 453 $x->{sign} = $y->{sign}; # calc sign first
2279 453 ▶ if ($cmp < 0 && $neg == 0) { # $x < $y => result $x
Branch: 1 branch
BranchTrueFalseTotal
if ($cmp < 0 and $neg == 0) 48 405 453
Condition100%3/3
$cmp < 0 and $neg == 0
!ll&&!rl&&r
3911448
MC/DC100%2/2
$cmp < 0 and $neg == 0
$cmp < 0 $neg == 0
Truth table100%3/3
A and B
A= $cmp < 0B= $neg == 0
ABresult
0X0
100
111
2280 48 return $x -> round(@r);
2281 }
2282  
2283 405 my $ym = $LIB->_copy($y->{_m});
2284  
2285 # 2e1 => 20
2286 $ym = $LIB->_lsft($ym, $y->{_e}, 10)
2287 405 ▶ if $y->{_es} eq '+' && !$LIB->_is_zero($y->{_e});
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"_es"} eq "+" and not $LIB->_is_zero($y->{"_e"}) 25 380 405
Condition100%3/3
$y->{"_es"} eq "+" and not $LIB->_is_zero($y->{"_e"})
!ll&&!rl&&r
1636425
MC/DC100%2/2
$y->{"_es"} eq "+" and not $LIB->_is_zero($y->{"_e"})
$y->{"_es"} eq "+" not $LIB->_is_zero($y->{"_e"})
Truth table100%3/3
A and not(B)
A= $y->{"_es"} eq "+"B= not $LIB->_is_zero($y->{"_e"})
ABresult
0X0
100
111
2288  
2289 # if $y has digits after dot
2290 405 my $shifty = 0; # correct _e of $x by this
2291 405 ▶ if ($y->{_es} eq '-') # has digits after dot
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{"_es"} eq "-") 16 389 405
2292 {
2293 # 123 % 2.5 => 1230 % 25 => 5 => 0.5
2294 16 $shifty = $LIB->_num($y->{_e}); # no more digits after dot
2295 # 123 => 1230, $y->{_m} is already 25
2296 16 $x->{_m} = $LIB->_lsft($x->{_m}, $y->{_e}, 10);
2297 }
2298 # $ym is now mantissa of $y based on exponent 0
2299  
2300 405 my $shiftx = 0; # correct _e of $x by this
2301 405 ▶ if ($x->{_es} eq '-') # has digits after dot
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 27 378 405
2302 {
2303 # 123.4 % 20 => 1234 % 200
2304 27 $shiftx = $LIB->_num($x->{_e}); # no more digits after dot
2305 27 $ym = $LIB->_lsft($ym, $x->{_e}, 10); # 123 => 1230
2306 }
2307 # 123e1 % 20 => 1230 % 20
2308 405 ▶ if ($x->{_es} eq '+' && !$LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "+" and not $LIB->_is_zero($x->{"_e"})) 117 288 405
Condition100%3/3
$x->{"_es"} eq "+" and not $LIB->_is_zero($x->{"_e"})
!ll&&!rl&&r
27261117
MC/DC100%2/2
$x->{"_es"} eq "+" and not $LIB->_is_zero($x->{"_e"})
$x->{"_es"} eq "+" not $LIB->_is_zero($x->{"_e"})
Truth table100%3/3
A and not(B)
A= $x->{"_es"} eq "+"B= not $LIB->_is_zero($x->{"_e"})
ABresult
0X0
100
111
2309 117 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10); # es => '+' here
2310 }
2311  
2312 405 $x->{_e} = $LIB->_new($shiftx);
2313 405 $x->{_es} = '+';
2314 405 ▶ $x->{_es} = '-' if $shiftx != 0 || $shifty != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $shiftx != 0 or $shifty != 0 35 370 405
Condition100%3/3
$shiftx != 0 or $shifty != 0
l!l&&r!l&&!r
278370
MC/DC100%2/2
$shiftx != 0 or $shifty != 0
$shiftx != 0 $shifty != 0
Truth table100%3/3
A or B
A= $shiftx != 0B= $shifty != 0
ABresult
1X1
011
000
2315 405 ▶ $x->{_e} = $LIB->_add($x->{_e}, $LIB->_new($shifty)) if $shifty != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $shifty != 0 16 389 405
2316  
2317 # now mantissas are equalized, exponent of $x is adjusted, so calc result
2318  
2319 405 $x->{_m} = $LIB->_mod($x->{_m}, $ym);
2320  
2321 405 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{_m}); # fix sign for -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 101 304 405
2322 405 $x = $x->bnorm();
2323  
2324 # if one of them negative => correct in place
2325 405 ▶ if ($neg != 0 && ! $x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($neg != 0 and not $x->is_zero) 51 354 405
Condition66%2/3
$neg != 0 and not $x->is_zero
!ll&&!rl&&r
354051
MC/DC50%1/2
$neg != 0 and not $x->is_zero
$neg != 0 not $x->is_zero
Truth table66%2/3
A and not(B)
A= $neg != 0B= not $x->is_zero
ABresult
0X0
100
111
2326 51 my $r = $y - $x;
2327 51 $x->{_m} = $r->{_m};
2328 51 $x->{_e} = $r->{_e};
2329 51 $x->{_es} = $r->{_es};
2330 51 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{_m}); # fix sign for -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 0 51 51
2331 51 $x = $x->bnorm();
2332 }
2333  
2334 405 $x = $x->round($r[0], $r[1], $r[2], $y);
2335 405 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 405 405
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
40500
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
2336 if defined($downgrade) && ($x->is_int() || $x->is_inf() || $x->is_nan());
2337 405 return $x;
2338 }
2339  
2340 sub bmodpow {
2341 # takes a very large number to a very large exponent in a given very
2342 # large modulus, quickly, thanks to binary exponentiation. Supports
2343 # negative exponents.
2344 20 ▶ my ($class, $num, $exp, $mod, @r)
Subroutine bmodpow: called CC=17 SCAR=40.9
Pod documented
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
2345 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
2346 ? (ref($_[0]), @_)
2347 : objectify(3, @_);
2348  
2349 20 ▶ return $num if $num->modify('bmodpow');
Branch: 1 branch
BranchTrueFalseTotal
if $num->modify("bmodpow") 0 20 20
2350  
2351 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
2352 if $mod->is_nan() || $exp->is_nan() || $mod->is_nan();
2353  
2354 # check modulus for valid values
2355 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
2356  
2357 # check exponent for valid values
2358 20 ▶ if ($exp->{sign} =~ /\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exp->{"sign"} =~ /\w/) 0 20 20
2359 # i.e., if it's NaN, +inf, or -inf...
2360 0 return $num->bnan(@r);
2361 }
2362  
2363 20 ▶ $num = $num->bmodinv($mod, @r) if $exp->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $exp->{"sign"} eq "-" 0 20 20
2364  
2365 # check num for valid values (also NaN if there was no inverse but $exp < 0)
2366 20 ▶ return $num->bnan(@r) if $num->{sign} !~ /^[+-]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $num->{"sign"} =~ /^[+-]$/ 0 20 20
2367  
2368 # $mod is positive, sign on $exp is ignored, result also positive
2369  
2370 # XXX TODO: speed it up when all three numbers are integers
2371 20 $num = $num->bpow($exp)->bmod($mod);
2372  
2373 20 ▶ return $downgrade -> new($num -> bdstr(), @r) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $num->is_int || $num->is_inf || $num->is_nan 0 20 20
Condition11%1/9
defined $Math::BigFloat::downgrade and $num->is_int || $num->is_inf || $num->is_nan
!ll&&!rl&&r
2000
$num->is_int or $num->is_inf or $num->is_nan
l!l&&r!l&&!r
000
$num->is_int or $num->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/4
defined $Math::BigFloat::downgrade and $num->is_int || $num->is_inf || $num->is_nan
defined $Math::BigFloat::downgrade $num->is_int $num->is_inf $num->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $num->is_intC= $num->is_infD= $num->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
2374 && ($num->is_int() || $num->is_inf() || $num->is_nan());
2375 20 return $num -> round(@r);
2376 }
2377  
2378 sub bpow {
2379 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
2380 # compute power of two numbers, second arg is used as integer
2381 # modifies first argument
2382  
2383 # set up parameters
2384 1042 ▶ my ($class, $x, $y, $a, $p, $r) = (ref($_[0]), @_);
Subroutine bpow: called CC=41 SCAR=37.2
Pod documented
2385 # objectify is costly, so avoid it
2386 1042 ▶ if ((!ref($_[0])) || (ref($_[0]) ne ref($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] or ref $_[0] ne ref $_[1]) 110 932 1042
Condition100%3/3
not ref $_[0] or ref $_[0] ne ref $_[1]
l!l&&r!l&&!r
4565932
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
2387 110 ($class, $x, $y, $a, $p, $r) = objectify(2, @_);
2388 }
2389  
2390 1042 ▶ return $x if $x -> modify('bpow');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bpow") 0 1042 1042
2391  
2392 # $x and/or $y is a NaN
2393 1042 ▶ return $x -> bnan() if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 116 926 1042
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
6056926
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
2394  
2395 # $x and/or $y is a +/-Inf
2396 926 ▶ if ($x -> is_inf("-")) {
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf('-')) { } 60 866 926
elsif ($x->is_inf('+')) { } 60 806 866
elsif ($y->is_inf('-')) { } 44 762 806
elsif ($y->is_inf('+')) { } 44 718 762
2397 60 ▶ return $x -> bzero() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 28 32 60
2398 32 ▶ return $x -> bnan() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 28 32
2399 28 ▶ return $x if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 8 20 28
2400 20 return $x -> bneg();
2401 } elsif ($x -> is_inf("+")) {
2402 60 ▶ return $x -> bzero() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 28 32 60
2403 32 ▶ return $x -> bnan() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 28 32
2404 28 return $x;
2405 } elsif ($y -> is_inf("-")) {
2406 44 ▶ return $x -> bnan() if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 4 40 44
2407 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/DC0%0/2
$x > -1 and $x < 1
$x > -1 $x < 1
Truth table33%1/3
A and B
A= $x > -1B= $x < 1
ABresult
0X0
100
111
2408 28 ▶ return $x -> bone() if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 4 24 28
2409 24 return $x -> bzero();
2410 } elsif ($y -> is_inf("+")) {
2411 44 ▶ return $x -> bnan() if $x -> is_one("-");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("-") 4 40 44
2412 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/DC0%0/2
$x > -1 and $x < 1
$x > -1 $x < 1
Truth table33%1/3
A and B
A= $x > -1B= $x < 1
ABresult
0X0
100
111
2413 28 ▶ return $x -> bone() if $x -> is_one("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one("+") 4 24 28
2414 24 return $x -> binf("+");
2415 }
2416  
2417 718 ▶ if ($x -> is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 48 670 718
2418 48 ▶ return $x -> bone() if $y -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_zero 4 44 48
2419 44 ▶ return $x -> binf() if $y -> is_negative();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_negative 20 24 44
2420 24 return $x;
2421 }
2422  
2423 # We don't support complex numbers, so upgrade or return NaN.
2424  
2425 670 ▶ if ($x -> is_negative() && !$y -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_negative and not $y->is_int) 80 590 670
Condition100%3/3
$x->is_negative and not $y->is_int
!ll&&!rl&&r
43815280
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
2426 80 ▶ return $upgrade -> bpow($x, $y, $a, $p, $r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 0 80 80
2427 80 return $x -> bnan();
2428 }
2429  
2430 590 ▶ if ($x -> is_one("+") || $y -> is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("+") or $y->is_one) 119 471 590
Condition100%3/3
$x->is_one("+") or $y->is_one
l!l&&r!l&&!r
4475471
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
2431 119 return $x;
2432 }
2433  
2434 471 ▶ if ($x -> is_one("-")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_one("-")) 24 447 471
2435 24 ▶ return $x if $y -> is_odd();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_odd 12 12 24
2436 12 return $x -> bneg();
2437 }
2438  
2439 447 ▶ return $x -> _pow($y, $a, $p, $r) if !$y -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $y->is_int 114 333 447
2440  
2441 333 my $y1 = $y -> as_int()->{value}; # make MBI part
2442  
2443 333 my $new_sign = '+';
2444 333 ▶ $new_sign = $LIB -> _is_odd($y1) ? '-' : '+' if $x->{sign} ne '+';
Branch: 2 branches
BranchTrueFalseTotal
if $x->{"sign"} ne "+" 108 225 333
$LIB->_is_odd($y1) ? : 54 54 108
2445  
2446 # calculate $x->{_m} ** $y and $x->{_e} * $y separately (faster)
2447 333 $x->{_m} = $LIB -> _pow($x->{_m}, $y1);
2448 333 $x->{_e} = $LIB -> _mul($x->{_e}, $y1);
2449  
2450 333 $x->{sign} = $new_sign;
2451 333 $x = $x -> bnorm();
2452  
2453 # x ** (-y) = 1 / (x ** y)
2454  
2455 333 ▶ if ($y->{sign} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->{"sign"} eq "-") 105 228 333
2456 # modify $x in place!
2457 105 my $z = $x -> copy();
2458 105 $x = $x -> bone();
2459 # round in one go (might ignore y's A!)
2460 105 return scalar $x -> bdiv($z, $a, $p, $r);
2461 }
2462  
2463 228 $x = $x -> round($a, $p, $r, $y);
2464  
2465 228 ▶ return $downgrade -> new($x)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 226 226
Condition66%6/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
22530
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
201
$x->is_int or $x->is_inf
l!l&&r!l&&!r
201
MC/DC0%0/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table40%2/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
2466 if defined($downgrade) && ($x->is_int() || $x->is_inf() || $x->is_nan());
2467 226 return $x;
2468 }
2469  
2470 sub blog {
2471 # Return the logarithm of the operand. If a second operand is defined, that
2472 # value is used as the base, otherwise the base is assumed to be Euler's
2473 # constant.
2474  
2475 256 ▶ my ($class, $x, $base, @r);
Subroutine blog: called CC=42 SCAR=38.9
Pod documented
2476  
2477 # Only objectify the base if it is defined, since an undefined base, as in
2478 # $x->blog() or $x->blog(undef) signals that the base is Euler's number =
2479 # 2.718281828...
2480  
2481 256 ▶ if (!ref($_[0]) && $_[0] =~ /^[A-Za-z]|::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/) { } 18 238 256
Condition66%2/3
not ref $_[0] and $_[0] =~ /^[A-Za-z]|::/
!ll&&!rl&&r
238018
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
2482 # E.g., Math::BigFloat->blog(256, 2)
2483 18 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[2] ? : 3 15 18
2484 defined $_[2] ? objectify(2, @_) : objectify(1, @_);
2485 } else {
2486 # E.g., $x->blog(2) or the deprecated Math::BigFloat::blog(256, 2)
2487 238 ▶ ($class, $x, $base, @r) =
Branch: 1 branch
BranchTrueFalseTotal
defined $_[1] ? : 39 199 238
2488 defined $_[1] ? objectify(2, @_) : objectify(1, @_);
2489 }
2490  
2491 256 ▶ return $x if $x->modify('blog');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blog") 0 256 256
2492  
2493 # Handle all exception cases and all trivial cases. I have used Wolfram
2494 # Alpha (http://www.wolframalpha.com) as the reference for these cases.
2495  
2496 256 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 252 256
2497  
2498 252 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 42 210 252
2499 42 ▶ $base = $class -> new($base)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($base) and $base->isa(__PACKAGE__) 0 42 42
Condition33%1/3
defined builtin::blessed($base) and $base->isa(__PACKAGE__)
!ll&&!rl&&r
0042
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
2500 unless defined(blessed($base)) && $base -> isa(__PACKAGE__);
2501 42 ▶ if ($base -> is_nan() || $base -> is_one()) {
Branch: 3 branches
BranchTrueFalseTotal
if ($base->is_nan or $base->is_one) { } 8 34 42
elsif ($base->is_inf or $base->is_zero) { } 4 30 34
elsif ($base->is_negative) { } 4 26 30
Condition66%2/3
$base->is_nan or $base->is_one
l!l&&r!l&&!r
0834
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
2502 8 return $x -> bnan(@r);
2503 ▶ } elsif ($base -> is_inf() || $base -> is_zero()) {
Condition66%2/3
$base->is_inf or $base->is_zero
l!l&&r!l&&!r
0430
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
2504 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
2505 4 return $x -> bzero(@r);
2506 } elsif ($base -> is_negative()) { # -inf < base < 0
2507 4 ▶ return $x -> bzero(@r) if $x -> is_one(); # x = 1
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 0 4 4
2508 4 ▶ return $x -> bone('+', @r) if $x == $base; # x = base
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 0 4 4
2509 # we can't handle these cases, so upgrade, if we can
2510 4 ▶ return $upgrade -> blog($x, $base, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 0 4 4
2511 4 return $x -> bnan(@r);
2512 }
2513 26 ▶ return $x -> bone(@r) if $x == $base; # 0 < base && 0 < x < inf
Branch: 1 branch
BranchTrueFalseTotal
if $x == $base 11 15 26
2514 }
2515  
2516 225 ▶ if ($x -> is_inf()) { # x = +/-inf
Branch: 4 branches
BranchTrueFalseTotal
if ($x->is_inf) { } 8 217 225
elsif ($x->is_neg) { } 16 201 217
elsif ($x->is_one) { } 16 185 201
elsif ($x->is_zero) { } 8 177 185
2517 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
2518 8 return $x -> binf($sign, @r);
2519 } elsif ($x -> is_neg()) { # -inf < x < 0
2520 16 ▶ return $upgrade -> blog($x, $base, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 0 16 16
2521 16 return $x -> bnan(@r);
2522 } elsif ($x -> is_one()) { # x = 1
2523 16 return $x -> bzero(@r);
2524 } elsif ($x -> is_zero()) { # x = 0
2525 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
2526 8 return $x -> binf($sign, @r);
2527 }
2528  
2529 # we need to limit the accuracy to protect against overflow
2530 177 my $fallback = 0;
2531 177 my ($scale, @params);
2532 177 ($x, @params) = $x->_find_round_parameters(@r);
2533  
2534 # no rounding at all, so must use fallback
2535 177 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 91 86 177
2536 # simulate old behaviour
2537 91 $params[0] = $class->div_scale(); # and round to it as accuracy
2538 91 $params[1] = undef; # P = undef
2539 91 $scale = $params[0]+4; # at least four more for proper round
2540 91 $params[2] = $r[2]; # round mode by caller or undef
2541 91 $fallback = 1; # to clear a/p afterwards
2542 } else {
2543 # the 4 below is empirical, and there might be cases where it is not
2544 # enough...
2545 86 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition33%1/3
$params[0] || $params[1]
l!l&&r!l&&!r
8600
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
2546 }
2547  
2548 # when user set globals, they would interfere with our calculation, so
2549 # disable them and later re-enable them
2550 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2551 177 my $abr = "$class\::accuracy";
2552 177 my $ab = $$abr;
2553 177 $$abr = undef;
2554 177 my $pbr = "$class\::precision";
2555 177 my $pb = $$pbr;
2556 177 $$pbr = undef;
2557 # we also need to disable any set A or P on $x (_find_round_parameters took
2558 # them already into account), since these would interfere, too
2559 177 $x->{_a} = undef;
2560 177 $x->{_p} = undef;
2561  
2562 177 my $done = 0;
2563  
2564 # If both $x and $base are integers, try to calculate an integer result
2565 # first. This is very fast, and if the exact result was found, we are done.
2566  
2567 177 ▶ 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 166 177
Condition66%4/6
defined $base and $base->is_int and $x->is_int
!ll&&!rl&&r
166011
defined $base and $base->is_int
!ll&&!rl&&r
166011
MC/DC33%1/3
defined $base and $base->is_int and $x->is_int
defined $base $base->is_int $x->is_int
Truth table50%2/4
A and B and C
A= defined $baseB= $base->is_intC= $x->is_int
ABCresult
0XX0
10X0
1100
1111
2568 11 my $x_lib = $LIB -> _new($x -> bdstr());
2569 11 my $b_lib = $LIB -> _new($base -> bdstr());
2570 11 ($x_lib, my $exact) = $LIB -> _log_int($x_lib, $b_lib);
2571 11 ▶ if ($exact) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exact) 10 1 11
2572 10 $x->{_m} = $x_lib;
2573 10 $x->{_e} = $LIB -> _zero();
2574 10 $x = $x -> bnorm();
2575 10 $done = 1;
2576 }
2577 }
2578  
2579 # If the integer result was not accurate, compute the natural logarithm
2580 # log($x) (using reduction by 10 and possibly also by 2), and if a
2581 # different base was requested, convert the result with log($x)/log($base).
2582  
2583 177 ▶ unless ($done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($done) 167 10 177
2584 167 $x = $x -> _log_10($scale);
2585 167 ▶ if (defined $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $base) 1 166 167
2586 # log_b(x) = ln(x) / ln(b), so compute ln(b)
2587 1 my $base_log_e = $base -> copy() -> _log_10($scale);
2588 1 $x = $x -> bdiv($base_log_e, $scale);
2589 }
2590 }
2591  
2592 # shortcut to not run through _find_round_parameters again
2593  
2594 177 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 177 0 177
2595 177 $x = $x -> bround($params[0], $params[2]); # then round accordingly
2596 } else {
2597 0 $x = $x -> bfround($params[1], $params[2]); # then round accordingly
2598 }
2599 177 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 91 86 177
2600 # clear a/p after round, since user did not request it
2601 91 $x->{_a} = undef;
2602 91 $x->{_p} = undef;
2603 }
2604 # restore globals
2605 177 $$abr = $ab;
2606 177 $$pbr = $pb;
2607  
2608 177 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 1 176 177
Condition100%3/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
17511
MC/DC100%2/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table100%3/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
2609 if defined($downgrade) && $x -> is_int();
2610 176 return $x;
2611 }
2612  
2613 sub bexp {
2614 # Calculate e ** X (Euler's number to the power of X)
2615 20 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bexp: called CC=21 SCAR=31.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 11 9 20
2616  
2617 20 ▶ return $x if $x->modify('bexp');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bexp") 0 20 20
2618  
2619 20 ▶ return $x->bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 20 20
2620 20 ▶ return $x->binf(@r) if $x->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 0 20 20
2621 20 ▶ return $x->bzero(@r) if $x->{sign} eq '-inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-inf" 0 20 20
2622  
2623 # we need to limit the accuracy to protect against overflow
2624 20 my $fallback = 0;
2625 20 my ($scale, @params);
2626 20 ($x, @params) = $x->_find_round_parameters(@r);
2627  
2628 # error in _find_round_parameters?
2629 20 ▶ return $x->bnan(@r) if $x->{sign} eq 'NaN';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "NaN" 0 20 20
2630  
2631 # no rounding at all, so must use fallback
2632 20 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 11 9 20
2633 # simulate old behaviour
2634 11 $params[0] = $class->div_scale(); # and round to it as accuracy
2635 11 $params[1] = undef; # P = undef
2636 11 $scale = $params[0]+4; # at least four more for proper round
2637 11 $params[2] = $r[2]; # round mode by caller or undef
2638 11 $fallback = 1; # to clear a/p afterwards
2639 } else {
2640 # the 4 below is empirical, and there might be cases where it's not
2641 # enough ...
2642 9 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition33%1/3
$params[0] || $params[1]
l!l&&r!l&&!r
900
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
2643 }
2644  
2645 20 ▶ return $x->bone(@params) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 20 20
2646  
2647 20 ▶ if (!$x->isa('Math::BigFloat')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->isa("Math::BigFloat")) 0 20 20
2648 0 $x = Math::BigFloat->new($x);
2649 0 $class = ref($x);
2650 }
2651  
2652 # when user set globals, they would interfere with our calculation, so
2653 # disable them and later re-enable them
2654 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2655 20 my $abr = "$class\::accuracy";
2656 20 my $ab = $$abr;
2657 20 $$abr = undef;
2658 20 my $pbr = "$class\::precision";
2659 20 my $pb = $$pbr;
2660 20 $$pbr = undef;
2661 # we also need to disable any set A or P on $x (_find_round_parameters took
2662 # them already into account), since these would interfere, too
2663 20 $x->{_a} = undef;
2664 20 $x->{_p} = undef;
2665  
2666 # Disabling upgrading and downgrading is no longer necessary to avoid an
2667 # infinite recursion, but it avoids unnecessary upgrading and downgrading in
2668 # the intermediate computations.
2669  
2670 # Temporarily disable downgrading
2671  
2672 20 my $dng = Math::BigFloat -> downgrade();
2673 20 Math::BigFloat -> downgrade(undef);
2674  
2675 20 my $x_org = $x->copy();
2676  
2677 # We use the following Taylor series:
2678  
2679 # x x^2 x^3 x^4
2680 # e = 1 + --- + --- + --- + --- ...
2681 # 1! 2! 3! 4!
2682  
2683 # The difference for each term is X and N, which would result in:
2684 # 2 copy, 2 mul, 2 add, 1 inc, 1 div operations per term
2685  
2686 # But it is faster to compute exp(1) and then raising it to the
2687 # given power, esp. if $x is really big and an integer because:
2688  
2689 # * The numerator is always 1, making the computation faster
2690 # * the series converges faster in the case of x == 1
2691 # * We can also easily check when we have reached our limit: when the
2692 # term to be added is smaller than "1E$scale", we can stop - f.i.
2693 # scale == 5, and we have 1/40320, then we stop since 1/40320 < 1E-5.
2694 # * we can compute the *exact* result by simulating bigrat math:
2695  
2696 # 1 1 gcd(3, 4) = 1 1*24 + 1*6 5
2697 # - + - = ---------- = --
2698 # 6 24 6*24 24
2699  
2700 # We do not compute the gcd() here, but simple do:
2701 # 1 1 1*24 + 1*6 30
2702 # - + - = --------- = --
2703 # 6 24 6*24 144
2704  
2705 # In general:
2706 # a c a*d + c*b and note that c is always 1 and d = (b*f)
2707 # - + - = ---------
2708 # b d b*d
2709  
2710 # This leads to: which can be reduced by b to:
2711 # a 1 a*b*f + b a*f + 1
2712 # - + - = --------- = -------
2713 # b b*f b*b*f b*f
2714  
2715 # The first terms in the series are:
2716  
2717 # 1 1 1 1 1 1 1 1 13700
2718 # -- + -- + -- + -- + -- + --- + --- + ---- = -----
2719 # 1 1 2 6 24 120 720 5040 5040
2720  
2721 # Note that we cannot simply reduce 13700/5040 to 685/252, but must keep
2722 # the numerator and the denominator!
2723  
2724 20 ▶ if ($scale <= 75) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= 75) { } 17 3 20
2725 # set $x directly from a cached string form
2726 17 $x->{_m} = $LIB->_new("2718281828459045235360287471352662497757" .
2727 "2470936999595749669676277240766303535476");
2728 17 $x->{sign} = '+';
2729 17 $x->{_es} = '-';
2730 17 $x->{_e} = $LIB->_new(79);
2731 } else {
2732 # compute A and B so that e = A / B.
2733  
2734 # After some terms we end up with this, so we use it as a starting
2735 # point:
2736 3 my $A = $LIB->_new("9093339520860578540197197" .
2737 "0164779391644753259799242");
2738 3 my $F = $LIB->_new(42);
2739 3 my $step = 42;
2740  
2741 # Compute how many steps we need to take to get $A and $B sufficiently
2742 # big
2743 3 my $steps = _len_to_steps($scale - 4);
2744 # print STDERR "# Doing $steps steps for ", $scale-4, " digits\n";
2745 3 ▶ while ($step++ <= $steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($step++ <= $steps) 79 3 82
2746 # calculate $a * $f + 1
2747 79 $A = $LIB->_mul($A, $F);
2748 79 $A = $LIB->_inc($A);
2749 # increment f
2750 79 $F = $LIB->_inc($F);
2751 }
2752  
2753 # Compute $B as factorial of $steps (this is faster than doing it
2754 # manually)
2755 3 my $B = $LIB->_fac($LIB->_new($steps));
2756  
2757 # print "A ", $LIB->_str($A), "\nB ", $LIB->_str($B), "\n";
2758  
2759 # compute A/B with $scale digits in the result (truncate, not round)
2760 3 $A = $LIB->_lsft($A, $LIB->_new($scale), 10);
2761 3 $A = $LIB->_div($A, $B);
2762  
2763 3 $x->{_m} = $A;
2764 3 $x->{sign} = '+';
2765 3 $x->{_es} = '-';
2766 3 $x->{_e} = $LIB->_new($scale);
2767 }
2768  
2769 # $x contains now an estimate of e, with some surplus digits, so we can
2770 # round
2771 20 ▶ if (!$x_org->is_one()) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $x_org->is_one) { } 10 10 20
2772 # Reduce size of fractional part, followup with integer power of two.
2773 10 my $lshift = 0;
2774 10 ▶ while ($lshift < 30 && $x_org->bacmp(2 << $lshift) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lshift < 30 and $x_org->bacmp(2 << $lshift) > 0) 21 10 31
Condition66%2/3
$lshift < 30 and $x_org->bacmp(2 << $lshift) > 0
!ll&&!rl&&r
01021
MC/DC50%1/2
$lshift < 30 and $x_org->bacmp(2 << $lshift) > 0
$lshift < 30 $x_org->bacmp(2 << $lshift) > 0
Truth table66%2/3
A and B
A= $lshift < 30B= $x_org->bacmp(2 << $lshift) > 0
ABresult
0X0
100
111
2775 21 $lshift++;
2776 }
2777 # Raise $x to the wanted power and round it.
2778 10 ▶ if ($lshift == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lshift == 0) { } 5 5 10
2779 5 $x = $x->bpow($x_org, @params);
2780 } else {
2781 5 my($mul, $rescale) = (1 << $lshift, $scale+1+$lshift);
2782 5 $x = $x -> bpow(scalar $x_org->bdiv($mul, $rescale), $rescale)
2783 -> bpow($mul, @params);
2784 }
2785 } else {
2786 # else just round the already computed result
2787 10 $x->{_a} = undef;
2788 10 $x->{_p} = undef;
2789 # shortcut to not run through _find_round_parameters again
2790 10 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 10 0 10
2791 10 $x = $x->bround($params[0], $params[2]); # then round accordingly
2792 } else {
2793 0 $x = $x->bfround($params[1], $params[2]); # then round accordingly
2794 }
2795 }
2796  
2797 20 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 11 9 20
2798 # clear a/p after round, since user did not request it
2799 11 $x->{_a} = undef;
2800 11 $x->{_p} = undef;
2801 }
2802  
2803 # Restore globals
2804 20 $$abr = $ab;
2805 20 $$pbr = $pb;
2806  
2807 # Restore downgrading.
2808  
2809 20 Math::BigFloat -> downgrade($dng);
2810  
2811 20 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 20 20
Condition33%1/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
2000
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
2812 if defined($downgrade) && $x -> is_int();
2813 20 $x;
2814 }
2815  
2816 sub bnok {
2817 # Calculate n over k (binomial coefficient or "choose" function) as integer.
2818 # set up parameters
2819 60 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bnok: called CC=12 SCAR=27.3
Pod documented
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
2820 ? (ref($_[0]), @_)
2821 : objectify(2, @_);
2822  
2823 60 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 60 60
2824  
2825 60 ▶ return $x if $x->modify('bnok');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bnok") 0 60 60
2826  
2827 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
2828 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
2829 ($y->is_finite() && !$y->is_int()));
2830  
2831 48 my $xint = Math::BigInt -> new($x -> bsstr());
2832 48 my $yint = Math::BigInt -> new($y -> bsstr());
2833 48 $xint = $xint -> bnok($yint);
2834  
2835 48 ▶ return $xint if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 48 48
2836  
2837 48 my $xflt = Math::BigFloat -> new($xint);
2838  
2839 48 $x->{_m} = $xflt->{_m};
2840 48 $x->{_e} = $xflt->{_e};
2841 48 $x->{_es} = $xflt->{_es};
2842 48 $x->{sign} = $xflt->{sign};
2843  
2844 48 return $x;
2845 }
2846  
2847 sub bsin {
2848 # Calculate a sinus of x.
2849 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bsin: called CC=15 SCAR=28.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
2850  
2851 # taylor: x^3 x^5 x^7 x^9
2852 # sin = x - --- + --- - --- + --- ...
2853 # 3! 5! 7! 9!
2854  
2855 40 ▶ return $x if $x->modify('bsin');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsin") 0 40 40
2856  
2857 40 ▶ return $x -> bzero(@r) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 32 40
2858 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
2859  
2860 # we need to limit the accuracy to protect against overflow
2861 20 my $fallback = 0;
2862 20 my ($scale, @params);
2863 20 ($x, @params) = $x->_find_round_parameters(@r);
2864  
2865 # error in _find_round_parameters?
2866 20 ▶ return $x->bnan(@r) if $x->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 20 20
2867  
2868 # no rounding at all, so must use fallback
2869 20 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 0 20 20
2870 # simulate old behaviour
2871 0 $params[0] = $class->div_scale(); # and round to it as accuracy
2872 0 $params[1] = undef; # disable P
2873 0 $scale = $params[0]+4; # at least four more for proper round
2874 0 $params[2] = $r[2]; # round mode by caller or undef
2875 0 $fallback = 1; # to clear a/p afterwards
2876 } else {
2877 # the 4 below is empirical, and there might be cases where it is not
2878 # enough...
2879 20 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition33%1/3
$params[0] || $params[1]
l!l&&r!l&&!r
2000
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
2880 }
2881  
2882 # when user set globals, they would interfere with our calculation, so
2883 # disable them and later re-enable them
2884 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2885 20 my $abr = "$class\::accuracy";
2886 20 my $ab = $$abr;
2887 20 $$abr = undef;
2888 20 my $pbr = "$class\::precision";
2889 20 my $pb = $$pbr;
2890 20 $$pbr = undef;
2891 # we also need to disable any set A or P on $x (_find_round_parameters took
2892 # them already into account), since these would interfere, too
2893 20 $x->{_a} = undef;
2894 20 $x->{_p} = undef;
2895  
2896 # Disabling upgrading and downgrading is no longer necessary to avoid an
2897 # infinite recursion, but it avoids unnecessary upgrading and downgrading in
2898 # the intermediate computations.
2899  
2900 20 local $Math::BigInt::upgrade = undef;
2901 20 local $Math::BigFloat::downgrade = undef;
2902  
2903 20 my $over = $x * $x; # X ^ 2
2904 20 my $x2 = $over->copy(); # X ^ 2; difference between terms
2905 20 $over = $over->bmul($x); # X ^ 3 as starting value
2906 20 my $sign = 1; # start with -=
2907 20 my $below = $class->new(6);
2908 20 my $factorial = $class->new(4);
2909 20 $x->{_a} = undef;
2910 20 $x->{_p} = undef;
2911  
2912 20 my $limit = $class->new("1E-". ($scale-1));
2913 20 while (1) {
2914 # we calculate the next term, and add it to the last
2915 # when the next term is below our limit, it won't affect the outcome
2916 # anymore, so we stop:
2917 196 my $next = $over->copy()->bdiv($below, $scale);
2918 196 ▶ last if $next->bacmp($limit) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next->bacmp($limit) <= 0 20 176 196
2919  
2920 176 ▶ if ($sign == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign == 0) { } 80 96 176
2921 80 $x = $x->badd($next);
2922 } else {
2923 96 $x = $x->bsub($next);
2924 }
2925 176 $sign = 1-$sign; # alternate
2926 # calculate things for the next term
2927 176 $over = $over->bmul($x2); # $x*$x
2928 176 $below = $below->bmul($factorial); # n*(n+1)
2929 176 $factorial = $factorial->binc();
2930 176 $below = $below -> bmul($factorial); # n*(n+1)
2931 176 $factorial = $factorial->binc();
2932 }
2933  
2934 # shortcut to not run through _find_round_parameters again
2935 20 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 20 0 20
2936 20 $x = $x->bround($params[0], $params[2]); # then round accordingly
2937 } else {
2938 0 $x = $x->bfround($params[1], $params[2]); # then round accordingly
2939 }
2940 20 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 0 20 20
2941 # clear a/p after round, since user did not request it
2942 0 $x->{_a} = undef;
2943 0 $x->{_p} = undef;
2944 }
2945 # restore globals
2946 20 $$abr = $ab;
2947 20 $$pbr = $pb;
2948  
2949 20 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 20 20
Condition33%1/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
2000
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
2950 if defined($downgrade) && $x -> is_int();
2951 20 $x;
2952 }
2953  
2954 sub bcos {
2955 # Calculate a cosinus of x.
2956 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bcos: called CC=14 SCAR=27.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
2957  
2958 # Taylor: x^2 x^4 x^6 x^8
2959 # cos = 1 - --- + --- - --- + --- ...
2960 # 2! 4! 6! 8!
2961  
2962 # we need to limit the accuracy to protect against overflow
2963 36 my $fallback = 0;
2964 36 my ($scale, @params);
2965 36 ($x, @params) = $x->_find_round_parameters(@r);
2966  
2967 # constant object or error in _find_round_parameters?
2968 36 ▶ return $x if $x->modify('bcos') || $x->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bcos") or $x->is_nan 4 32 36
Condition66%2/3
$x->modify("bcos") or $x->is_nan
l!l&&r!l&&!r
0432
MC/DC50%1/2
$x->modify("bcos") or $x->is_nan
$x->modify("bcos") $x->is_nan
Truth table66%2/3
A or B
A= $x->modify("bcos")B= $x->is_nan
ABresult
1X1
011
000
2969 32 ▶ return $x->bnan() if $x->is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 8 24 32
2970 24 ▶ return $x->bone(@r) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 16 24
2971  
2972 # no rounding at all, so must use fallback
2973 16 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 0 16 16
2974 # simulate old behaviour
2975 0 $params[0] = $class->div_scale(); # and round to it as accuracy
2976 0 $params[1] = undef; # disable P
2977 0 $scale = $params[0]+4; # at least four more for proper round
2978 0 $params[2] = $r[2]; # round mode by caller or undef
2979 0 $fallback = 1; # to clear a/p afterwards
2980 } else {
2981 # the 4 below is empirical, and there might be cases where it is not
2982 # enough...
2983 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
2984 }
2985  
2986 # when user set globals, they would interfere with our calculation, so
2987 # disable them and later re-enable them
2988 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2989 16 my $abr = "$class\::accuracy";
2990 16 my $ab = $$abr;
2991 16 $$abr = undef;
2992 16 my $pbr = "$class\::precision";
2993 16 my $pb = $$pbr;
2994 16 $$pbr = undef;
2995 # we also need to disable any set A or P on $x (_find_round_parameters took
2996 # them already into account), since these would interfere, too
2997 16 $x->{_a} = undef;
2998 16 $x->{_p} = undef;
2999  
3000 16 my $over = $x * $x; # X ^ 2
3001 16 my $x2 = $over->copy(); # X ^ 2; difference between terms
3002 16 my $sign = 1; # start with -=
3003 16 my $below = $class->new(2);
3004 16 my $factorial = $class->new(3);
3005 16 $x = $x->bone();
3006 16 $x->{_a} = undef;
3007 16 $x->{_p} = undef;
3008  
3009 16 my $limit = $class->new("1E-". ($scale-1));
3010 #my $steps = 0;
3011 16 while (3 < 5) {
3012 # we calculate the next term, and add it to the last
3013 # when the next term is below our limit, it won't affect the outcome
3014 # anymore, so we stop:
3015 156 my $next = $over->copy()->bdiv($below, $scale);
3016 156 ▶ last if $next->bacmp($limit) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next->bacmp($limit) <= 0 16 140 156
3017  
3018 140 ▶ if ($sign == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign == 0) { } 68 72 140
3019 68 $x = $x->badd($next);
3020 } else {
3021 72 $x = $x->bsub($next);
3022 }
3023 140 $sign = 1-$sign; # alternate
3024 # calculate things for the next term
3025 140 $over = $over->bmul($x2); # $x*$x
3026 140 $below = $below->bmul($factorial); # n*(n+1)
3027 140 $factorial = $factorial -> binc();
3028 140 $below = $below->bmul($factorial); # n*(n+1)
3029 140 $factorial = $factorial -> binc();
3030 }
3031  
3032 # shortcut to not run through _find_round_parameters again
3033 16 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 16 0 16
3034 16 $x = $x->bround($params[0], $params[2]); # then round accordingly
3035 } else {
3036 0 $x = $x->bfround($params[1], $params[2]); # then round accordingly
3037 }
3038 16 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 0 16 16
3039 # clear a/p after round, since user did not request it
3040 0 $x->{_a} = undef;
3041 0 $x->{_p} = undef;
3042 }
3043 # restore globals
3044 16 $$abr = $ab;
3045 16 $$pbr = $pb;
3046  
3047 16 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 16 16
Condition33%1/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
1600
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
3048 if defined($downgrade) && $x -> is_int();
3049 16 $x;
3050 }
3051  
3052 sub batan {
3053 # Calculate a arcus tangens of x.
3054 175 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine batan: called CC=21 SCAR=31.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 175 0 175
3055  
3056 # taylor: x^3 x^5 x^7 x^9
3057 # atan = x - --- + --- - --- + --- ...
3058 # 3 5 7 9
3059  
3060 175 ▶ return $x if $x->modify('batan');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("batan") 0 175 175
3061  
3062 175 ▶ return $x -> bnan(@r) if $x->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 171 175
3063  
3064 # We need to limit the accuracy to protect against overflow.
3065  
3066 171 my $fallback = 0;
3067 171 my ($scale, @params);
3068 171 ($x, @params) = $x->_find_round_parameters(@r);
3069  
3070 # Error in _find_round_parameters?
3071  
3072 171 ▶ return $x -> bnan(@r) if $x->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 171 171
3073  
3074 171 ▶ if ($x->{sign} =~ /^[+-]inf\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]inf\z/) 16 155 171
3075 # +inf result is PI/2
3076 # -inf result is -PI/2
3077 # calculate PI/2
3078 16 my $pi = $class->bpi(@r);
3079 # modify $x in place
3080 16 $x->{_m} = $pi->{_m};
3081 16 $x->{_e} = $pi->{_e};
3082 16 $x->{_es} = $pi->{_es};
3083 # -y => -PI/2, +y => PI/2
3084 16 $x->{sign} = substr($x->{sign}, 0, 1); # "+inf" => "+"
3085 16 $x -> {_m} = $LIB->_div($x->{_m}, $LIB->_new(2));
3086 16 return $x;
3087 }
3088  
3089 155 ▶ return $x->bzero(@r) if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 26 129 155
3090  
3091 # no rounding at all, so must use fallback
3092 129 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 0 129 129
3093 # simulate old behaviour
3094 0 $params[0] = $class->div_scale(); # and round to it as accuracy
3095 0 $params[1] = undef; # disable P
3096 0 $scale = $params[0]+4; # at least four more for proper round
3097 0 $params[2] = $r[2]; # round mode by caller or undef
3098 0 $fallback = 1; # to clear a/p afterwards
3099 } else {
3100 # the 4 below is empirical, and there might be cases where it is not
3101 # enough...
3102 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
3103 }
3104  
3105 # 1 or -1 => PI/4
3106 # inlined is_one() && is_one('-')
3107 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
3108 27 my $pi = $class->bpi($scale - 3);
3109 # modify $x in place
3110 27 $x->{_m} = $pi->{_m};
3111 27 $x->{_e} = $pi->{_e};
3112 27 $x->{_es} = $pi->{_es};
3113 # leave the sign of $x alone (+1 => +PI/4, -1 => -PI/4)
3114 27 $x->{_m} = $LIB->_div($x->{_m}, $LIB->_new(4));
3115 27 return $x;
3116 }
3117  
3118 # This series is only valid if -1 < x < 1, so for other x we need to
3119 # calculate PI/2 - atan(1/x):
3120 102 my $pi = undef;
3121 102 ▶ if ($x->bacmp($x->copy()->bone) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bacmp($x->copy->bone) >= 0) 40 62 102
3122 # calculate PI/2
3123 40 $pi = $class->bpi($scale - 3);
3124 40 $pi->{_m} = $LIB->_div($pi->{_m}, $LIB->_new(2));
3125 # calculate 1/$x:
3126 40 my $x_copy = $x->copy();
3127 # modify $x in place
3128 40 $x = $x->bone();
3129 40 $x = $x->bdiv($x_copy, $scale);
3130 }
3131  
3132 102 my $fmul = 1;
3133 102 foreach (0 .. int($scale / 20)) {
3134 174 $fmul *= 2;
3135 174 $x = $x->bdiv($x->copy()->bmul($x)->binc()->bsqrt($scale + 4)->binc(),
3136 $scale + 4);
3137 }
3138  
3139 # When user set globals, they would interfere with our calculation, so
3140 # disable them and later re-enable them.
3141 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
3142 102 my $abr = "$class\::accuracy";
3143 102 my $ab = $$abr;
3144 102 $$abr = undef;
3145 102 my $pbr = "$class\::precision";
3146 102 my $pb = $$pbr;
3147 102 $$pbr = undef;
3148 # We also need to disable any set A or P on $x (_find_round_parameters
3149 # took them already into account), since these would interfere, too
3150 102 $x->{_a} = undef;
3151 102 $x->{_p} = undef;
3152  
3153 # Disabling upgrading and downgrading is no longer necessary to avoid an
3154 # infinite recursion, but it avoids unnecessary upgrading and downgrading in
3155 # the intermediate computations.
3156  
3157 102 local $Math::BigInt::upgrade = undef;
3158 102 local $Math::BigFloat::downgrade = undef;
3159  
3160 102 my $over = $x * $x; # X ^ 2
3161 102 my $x2 = $over->copy(); # X ^ 2; difference between terms
3162 102 $over = $over->bmul($x); # X ^ 3 as starting value
3163 102 my $sign = 1; # start with -=
3164 102 my $below = $class->new(3);
3165 102 my $two = $class->new(2);
3166 102 $x->{_a} = undef;
3167 102 $x->{_p} = undef;
3168  
3169 102 my $limit = $class->new("1E-". ($scale-1));
3170 #my $steps = 0;
3171 102 while (1) {
3172 # We calculate the next term, and add it to the last. When the next
3173 # term is below our limit, it won't affect the outcome anymore, so we
3174 # stop:
3175 990 my $next = $over->copy()->bdiv($below, $scale);
3176 990 ▶ last if $next->bacmp($limit) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next->bacmp($limit) <= 0 102 888 990
3177  
3178 888 ▶ if ($sign == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sign == 0) { } 416 472 888
3179 416 $x = $x->badd($next);
3180 } else {
3181 472 $x = $x->bsub($next);
3182 }
3183 888 $sign = 1-$sign; # alternatex
3184 # calculate things for the next term
3185 888 $over = $over->bmul($x2); # $x*$x
3186 888 $below = $below->badd($two); # n += 2
3187 }
3188 102 $x = $x->bmul($fmul);
3189  
3190 102 ▶ if (defined $pi) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pi) 40 62 102
3191 40 my $x_copy = $x->copy();
3192 # modify $x in place
3193 40 $x->{_m} = $pi->{_m};
3194 40 $x->{_e} = $pi->{_e};
3195 40 $x->{_es} = $pi->{_es};
3196 # PI/2 - $x
3197 40 $x = $x->bsub($x_copy);
3198 }
3199  
3200 # Shortcut to not run through _find_round_parameters again.
3201 102 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 102 0 102
3202 102 $x = $x->bround($params[0], $params[2]); # then round accordingly
3203 } else {
3204 0 $x = $x->bfround($params[1], $params[2]); # then round accordingly
3205 }
3206 102 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 0 102 102
3207 # Clear a/p after round, since user did not request it.
3208 0 $x->{_a} = undef;
3209 0 $x->{_p} = undef;
3210 }
3211  
3212 # restore globals
3213 102 $$abr = $ab;
3214 102 $$pbr = $pb;
3215  
3216 102 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf 0 102 102
Condition16%1/6
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf
!ll&&!rl&&r
10200
$x->is_int or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf
Truth table25%1/4
A and B or C
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_inf
ABCresult
0XX0
1000
11X1
1011
3217 if defined($downgrade) && ($x -> is_int() || $x -> is_inf());
3218 102 $x;
3219 }
3220  
3221 sub batan2 {
3222 # $y -> batan2($x) returns the arcus tangens of $y / $x.
3223  
3224 # Set up parameters.
3225 213 ▶ my ($class, $y, $x, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine batan2: called CC=24 SCAR=31.9
Pod documented
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
3226 ? (ref($_[0]), @_)
3227 : objectify(2, @_);
3228  
3229 # Quick exit if $y is read-only.
3230 213 ▶ return $y if $y -> modify('batan2');
Branch: 1 branch
BranchTrueFalseTotal
if $y->modify("batan2") 0 213 213
3231  
3232 # Handle all NaN cases.
3233 213 ▶ return $y -> bnan() if $x->{sign} eq $nan || $y->{sign} eq $nan;
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq $nan or $y->{"sign"} eq $nan 12 201 213
Condition100%3/3
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
l!l&&r!l&&!r
84201
MC/DC100%2/2
$x->{"sign"} eq $nan or $y->{"sign"} eq $nan
$x->{"sign"} eq $nan $y->{"sign"} eq $nan
Truth table100%3/3
A or B
A= $x->{"sign"} eq $nanB= $y->{"sign"} eq $nan
ABresult
1X1
011
000
3234  
3235 # We need to limit the accuracy to protect against overflow.
3236 201 my $fallback = 0;
3237 201 my ($scale, @params);
3238 201 ($y, @params) = $y -> _find_round_parameters(@r);
3239  
3240 # Error in _find_round_parameters?
3241 201 ▶ return $y if $y->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $y->is_nan 0 201 201
3242  
3243 # No rounding at all, so must use fallback.
3244 201 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 45 156 201
3245 # Simulate old behaviour
3246 45 $params[0] = $class -> div_scale(); # and round to it as accuracy
3247 45 $params[1] = undef; # disable P
3248 45 $scale = $params[0] + 4; # at least four more for proper round
3249 45 $params[2] = $r[2]; # round mode by caller or undef
3250 45 $fallback = 1; # to clear a/p afterwards
3251 } else {
3252 # The 4 below is empirical, and there might be cases where it is not
3253 # enough ...
3254 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
3255 }
3256  
3257 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
3258 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
3259 4 $y = $y -> bpi($scale) -> bmul("0.25"); # pi/4
3260 } elsif ($y -> is_inf("-")) { # y = -inf
3261 4 $y = $y -> bpi($scale) -> bmul("-0.25"); # -pi/4
3262 } else { # -inf < y < inf
3263 12 return $y -> bzero(@r); # 0
3264 }
3265 } elsif ($x -> is_inf("-")) { # x = -inf
3266 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
3267 4 $y = $y -> bpi($scale) -> bmul("0.75"); # 3/4 pi
3268 } elsif ($y -> is_inf("-")) { # y = -inf
3269 4 $y = $y -> bpi($scale) -> bmul("-0.75"); # -3/4 pi
3270 } elsif ($y >= 0) { # y >= 0
3271 8 $y = $y -> bpi($scale); # pi
3272 } else { # y < 0
3273 4 $y = $y -> bpi($scale) -> bneg(); # -pi
3274 }
3275 } elsif ($x > 0) { # 0 < x < inf
3276 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
3277 4 $y = $y -> bpi($scale) -> bmul("0.5"); # pi/2
3278 } elsif ($y -> is_inf("-")) { # y = -inf
3279 4 $y = $y -> bpi($scale) -> bmul("-0.5"); # -pi/2
3280 } else { # -inf < y < inf
3281 79 $y = $y -> bdiv($x, $scale) -> batan($scale); # atan(y/x)
3282 }
3283 } elsif ($x < 0) { # -inf < x < 0
3284 20 my $pi = $class -> bpi($scale);
3285 20 ▶ if ($y >= 0) { # y >= 0
Branch: 1 branch
BranchTrueFalseTotal
if ($y >= 0) { } 12 8 20
3286 12 $y = $y -> bdiv($x, $scale) -> batan() # atan(y/x) + pi
3287 -> badd($pi);
3288 } else { # y < 0
3289 8 $y = $y -> bdiv($x, $scale) -> batan() # atan(y/x) - pi
3290 -> bsub($pi);
3291 }
3292 } else { # x = 0
3293 54 ▶ if ($y > 0) { # y > 0
Branch: 2 branches
BranchTrueFalseTotal
if ($y > 0) { } 25 29 54
elsif ($y < 0) { } 22 7 29
3294 25 $y = $y -> bpi($scale) -> bmul("0.5"); # pi/2
3295 } elsif ($y < 0) { # y < 0
3296 22 $y = $y -> bpi($scale) -> bmul("-0.5"); # -pi/2
3297 } else { # y = 0
3298 7 return $y -> bzero(@r); # 0
3299 }
3300 }
3301  
3302 182 $y = $y -> round(@r);
3303  
3304 182 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 42 140 182
3305 42 $y->{_a} = undef;
3306 42 $y->{_p} = undef;
3307 }
3308  
3309 182 return $y;
3310 }
3311  
3312 sub bsqrt {
3313 # calculate square root
3314 442 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bsqrt: called CC=27 SCAR=33.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 420 22 442
3315  
3316 442 ▶ return $x if $x->modify('bsqrt');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bsqrt") 0 442 442
3317  
3318 # Handle trivial cases.
3319  
3320 442 ▶ return $x -> bnan(@r) if $x->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 8 434 442
3321 434 ▶ return $x -> binf("+", @r) if $x->{sign} eq '+inf';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "+inf" 4 430 434
3322 430 ▶ return $x -> round(@r) if $x->is_zero() || $x->is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one 10 420 430
Condition100%3/3
$x->is_zero or $x->is_one
l!l&&r!l&&!r
55420
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
3323  
3324 # We don't support complex numbers.
3325  
3326 420 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 20 400 420
3327 20 ▶ return $upgrade -> bsqrt($x, @r) if defined($upgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 0 20 20
3328 20 return $x -> bnan(@r);
3329 }
3330  
3331 # we need to limit the accuracy to protect against overflow
3332 400 my $fallback = 0;
3333 400 my (@params, $scale);
3334 400 ($x, @params) = $x->_find_round_parameters(@r);
3335  
3336 # error in _find_round_parameters?
3337 400 ▶ return $x -> bnan(@r) if $x->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 400 400
3338  
3339 # no rounding at all, so must use fallback
3340 400 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 123 277 400
3341 # simulate old behaviour
3342 123 $params[0] = $class->div_scale(); # and round to it as accuracy
3343 123 $scale = $params[0]+4; # at least four more for proper round
3344 123 $params[2] = $r[2]; # round mode by caller or undef
3345 123 $fallback = 1; # to clear a/p afterwards
3346 } else {
3347 # the 4 below is empirical, and there might be cases where it is not
3348 # enough...
3349 277 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition100%3/3
$params[0] || $params[1]
l!l&&r!l&&!r
255184
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
3350 }
3351  
3352 # when user set globals, they would interfere with our calculation, so
3353 # disable them and later re-enable them
3354 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
3355 400 my $abr = "$class\::accuracy";
3356 400 my $ab = $$abr;
3357 400 $$abr = undef;
3358 400 my $pbr = "$class\::precision";
3359 400 my $pb = $$pbr;
3360 400 $$pbr = undef;
3361 # we also need to disable any set A or P on $x (_find_round_parameters took
3362 # them already into account), since these would interfere, too
3363 400 $x->{_a} = undef;
3364 400 $x->{_p} = undef;
3365  
3366 # Disabling upgrading and downgrading is no longer necessary to avoid an
3367 # infinite recursion, but it avoids unnecessary upgrading and downgrading in
3368 # the intermediate computations.
3369  
3370 400 local $Math::BigInt::upgrade = undef;
3371 400 local $Math::BigFloat::downgrade = undef;
3372  
3373 400 my $i = $LIB->_copy($x->{_m});
3374 400 ▶ $i = $LIB->_lsft($i, $x->{_e}, 10) unless $LIB->_is_zero($x->{_e});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_zero($x->{"_e"}) 315 85 400
3375 400 my $xas = Math::BigInt->bzero();
3376 400 $xas->{value} = $i;
3377  
3378 400 my $gs = $xas->copy()->bsqrt(); # some guess
3379  
3380 400 ▶ if (($x->{_es} ne '-') # guess can't be accurate if there are
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} ne "-" and $xas->bacmp($gs * $gs) == 0) 50 350 400
Condition100%3/3
$x->{"_es"} ne "-" and $xas->bacmp($gs * $gs) == 0
!ll&&!rl&&r
2787250
MC/DC0%0/2
$x->{"_es"} ne "-" and $xas->bacmp($gs * $gs) == 0
$x->{"_es"} ne "-" $xas->bacmp($gs * $gs) == 0
Truth table33%1/3
A and B
A= $x->{"_es"} ne "-"B= $xas->bacmp($gs * $gs) == 0
ABresult
0X0
100
111
3381 # digits after the dot
3382 && ($xas->bacmp($gs * $gs) == 0)) # guess hit the nail on the head?
3383 {
3384 # exact result, copy result over to keep $x
3385 50 $x->{_m} = $gs->{value};
3386 50 $x->{_e} = $LIB->_zero();
3387 50 $x->{_es} = '+';
3388 50 $x = $x->bnorm();
3389 # shortcut to not run through _find_round_parameters again
3390 50 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 50 0 50
3391 50 $x = $x->bround($params[0], $params[2]); # then round accordingly
3392 } else {
3393 0 $x = $x->bfround($params[1], $params[2]); # then round accordingly
3394 }
3395 50 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 33 17 50
3396 # clear a/p after round, since user did not request it
3397 33 $x->{_a} = undef;
3398 33 $x->{_p} = undef;
3399 }
3400 # re-enable A and P, upgrade is taken care of by "local"
3401 50 ${"$class\::accuracy"} = $ab;
3402 50 ${"$class\::precision"} = $pb;
3403 50 return $x;
3404 }
3405  
3406 # sqrt(2) = 1.4 because sqrt(2*100) = 1.4*10; so we can increase the
3407 # accuracy of the result by multiplying the input by 100 and then divide the
3408 # integer result of sqrt(input) by 10. Rounding afterwards returns the real
3409 # result.
3410  
3411 # The following steps will transform 123.456 (in $x) into 123456 (in $y1)
3412 350 my $y1 = $LIB->_copy($x->{_m});
3413  
3414 350 my $length = $LIB->_len($y1);
3415  
3416 # Now calculate how many digits the result of sqrt(y1) would have
3417 350 my $digits = int($length / 2);
3418  
3419 # But we need at least $scale digits, so calculate how many are missing
3420 350 my $shift = $scale - $digits;
3421  
3422 # This happens if the input had enough digits
3423 # (we take care of integer guesses above)
3424 350 ▶ $shift = 0 if $shift < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $shift < 0 2 348 350
3425  
3426 # Multiply in steps of 100, by shifting left two times the "missing" digits
3427 350 my $s2 = $shift * 2;
3428  
3429 # We now make sure that $y1 has the same odd or even number of digits than
3430 # $x had. So when _e of $x is odd, we must shift $y1 by one digit left,
3431 # because we always must multiply by steps of 100 (sqrt(100) is 10) and not
3432 # steps of 10. The length of $x does not count, since an even or odd number
3433 # of digits before the dot is not changed by adding an even number of digits
3434 # after the dot (the result is still odd or even digits long).
3435 350 ▶ $s2++ if $LIB->_is_odd($x->{_e});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_odd($x->{"_e"}) 139 211 350
3436  
3437 350 $y1 = $LIB->_lsft($y1, $LIB->_new($s2), 10);
3438  
3439 # now take the square root and truncate to integer
3440 350 $y1 = $LIB->_sqrt($y1);
3441  
3442 # By "shifting" $y1 right (by creating a negative _e) we calculate the final
3443 # result, which is than later rounded to the desired scale.
3444  
3445 # calculate how many zeros $x had after the '.' (or before it, depending
3446 # on sign of $dat, the result should have half as many:
3447 350 my $dat = $LIB->_num($x->{_e});
3448 350 ▶ $dat = -$dat if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 278 72 350
3449 350 $dat += $length;
3450  
3451 350 ▶ if ($dat > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dat > 0) { } 320 30 350
3452 # no zeros after the dot (e.g. 1.23, 0.49 etc)
3453 # preserve half as many digits before the dot than the input had
3454 # (but round this "up")
3455 320 $dat = int(($dat+1)/2);
3456 } else {
3457 30 $dat = int(($dat)/2);
3458 }
3459 350 $dat -= $LIB->_len($y1);
3460 350 ▶ if ($dat < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dat < 0) { } 350 0 350
3461 350 $dat = abs($dat);
3462 350 $x->{_e} = $LIB->_new($dat);
3463 350 $x->{_es} = '-';
3464 } else {
3465 0 $x->{_e} = $LIB->_new($dat);
3466 0 $x->{_es} = '+';
3467 }
3468 350 $x->{_m} = $y1;
3469 350 $x = $x->bnorm();
3470  
3471 # shortcut to not run through _find_round_parameters again
3472 350 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 328 22 350
3473 328 $x = $x->bround($params[0], $params[2]); # then round accordingly
3474 } else {
3475 22 $x = $x->bfround($params[1], $params[2]); # then round accordingly
3476 }
3477 350 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 90 260 350
3478 # clear a/p after round, since user did not request it
3479 90 $x->{_a} = undef;
3480 90 $x->{_p} = undef;
3481 }
3482 # restore globals
3483 350 $$abr = $ab;
3484 350 $$pbr = $pb;
3485  
3486 350 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf 0 350 350
Condition16%1/6
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf
!ll&&!rl&&r
35000
$x->is_int or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf
Truth table25%1/4
A and B or C
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_inf
ABCresult
0XX0
1000
11X1
1011
3487 if defined($downgrade) && ($x -> is_int() || $x -> is_inf());
3488 350 $x;
3489 }
3490  
3491 sub broot {
3492 # calculate $y'th root of $x
3493  
3494 # set up parameters
3495 186 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine broot: called CC=38 SCAR=39.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 176 10 186
Condition100%3/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
28176
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
3496 ? (ref($_[0]), @_)
3497 : objectify(2, @_);
3498  
3499 186 ▶ return $x if $x->modify('broot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("broot") 0 186 186
3500  
3501 # Handle trivial cases.
3502  
3503 186 ▶ return $x -> bnan(@r) if $x->is_nan() || $y->is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 36 150 186
Condition100%3/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
1620150
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
3504  
3505 150 ▶ if ($x -> is_neg()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg) 44 106 150
3506 # -27 ** (1/3) = -3
3507 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 4 40 44
Condition83%5/6
$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/DC66%2/3
$x->is_int and $y->is_int and $y->is_neg
$x->is_int $y->is_int $y->is_neg
Truth table75%3/4
A and B and C
A= $x->is_intB= $y->is_intC= $y->is_neg
ABCresult
0XX0
10X0
1100
1111
3508 if $x -> is_int() && $y -> is_int() && $y -> is_neg();
3509 40 ▶ return $upgrade -> broot($x, $y, @r) if defined $upgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 0 40 40
3510 40 return $x -> bnan(@r);
3511 }
3512  
3513 # NaN handling: $x ** 1/0, x or y NaN, or y inf/-inf or y == 0
3514 return $x->bnan() if $x->{sign} !~ /^\+/ || $y->is_zero() ||
3515 106 ▶ $y->{sign} !~ /^\+$/;
Branch: 1 branch
BranchTrueFalseTotal
if not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/ 24 82 106
Condition83%5/6
not $x->{"sign"} =~ /^\+/ or $y->is_zero or not $y->{"sign"} =~ /^\+$/
l!l&&r!l&&!r
121282
not $x->{"sign"} =~ /^\+/ or $y->is_zero
l!l&&r!l&&!r
01294
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
3516  
3517 82 ▶ return $x if $x->is_zero() || $x->is_one() || $x->is_inf() || $y->is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one or $x->is_inf or $y->is_one 8 66 74
Condition100%9/9
$x->is_zero or $x->is_one or $x->is_inf or $y->is_one
l!l&&r!l&&!r
12466
$x->is_zero or $x->is_one or $x->is_inf
l!l&&r!l&&!r
8470
$x->is_zero or $x->is_one
l!l&&r!l&&!r
4474
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
3518  
3519 # we need to limit the accuracy to protect against overflow
3520 66 my $fallback = 0;
3521 66 my (@params, $scale);
3522 66 ($x, @params) = $x->_find_round_parameters(@r);
3523  
3524 66 ▶ return $x if $x->is_nan(); # error in _find_round_parameters?
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 66 66
3525  
3526 # no rounding at all, so must use fallback
3527 66 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 66 0 66
3528 # simulate old behaviour
3529 66 $params[0] = $class->div_scale(); # and round to it as accuracy
3530 66 $scale = $params[0]+4; # at least four more for proper round
3531 66 $params[2] = $r[2]; # round mode by caller or undef
3532 66 $fallback = 1; # to clear a/p afterwards
3533 } else {
3534 # the 4 below is empirical, and there might be cases where it is not
3535 # enough...
3536 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
3537 }
3538  
3539 # when user set globals, they would interfere with our calculation, so
3540 # disable them and later re-enable them
3541 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
3542 66 my $abr = "$class\::accuracy";
3543 66 my $ab = $$abr;
3544 66 $$abr = undef;
3545 66 my $pbr = "$class\::precision";
3546 66 my $pb = $$pbr;
3547 66 $$pbr = undef;
3548 # we also need to disable any set A or P on $x (_find_round_parameters took
3549 # them already into account), since these would interfere, too
3550 66 $x->{_a} = undef;
3551 66 $x->{_p} = undef;
3552  
3553 # Disabling upgrading and downgrading is no longer necessary to avoid an
3554 # infinite recursion, but it avoids unnecessary upgrading and downgrading in
3555 # the intermediate computations.
3556  
3557 66 local $Math::BigInt::upgrade = undef;
3558 66 local $Math::BigFloat::downgrade = undef;
3559  
3560 # remember sign and make $x positive, since -4 ** (1/2) => -2
3561 66 my $sign = 0;
3562 66 ▶ $sign = 1 if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 0 66 66
3563 66 $x->{sign} = '+';
3564  
3565 66 my $is_two = 0;
3566 66 ▶ if ($y->isa('Math::BigFloat')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->isa('Math::BigFloat')) { } 66 0 66
3567 $is_two = $y->{sign} eq '+' && $LIB->_is_two($y->{_m})
3568 66 ▶ && $LIB->_is_zero($y->{_e});
Condition66%4/6
$y->{'sign'} eq '+' && $LIB->_is_two($y->{'_m'}) && $LIB->_is_zero($y->{'_e'})
!ll&&!rl&&r
20046
$y->{"sign"} eq "+" and $LIB->_is_two($y->{"_m"})
!ll&&!rl&&r
02046
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
3569 } else {
3570 0 $is_two = $y == 2;
3571 }
3572  
3573 # normal square root if $y == 2:
3574 66 ▶ if ($is_two) {
Branch: 2 branches
BranchTrueFalseTotal
if ($is_two) { } 46 20 66
elsif ($y->is_one('-')) { } 0 20 20
3575 46 $x = $x->bsqrt($scale+4);
3576 } elsif ($y->is_one('-')) {
3577 # $x ** -1 => 1/$x
3578 0 my $u = $class->bone()->bdiv($x, $scale);
3579 # copy private parts over
3580 0 $x->{_m} = $u->{_m};
3581 0 $x->{_e} = $u->{_e};
3582 0 $x->{_es} = $u->{_es};
3583 } else {
3584 # calculate the broot() as integer result first, and if it fits, return
3585 # it rightaway (but only if $x and $y are integer):
3586  
3587 20 my $done = 0; # not yet
3588 20 ▶ if ($y->is_int() && $x->is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($y->is_int and $x->is_int) 20 0 20
Condition33%1/3
$y->is_int and $x->is_int
!ll&&!rl&&r
0020
MC/DC0%0/2
$y->is_int and $x->is_int
$y->is_int $x->is_int
Truth table33%1/3
A and B
A= $y->is_intB= $x->is_int
ABresult
0X0
100
111
3589 20 my $i = $LIB->_copy($x->{_m});
3590 20 ▶ $i = $LIB->_lsft($i, $x->{_e}, 10) unless $LIB->_is_zero($x->{_e});
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->_is_zero($x->{"_e"}) 0 20 20
3591 20 my $int = Math::BigInt->bzero();
3592 20 $int->{value} = $i;
3593 20 $int = $int->broot($y->as_number());
3594 # if ($exact)
3595 20 ▶ if ($int->copy()->bpow($y) == $x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int->copy->bpow($y) == $x) 14 6 20
3596 # found result, return it
3597 14 $x->{_m} = $int->{value};
3598 14 $x->{_e} = $LIB->_zero();
3599 14 $x->{_es} = '+';
3600 14 $x = $x->bnorm();
3601 14 $done = 1;
3602 }
3603 }
3604 20 ▶ if ($done == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done == 0) 6 14 20
3605 6 my $u = $class->bone()->bdiv($y, $scale+4);
3606 6 $u->{_a} = undef;
3607 6 $u->{_p} = undef;
3608 6 $x = $x->bpow($u, $scale+4); # el cheapo
3609 }
3610 }
3611 66 ▶ $x = $x->bneg() if $sign == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $sign == 1 0 66 66
3612  
3613 # shortcut to not run through _find_round_parameters again
3614 66 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 66 0 66
3615 66 $x = $x->bround($params[0], $params[2]); # then round accordingly
3616 } else {
3617 0 $x = $x->bfround($params[1], $params[2]); # then round accordingly
3618 }
3619 66 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 66 0 66
3620 # clear a/p after round, since user did not request it
3621 66 $x->{_a} = undef;
3622 66 $x->{_p} = undef;
3623 }
3624 # restore globals
3625 66 $$abr = $ab;
3626 66 $$pbr = $pb;
3627  
3628 66 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf 0 66 66
Condition16%1/6
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf
!ll&&!rl&&r
6600
$x->is_int or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf
Truth table25%1/4
A and B or C
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_inf
ABCresult
0XX0
1000
11X1
1011
3629 if defined($downgrade) && ($x -> is_int() || $x -> is_inf());
3630 66 $x;
3631 }
3632  
3633 sub bfac {
3634 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
3635 # compute factorial number, modifies first argument
3636  
3637 # set up parameters
3638 80 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bfac: called CC=15 SCAR=28.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 80 0 80
3639  
3640 # inf => inf
3641 80 ▶ return $x if $x->modify('bfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfac") 0 80 80
3642  
3643 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
3644 72 ▶ return $x -> binf("+", @r) if $x->is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 4 68 72
3645 68 ▶ return $x -> bone(@r) if $x->is_zero() || $x->is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero or $x->is_one 8 60 68
Condition100%3/3
$x->is_zero or $x->is_one
l!l&&r!l&&!r
4460
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
3646  
3647 60 ▶ if ($x -> is_neg() || !$x -> is_int()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_neg or not $x->is_int) 4 56 60
Condition66%2/3
$x->is_neg or not $x->is_int
l!l&&r!l&&!r
4056
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
3648 4 ▶ return $upgrade -> bfac($x, @r) if defined($upgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 0 4 4
3649 4 return $x -> bnan(@r);
3650 }
3651  
3652 56 ▶ if (! $LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 8 48 56
3653 8 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10); # change 12e1 to 120e0
3654 8 $x->{_e} = $LIB->_zero(); # normalize
3655 8 $x->{_es} = '+';
3656 }
3657 56 $x->{_m} = $LIB->_fac($x->{_m}); # calculate factorial
3658  
3659 56 $x = $x->bnorm()->round(@r); # norm again and round result
3660  
3661 56 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf 0 56 56
Condition16%1/6
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf
!ll&&!rl&&r
5600
$x->is_int or $x->is_inf
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf
Truth table25%1/4
A and B or C
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_inf
ABCresult
0XX0
1000
11X1
1011
3662 && ($x -> is_int() || $x -> is_inf());
3663 56 $x;
3664 }
3665  
3666 sub bdfac {
3667 # compute double factorial
3668  
3669 # set up parameters
3670 72 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bdfac: called CC=14 SCAR=27.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 72 0 72
3671  
3672 72 ▶ return $x if $x->modify('bdfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bdfac") 0 72 72
3673  
3674 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
3675 64 ▶ return $x -> binf("+", @r) if $x->is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 4 60 64
3676  
3677 60 ▶ 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
3678 4 ▶ return $upgrade -> bdfac($x, @r) if defined($upgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 0 4 4
3679 4 return $x -> bnan(@r);
3680 }
3681  
3682 56 ▶ return $x->bone() if $x <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= 1 12 44 56
3683  
3684 44 ▶ croak("bdfac() requires a newer version of the $LIB library.")
Branch: 1 branch
BranchTrueFalseTotal
unless $LIB->can("_dfac") 0 44 44
3685 unless $LIB->can('_dfac');
3686  
3687 44 ▶ if (! $LIB->_is_zero($x->{_e})) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 4 40 44
3688 4 $x->{_m} = $LIB->_lsft($x->{_m}, $x->{_e}, 10); # change 12e1 to 120e0
3689 4 $x->{_e} = $LIB->_zero(); # normalize
3690 4 $x->{_es} = '+';
3691 }
3692 44 $x->{_m} = $LIB->_dfac($x->{_m}); # calculate factorial
3693  
3694 44 $x = $x->bnorm()->round(@r); # norm again and round result
3695  
3696 44 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 44 44
Condition33%1/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
4400
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
3697 if defined($downgrade) && $x -> is_int();
3698 44 return $x;
3699 }
3700  
3701 sub btfac {
3702 # compute triple factorial
3703  
3704 # set up parameters
3705 76 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine btfac: called CC=14 SCAR=27.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 76 0 76
3706  
3707 76 ▶ return $x if $x->modify('btfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("btfac") 0 76 76
3708  
3709 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
3710 68 ▶ return $x -> binf("+", @r) if $x->is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 4 64 68
3711  
3712 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
3713 4 ▶ return $upgrade -> btfac($x, @r) if defined($upgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 0 4 4
3714 4 return $x -> bnan(@r);
3715 }
3716  
3717 60 my $k = $class -> new("3");
3718 60 ▶ return $x->bnan(@r) if $x <= -$k;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= -$k 0 60 60
3719  
3720 60 my $one = $class -> bone();
3721 60 ▶ return $x->bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 16 44 60
3722  
3723 44 my $f = $x -> copy();
3724 44 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 60 44 104
3725 60 $x = $x -> bmul($f);
3726 }
3727  
3728 44 $x = $x->round(@r);
3729  
3730 44 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 44 44
Condition33%1/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
4400
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
3731 if defined($downgrade) && $x -> is_int();
3732 44 return $x;
3733 }
3734  
3735 sub bmfac {
3736 364 ▶ my ($class, $x, $k, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bmfac: called CC=17 SCAR=29.1
Pod documented
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
3737 ? (ref($_[0]), @_)
3738 : objectify(2, @_);
3739  
3740 364 ▶ return $x if $x->modify('bmfac');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bmfac") 0 364 364
3741  
3742 364 ▶ return $x -> bnan(@r) if $x->is_nan() || $x->is_inf("-") || !$k->is_pos();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $x->is_inf("-") or not $k->is_pos 36 308 344
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
3743 308 ▶ return $x -> binf("+", @r) if $x->is_inf("+");
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf("+") 20 288 308
3744  
3745 288 ▶ if ($x <= -$k || !$x -> is_int() ||
Branch: 1 branch
BranchTrueFalseTotal
if ($x <= -$k or not $x->is_int or $k->is_finite and not $k->is_int) 4 264 268
Condition88%8/9
$x <= -$k or not $x->is_int or $k->is_finite and not $k->is_int
l!l&&r!l&&!r
204264
$x <= -$k or not $x->is_int
l!l&&r!l&&!r
200268
$k->is_finite and not $k->is_int
!ll&&!rl&&r
42604
MC/DC50%2/4
$x <= -$k or not $x->is_int or $k->is_finite and not $k->is_int
$x <= -$k not $x->is_int $k->is_finite not $k->is_int
Truth table60%3/5
A or not(B) or C and not(D)
A= $x <= -$kB= not $x->is_intC= $k->is_finiteD= not $k->is_int
ABCDresult
1XXX1
01XX1
00111
000X0
00100
3746 ($k -> is_finite() && !$k -> is_int()))
3747 {
3748 24 ▶ return $upgrade -> bmfac($x, $k, @r) if defined($upgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade 0 24 24
3749 24 return $x -> bnan(@r);
3750 }
3751  
3752 264 my $one = $class -> bone();
3753 264 ▶ return $x->bone(@r) if $x <= $one;
Branch: 1 branch
BranchTrueFalseTotal
if $x <= $one 80 184 264
3754  
3755 184 my $f = $x -> copy();
3756 184 ▶ while ($f -> bsub($k) > $one) {
Branch: 1 branch
BranchTrueFalseTotal
if ($f->bsub($k) > $one) 284 184 468
3757 284 $x = $x -> bmul($f);
3758 }
3759  
3760 184 $x = $x->round(@r);
3761  
3762 184 ▶ return $downgrade -> new($x -> bdstr(), @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 184 184
Condition33%1/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
18400
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
3763 if defined($downgrade) && $x -> is_int();
3764 184 return $x;
3765 }
3766  
3767 sub blsft {
3768 # shift left by $y in base $b, i.e., multiply by $b ** $y
3769  
3770 # set up parameters
3771 33 ▶ my ($class, $x, $y, $b, @r)
Subroutine blsft: called CC=16 SCAR=34.9
Pod documented
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
3772 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
3773 ? (ref($_[0]), @_)
3774 : objectify(2, @_);
3775  
3776 33 ▶ return $x if $x -> modify('blsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("blsft") 0 33 33
3777  
3778 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
3779  
3780 29 ▶ $b = 2 if !defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 29 0 29
3781 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
3782 unless defined(blessed($b)) && $b -> isa(__PACKAGE__);
3783 29 ▶ return $x -> bnan(@r) if $b -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $b->is_nan 0 29 29
3784  
3785 # There needs to be more checking for special cases here. Fixme!
3786  
3787 # shift by a negative amount?
3788 29 ▶ return $x -> brsft($y -> copy() -> babs(), $b) if $y -> {sign} =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} =~ /^-/ 0 29 29
3789  
3790 29 $x = $x -> bmul($b -> bpow($y), $r[0], $r[1], $r[2], $y);
3791  
3792 29 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 29 29
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
2900
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
3793 && ($x -> is_int() || $x -> is_inf() || $x -> is_nan());
3794 29 return $x;
3795 }
3796  
3797 sub brsft {
3798 # shift right by $y in base $b, i.e., divide by $b ** $y
3799  
3800 # set up parameters
3801 48 ▶ my ($class, $x, $y, $b, @r)
Subroutine brsft: called CC=16 SCAR=33.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] && ref $_[1] eq ref $_[2] ? : 0 48 48
Condition83%5/6
ref $_[0] and ref $_[0] eq ref $_[1] and ref $_[1] eq ref $_[2]
!ll&&!rl&&r
20280
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
17328
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
3802 = ref($_[0]) && ref($_[0]) eq ref($_[1]) && ref($_[1]) eq ref($_[2])
3803 ? (ref($_[0]), @_)
3804 : objectify(2, @_);
3805  
3806 48 ▶ return $x if $x -> modify('brsft');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("brsft") 0 48 48
3807  
3808 48 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 4 44 48
Condition66%2/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
4044
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
3809  
3810 # There needs to be more checking for special cases here. Fixme!
3811  
3812 44 ▶ $b = 2 if !defined $b;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $b 33 11 44
3813 44 ▶ $b = $class -> new($b)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($b) and $b->isa(__PACKAGE__) 44 0 44
Condition66%2/3
defined builtin::blessed($b) and $b->isa(__PACKAGE__)
!ll&&!rl&&r
33110
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
3814 unless defined(blessed($b)) && $b -> isa(__PACKAGE__);
3815 44 ▶ return $x -> bnan(@r) if $b -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $b->is_nan 0 44 44
3816  
3817 # shift by a negative amount?
3818 44 ▶ return $x -> blsft($y -> copy() -> babs(), $b) if $y -> {sign} =~ /^-/;
Branch: 1 branch
BranchTrueFalseTotal
if $y->{"sign"} =~ /^-/ 0 44 44
3819  
3820 # call bdiv()
3821 44 $x = $x -> bdiv($b -> bpow($y), $r[0], $r[1], $r[2], $y);
3822  
3823 44 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 44 44
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
4400
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
3824 && ($x -> is_int() || $x -> is_inf() || $x -> is_nan());
3825 44 return $x;
3826 }
3827  
3828 ###############################################################################
3829 # Bitwise methods
3830 ###############################################################################
3831  
3832 # Bitwise left shift.
3833  
3834 sub bblsft {
3835 8 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bblsft: called CC=4 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 8 0 8
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
008
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
3836 ? (ref($_[0]), @_)
3837 : objectify(2, @_);
3838  
3839 8 my $xint = Math::BigInt -> bblsft($x, $y, @r);
3840  
3841 # disable downgrading
3842  
3843 8 my $dng = $class -> downgrade();
3844 8 $class -> downgrade(undef);
3845  
3846 # convert to Math::BigFloat without downgrading
3847  
3848 8 my $xflt = $class -> new($xint);
3849  
3850 # reset downgrading
3851  
3852 8 $class -> downgrade($dng);
3853  
3854 8 $x -> {sign} = $xflt -> {sign};
3855 8 $x -> {_m} = $xflt -> {_m};
3856 8 $x -> {_es} = $xflt -> {_es};
3857 8 $x -> {_e} = $xflt -> {_e};
3858  
3859 # now we might downgrade
3860  
3861 8 ▶ return $downgrade -> new($x) if defined($downgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 8 8
3862 8 $x -> round(@r);
3863 }
3864  
3865 # Bitwise left shift.
3866  
3867 sub bbrsft {
3868 8 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bbrsft: called CC=4 SCAR=14.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 8 0 8
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
008
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
3869 ? (ref($_[0]), @_)
3870 : objectify(2, @_);
3871  
3872 8 my $xint = Math::BigInt -> bbrsft($x, $y, @r);
3873  
3874 # disable downgrading
3875  
3876 8 my $dng = $class -> downgrade();
3877 8 $class -> downgrade(undef);
3878  
3879 # convert to Math::BigFloat without downgrading
3880  
3881 8 my $xflt = $class -> new($xint);
3882  
3883 # reset downgrading
3884  
3885 8 $class -> downgrade($dng);
3886  
3887 8 $x -> {sign} = $xflt -> {sign};
3888 8 $x -> {_m} = $xflt -> {_m};
3889 8 $x -> {_es} = $xflt -> {_es};
3890 8 $x -> {_e} = $xflt -> {_e};
3891  
3892 # now we might downgrade
3893  
3894 8 ▶ return $downgrade -> new($x) if defined($downgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 8 8
3895 8 $x -> round(@r);
3896 }
3897  
3898 sub band {
3899 1 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine band: called CC=7 SCAR=21.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 0 1 1
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
100
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
3900 ? (ref($_[0]), @_)
3901 : objectify(2, @_);
3902  
3903 1 ▶ return if $x -> modify('band');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("band") 0 1 1
3904  
3905 1 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 0 1 1
Condition33%1/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
001
MC/DC0%0/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table33%1/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
3906  
3907 1 my $xint = $x -> as_int(); # to Math::BigInt
3908 1 my $yint = $y -> as_int(); # to Math::BigInt
3909  
3910 1 $xint = $xint -> band($yint);
3911  
3912 1 ▶ return $xint -> round(@r) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 1 1
3913  
3914 1 my $xflt = $class -> new($xint); # back to Math::BigFloat
3915 1 $x -> {sign} = $xflt -> {sign};
3916 1 $x -> {_m} = $xflt -> {_m};
3917 1 $x -> {_es} = $xflt -> {_es};
3918 1 $x -> {_e} = $xflt -> {_e};
3919  
3920 1 return $x -> round(@r);
3921 }
3922  
3923 sub bior {
3924 1 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bior: called CC=7 SCAR=21.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 0 1 1
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
100
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
3925 ? (ref($_[0]), @_)
3926 : objectify(2, @_);
3927  
3928 1 ▶ return if $x -> modify('bior');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bior") 0 1 1
3929  
3930 1 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 0 1 1
Condition33%1/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
001
MC/DC0%0/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table33%1/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
3931  
3932 1 my $xint = $x -> as_int(); # to Math::BigInt
3933 1 my $yint = $y -> as_int(); # to Math::BigInt
3934  
3935 1 $xint = $xint -> bior($yint);
3936  
3937 1 ▶ return $xint -> round(@r) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 1 1
3938  
3939 1 my $xflt = $class -> new($xint); # back to Math::BigFloat
3940 1 $x -> {sign} = $xflt -> {sign};
3941 1 $x -> {_m} = $xflt -> {_m};
3942 1 $x -> {_es} = $xflt -> {_es};
3943 1 $x -> {_e} = $xflt -> {_e};
3944  
3945 1 return $x -> round(@r);
3946 }
3947  
3948 sub bxor {
3949 1 ▶ my ($class, $x, $y, @r) = ref($_[0]) && ref($_[0]) eq ref($_[1])
Subroutine bxor: called CC=7 SCAR=21.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] && ref $_[0] eq ref $_[1] ? : 0 1 1
Condition33%1/3
ref $_[0] and ref $_[0] eq ref $_[1]
!ll&&!rl&&r
100
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
3950 ? (ref($_[0]), @_)
3951 : objectify(2, @_);
3952  
3953 1 ▶ return if $x -> modify('bxor');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bxor") 0 1 1
3954  
3955 1 ▶ return $x -> bnan(@r) if $x -> is_nan() || $y -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan or $y->is_nan 0 1 1
Condition33%1/3
$x->is_nan or $y->is_nan
l!l&&r!l&&!r
001
MC/DC0%0/2
$x->is_nan or $y->is_nan
$x->is_nan $y->is_nan
Truth table33%1/3
A or B
A= $x->is_nanB= $y->is_nan
ABresult
1X1
011
000
3956  
3957 1 my $xint = $x -> as_int(); # to Math::BigInt
3958 1 my $yint = $y -> as_int(); # to Math::BigInt
3959  
3960 1 $xint = $xint -> bxor($yint);
3961  
3962 1 ▶ return $xint -> round(@r) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 1 1
3963  
3964 1 my $xflt = $class -> new($xint); # back to Math::BigFloat
3965 1 $x -> {sign} = $xflt -> {sign};
3966 1 $x -> {_m} = $xflt -> {_m};
3967 1 $x -> {_es} = $xflt -> {_es};
3968 1 $x -> {_e} = $xflt -> {_e};
3969  
3970 1 return $x -> round(@r);
3971 }
3972  
3973 sub bnot {
3974 4 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bnot: called CC=5 SCAR=16.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4 0 4
3975  
3976 4 ▶ return if $x -> modify('bnot');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bnot") 0 4 4
3977  
3978 4 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 4 4
3979  
3980 4 my $xint = $x -> as_int(); # to Math::BigInt
3981 4 $xint = $xint -> bnot();
3982  
3983 4 ▶ return $xint -> round(@r) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 4 4
3984  
3985 4 my $xflt = $class -> new($xint); # back to Math::BigFloat
3986 4 $x -> {sign} = $xflt -> {sign};
3987 4 $x -> {_m} = $xflt -> {_m};
3988 4 $x -> {_es} = $xflt -> {_es};
3989 4 $x -> {_e} = $xflt -> {_e};
3990  
3991 4 return $x -> round(@r);
3992 }
3993  
3994 ###############################################################################
3995 # Rounding methods
3996 ###############################################################################
3997  
3998 sub bround {
3999 # accuracy: preserve $N digits, and overwrite the rest with 0's
4000  
4001 41467 ▶ my ($class, $x, @a) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bround: called CC=30 SCAR=39.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 41467 0 41467
4002  
4003 41467 ▶ if (($a[0] || 0) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($a[0] || 0) < 0) 3 41464 41467
Condition100%2/2
$a[0] || 0
l!l
375753892
MC/DC100%1/1
$a[0] || 0
$a[0]
Truth table100%2/2
A
A= $a[0]
Aresult
11
00
4004 3 croak('bround() needs positive accuracy');
4005 }
4006  
4007 41464 ▶ return $x if $x->modify('bround');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bround") 0 41464 41464
4008  
4009 41464 my ($scale, $mode) = $x->_scale_a(@a);
4010 41464 ▶ if (!defined $scale) { # no-op
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scale) 3889 37575 41464
4011 3889 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 2 3885 3887
Condition77%7/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
388504
$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/DC25%1/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table40%2/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4012 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4013 3885 return $x;
4014 }
4015  
4016 # Scale is now either $x->{_a}, $accuracy, or the input argument. Test
4017 # whether $x already has lower accuracy, do nothing in this case but do
4018 # round if the accuracy is the same, since a math operation might want to
4019 # round a number with A=5 to 5 digits afterwards again
4020  
4021 37575 ▶ if (defined $x->{_a} && $x->{_a} < $scale) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x->{"_a"} and $x->{"_a"} < $scale) 4 37571 37575
Condition100%3/3
defined $x->{"_a"} and $x->{"_a"} < $scale
!ll&&!rl&&r
10799267724
MC/DC100%2/2
defined $x->{"_a"} and $x->{"_a"} < $scale
defined $x->{"_a"} $x->{"_a"} < $scale
Truth table100%3/3
A and B
A= defined $x->{"_a"}B= $x->{"_a"} < $scale
ABresult
0X0
100
111
4022 4 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 4 4
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
400
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4023 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4024 4 return $x;
4025 }
4026  
4027 # scale < 0 makes no sense
4028 # scale == 0 => keep all digits
4029 # never round a +-inf, NaN
4030  
4031 37571 ▶ if ($scale <= 0 || $x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= 0 or not $x->{"sign"} =~ /^[+-]$/) 12 37559 37571
Condition66%2/3
$scale <= 0 or not $x->{"sign"} =~ /^[+-]$/
l!l&&r!l&&!r
01237559
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
4032 12 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 12 12
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
1200
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4033 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4034 12 return $x;
4035 }
4036  
4037 # 1: never round a 0
4038 # 2: if we should keep more digits than the mantissa has, do nothing
4039 37559 ▶ if ($x->is_zero() || $LIB->_len($x->{_m}) <= $scale) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero or $LIB->_len($x->{"_m"}) <= $scale) 12805 24754 37559
Condition100%3/3
$x->is_zero or $LIB->_len($x->{"_m"}) <= $scale
l!l&&r!l&&!r
591274624754
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
4040 12805 ▶ $x->{_a} = $scale if !defined $x->{_a} || $x->{_a} > $scale;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x->{"_a"} or $x->{"_a"} > $scale 1979 10826 12805
Condition100%3/3
not defined $x->{"_a"} or $x->{"_a"} > $scale
l!l&&r!l&&!r
19334610826
MC/DC100%2/2
not defined $x->{"_a"} or $x->{"_a"} > $scale
not defined $x->{"_a"} $x->{"_a"} > $scale
Truth table100%3/3
not(A) or B
A= not defined $x->{"_a"}B= $x->{"_a"} > $scale
ABresult
1X1
011
000
4041 12805 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 12805 12805
Condition44%4/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
1280410
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
001
$x->is_int or $x->is_inf
l!l&&r!l&&!r
001
MC/DC0%0/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table40%2/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4042 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4043 12805 return $x;
4044 }
4045  
4046 # pass sign to bround for '+inf' and '-inf' rounding modes
4047 24754 my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt';
4048  
4049 24754 $m = $m->bround($scale, $mode); # round mantissa
4050 24754 $x->{_m} = $m->{value}; # get our mantissa back
4051 24754 $x->{_a} = $scale; # remember rounding
4052 24754 $x->{_p} = undef; # and clear P
4053  
4054 # bnorm() downgrades if necessary, so no need to check whether to downgrade.
4055 24754 $x->bnorm(); # del trailing zeros gen. by bround()
4056 }
4057  
4058 sub bfround {
4059 # precision: round to the $Nth digit left (+$n) or right (-$n) from the '.'
4060 # $n == 0 means round to integer
4061 # expects and returns normalized numbers!
4062  
4063 724 ▶ my ($class, $x, @p) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bfround: called CC=57 SCAR=56.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 724 0 724
4064  
4065 724 ▶ return $x if $x->modify('bfround'); # no-op
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfround") 0 724 724
4066  
4067 724 my ($scale, $mode) = $x->_scale_p(@p);
4068 724 ▶ if (!defined $scale) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $scale) 4 720 724
4069 4 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 2 0 2
Condition66%6/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
004
$x->is_int or $x->is_inf or $x->is_nan
l!l&&r!l&&!r
310
$x->is_int or $x->is_inf
l!l&&r!l&&!r
211
MC/DC0%0/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4070 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4071 0 return $x;
4072 }
4073  
4074 # never round a 0, +-inf, NaN
4075  
4076 720 ▶ if ($x->is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_zero) 20 700 720
4077 20 ▶ $x->{_p} = $scale if !defined $x->{_p} || $x->{_p} < $scale; # -3 < -2
Branch: 1 branch
BranchTrueFalseTotal
if not defined $x->{"_p"} or $x->{"_p"} < $scale 20 0 20
Condition33%1/3
not defined $x->{"_p"} or $x->{"_p"} < $scale
l!l&&r!l&&!r
2000
MC/DC0%0/2
not defined $x->{"_p"} or $x->{"_p"} < $scale
not defined $x->{"_p"} $x->{"_p"} < $scale
Truth table33%1/3
not(A) or B
A= not defined $x->{"_p"}B= $x->{"_p"} < $scale
ABresult
1X1
011
000
4078 20 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 20 20
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
2000
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4079 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4080 20 return $x;
4081 }
4082  
4083 700 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 12 688 700
4084 12 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 12 12
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
1200
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4085 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4086 12 return $x;
4087 }
4088  
4089 # don't round if x already has lower precision
4090 688 ▶ if (defined $x->{_p} && $x->{_p} < 0 && $scale < $x->{_p}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $x->{"_p"} and $x->{"_p"} < 0 and $scale < $x->{"_p"}) 0 688 688
Condition83%5/6
defined $x->{"_p"} and $x->{"_p"} < 0 and $scale < $x->{"_p"}
!ll&&!rl&&r
68260
defined $x->{"_p"} and $x->{"_p"} < 0
!ll&&!rl&&r
68116
MC/DC0%0/3
defined $x->{"_p"} and $x->{"_p"} < 0 and $scale < $x->{"_p"}
defined $x->{"_p"} $x->{"_p"} < 0 $scale < $x->{"_p"}
Truth table75%3/4
A and B and C
A= defined $x->{"_p"}B= $x->{"_p"} < 0C= $scale < $x->{"_p"}
ABCresult
0XX0
10X0
1100
1111
4091 0 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 0 0
Condition0%0/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
000
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table0%0/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4092 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4093 0 return $x;
4094 }
4095  
4096 688 $x->{_p} = $scale; # remember round in any case
4097 688 $x->{_a} = undef; # and clear A
4098 688 ▶ if ($scale < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < 0) { } 532 156 688
4099 # round right from the '.'
4100  
4101 532 ▶ if ($x->{_es} eq '+') { # e >= 0 => nothing to round
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "+") 28 504 532
4102 28 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 28 28
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
2800
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4103 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4104 28 return $x;
4105 }
4106  
4107 504 $scale = -$scale; # positive for simplicity
4108 504 my $len = $LIB->_len($x->{_m}); # length of mantissa
4109  
4110 # the following poses a restriction on _e, but if _e is bigger than a
4111 # scalar, you got other problems (memory etc) anyway
4112 504 my $dad = -(0+ ($x->{_es}.$LIB->_num($x->{_e}))); # digits after dot
4113 504 my $zad = 0; # zeros after dot
4114 504 ▶ $zad = $dad - $len if (-$dad < -$len); # for 0.00..00xxx style
Branch: 1 branch
BranchTrueFalseTotal
if -$dad < -$len 195 309 504
4115  
4116 # print "scale $scale dad $dad zad $zad len $len\n";
4117 # number bsstr len zad dad
4118 # 0.123 123e-3 3 0 3
4119 # 0.0123 123e-4 3 1 4
4120 # 0.001 1e-3 1 2 3
4121 # 1.23 123e-2 3 0 2
4122 # 1.2345 12345e-4 5 0 4
4123  
4124 # do not round after/right of the $dad
4125  
4126 504 ▶ if ($scale > $dad) { # 0.123, scale >= 3 => exit
Branch: 1 branch
BranchTrueFalseTotal
if ($scale > $dad) 47 457 504
4127 47 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 47 47
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
4700
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4128 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4129 47 return $x;
4130 }
4131  
4132 # round to zero if rounding inside the $zad, but not for last zero like:
4133 # 0.0065, scale -2, round last '0' with following '65' (scale == zad
4134 # case)
4135 457 ▶ if ($scale < $zad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale < $zad) 40 417 457
4136 40 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 40 40
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
4000
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4137 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4138 40 return $x->bzero();
4139 }
4140  
4141 417 ▶ if ($scale == $zad) { # for 0.006, scale -3 and trunc
Branch: 1 branch
BranchTrueFalseTotal
if ($scale == $zad) { } 41 376 417
4142 41 $scale = -$len;
4143 } else {
4144 # adjust round-point to be inside mantissa
4145 376 ▶ if ($zad != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($zad != 0) { } 78 298 376
4146 78 $scale = $scale-$zad;
4147 } else {
4148 298 my $dbd = $len - $dad;
4149 298 ▶ $dbd = 0 if $dbd < 0; # digits before dot
Branch: 1 branch
BranchTrueFalseTotal
if $dbd < 0 0 298 298
4150 298 $scale = $dbd+$scale;
4151 }
4152 }
4153 } else {
4154 # round left from the '.'
4155  
4156 # 123 => 100 means length(123) = 3 - $scale (2) => 1
4157  
4158 156 my $dbt = $LIB->_len($x->{_m});
4159 # digits before dot
4160 156 my $dbd = $dbt + ($x->{_es} . $LIB->_num($x->{_e}));
4161 # should be the same, so treat it as this
4162 156 ▶ $scale = 1 if $scale == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $scale == 0 103 53 156
4163 # shortcut if already integer
4164 156 ▶ if ($scale == 1 && $dbt <= $dbd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale == 1 and $dbt <= $dbd) 10 146 156
Condition100%3/3
$scale == 1 and $dbt <= $dbd
!ll&&!rl&&r
4010610
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
4165 10 ▶ return $downgrade -> new($x) if defined($downgrade)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan 0 10 10
Condition11%1/9
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
!ll&&!rl&&r
1000
$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/4
defined $Math::BigFloat::downgrade and $x->is_int || $x->is_inf || $x->is_nan
defined $Math::BigFloat::downgrade $x->is_int $x->is_inf $x->is_nan
Truth table20%1/5
A and B or C or D
A= defined $Math::BigFloat::downgradeB= $x->is_intC= $x->is_infD= $x->is_nan
ABCDresult
0XXX0
10000
11XX1
101X1
10011
4166 && ($x->is_int() || $x->is_inf() || $x->is_nan());
4167 10 return $x;
4168 }
4169 # maximum digits before dot
4170 146 ++$dbd;
4171  
4172 146 ▶ if ($scale > $dbd) {
Branch: 2 branches
BranchTrueFalseTotal
if ($scale > $dbd) { } 27 119 146
elsif ($scale == $dbd) { } 72 47 119
4173 # not enough digits before dot, so round to zero
4174 27 ▶ return $downgrade -> new($x) if defined($downgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 27 27
4175 27 return $x->bzero;
4176 } elsif ($scale == $dbd) {
4177 # maximum
4178 72 $scale = -$dbt;
4179 } else {
4180 47 $scale = $dbd - $scale;
4181 }
4182 }
4183  
4184 # pass sign to bround for rounding modes '+inf' and '-inf'
4185 536 my $m = bless { sign => $x->{sign}, value => $x->{_m} }, 'Math::BigInt';
4186 536 $m = $m->bround($scale, $mode);
4187 536 $x->{_m} = $m->{value}; # get our mantissa back
4188  
4189 # bnorm() downgrades if necessary, so no need to check whether to downgrade.
4190 536 $x->bnorm();
4191 }
4192  
4193 sub bfloor {
4194 # round towards minus infinity
4195 84 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bfloor: called CC=8 SCAR=20.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 84 0 84
4196  
4197 84 ▶ return $x if $x->modify('bfloor');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bfloor") 0 84 84
4198  
4199 84 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 79 84
4200  
4201 79 ▶ if ($x->{sign} =~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]$/) 70 9 79
4202 # if $x has digits after dot, remove them
4203 70 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 47 23 70
4204 47 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10);
4205 47 $x->{_e} = $LIB->_zero();
4206 47 $x->{_es} = '+';
4207 # increment if negative
4208 47 ▶ $x->{_m} = $LIB->_inc($x->{_m}) if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 6 41 47
4209 }
4210 70 $x = $x->round(@r);
4211 }
4212 79 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined($downgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 2 77 79
4213 77 return $x;
4214 }
4215  
4216 sub bceil {
4217 # round towards plus infinity
4218 43 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bceil: called CC=9 SCAR=22.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 43 0 43
4219  
4220 43 ▶ return $x if $x->modify('bceil');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bceil") 0 43 43
4221  
4222 43 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 38 43
4223  
4224 # if $x has digits after dot, remove them
4225 38 ▶ if ($x->{sign} =~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]$/) 29 9 38
4226 29 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 17 12 29
4227 17 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10);
4228 17 $x->{_e} = $LIB->_zero();
4229 17 $x->{_es} = '+';
4230 17 ▶ if ($x->{sign} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'sign'} eq '+') { } 9 8 17
4231 9 $x->{_m} = $LIB->_inc($x->{_m}); # increment if positive
4232 } else {
4233 8 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{_m}); # avoid -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 4 4 8
4234 }
4235 }
4236 29 $x = $x->round(@r);
4237 }
4238  
4239 38 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined($downgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 2 36 38
4240 36 return $x;
4241 }
4242  
4243 sub bint {
4244 # round towards zero
4245 179 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bint: called CC=8 SCAR=20.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 179 0 179
4246  
4247 179 ▶ return $x if $x->modify('bint');
Branch: 1 branch
BranchTrueFalseTotal
if $x->modify("bint") 0 179 179
4248  
4249 179 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 5 174 179
4250  
4251 174 ▶ if ($x->{sign} =~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} =~ /^[+-]$/) 165 9 174
4252 # if $x has digits after the decimal point
4253 165 ▶ if ($x->{_es} eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"_es"} eq "-") 13 152 165
4254 13 $x->{_m} = $LIB->_rsft($x->{_m}, $x->{_e}, 10); # remove frac part
4255 13 $x->{_e} = $LIB->_zero(); # truncate/normalize
4256 13 $x->{_es} = '+'; # abs e
4257 13 ▶ $x->{sign} = '+' if $LIB->_is_zero($x->{_m}); # avoid -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 5 8 13
4258 }
4259 165 $x = $x->round(@r);
4260 }
4261  
4262 174 ▶ return $downgrade -> new($x -> bdstr(), @r) if defined($downgrade);
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 2 172 174
4263 172 return $x;
4264 }
4265  
4266 ###############################################################################
4267 # Other mathematical methods
4268 ###############################################################################
4269  
4270 sub bgcd {
4271 # (BINT or num_str, BINT or num_str) return BINT
4272 # does not modify arguments, but returns new object
4273  
4274 # Class::method(...) -> Class->method(...)
4275 133 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine bgcd: called CC=16 SCAR=29.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 1 132 133
Condition77%7/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
01321
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
13111
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
20131
MC/DC25%1/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
4276 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
4277 {
4278 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
4279 # " use is as a method instead";
4280 1 unshift @_, __PACKAGE__;
4281 }
4282  
4283 133 my ($class, @args) = objectify(0, @_);
4284  
4285 133 my $x = shift @args;
4286 133 ▶ $x = defined(blessed($x)) && $x -> isa(__PACKAGE__) ? $x -> copy()
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($x) && $x->isa(__PACKAGE__) ? : 133 0 133
Condition33%1/3
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
!ll&&!rl&&r
00133
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
4287 : $class -> new($x);
4288 133 ▶ return $class->bnan() unless $x -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $x->is_int 28 105 133
4289  
4290 105 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 116 53 169
4291 116 my $y = shift @args;
4292 116 ▶ $y = $class->new($y)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($y) and $y->isa(__PACKAGE__) 0 116 116
Condition33%1/3
defined builtin::blessed($y) and $y->isa(__PACKAGE__)
!ll&&!rl&&r
00116
MC/DC0%0/2
defined builtin::blessed($y) and $y->isa(__PACKAGE__)
defined builtin::blessed($y) $y->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($y)B= $y->isa(__PACKAGE__)
ABresult
0X0
100
111
4293 unless defined(blessed($y)) && $y -> isa(__PACKAGE__);
4294 116 ▶ return $class->bnan() unless $y -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $y->is_int 12 104 116
4295  
4296 # greatest common divisor
4297 104 ▶ while (! $y->is_zero()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($y->is_zero) 246 104 350
4298 246 ($x, $y) = ($y->copy(), $x->copy()->bmod($y));
4299 }
4300  
4301 104 ▶ last if $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 40 64 104
4302 }
4303 93 $x = $x -> babs();
4304  
4305 93 ▶ return $downgrade -> new($x)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 93 93
Condition33%1/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
9300
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
4306 if defined $downgrade && $x->is_int();
4307 93 return $x;
4308 }
4309  
4310 sub blcm {
4311 # (BFLOAT or num_str, BFLOAT or num_str) return BFLOAT
4312 # does not modify arguments, but returns new object
4313 # Least Common Multiple
4314  
4315 # Class::method(...) -> Class->method(...)
4316 35 ▶ unless (@_ && (defined(blessed($_[0])) && $_[0] -> isa(__PACKAGE__) ||
Subroutine blcm: called CC=14 SCAR=28.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i) 1 34 35
Condition77%7/9
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
!ll&&!rl&&r
0341
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) or $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
l!l&&r!l&&!r
3311
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
2033
MC/DC25%1/4
@_ and defined builtin::blessed($_[0]) && $_[0]->isa(__PACKAGE__) || $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
@_ defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
Truth table33%2/6
A and B and C or D
A= @_B= defined builtin::blessed($_[0])C= $_[0]->isa(__PACKAGE__)D= $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
4317 $_[0] =~ /^[a-z]\w*(?:::[a-z]\w*)*$/i))
4318 {
4319 #carp "Using ", (caller(0))[3], "() as a function is deprecated;",
4320 # " use is as a method instead";
4321 1 unshift @_, __PACKAGE__;
4322 }
4323  
4324 35 my ($class, @args) = objectify(0, @_);
4325  
4326 35 my $x = shift @args;
4327 35 ▶ $x = defined(blessed($x)) && $x -> isa(__PACKAGE__) ? $x -> copy()
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($x) && $x->isa(__PACKAGE__) ? : 35 0 35
Condition33%1/3
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
!ll&&!rl&&r
0035
MC/DC0%0/2
defined builtin::blessed($x) and $x->isa(__PACKAGE__)
defined builtin::blessed($x) $x->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($x)B= $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4328 : $class -> new($x);
4329 35 ▶ return $class->bnan() if $x->{sign} !~ /^[+-]$/; # x NaN?
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 8 27 35
4330  
4331 27 ▶ while (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 30 23 53
4332 30 my $y = shift @args;
4333 30 ▶ $y = $class -> new($y)
Branch: 1 branch
BranchTrueFalseTotal
unless defined builtin::blessed($y) and $y->isa(__PACKAGE__) 0 30 30
Condition33%1/3
defined builtin::blessed($y) and $y->isa(__PACKAGE__)
!ll&&!rl&&r
0030
MC/DC0%0/2
defined builtin::blessed($y) and $y->isa(__PACKAGE__)
defined builtin::blessed($y) $y->isa(__PACKAGE__)
Truth table33%1/3
A and B
A= defined builtin::blessed($y)B= $y->isa(__PACKAGE__)
ABresult
0X0
100
111
4334 unless defined(blessed($y)) && $y -> isa(__PACKAGE__);
4335 30 ▶ return $x->bnan() unless $y -> is_int();
Branch: 1 branch
BranchTrueFalseTotal
unless $y->is_int 4 26 30
4336 26 my $gcd = $x -> bgcd($y);
4337 26 $x = $x -> bdiv($gcd) -> bmul($y);
4338 }
4339  
4340 23 $x = $x -> babs();
4341  
4342 23 ▶ return $downgrade -> new($x)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade and $x->is_int 0 23 23
Condition33%1/3
defined $Math::BigFloat::downgrade and $x->is_int
!ll&&!rl&&r
2300
MC/DC0%0/2
defined $Math::BigFloat::downgrade and $x->is_int
defined $Math::BigFloat::downgrade $x->is_int
Truth table33%1/3
A and B
A= defined $Math::BigFloat::downgradeB= $x->is_int
ABresult
0X0
100
111
4343 if defined $downgrade && $x->is_int();
4344 23 return $x;
4345 }
4346  
4347 ###############################################################################
4348 # Object property methods
4349 ###############################################################################
4350  
4351 sub length {
4352 24 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine length: called CC=7 SCAR=23.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 24 0 24
4353  
4354 24 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 24 24
4355  
4356 24 ▶ return 1 if $LIB->_is_zero($x->{_m});
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($x->{"_m"}) 4 20 24
4357  
4358 20 my $len = $LIB->_len($x->{_m});
4359 20 ▶ $len += $LIB->_num($x->{_e}) if $x->{_es} eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "+" 20 0 20
4360 20 ▶ if (wantarray()) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 20 20
4361 0 my $t = 0;
4362 0 ▶ $t = $LIB->_num($x->{_e}) if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 0 0 0
4363 0 return ($len, $t);
4364 }
4365 20 $len;
4366 }
4367  
4368 sub mantissa {
4369 # return a copy of the mantissa
4370 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine mantissa: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
4371  
4372 # The following line causes a lot of noise in the test suits for
4373 # the Math-BigRat and bignum distributions. Fixme!
4374 #carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
4375  
4376 36 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 32 36
4377  
4378 32 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 8 24 32
4379 8 my $s = $x->{sign};
4380 8 $s =~ s/^\+//;
4381 8 return Math::BigInt->new($s, undef, undef); # -inf, +inf => +inf
4382 }
4383 24 my $m = Math::BigInt->new($LIB->_str($x->{_m}), undef, undef);
4384 24 ▶ $m = $m->bneg() if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 8 16 24
4385 24 $m;
4386 }
4387  
4388 sub exponent {
4389 # return a copy of the exponent
4390 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine exponent: called CC=4 SCAR=13.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
4391  
4392 # The following line causes a lot of noise in the test suits for
4393 # the Math-BigRat and bignum distributions. Fixme!
4394 #carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
4395  
4396 36 ▶ return $x -> bnan(@r) if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 4 32 36
4397  
4398 32 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 8 24 32
4399 8 my $s = $x->{sign};
4400 8 $s =~ s/^[+-]//;
4401 8 return Math::BigInt->new($s, undef, undef); # -inf, +inf => +inf
4402 }
4403 24 Math::BigInt->new($x->{_es} . $LIB->_str($x->{_e}), undef, undef);
4404 }
4405  
4406 sub parts {
4407 # return a copy of both the exponent and the mantissa
4408 32 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine parts: called CC=5 SCAR=16.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 32 0 32
4409  
4410 32 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 32 32
4411  
4412 32 ▶ if ($x->{sign} !~ /^[+-]$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->{"sign"} =~ /^[+-]$/) 12 20 32
4413 12 my $s = $x->{sign};
4414 12 $s =~ s/^\+//;
4415 12 my $se = $s;
4416 12 $se =~ s/^-//;
4417 # +inf => inf and -inf, +inf => inf
4418 12 return ($class->new($s), $class->new($se));
4419 }
4420 20 my $m = Math::BigInt->bzero();
4421 20 $m->{value} = $LIB->_copy($x->{_m});
4422 20 ▶ $m = $m->bneg() if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 8 12 20
4423 20 ($m, Math::BigInt->new($x->{_es} . $LIB->_num($x->{_e})));
4424 }
4425  
4426 # Parts used for scientific notation with significand/mantissa and exponent as
4427 # integers. E.g., "12345.6789" is returned as "123456789" (mantissa) and "-4"
4428 # (exponent).
4429  
4430 sub sparts {
4431 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine sparts: not called CC=10 SCAR=47.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4432  
4433 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4434  
4435 # Not-a-number.
4436  
4437 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
4438 0 my $mant = $class -> bnan(); # mantissa
4439 0 ▶ return $mant unless wantarray; # scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4440 0 my $expo = $class -> bnan(); # exponent
4441 0 return ($mant, $expo); # list context
4442 }
4443  
4444 # Infinity.
4445  
4446 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
4447 0 my $mant = $class -> binf($x->{sign}); # mantissa
4448 0 ▶ return $mant unless wantarray; # scalar context
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4449 0 my $expo = $class -> binf('+'); # exponent
4450 0 return ($mant, $expo); # list context
4451 }
4452  
4453 # Finite number.
4454  
4455 0 my $mant = $class -> new($x);
4456 0 $mant->{_es} = '+';
4457 0 $mant->{_e} = $LIB->_zero();
4458 0 ▶ $mant = $downgrade -> new($mant) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 0 0
4459 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4460  
4461 0 my $expo = $class -> new($x -> {_es} . $LIB->_str($x -> {_e}));
4462 0 ▶ $expo = $downgrade -> new($expo) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 0 0
4463 0 return ($mant, $expo);
4464 }
4465  
4466 # Parts used for normalized notation with significand/mantissa as either 0 or a
4467 # number in the semi-open interval [1,10). E.g., "12345.6789" is returned as
4468 # "1.23456789" and "4".
4469  
4470 sub nparts {
4471 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine nparts: not called CC=9 SCAR=45.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4472  
4473 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4474  
4475 # Not-a-number and Infinity.
4476  
4477 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
4478  
4479 # Finite number.
4480  
4481 0 my ($mant, $expo) = $x -> sparts();
4482  
4483 0 ▶ if ($mant -> bcmp(0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant->bcmp(0)) 0 0 0
4484 0 my ($ndigtot, $ndigfrac) = $mant -> length();
4485 0 my $expo10adj = $ndigtot - $ndigfrac - 1;
4486  
4487 0 ▶ if ($expo10adj > 0) { # if mantissa is not an integer
Branch: 1 branch
BranchTrueFalseTotal
if ($expo10adj > 0) 0 0 0
4488 0 $mant = $mant -> brsft($expo10adj, 10);
4489 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4490 0 $expo = $expo -> badd($expo10adj);
4491 0 return ($mant, $expo);
4492 }
4493 }
4494  
4495 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4496 0 return ($mant, $expo);
4497 }
4498  
4499 # Parts used for engineering notation with significand/mantissa as either 0 or a
4500 # number in the semi-open interval [1,1000) and the exponent is a multiple of 3.
4501 # E.g., "12345.6789" is returned as "12.3456789" and "3".
4502  
4503 sub eparts {
4504 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine eparts: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4505  
4506 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4507  
4508 # Not-a-number and Infinity.
4509  
4510 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
4511  
4512 # Finite number.
4513  
4514 0 my ($mant, $expo) = $x -> nparts();
4515  
4516 0 my $c = $expo -> copy() -> bmod(3);
4517 0 $mant = $mant -> blsft($c, 10);
4518 0 ▶ return $mant unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4519  
4520 0 $expo = $expo -> bsub($c);
4521 0 return ($mant, $expo);
4522 }
4523  
4524 # Parts used for decimal notation, e.g., "12345.6789" is returned as "12345"
4525 # (integer part) and "0.6789" (fraction part).
4526  
4527 sub dparts {
4528 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine dparts: not called CC=12 SCAR=50.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4529  
4530 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4531  
4532 # Not-a-number.
4533  
4534 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
4535 0 my $int = $class -> bnan();
4536 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4537 0 my $frc = $class -> bzero(); # or NaN?
4538 0 return ($int, $frc);
4539 }
4540  
4541 # Infinity.
4542  
4543 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
4544 0 my $int = $class -> binf($x->{sign});
4545 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4546 0 my $frc = $class -> bzero();
4547 0 return ($int, $frc);
4548 }
4549  
4550 # Finite number.
4551  
4552 0 my $int = $x -> copy();
4553 0 my $frc;
4554  
4555 # If the input is an integer.
4556  
4557 0 ▶ if ($int->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($int->{'_es'} eq '+') { } 0 0 0
4558 0 $frc = $class -> bzero();
4559 }
4560  
4561 # If the input has a fraction part
4562  
4563 else {
4564 0 $int->{_m} = $LIB -> _rsft($int->{_m}, $int->{_e}, 10);
4565 0 $int->{_e} = $LIB -> _zero();
4566 0 $int->{_es} = '+';
4567 0 ▶ $int->{sign} = '+' if $LIB->_is_zero($int->{_m}); # avoid -0
Branch: 1 branch
BranchTrueFalseTotal
if $LIB->_is_zero($int->{"_m"}) 0 0 0
4568 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4569 0 $frc = $x -> copy() -> bsub($int);
4570 0 return ($int, $frc);
4571 }
4572  
4573 0 ▶ $int = $downgrade -> new($int) if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 0 0
4574 0 ▶ return $int unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4575 0 return $int, $frc;
4576 }
4577  
4578 # Fractional parts with the numerator and denominator as integers. E.g.,
4579 # "123.4375" is returned as "1975" and "16".
4580  
4581 sub fparts {
4582 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine fparts: not called CC=10 SCAR=47.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4583  
4584 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4585  
4586 # NaN => NaN/NaN
4587  
4588 0 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 0 0
4589 0 ▶ return $class -> bnan() unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4590 0 return $class -> bnan(), $class -> bnan();
4591 }
4592  
4593 # ±Inf => ±Inf/1
4594  
4595 0 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 0 0
4596 0 my $numer = $class -> binf($x->{sign});
4597 0 ▶ return $numer unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4598 0 my $denom = $class -> bone();
4599 0 return $numer, $denom;
4600 }
4601  
4602 # Finite number.
4603  
4604 # If we get here, we know that the output is an integer.
4605  
4606 0 ▶ $class = $downgrade if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 0 0
4607  
4608 0 my @flt_parts = ($x->{sign}, $x->{_m}, $x->{_es}, $x->{_e});
4609 0 my @rat_parts = $class -> _flt_lib_parts_to_rat_lib_parts(@flt_parts);
4610 0 my $num = $class -> new($LIB -> _str($rat_parts[1]));
4611 0 my $den = $class -> new($LIB -> _str($rat_parts[2]));
4612 0 ▶ $num = $num -> bneg() if $rat_parts[0] eq "-";
Branch: 1 branch
BranchTrueFalseTotal
if $rat_parts[0] eq "-" 0 0 0
4613 0 ▶ return $num unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
4614 0 return $num, $den;
4615 }
4616  
4617 # Given "123.4375", returns "1975", since "123.4375" is "1975/16".
4618  
4619 sub numerator {
4620 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine numerator: not called CC=9 SCAR=45.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4621  
4622 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4623  
4624 0 ▶ return $class -> bnan() if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4625 0 ▶ return $class -> binf($x -> sign()) if $x -> is_inf();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_inf 0 0 0
4626 0 ▶ return $class -> bzero() if $x -> is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 0 0 0
4627  
4628 # If we get here, we know that the output is an integer.
4629  
4630 0 ▶ $class = $downgrade if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 0 0
4631  
4632 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
4633 0 my $numer_lib = $LIB -> _copy($x -> {_m});
4634 0 my $denom_lib = $LIB -> _1ex($x -> {_e});
4635 0 my $gcd_lib = $LIB -> _gcd($LIB -> _copy($numer_lib), $denom_lib);
4636 0 $numer_lib = $LIB -> _div($numer_lib, $gcd_lib);
4637 0 return $class -> new($x -> {sign} . $LIB -> _str($numer_lib));
4638 }
4639  
4640 elsif (! $LIB -> _is_zero($x -> {_e})) { # exponent > 0
4641 0 my $numer_lib = $LIB -> _copy($x -> {_m});
4642 0 $numer_lib = $LIB -> _lsft($numer_lib, $x -> {_e}, 10);
4643 0 return $class -> new($x -> {sign} . $LIB -> _str($numer_lib));
4644 }
4645  
4646 else { # exponent = 0
4647 0 return $class -> new($x -> {sign} . $LIB -> _str($x -> {_m}));
4648 }
4649 }
4650  
4651 # Given "123.4375", returns "16", since "123.4375" is "1975/16".
4652  
4653 sub denominator {
4654 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine denominator: not called CC=6 SCAR=37.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4655  
4656 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4657  
4658 0 ▶ return $class -> bnan() if $x -> is_nan();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 0 0
4659  
4660 # If we get here, we know that the output is an integer.
4661  
4662 0 ▶ $class = $downgrade if defined $downgrade;
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::downgrade 0 0 0
4663  
4664 0 ▶ if ($x -> {_es} eq '-') { # exponent < 0
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'_es'} eq '-') { } 0 0 0
4665 0 my $numer_lib = $LIB -> _copy($x -> {_m});
4666 0 my $denom_lib = $LIB -> _1ex($x -> {_e});
4667 0 my $gcd_lib = $LIB -> _gcd($LIB -> _copy($numer_lib), $denom_lib);
4668 0 $denom_lib = $LIB -> _div($denom_lib, $gcd_lib);
4669 0 return $class -> new($LIB -> _str($denom_lib));
4670 }
4671  
4672 else { # exponent >= 0
4673 0 return $class -> bone();
4674 }
4675 }
4676  
4677 ###############################################################################
4678 # String conversion methods
4679 ###############################################################################
4680  
4681 sub bstr {
4682 # (ref to BFLOAT or num_str) return num_str
4683 # Convert number from internal format to (non-scientific) string format.
4684 # internal format is always normalized (no leading zeros, "-0" => "+0")
4685 8480 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bstr: called CC=21 SCAR=30.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 8468 12 8480
4686  
4687 8480 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 8480 8480
4688  
4689 # Inf and NaN
4690  
4691 8480 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 2477 6003 8480
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
499010132477
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
4692 2477 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 1928 549 2477
4693 549 return 'inf'; # +inf
4694 }
4695  
4696 # Finite number
4697  
4698 6003 my $es = '0';
4699 6003 my $len = 1;
4700 6003 my $cad = 0;
4701 6003 my $dot = '.';
4702  
4703 # $x is zero?
4704 6003 ▶ 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
101337121278
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
4705 6003 ▶ if ($not_zero) {
Branch: 1 branch
BranchTrueFalseTotal
if ($not_zero) 4725 1278 6003
4706 4725 $es = $LIB->_str($x->{_m});
4707 4725 $len = CORE::length($es);
4708 4725 my $e = $LIB->_num($x->{_e});
4709 4725 ▶ $e = -$e if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 1602 3123 4725
4710 4725 ▶ if ($e < 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($e < 0) { } 1586 3139 4725
elsif ($e > 0) { } 678 2461 3139
4711 1586 $dot = '';
4712 # if _e is bigger than a scalar, the following will blow your memory
4713 1586 ▶ if ($e <= -$len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e <= -$len) { } 576 1010 1586
4714 576 my $r = abs($e) - $len;
4715 576 $es = '0.'. ('0' x $r) . $es;
4716 576 $cad = -($len+$r);
4717 } else {
4718 1010 substr($es, $e, 0) = '.';
4719 1010 $cad = $LIB->_num($x->{_e});
4720 1010 ▶ $cad = -$cad if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 1010 0 1010
4721 }
4722 } elsif ($e > 0) {
4723 # expand with zeros
4724 678 $es .= '0' x $e;
4725 678 $len += $e;
4726 678 $cad = 0;
4727 }
4728 } # if not zero
4729  
4730 6003 ▶ $es = '-'.$es if $x->{sign} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"sign"} eq "-" 1013 4990 6003
4731 # if set accuracy or precision, pad with zeros on the right side
4732 6003 ▶ if ((defined $x->{_a}) && ($not_zero)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $x->{'_a'} and $not_zero) { } 694 5309 6003
elsif (($x->{'_p'} || 0) < 0) { } 502 4807 5309
Condition100%3/3
defined $x->{"_a"} and $not_zero
!ll&&!rl&&r
526148694
MC/DC100%2/2
defined $x->{"_a"} and $not_zero
defined $x->{"_a"} $not_zero
Truth table100%3/3
A and B
A= defined $x->{"_a"}B= $not_zero
ABresult
0X0
100
111
4733 # 123400 => 6, 0.1234 => 4, 0.001234 => 4
4734 694 my $zeros = $x->{_a} - $cad; # cad == 0 => 12340
4735 694 ▶ $zeros = $x->{_a} - $len if $cad != $len;
Branch: 1 branch
BranchTrueFalseTotal
if $cad != $len 694 0 694
4736 694 ▶ $es .= $dot.'0' x $zeros if $zeros > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $zeros > 0 35 659 694
4737 ▶ } elsif ((($x->{_p} || 0) < 0)) {
Condition100%2/2
$x->{'_p'} || 0
l!l
5394770
MC/DC100%1/1
$x->{'_p'} || 0
$x->{'_p'}
Truth table100%2/2
A
A= $x->{'_p'}
Aresult
11
00
4738 # 123400 => 6, 0.1234 => 4, 0.001234 => 6
4739 502 my $zeros = -$x->{_p} + $cad;
4740 502 ▶ $es .= $dot.'0' x $zeros if $zeros > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $zeros > 0 112 390 502
4741 }
4742 6003 $es;
4743 }
4744  
4745 # Decimal notation, e.g., "12345.6789" (no exponent).
4746  
4747 sub bdstr {
4748 48 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bdstr: called CC=13 SCAR=30.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 48 0 48
4749  
4750 48 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 48 48
4751  
4752 # Inf and NaN
4753  
4754 48 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 9 39 48
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
3729
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
4755 9 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 2 7 9
4756 7 return 'inf'; # +inf
4757 }
4758  
4759 # Upgrade?
4760  
4761 39 ▶ return $upgrade -> bdstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__) 0 39 39
Condition33%1/3
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
3900
MC/DC0%0/2
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigFloat::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigFloat::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4762 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4763  
4764 # Finite number
4765  
4766 39 my $mant = $LIB->_str($x->{_m});
4767 39 my $esgn = $x->{_es};
4768 39 my $eabs = $LIB -> _num($x->{_e});
4769  
4770 39 my $uintmax = ~0;
4771  
4772 39 my $str = $mant;
4773 39 ▶ if ($esgn eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($esgn eq '+') { } 39 0 39
4774  
4775 39 ▶ croak("The absolute value of the exponent is too large")
Branch: 1 branch
BranchTrueFalseTotal
if $eabs > $uintmax 0 39 39
4776 if $eabs > $uintmax;
4777  
4778 39 $str .= "0" x $eabs;
4779  
4780 } else {
4781 0 my $mlen = CORE::length($mant);
4782 0 my $c = $mlen - $eabs;
4783  
4784 0 my $intmax = ($uintmax - 1) / 2;
4785 0 ▶ croak("The absolute value of the exponent is too large")
Branch: 1 branch
BranchTrueFalseTotal
if 1 - $c > $intmax 0 0 0
4786 if (1 - $c) > $intmax;
4787  
4788 0 ▶ $str = "0" x (1 - $c) . $str if $c <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $c <= 0 0 0 0
4789 0 substr($str, -$eabs, 0) = '.';
4790 }
4791  
4792 39 ▶ return $x->{sign} eq '-' ? '-' . $str : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 2 37 39
4793 }
4794  
4795 # Scientific notation with significand/mantissa and exponent as integers, e.g.,
4796 # "12345.6789" is written as "123456789e-4".
4797  
4798 sub bsstr {
4799 175 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bsstr: called CC=9 SCAR=22.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 159 16 175
4800  
4801 175 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 175 175
4802  
4803 # Inf and NaN
4804  
4805 175 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 28 147 175
Condition100%3/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
1272028
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
4806 28 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 16 12 28
4807 12 return 'inf'; # +inf
4808 }
4809  
4810 # Upgrade?
4811  
4812 147 ▶ return $upgrade -> bsstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__) 0 147 147
Condition33%1/3
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
14700
MC/DC0%0/2
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigFloat::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigFloat::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4813 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4814  
4815 # Finite number
4816  
4817 ($x->{sign} eq '-' ? '-' : '') . $LIB->_str($x->{_m})
4818 147 ▶ . 'e' . $x->{_es} . $LIB->_str($x->{_e});
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 20 127 147
4819 }
4820  
4821 # Normalized notation, e.g., "12345.6789" is written as "1.23456789e+4".
4822  
4823 sub bnstr {
4824 483 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bnstr: called CC=10 SCAR=25.7
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 483 0 483
4825  
4826 483 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 483 483
4827  
4828 # Inf and NaN
4829  
4830 483 ▶ if ($x->{sign} ne '+' && $x->{sign} ne '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{"sign"} ne "+" and $x->{"sign"} ne "-") 0 483 483
Condition66%2/3
$x->{"sign"} ne "+" and $x->{"sign"} ne "-"
!ll&&!rl&&r
47580
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
4831 0 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 0 0 0
4832 0 return 'inf'; # +inf
4833 }
4834  
4835 # Upgrade?
4836  
4837 483 ▶ return $upgrade -> bnstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__) 0 483 483
Condition33%1/3
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
48300
MC/DC0%0/2
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigFloat::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigFloat::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4838 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4839  
4840 # Finite number
4841  
4842 483 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 8 475 483
4843  
4844 # Get the mantissa and the length of the mantissa.
4845  
4846 483 my $mant = $LIB->_str($x->{_m});
4847 483 my $mantlen = CORE::length($mant);
4848  
4849 483 ▶ if ($mantlen == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mantlen == 1) { } 65 418 483
4850  
4851 # Not decimal point when the mantissa has length one, i.e., return the
4852 # number 2 as the string "2", not "2.".
4853  
4854 65 $str .= $mant . 'e' . $x->{_es} . $LIB->_str($x->{_e});
4855  
4856 } else {
4857  
4858 # Compute new exponent where the original exponent is adjusted by the
4859 # length of the mantissa minus one (because the decimal point is after
4860 # one digit).
4861  
4862 my ($eabs, $esgn) = $LIB -> _sadd($LIB -> _copy($x->{_e}), $x->{_es},
4863 418 $LIB -> _new($mantlen - 1), "+");
4864 418 substr $mant, 1, 0, ".";
4865 418 $str .= $mant . 'e' . $esgn . $LIB->_str($eabs);
4866  
4867 }
4868  
4869 483 return $str;
4870 }
4871  
4872 # Engineering notation, e.g., "12345.6789" is written as "12.3456789e+3".
4873  
4874 sub bestr {
4875 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine bestr: not called CC=13 SCAR=52.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4876  
4877 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4878  
4879 # Inf and NaN
4880  
4881 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
4882 0 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 0 0 0
4883 0 return 'inf'; # +inf
4884 }
4885  
4886 # Upgrade?
4887  
4888 0 ▶ return $upgrade -> bestr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigFloat::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigFloat::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4889 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4890  
4891 # Finite number
4892  
4893 0 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
4894  
4895 # Get the mantissa, the length of the mantissa, and adjust the exponent by
4896 # the length of the mantissa minus 1 (because the dot is after one digit).
4897  
4898 0 my $mant = $LIB->_str($x->{_m});
4899 0 my $mantlen = CORE::length($mant);
4900 my ($eabs, $esgn) = $LIB -> _sadd($LIB -> _copy($x->{_e}), $x->{_es},
4901 0 $LIB -> _new($mantlen - 1), "+");
4902  
4903 0 my $dotpos = 1;
4904 0 my $mod = $LIB -> _mod($LIB -> _copy($eabs), $LIB -> _new("3"));
4905 0 ▶ unless ($LIB -> _is_zero($mod)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($mod)) 0 0 0
4906 0 ▶ if ($esgn eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($esgn eq '+') { } 0 0 0
4907 0 $eabs = $LIB -> _sub($eabs, $mod);
4908 0 $dotpos += $LIB -> _num($mod);
4909 } else {
4910 0 my $delta = $LIB -> _sub($LIB -> _new("3"), $mod);
4911 0 $eabs = $LIB -> _add($eabs, $delta);
4912 0 $dotpos += $LIB -> _num($delta);
4913 }
4914 }
4915  
4916 0 ▶ if ($dotpos < $mantlen) {
Branch: 2 branches
BranchTrueFalseTotal
if ($dotpos < $mantlen) { } 0 0 0
elsif ($dotpos > $mantlen) { } 0 0 0
4917 0 substr $mant, $dotpos, 0, ".";
4918 } elsif ($dotpos > $mantlen) {
4919 0 $mant .= "0" x ($dotpos - $mantlen);
4920 }
4921  
4922 0 $str .= $mant . 'e' . $esgn . $LIB->_str($eabs);
4923  
4924 0 return $str;
4925 }
4926  
4927 # Fractional notation, e.g., "123.4375" is written as "1975/16".
4928  
4929 sub bfstr {
4930 0 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine bfstr: not called CC=11 SCAR=48.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
4931  
4932 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
4933  
4934 # Inf and NaN
4935  
4936 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
4937 0 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 0 0 0
4938 0 return 'inf'; # +inf
4939 }
4940  
4941 # Upgrade?
4942  
4943 0 ▶ return $upgrade -> bfstr($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__) 0 0 0
Condition0%0/3
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigFloat::upgrade not $x->isa(__PACKAGE__)
Truth table0%0/3
A and not(B)
A= defined $Math::BigFloat::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4944 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4945  
4946 # Finite number
4947  
4948 0 ▶ my $str = $x->{sign} eq '-' ? '-' : '';
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 0 0 0
4949  
4950 0 ▶ if ($x->{_es} eq '+') {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->{'_es'} eq '+') { } 0 0 0
4951 0 $str .= $LIB -> _str($x->{_m}) . ("0" x $LIB -> _num($x->{_e}));
4952 } else {
4953 0 my @flt_parts = ($x->{sign}, $x->{_m}, $x->{_es}, $x->{_e});
4954 0 my @rat_parts = $class -> _flt_lib_parts_to_rat_lib_parts(@flt_parts);
4955 0 $str = $LIB -> _str($rat_parts[1]) . "/" . $LIB -> _str($rat_parts[2]);
4956 0 ▶ $str = "-" . $str if $rat_parts[0] eq "-";
Branch: 1 branch
BranchTrueFalseTotal
if $rat_parts[0] eq "-" 0 0 0
4957 }
4958  
4959 0 return $str;
4960 }
4961  
4962 sub to_hex {
4963 # return number as hexadecimal string (only for integers defined)
4964 36 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine to_hex: called CC=12 SCAR=26.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
4965  
4966 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
4967  
4968 # Inf and NaN
4969  
4970 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
4971 12 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 8 4 12
4972 4 return 'inf'; # +inf
4973 }
4974  
4975 # Upgrade?
4976  
4977 24 ▶ return $upgrade -> to_hex($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__) 0 24 24
Condition33%1/3
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2400
MC/DC0%0/2
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigFloat::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigFloat::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
4978 if defined($upgrade) && !$x -> isa(__PACKAGE__);
4979  
4980 # Finite number
4981  
4982 24 ▶ return '0' if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 16 24
4983  
4984 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
4985  
4986 16 my $z = $LIB->_copy($x->{_m});
4987 16 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 16 16
4988 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
4989 }
4990 16 my $str = $LIB->_to_hex($z);
4991 16 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 12 16
4992 }
4993  
4994 sub to_oct {
4995 # return number as octal digit string (only for integers defined)
4996 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine to_oct: called CC=12 SCAR=26.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
4997  
4998 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
4999  
5000 # Inf and NaN
5001  
5002 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
5003 12 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 8 4 12
5004 4 return 'inf'; # +inf
5005 }
5006  
5007 # Upgrade?
5008  
5009 28 ▶ return $upgrade -> to_hex($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__) 0 28 28
Condition33%1/3
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2800
MC/DC0%0/2
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigFloat::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigFloat::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
5010 if defined($upgrade) && !$x -> isa(__PACKAGE__);
5011  
5012 # Finite number
5013  
5014 28 ▶ return '0' if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 20 28
5015  
5016 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
5017  
5018 20 my $z = $LIB->_copy($x->{_m});
5019 20 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 20 20
5020 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
5021 }
5022 20 my $str = $LIB->_to_oct($z);
5023 20 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 16 20
5024 }
5025  
5026 sub to_bin {
5027 # return number as binary digit string (only for integers defined)
5028 40 ▶ my ($class, $x, @r) = ref($_[0]) ? (ref($_[0]), $_[0]) : objectify(1, @_);
Subroutine to_bin: called CC=12 SCAR=26.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
5029  
5030 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
5031  
5032 # Inf and NaN
5033  
5034 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
5035 12 ▶ return $x->{sign} unless $x->{sign} eq '+inf'; # -inf, NaN
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} eq "+inf" 8 4 12
5036 4 return 'inf'; # +inf
5037 }
5038  
5039 # Upgrade?
5040  
5041 28 ▶ return $upgrade -> to_hex($x, @r)
Branch: 1 branch
BranchTrueFalseTotal
if defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__) 0 28 28
Condition33%1/3
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
!ll&&!rl&&r
2800
MC/DC0%0/2
defined $Math::BigFloat::upgrade and not $x->isa(__PACKAGE__)
defined $Math::BigFloat::upgrade not $x->isa(__PACKAGE__)
Truth table33%1/3
A and not(B)
A= defined $Math::BigFloat::upgradeB= not $x->isa(__PACKAGE__)
ABresult
0X0
100
111
5042 if defined($upgrade) && !$x -> isa(__PACKAGE__);
5043  
5044 # Finite number
5045  
5046 28 ▶ return '0' if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 20 28
5047  
5048 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
5049  
5050 20 my $z = $LIB->_copy($x->{_m});
5051 20 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 20 20
5052 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
5053 }
5054 20 my $str = $LIB->_to_bin($z);
5055 20 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 16 20
5056 }
5057  
5058 sub to_ieee754 {
5059 0 ▶ my ($class, $x, $format, @r) = ref($_[0]) ? (ref($_[0]), @_) : objectify(1, @_);
Subroutine to_ieee754: not called CC=35 SCAR=71.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
5060  
5061 0 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 0 0
5062  
5063 0 my $enc; # significand encoding (applies only to decimal)
5064 my $k; # storage width in bits
5065 0 my $b; # base
5066  
5067 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
5068 0 $k = $1;
5069 0 $b = 2;
5070 } elsif ($format =~ /^decimal(\d+)(dpd|bcd)?\z/) {
5071 0 $k = $1;
5072 0 $b = 10;
5073 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
5074 } elsif ($format eq 'half') {
5075 0 $k = 16;
5076 0 $b = 2;
5077 } elsif ($format eq 'single') {
5078 0 $k = 32;
5079 0 $b = 2;
5080 } elsif ($format eq 'double') {
5081 0 $k = 64;
5082 0 $b = 2;
5083 } elsif ($format eq 'quadruple') {
5084 0 $k = 128;
5085 0 $b = 2;
5086 } elsif ($format eq 'octuple') {
5087 0 $k = 256;
5088 0 $b = 2;
5089 } elsif ($format eq 'sexdecuple') {
5090 0 $k = 512;
5091 0 $b = 2;
5092 }
5093  
5094 0 ▶ if ($b == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($b == 2) 0 0 0
5095  
5096 # Get the parameters for this format.
5097  
5098 0 my $p; # precision (in bits)
5099 my $t; # number of bits in significand
5100 0 my $w; # number of bits in exponent
5101  
5102 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
5103 0 $p = 11;
5104 0 $t = 10;
5105 0 $w = 5;
5106 } elsif ($k == 32) { # binary32 (single-precision)
5107 0 $p = 24;
5108 0 $t = 23;
5109 0 $w = 8;
5110 } elsif ($k == 64) { # binary64 (double-precision)
5111 0 $p = 53;
5112 0 $t = 52;
5113 0 $w = 11;
5114 } else { # binaryN (quadruple-precition and above)
5115 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
5116 0 croak "Number of bits must be 16, 32, 64, or >= 128 and",
5117 " a multiple of 32";
5118 }
5119 0 $p = $k - sprintf('%.0f', 4 * log($k) / log(2)) + 13;
5120 0 $t = $p - 1;
5121 0 $w = $k - $t - 1;
5122 }
5123  
5124 # The maximum exponent, minimum exponent, and exponent bias.
5125  
5126 0 my $emax = $class -> new(2) -> bpow($w - 1) -> bdec();
5127 0 my $emin = 1 - $emax;
5128 0 my $bias = $emax;
5129  
5130 # Get numerical sign, exponent, and mantissa/significand for bit
5131 # string.
5132  
5133 0 my $sign = 0;
5134 0 my $expo;
5135 my $mant;
5136  
5137 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
5138 0 $sign = 1;
5139 0 $expo = $emax -> copy() -> binc();
5140 0 $mant = $class -> new(2) -> bpow($t - 1);
5141 } elsif ($x -> is_inf()) { # inf
5142 0 ▶ $sign = 1 if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
5143 0 $expo = $emax -> copy() -> binc();
5144 0 $mant = $class -> bzero();
5145 } elsif ($x -> is_zero()) { # zero
5146 0 $expo = $emin -> copy() -> bdec();
5147 0 $mant = $class -> bzero();
5148 } else { # normal and subnormal
5149  
5150 0 ▶ $sign = 1 if $x -> is_neg();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_neg 0 0 0
5151  
5152 # Now we need to compute the mantissa and exponent in base $b.
5153  
5154 0 my $binv = $class -> new("0.5");
5155 0 my $b = $class -> new(2);
5156 0 my $one = $class -> bone();
5157  
5158 # We start off by initializing the exponent to zero and the
5159 # mantissa to the input value. Then we increase the mantissa and
5160 # decrease the exponent, or vice versa, until the mantissa is in
5161 # the desired range or we hit one of the limits for the exponent.
5162  
5163 0 $mant = $x -> copy() -> babs();
5164  
5165 # We need to find the base 2 exponent. First make an estimate of
5166 # the base 2 exponent, before adjusting it below. We could skip
5167 # this estimation and go straight to the while-loops below, but the
5168 # loops are slow, especially when the final exponent is far from
5169 # zero and even more so if the number of digits is large. This
5170 # initial estimation speeds up the computation dramatically.
5171 #
5172 # log2($m * 10**$e) = log10($m + 10**$e) * log(10)/log(2)
5173 # = (log10($m) + $e) * log(10)/log(2)
5174 # = (log($m)/log(10) + $e) * log(10)/log(2)
5175  
5176 0 my ($m, $e) = $x -> nparts();
5177 0 my $ms = $m -> numify();
5178 0 my $es = $e -> numify();
5179  
5180 0 my $expo_est = (log(abs($ms))/log(10) + $es) * log(10)/log(2);
5181 0 $expo_est = int($expo_est);
5182  
5183 # Limit the exponent.
5184  
5185 0 ▶ if ($expo_est > $emax) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo_est > $emax) { } 0 0 0
elsif ($expo_est < $emin) { } 0 0 0
5186 0 $expo_est = $emax;
5187 } elsif ($expo_est < $emin) {
5188 0 $expo_est = $emin;
5189 }
5190  
5191 # Don't multiply by a number raised to a negative exponent. This
5192 # will cause a division, whose result is truncated to some fixed
5193 # number of digits. Instead, multiply by the inverse number raised
5194 # to a positive exponent.
5195  
5196 0 $expo = $class -> new($expo_est);
5197 0 ▶ if ($expo_est > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo_est > 0) { } 0 0 0
elsif ($expo_est < 0) { } 0 0 0
5198 0 $mant = $mant -> bmul($binv -> copy() -> bpow($expo));
5199 } elsif ($expo_est < 0) {
5200 0 my $expo_abs = $expo -> copy() -> bneg();
5201 0 $mant = $mant -> bmul($b -> copy() -> bpow($expo_abs));
5202 }
5203  
5204 # Final adjustment of the estimate above.
5205  
5206 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
5207 0 $mant = $mant -> bmul($binv);
5208 0 $expo = $expo -> binc();
5209 }
5210  
5211 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
5212 0 $mant = $mant -> bmul($b);
5213 0 $expo = $expo -> bdec();
5214 }
5215  
5216 # This is when the magnitude is larger than what can be represented
5217 # in this format. Encode as infinity.
5218  
5219 0 ▶ if ($expo > $emax) {
Branch: 2 branches
BranchTrueFalseTotal
if ($expo > $emax) { } 0 0 0
elsif ($expo < $emin) { } 0 0 0
5220 0 $mant = $class -> bzero();
5221 0 $expo = $emax -> copy() -> binc();
5222 }
5223  
5224 # This is when the magnitude is so small that the number is encoded
5225 # as a subnormal number.
5226 #
5227 # If the magnitude is smaller than that of the smallest subnormal
5228 # number, and rounded downwards, it is encoded as zero. This works
5229 # transparently and does not need to be treated as a special case.
5230 #
5231 # If the number is between the largest subnormal number and the
5232 # smallest normal number, and the value is rounded upwards, the
5233 # value must be encoded as a normal number. This must be treated as
5234 # a special case.
5235  
5236 elsif ($expo < $emin) {
5237  
5238 # Scale up the mantissa (significand), and round to integer.
5239  
5240 0 my $const = $class -> new($b) -> bpow($t - 1);
5241 0 $mant = $mant -> bmul($const);
5242 0 $mant = $mant -> bfround(0);
5243  
5244 # If the mantissa overflowed, encode as the smallest normal
5245 # number.
5246  
5247 0 ▶ if ($mant == $const -> bmul($b)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == $const->bmul($b)) 0 0 0
5248 0 $mant = $mant -> bzero();
5249 0 $expo = $expo -> binc();
5250 }
5251 }
5252  
5253 # This is when the magnitude is within the range of what can be
5254 # encoded as a normal number.
5255  
5256 else {
5257  
5258 # Remove implicit leading bit, scale up the mantissa
5259 # (significand) to an integer, and round.
5260  
5261 0 $mant = $mant -> bdec();
5262 0 my $const = $class -> new($b) -> bpow($t);
5263 0 $mant = $mant -> bmul($const) -> bfround(0);
5264  
5265 # If the mantissa overflowed, encode as the next larger value.
5266 # This works correctly also when the next larger value is
5267 # infinity.
5268  
5269 0 ▶ if ($mant == $const) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant == $const) 0 0 0
5270 0 $mant = $mant -> bzero();
5271 0 $expo = $expo -> binc();
5272 }
5273 }
5274 }
5275  
5276 0 $expo = $expo -> badd($bias); # add bias
5277  
5278 0 my $signbit = "$sign";
5279  
5280 0 my $mantbits = $mant -> to_bin();
5281 0 $mantbits = ("0" x ($t - CORE::length($mantbits))) . $mantbits;
5282  
5283 0 my $expobits = $expo -> to_bin();
5284 0 $expobits = ("0" x ($w - CORE::length($expobits))) . $expobits;
5285  
5286 0 my $bin = $signbit . $expobits . $mantbits;
5287 0 return pack "B*", $bin;
5288 }
5289  
5290 0 croak("The format '$format' is not yet supported.");
5291 }
5292  
5293 sub as_hex {
5294 # return number as hexadecimal string (only for integers defined)
5295  
5296 36 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine as_hex: called CC=8 SCAR=21.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 36 0 36
5297  
5298 36 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 36 36
5299  
5300 36 ▶ return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 24 36
5301 24 ▶ return '0x0' if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 16 24
5302  
5303 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
5304  
5305 16 my $z = $LIB->_copy($x->{_m});
5306 16 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 16 16
5307 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
5308 }
5309 16 my $str = $LIB->_as_hex($z);
5310 16 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 12 16
5311 }
5312  
5313 sub as_oct {
5314 # return number as octal digit string (only for integers defined)
5315  
5316 40 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine as_oct: called CC=8 SCAR=21.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
5317  
5318 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
5319  
5320 40 ▶ return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 28 40
5321 28 ▶ return '00' if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 20 28
5322  
5323 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
5324  
5325 20 my $z = $LIB->_copy($x->{_m});
5326 20 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 20 20
5327 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
5328 }
5329 20 my $str = $LIB->_as_oct($z);
5330 20 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 16 20
5331 }
5332  
5333 sub as_bin {
5334 # return number as binary digit string (only for integers defined)
5335  
5336 40 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine as_bin: called CC=8 SCAR=21.5
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 40 0 40
5337  
5338 40 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 40 40
5339  
5340 40 ▶ return $x->bstr() if $x->{sign} !~ /^[+-]$/; # inf, nan etc
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[+-]$/ 12 28 40
5341 28 ▶ return '0b0' if $x->is_zero();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_zero 8 20 28
5342  
5343 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
5344  
5345 20 my $z = $LIB->_copy($x->{_m});
5346 20 ▶ if (! $LIB->_is_zero($x->{_e})) { # > 0
Branch: 1 branch
BranchTrueFalseTotal
unless ($LIB->_is_zero($x->{"_e"})) 0 20 20
5347 0 $z = $LIB->_lsft($z, $x->{_e}, 10);
5348 }
5349 20 my $str = $LIB->_as_bin($z);
5350 20 ▶ return $x->{sign} eq '-' ? "-$str" : $str;
Branch: 1 branch
BranchTrueFalseTotal
$x->{'sign'} eq '-' ? : 4 16 20
5351 }
5352  
5353 sub numify {
5354 # Make a Perl scalar number from a Math::BigFloat object.
5355  
5356 483 ▶ my (undef, $x, @r) = ref($_[0]) ? (undef, @_) : objectify(1, @_);
Subroutine numify: called CC=6 SCAR=25.4
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 483 0 483
5357  
5358 483 ▶ carp "Rounding is not supported for ", (caller(0))[3], "()" if @r;
Branch: 1 branch
BranchTrueFalseTotal
if @r 0 483 483
5359  
5360 483 ▶ if ($x -> is_nan()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_nan) 0 483 483
5361 0 require Math::Complex;
5362 0 my $inf = $Math::Complex::Inf;
5363 0 return $inf - $inf;
5364 }
5365  
5366 483 ▶ if ($x -> is_inf()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($x->is_inf) 0 483 483
5367 0 require Math::Complex;
5368 0 my $inf = $Math::Complex::Inf;
5369 0 ▶ return $x -> is_negative() ? -$inf : $inf;
Branch: 1 branch
BranchTrueFalseTotal
$x->is_negative ? : 0 0 0
5370 }
5371  
5372 # Create a string and let Perl's atoi()/atof() handle the rest.
5373  
5374 483 return 0 + $x -> bnstr();
5375 }
5376  
5377 ###############################################################################
5378 # Private methods and functions.
5379 ###############################################################################
5380  
5381 sub import {
5382 55 ▶ my $class = shift;
Subroutine import: called CC=11 SCAR=27.7
5383 55 $IMPORT++; # remember we did import()
5384  
5385 55 my @import = ('objectify');
5386 55 my @a; # unrecognized arguments
5387  
5388 55 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 17 55 72
5389 17 my $param = shift;
5390  
5391 # Enable overloading of constants.
5392  
5393 17 ▶ if ($param eq ':constant') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq ":constant") 0 17 17
5394 overload::constant
5395  
5396 integer => sub {
5397 0 ▶ $class -> new(shift);
Subroutine __ANON__: not called CC=1 SCAR=6.9
5398 },
5399  
5400 float => sub {
5401 0 ▶ $class -> new(shift);
Subroutine __ANON__: not called CC=1 SCAR=6.9
5402 },
5403  
5404 binary => sub {
5405 # E.g., a literal 0377 shall result in an object whose value
5406 # is decimal 255, but new("0377") returns decimal 377.
5407 0 ▶ return $class -> from_oct($_[0]) if $_[0] =~ /^0_*[0-7]/;
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^0_*[0-7]/ 0 0 0
5408 0 $class -> new(shift);
5409 0 };
5410 0 next;
5411 }
5412  
5413 # Upgrading.
5414  
5415 17 ▶ if ($param eq 'upgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "upgrade") 2 15 17
5416 2 $class -> upgrade(shift);
5417 2 next;
5418 }
5419  
5420 # Downgrading.
5421  
5422 15 ▶ if ($param eq 'downgrade') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "downgrade") 1 14 15
5423 1 $class -> downgrade(shift);
5424 1 next;
5425 }
5426  
5427 # Accuracy.
5428  
5429 14 ▶ if ($param eq 'accuracy') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "accuracy") 0 14 14
5430 0 $class -> accuracy(shift);
5431 0 next;
5432 }
5433  
5434 # Precision.
5435  
5436 14 ▶ if ($param eq 'precision') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "precision") 0 14 14
5437 0 $class -> precision(shift);
5438 0 next;
5439 }
5440  
5441 # Rounding mode.
5442  
5443 14 ▶ if ($param eq 'round_mode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "round_mode") 0 14 14
5444 0 $class -> round_mode(shift);
5445 0 next;
5446 }
5447  
5448 # Backend library.
5449  
5450 14 ▶ if ($param =~ /^(lib|try|only)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(lib|try|only)\z/) 11 3 14
5451 11 push @import, $param;
5452 11 ▶ push @import, shift() if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 11 0 11
5453 11 next;
5454 }
5455  
5456 3 ▶ if ($param eq 'with') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "with") 3 0 3
5457 # alternative class for our private parts()
5458 # XXX: no longer supported
5459 # $LIB = shift() || 'Calc';
5460 # carp "'with' is no longer supported, use 'lib', 'try', or 'only'";
5461 3 shift;
5462 3 next;
5463 }
5464  
5465 # Unrecognized parameter.
5466  
5467 0 push @a, $param;
5468 }
5469  
5470 55 Math::BigInt -> import(@import);
5471  
5472 # find out which one was actually loaded
5473 55 $LIB = Math::BigInt -> config('lib');
5474  
5475 55 $class->export_to_level(1, $class, @a); # export wanted functions
5476 }
5477  
5478 sub _len_to_steps {
5479 # Given D (digits in decimal), compute N so that N! (N factorial) is
5480 # at least D digits long. D should be at least 50.
5481 3 ▶ my $d = shift;
Subroutine _len_to_steps: called CC=7 SCAR=19.7
5482  
5483 # two constants for the Ramanujan estimate of ln(N!)
5484 3 my $lg2 = log(2 * 3.14159265) / 2;
5485 3 my $lg10 = log(10);
5486  
5487 # D = 50 => N => 42, so L = 40 and R = 50
5488 3 my $l = 40;
5489 3 my $r = $d;
5490  
5491 # Otherwise this does not work under -Mbignum and we do not yet have "no
5492 # bignum;" :(
5493 3 ▶ $l = $l->numify if ref($l);
Branch: 1 branch
BranchTrueFalseTotal
if ref $l 0 3 3
5494 3 ▶ $r = $r->numify if ref($r);
Branch: 1 branch
BranchTrueFalseTotal
if ref $r 0 3 3
5495 3 ▶ $lg2 = $lg2->numify if ref($lg2);
Branch: 1 branch
BranchTrueFalseTotal
if ref $lg2 0 3 3
5496 3 ▶ $lg10 = $lg10->numify if ref($lg10);
Branch: 1 branch
BranchTrueFalseTotal
if ref $lg10 0 3 3
5497  
5498 # binary search for the right value (could this be written as the reverse of
5499 # lg(n!)?)
5500 3 ▶ while ($r - $l > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r - $l > 1) 15 3 18
5501 15 my $n = int(($r - $l) / 2) + $l;
5502 15 my $ramanujan
5503 = int(($n * log($n) - $n + log($n * (1 + 4*$n*(1+2*$n))) / 6 + $lg2)
5504 / $lg10);
5505 15 ▶ $ramanujan > $d ? $r = $n : $l = $n;
Branch: 1 branch
BranchTrueFalseTotal
$ramanujan > $d ? : 12 3 15
5506 }
5507 3 $l;
5508 }
5509  
5510 sub _log {
5511 # internal log function to calculate ln() based on Taylor series.
5512 # Modifies $x in place.
5513 131 ▶ my ($x, $scale) = @_;
Subroutine _log: called CC=3 SCAR=11.0
5514 131 my $class = ref $x;
5515  
5516 # in case of $x == 1, result is 0
5517 131 ▶ return $x -> bzero() if $x -> is_one();
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_one 23 108 131
5518  
5519 # XXX TODO: rewrite this in a similar manner to bexp()
5520  
5521 # http://www.efunda.com/math/taylor_series/logarithmic.cfm?search_string=log
5522  
5523 # u = x-1, v = x+1
5524 # _ _
5525 # Taylor: | u 1 u^3 1 u^5 |
5526 # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 0
5527 # |_ v 3 v^3 5 v^5 _|
5528  
5529 # This takes much more steps to calculate the result and is thus not used
5530 # u = x-1
5531 # _ _
5532 # Taylor: | u 1 u^2 1 u^3 |
5533 # ln (x) = 2 | --- + - * --- + - * --- + ... | x > 1/2
5534 # |_ x 2 x^2 3 x^3 _|
5535  
5536 # scale used in intermediate computations
5537 108 my $scaleup = $scale + 4;
5538  
5539 108 my ($v, $u, $numer, $denom, $factor, $f);
5540  
5541 108 $v = $x -> copy();
5542 108 $v = $v -> binc(); # v = x+1
5543 108 $x = $x -> bdec();
5544 108 $u = $x -> copy(); # u = x-1; x = x-1
5545  
5546 108 $x = $x -> bdiv($v, $scaleup); # first term: u/v
5547  
5548 108 $numer = $u -> copy(); # numerator
5549 108 $denom = $v -> copy(); # denominator
5550  
5551 108 $u = $u -> bmul($u); # u^2
5552 108 $v = $v -> bmul($v); # v^2
5553  
5554 108 $numer = $numer -> bmul($u); # u^3
5555 108 $denom = $denom -> bmul($v); # v^3
5556  
5557 108 $factor = $class -> new(3);
5558 108 $f = $class -> new(2);
5559  
5560 108 while (1) {
5561 3114 my $next = $numer -> copy() -> bround($scaleup)
5562 -> bdiv($denom -> copy() -> bmul($factor) -> bround($scaleup), $scaleup);
5563  
5564 3114 $next->{_a} = undef;
5565 3114 $next->{_p} = undef;
5566 3114 my $x_prev = $x -> copy();
5567 3114 $x = $x -> badd($next);
5568  
5569 3114 ▶ last if $x -> bacmp($x_prev) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $x->bacmp($x_prev) == 0 108 3006 3114
5570  
5571 # calculate things for the next term
5572 3006 $numer = $numer -> bmul($u);
5573 3006 $denom = $denom -> bmul($v);
5574 3006 $factor = $factor -> badd($f);
5575 }
5576  
5577 108 $x = $x -> bmul($f); # $x *= 2
5578 108 $x = $x -> bround($scale);
5579 }
5580  
5581 sub _log_10 {
5582 # Internal log function based on reducing input to the range of 0.1 .. 9.99
5583 # and then "correcting" the result to the proper one. Modifies $x in place.
5584 168 ▶ my ($x, $scale) = @_;
Subroutine _log_10: called CC=31 SCAR=34.4
5585 168 my $class = ref $x;
5586  
5587 # Taking blog() from numbers greater than 10 takes a *very long* time, so we
5588 # break the computation down into parts based on the observation that:
5589 # blog(X*Y) = blog(X) + blog(Y)
5590 # We set Y here to multiples of 10 so that $x becomes below 1 - the smaller
5591 # $x is the faster it gets. Since 2*$x takes about 10 times as
5592 # long, we make it faster by about a factor of 100 by dividing $x by 10.
5593  
5594 # The same observation is valid for numbers smaller than 0.1, e.g. computing
5595 # log(1) is fastest, and the further away we get from 1, the longer it
5596 # takes. So we also 'break' this down by multiplying $x with 10 and subtract
5597 # the log(10) afterwards to get the correct result.
5598  
5599 # To get $x even closer to 1, we also divide by 2 and then use log(2) to
5600 # correct for this. For instance if $x is 2.4, we use the formula:
5601 # blog(2.4 * 2) == blog(1.2) + blog(2)
5602 # and thus calculate only blog(1.2) and blog(2), which is faster in total
5603 # than calculating blog(2.4).
5604  
5605 # In addition, the values for blog(2) and blog(10) are cached.
5606  
5607 # Calculate the number of digits before the dot, i.e., 1 + floor(log10(x)):
5608 # x = 123 => dbd = 3
5609 # x = 1.23 => dbd = 1
5610 # x = 0.0123 => dbd = -1
5611 # x = 0.000123 => dbd = -3
5612 # etc.
5613  
5614 168 my $dbd = $LIB->_num($x->{_e});
5615 168 ▶ $dbd = -$dbd if $x->{_es} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $x->{"_es"} eq "-" 66 102 168
5616 168 $dbd += $LIB->_len($x->{_m});
5617  
5618 # more than one digit (e.g. at least 10), but *not* exactly 10 to avoid
5619 # infinite recursion
5620  
5621 168 my $calc = 1; # do some calculation?
5622  
5623 # No upgrading or downgrading in the intermediate computations.
5624  
5625 168 local $Math::BigInt::upgrade = undef;
5626 168 local $Math::BigFloat::downgrade = undef;
5627  
5628 # disable the shortcut for 10, since we need log(10) and this would recurse
5629 # infinitely deep
5630 168 ▶ 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 161 168
Condition83%5/6
$x->{"_es"} eq "+" and ($LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"}))
!ll&&!rl&&r
66957
$LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"})
!ll&&!rl&&r
9507
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
5631 ($LIB->_is_one($x->{_e}) &&
5632 $LIB->_is_one($x->{_m})))
5633 {
5634 7 $dbd = 0; # disable shortcut
5635 # we can use the cached value in these cases
5636 7 ▶ if ($scale <= $LOG_10_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_10_A) 7 0 7
5637 7 $x = $x->bzero();
5638 7 $x = $x->badd($LOG_10); # modify $x in place
5639 7 $calc = 0; # no need to calc, but round
5640 }
5641 # if we can't use the shortcut, we continue normally
5642 } else {
5643 # disable the shortcut for 2, since we maybe have it cached
5644 161 ▶ if (($LIB->_is_zero($x->{_e}) && # $x == 2
Branch: 1 branch
BranchTrueFalseTotal
if ($LIB->_is_zero($x->{"_e"}) and $LIB->_is_two($x->{"_m"})) 32 129 161
Condition100%3/3
$LIB->_is_zero($x->{"_e"}) and $LIB->_is_two($x->{"_m"})
!ll&&!rl&&r
755432
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
5645 $LIB->_is_two($x->{_m})))
5646 {
5647 32 $dbd = 0; # disable shortcut
5648 # we can use the cached value in these cases
5649 32 ▶ if ($scale <= $LOG_2_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_2_A) 32 0 32
5650 32 $x = $x->bzero();
5651 32 $x = $x->badd($LOG_2); # modify $x in place
5652 32 $calc = 0; # no need to calc, but round
5653 }
5654 # if we can't use the shortcut, we continue normally
5655 }
5656 }
5657  
5658 # if $x = 0.1, we know the result must be 0-log(10)
5659 168 ▶ 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 166 168
Condition100%9/9
$calc != 0 and ($x->{"_es"} eq "-" and ($LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"})))
!ll&&!rl&&r
391218
$x->{"_es"} eq "-" and ($LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"}))
!ll&&!rl&&r
63633
$LIB->_is_one($x->{"_e"}) and $LIB->_is_one($x->{"_m"})
!ll&&!rl&&r
26382
MC/DC50%2/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 table60%3/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
5660 ($x->{_es} eq '-' && # $x == 0.1
5661 ($LIB->_is_one($x->{_e}) &&
5662 $LIB->_is_one($x->{_m}))))
5663 {
5664 2 $dbd = 0; # disable shortcut
5665 # we can use the cached value in these cases
5666 2 ▶ if ($scale <= $LOG_10_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_10_A) 2 0 2
5667 2 $x = $x->bzero();
5668 2 $x = $x->bsub($LOG_10);
5669 2 $calc = 0; # no need to calc, but round
5670 }
5671 }
5672  
5673 168 ▶ return $x if $calc == 0; # already have the result
Branch: 1 branch
BranchTrueFalseTotal
if $calc == 0 41 127 168
5674  
5675 # default: these correction factors are undef and thus not used
5676 127 my $l_10; # value of ln(10) to A of $scale
5677 my $l_2; # value of ln(2) to A of $scale
5678  
5679 127 my $two = $class->new(2);
5680  
5681 # $x == 2 => 1, $x == 13 => 2, $x == 0.1 => 0, $x == 0.01 => -1
5682 # so don't do this shortcut for 1 or 0
5683 127 ▶ if (($dbd > 1) || ($dbd < 0)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbd > 1 or $dbd < 0) 48 79 127
Condition100%3/3
$dbd > 1 or $dbd < 0
l!l&&r!l&&!r
41779
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
5684 # convert our cached value to an object if not already (avoid doing this
5685 # at import() time, since not everybody needs this)
5686 48 ▶ $LOG_10 = $class->new($LOG_10, undef, undef) unless ref $LOG_10;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $LOG_10 10 38 48
5687  
5688 # got more than one digit before the dot, or more than one zero after
5689 # the dot, so do:
5690 # log(123) == log(1.23) + log(10) * 2
5691 # log(0.0123) == log(1.23) - log(10) * 2
5692  
5693 48 ▶ if ($scale <= $LOG_10_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_10_A) { } 47 1 48
5694 # use cached value
5695 47 $l_10 = $LOG_10->copy(); # copy for mul
5696 } else {
5697 # else: slower, compute and cache result
5698  
5699 # shorten the time to calculate log(10) based on the following:
5700 # log(1.25 * 8) = log(1.25) + log(8)
5701 # = log(1.25) + log(2) + log(2) + log(2)
5702  
5703 # first get $l_2 (and possible compute and cache log(2))
5704 1 ▶ $LOG_2 = $class->new($LOG_2, undef, undef) unless ref $LOG_2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $LOG_2 0 1 1
5705 1 ▶ if ($scale <= $LOG_2_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_2_A) { } 1 0 1
5706 # use cached value
5707 1 $l_2 = $LOG_2->copy(); # copy() for the mul below
5708 } else {
5709 # else: slower, compute and cache result
5710 0 $l_2 = $two->copy();
5711 0 $l_2 = $l_2->_log($scale); # scale+4, actually
5712 0 $LOG_2 = $l_2->copy(); # cache the result for later
5713 # the copy() is for mul below
5714 0 $LOG_2_A = $scale;
5715 }
5716  
5717 # now calculate log(1.25):
5718 1 $l_10 = $class->new('1.25');
5719 1 $l_10 = $l_10->_log($scale); # scale+4, actually
5720  
5721 # log(1.25) + log(2) + log(2) + log(2):
5722 1 $l_10 = $l_10->badd($l_2);
5723 1 $l_10 = $l_10->badd($l_2);
5724 1 $l_10 = $l_10->badd($l_2);
5725 1 $LOG_10 = $l_10->copy(); # cache the result for later
5726 # the copy() is for mul below
5727 1 $LOG_10_A = $scale;
5728 }
5729 48 ▶ $dbd-- if ($dbd > 1); # 20 => dbd=2, so make it dbd=1
Branch: 1 branch
BranchTrueFalseTotal
if $dbd > 1 41 7 48
5730 48 $l_10 = $l_10->bmul($class->new($dbd)); # log(10) * (digits_before_dot-1)
5731 48 my $dbd_sign = '+';
5732 48 ▶ if ($dbd < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbd < 0) 7 41 48
5733 7 $dbd = -$dbd;
5734 7 $dbd_sign = '-';
5735 }
5736 ($x->{_e}, $x->{_es}) =
5737 48 $LIB -> _ssub($x->{_e}, $x->{_es}, $LIB->_new($dbd), $dbd_sign);
5738 }
5739  
5740 # Now: 0.1 <= $x < 10 (and possible correction in l_10)
5741  
5742 ### Since $x in the range 0.5 .. 1.5 is MUCH faster, we do a repeated div
5743 ### or mul by 2 (maximum times 3, since x < 10 and x > 0.1)
5744  
5745 127 ▶ $HALF = $class->new($HALF) unless ref($HALF);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $HALF 8 119 127
5746  
5747 127 my $twos = 0; # default: none (0 times)
5748 127 ▶ while ($x->bacmp($HALF) <= 0) { # X <= 0.5
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bacmp($HALF) <= 0) 40 127 167
5749 40 $twos--;
5750 40 $x = $x->bmul($two);
5751 }
5752 127 ▶ while ($x->bacmp($two) >= 0) { # X >= 2
Branch: 1 branch
BranchTrueFalseTotal
if ($x->bacmp($two) >= 0) 52 127 179
5753 52 $twos++;
5754 52 $x = $x->bdiv($two, $scale+4); # keep all digits
5755 }
5756 127 $x = $x->bround($scale+4);
5757 # $twos > 0 => did mul 2, < 0 => did div 2 (but we never did both)
5758 # So calculate correction factor based on ln(2):
5759 127 ▶ if ($twos != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($twos != 0) { } 72 55 127
5760 72 ▶ $LOG_2 = $class->new($LOG_2, undef, undef) unless ref $LOG_2;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $LOG_2 8 64 72
5761 72 ▶ if ($scale <= $LOG_2_A) {
Branch: 1 branch
BranchTrueFalseTotal
if ($scale <= $LOG_2_A) { } 69 3 72
5762 # use cached value
5763 69 $l_2 = $LOG_2->copy(); # copy() for the mul below
5764 } else {
5765 # else: slower, compute and cache result
5766 3 $l_2 = $two->copy();
5767 3 $l_2 = $l_2->_log($scale); # scale+4, actually
5768 3 $LOG_2 = $l_2->copy(); # cache the result for later
5769 # the copy() is for mul below
5770 3 $LOG_2_A = $scale;
5771 }
5772 72 $l_2 = $l_2->bmul($twos); # * -2 => subtract, * 2 => add
5773 } else {
5774 55 undef $l_2;
5775 }
5776  
5777 127 $x = $x->_log($scale); # need to do the "normal" way
5778 127 ▶ $x = $x->badd($l_10) if defined $l_10; # correct it by ln(10)
Branch: 1 branch
BranchTrueFalseTotal
if defined $l_10 48 79 127
5779 127 ▶ $x = $x->badd($l_2) if defined $l_2; # and maybe by ln(2)
Branch: 1 branch
BranchTrueFalseTotal
if defined $l_2 72 55 127
5780  
5781 # all done, $x contains now the result
5782 127 $x;
5783 }
5784  
5785 sub _pow {
5786 # Calculate a power where $y is a non-integer, like 2 ** 0.3
5787 114 ▶ my ($x, $y, @r) = @_;
Subroutine _pow: called CC=12 SCAR=24.9
5788 114 my $class = ref($x);
5789  
5790 # if $y == 0.5, it is sqrt($x)
5791 114 ▶ $HALF = $class->new($HALF) unless ref($HALF);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $HALF 5 109 114
5792 114 ▶ return $x->bsqrt(@r, $y) if $y->bcmp($HALF) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $y->bcmp($HALF) == 0 45 69 114
5793  
5794 # Using:
5795 # a ** x == e ** (x * ln a)
5796  
5797 # u = y * ln x
5798 # _ _
5799 # Taylor: | u u^2 u^3 |
5800 # x ** y = 1 + | --- + --- + ----- + ... |
5801 # |_ 1 1*2 1*2*3 _|
5802  
5803 # we need to limit the accuracy to protect against overflow
5804 69 my $fallback = 0;
5805 69 my ($scale, @params);
5806 69 ($x, @params) = $x->_find_round_parameters(@r);
5807  
5808 69 ▶ return $x if $x->is_nan(); # error in _find_round_parameters?
Branch: 1 branch
BranchTrueFalseTotal
if $x->is_nan 0 69 69
5809  
5810 # no rounding at all, so must use fallback
5811 69 ▶ if (scalar @params == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @params == 0) { } 52 17 69
5812 # simulate old behaviour
5813 52 $params[0] = $class->div_scale(); # and round to it as accuracy
5814 52 $params[1] = undef; # disable P
5815 52 $scale = $params[0]+4; # at least four more for proper round
5816 52 $params[2] = $r[2]; # round mode by caller or undef
5817 52 $fallback = 1; # to clear a/p afterwards
5818 } else {
5819 # the 4 below is empirical, and there might be cases where it is not
5820 # enough...
5821 17 ▶ $scale = abs($params[0] || $params[1]) + 4; # take whatever is defined
Condition33%1/3
$params[0] || $params[1]
l!l&&r!l&&!r
1700
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
5822 }
5823  
5824 # when user set globals, they would interfere with our calculation, so
5825 # disable them and later re-enable them
5826 43 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
5827 69 my $abr = "$class\::accuracy";
5828 69 my $ab = $$abr;
5829 69 $$abr = undef;
5830 69 my $pbr = "$class\::precision";
5831 69 my $pb = $$pbr;
5832 69 $$pbr = undef;
5833 # we also need to disable any set A or P on $x (_find_round_parameters took
5834 # them already into account), since these would interfere, too
5835 69 $x->{_a} = undef;
5836 69 $x->{_p} = undef;
5837  
5838 # Disabling upgrading and downgrading is no longer necessary to avoid an
5839 # infinite recursion, but it avoids unnecessary upgrading and downgrading in
5840 # the intermediate computations.
5841  
5842 69 local $Math::BigInt::upgrade = undef;
5843 69 local $Math::BigFloat::downgrade = undef;
5844  
5845 69 my ($limit, $v, $u, $below, $factor, $next, $over);
5846  
5847 69 $u = $x->copy()->blog(undef, $scale)->bmul($y);
5848 69 my $do_invert = ($u->{sign} eq '-');
5849 69 ▶ $u = $u->bneg() if $do_invert;
Branch: 1 branch
BranchTrueFalseTotal
if $do_invert 32 37 69
5850 69 $v = $class->bone(); # 1
5851 69 $factor = $class->new(2); # 2
5852 69 $x = $x->bone(); # first term: 1
5853  
5854 69 $below = $v->copy();
5855 69 $over = $u->copy();
5856  
5857 69 $limit = $class->new("1E-". ($scale-1));
5858 69 while (3 < 5) {
5859 # we calculate the next term, and add it to the last
5860 # when the next term is below our limit, it won't affect the outcome
5861 # anymore, so we stop:
5862 3277 $next = $over->copy()->bdiv($below, $scale);
5863 3277 ▶ last if $next->bacmp($limit) <= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $next->bacmp($limit) <= 0 69 3208 3277
5864 3208 $x = $x->badd($next);
5865 # calculate things for the next term
5866 3208 $over *= $u;
5867 3208 $below *= $factor;
5868 3208 $factor = $factor->binc();
5869  
5870 3208 ▶ last if $x->{sign} !~ /^[-+]$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $x->{"sign"} =~ /^[-+]$/ 0 3208 3208
5871 }
5872  
5873 69 ▶ if ($do_invert) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_invert) 32 37 69
5874 32 my $x_copy = $x->copy();
5875 32 $x = $x->bone->bdiv($x_copy, $scale);
5876 }
5877  
5878 # shortcut to not run through _find_round_parameters again
5879 69 ▶ if (defined $params[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params[0]) { } 69 0 69
5880 69 $x = $x->bround($params[0], $params[2]); # then round accordingly
5881 } else {
5882 0 $x = $x->bfround($params[1], $params[2]); # then round accordingly
5883 }
5884 69 ▶ if ($fallback) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fallback) 52 17 69
5885 # clear a/p after round, since user did not request it
5886 52 $x->{_a} = undef;
5887 52 $x->{_p} = undef;
5888 }
5889 # restore globals
5890 69 $$abr = $ab;
5891 69 $$pbr = $pb;
5892 69 $x;
5893 }
5894  
5895 # These functions are only provided for backwards compabibility so that old
5896 # version of Math::BigRat etc. don't complain about missing them.
5897  
5898 sub _e_add {
5899 0 ▶ my ($x, $y, $xs, $ys) = @_;
Subroutine _e_add: not called CC=1 SCAR=6.9
5900 0 return $LIB -> _sadd($x, $xs, $y, $ys);
5901 }
5902  
5903 sub _e_sub {
5904 0 ▶ my ($x, $y, $xs, $ys) = @_;
Subroutine _e_sub: not called CC=1 SCAR=6.9
5905 0 return $LIB -> _ssub($x, $xs, $y, $ys);
5906 }
5907  
5908 43 1;
5909  
5910 __END__
« BigInt.pm ↑ Summary BigInt/Lib.pm »