bigint.pm

filter: Statementstmt 82.0% 142 / 173 Branchbran 61.5% 48 / 78 Conditioncond 70.0% 14 / 20 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 96.7% 30 / 31 Podpod 100% 13 / 13 totaltotal 76.4% 247 / 323 CC 47 SCAR 21.7
CC
47
Cov
75%
_oct_core
23.5
import
17.1
_float_constant
11.3
CRAP 8.8
« bignum.pm ↑ Summary bigrat.pm »
1 package bigint;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use Carp qw< carp croak >;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 our $VERSION = '0.67';
9  
10 14 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 our @ISA = qw( Exporter );
12 14 our @EXPORT_OK = qw( PI e bpi bexp hex oct );
13 14 our @EXPORT = qw( inf NaN );
14  
15 14 ▶use overload;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 14 my $obj_class = "Math::BigInt";
18  
19 ##############################################################################
20  
21 5 ▶sub accuracy {
Subroutine accuracy: called CC=1 SCAR=0.0
Pod documented
22 5 my $self = shift;
23 5 $obj_class -> accuracy(@_);
24 }
25  
26 5 ▶sub precision {
Subroutine precision: called CC=1 SCAR=0.0
Pod documented
27 5 my $self = shift;
28 5 $obj_class -> precision(@_);
29 }
30  
31 5 ▶sub round_mode {
Subroutine round_mode: called CC=1 SCAR=0.0
Pod documented
32 5 my $self = shift;
33 5 $obj_class -> round_mode(@_);
34 }
35  
36 0 ▶sub div_scale {
Subroutine div_scale: not called CC=1 SCAR=6.9
Pod documented
37 0 my $self = shift;
38 0 $obj_class -> div_scale(@_);
39 }
40  
41 6 ▶sub in_effect {
Subroutine in_effect: called CC=2 SCAR=7.1
Pod documented
42 6 ▶ my $level = shift || 0;
Condition50%1/2
shift() || 0
l!l
06
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
43 6 my $hinthash = (caller($level))[10];
44 6 $hinthash->{bigint};
45 }
46  
47 53 ▶sub _float_constant {
Subroutine _float_constant: called CC=10 SCAR=24.3
48 53 my $str = shift;
49  
50 # We can't pass input directly to new() because of the way it handles the
51 # combination of non-integers with no upgrading. Such cases are by
52 # Math::BigInt returned as NaN, but we truncate to an integer.
53  
54 # See if we can convert the input string to a string using a normalized form
55 # consisting of the significand as a signed integer, the character "e", and
56 # the exponent as a signed integer, e.g., "+0e+0", "+314e-2", and "-1e+3".
57  
58 53 my $nstr;
59  
60 53 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^0(?:[Oo]|_*[0-7])/ and $nstr = "Math::BigInt"->oct_str_to_dec_flt_str($str) or $nstr = "Math::BigInt"->dec_str_to_dec_flt_str($str) or $str =~ /^0[Xx]/ and $nstr = "Math::BigInt"->hex_str_to_dec_flt_str($str) or $str =~ /^0[Bb]/ and $nstr = "Math::BigInt"->bin_str_to_dec_flt_str($str)) 53 0 53
Condition72%13/18
$str =~ /^0(?:[Oo]|_*[0-7])/ and $nstr = "Math::BigInt"->oct_str_to_dec_flt_str($str) or $nstr = "Math::BigInt"->dec_str_to_dec_flt_str($str) or $str =~ /^0[Xx]/ and $nstr = "Math::BigInt"->hex_str_to_dec_flt_str($str) or $str =~ /^0[Bb]/ and $nstr = "Math::BigInt"->bin_str_to_dec_flt_str($str)
l!l&&r!l&&!r
41120
$str =~ /^0(?:[Oo]|_*[0-7])/ and $nstr = "Math::BigInt"->oct_str_to_dec_flt_str($str) or $nstr = "Math::BigInt"->dec_str_to_dec_flt_str($str) or $str =~ /^0[Xx]/ and $nstr = "Math::BigInt"->hex_str_to_dec_flt_str($str)
l!l&&r!l&&!r
33812
$str =~ /^0(?:[Oo]|_*[0-7])/ and $nstr = "Math::BigInt"->oct_str_to_dec_flt_str($str) or $nstr = "Math::BigInt"->dec_str_to_dec_flt_str($str)
l!l&&r!l&&!r
24920
$str =~ /^0(?:[Oo]|_*[0-7])/ and $nstr = "Math::BigInt"->oct_str_to_dec_flt_str($str)
!ll&&!rl&&r
29024
$str =~ /^0[Xx]/ and $nstr = "Math::BigInt"->hex_str_to_dec_flt_str($str)
!ll&&!rl&&r
1208
$str =~ /^0[Bb]/ and $nstr = "Math::BigInt"->bin_str_to_dec_flt_str($str)
!ll&&!rl&&r
0012
MC/DC0%0/7
$str =~ /^0(?:[Oo]|_*[0-7])/ and $nstr = "Math::BigInt"->oct_str_to_dec_flt_str($str) or $nstr = "Math::BigInt"->dec_str_to_dec_flt_str($str) or $str =~ /^0[Xx]/ and $nstr = "Math::BigInt"->hex_str_to_dec_flt_str($str) or $str =~ /^0[Bb]/ and $nstr = "Math::BigInt"->bin_str_to_dec_flt_str($str)
$str =~ /^0(?:[Oo]|_*[0-7])/ $nstr = "Math::BigInt"->oct_str_to_dec_flt_str($str) $nstr = "Math::BigInt"->dec_str_to_dec_flt_str($str) $str =~ /^0[Xx]/ $nstr = "Math::BigInt"->hex_str_to_dec_flt_str($str) $str =~ /^0[Bb]/ $nstr = "Math::BigInt"->bin_str_to_dec_flt_str($str)
Truth table23%4/17
A and B or C or D and E or F and G
A= $str =~ /^0(?:[Oo]|_*[0-7])/B= $nstr = "Math::BigInt"->oct_str_to_dec_flt_str($str)C= $nstr = "Math::BigInt"->dec_str_to_dec_flt_str($str)D= $str =~ /^0[Xx]/E= $nstr = "Math::BigInt"->hex_str_to_dec_flt_str($str)F= $str =~ /^0[Bb]/G= $nstr = "Math::BigInt"->bin_str_to_dec_flt_str($str)
ABCDEFGresult
11XXXXX1
0X1XXXX1
101XXXX1
0X011XX1
10011XX1
0X00X111
0X010111
1000X111
10010111
0X00X0X0
0X00X100
0X0100X0
0X010100
1000X0X0
1000X100
100100X0
10010100
61 # See if it is an octal number. An octal number like '0377' is also
62 # accepted by the functions parsing decimal and hexadecimal numbers, so
63 # handle octal numbers before decimal and hexadecimal numbers.
64  
65 $str =~ /^0(?:[Oo]|_*[0-7])/ and
66 $nstr = Math::BigInt -> oct_str_to_dec_flt_str($str)
67  
68 or
69  
70 # See if it is decimal number.
71  
72 $nstr = Math::BigInt -> dec_str_to_dec_flt_str($str)
73  
74 or
75  
76 # See if it is a hexadecimal number. Every hexadecimal number has a
77 # prefix, but the functions parsing numbers don't require it, so check
78 # to see if it actually is a hexadecimal number.
79  
80 $str =~ /^0[Xx]/ and
81 $nstr = Math::BigInt -> hex_str_to_dec_flt_str($str)
82  
83 or
84  
85 # See if it is a binary numbers. Every binary number has a prefix, but
86 # the functions parsing numbers don't require it, so check to see if it
87 # actually is a binary number.
88  
89 $str =~ /^0[Bb]/ and
90 $nstr = Math::BigInt -> bin_str_to_dec_flt_str($str))
91 {
92 53 my $pos = index($nstr, 'e');
93 53 my $expo_sgn = substr($nstr, $pos + 1, 1);
94 53 my $sign = substr($nstr, 0, 1);
95 53 my $mant = substr($nstr, 1, $pos - 1);
96 53 my $mant_len = CORE::length($mant);
97 53 my $expo = substr($nstr, $pos + 2);
98  
99 53 ▶ if ($expo_sgn eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($expo_sgn eq '-') { } 6 47 53
100 6 ▶ if ($mant_len <= $expo) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mant_len <= $expo) { } 0 6 6
101 0 return $obj_class -> bzero(); # underflow
102 } else {
103 6 $mant = substr $mant, 0, $mant_len - $expo; # truncate
104 6 return $obj_class -> new($sign . $mant);
105 }
106 } else {
107 47 $mant .= "0" x $expo; # pad with zeros
108 47 return $obj_class -> new($sign . $mant);
109 }
110 }
111  
112 # If we get here, there is a bug in the code above this point.
113  
114 0 warn "Internal error: unable to handle literal constant '$str'.",
115 " This is a bug, so please report this to the module author.";
116 0 return $obj_class -> bnan();
117 }
118  
119 #############################################################################
120 # the following two routines are for "use bigint qw/hex oct/;":
121  
122 14 ▶use constant LEXICAL => $] > 5.009004;
Subroutine BEGIN: called CC=1 SCAR=0.0
123  
124 # Internal function with the same semantics as CORE::hex(). This function is
125 # not used directly, but rather by other front-end functions.
126  
127 25 ▶sub _hex_core {
Subroutine _hex_core: called CC=4 SCAR=15.2
128 25 my $str = shift;
129  
130 # Strip off, clean, and parse as much as we can from the beginning.
131  
132 25 my $x;
133 25 ▶ if ($str =~ s/ ^ ( 0? [xX] )? ( [0-9a-fA-F]* ( _ [0-9a-fA-F]+ )* ) //x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/ ^ ( 0? [xX] )? ( [0-9a-fA-F]* ( _ [0-9a-fA-F]+ )* ) //x) { } 25 0 25
134 25 my $chrs = $2;
135 25 $chrs =~ tr/_//d;
136 25 ▶ $chrs = '0' unless CORE::length $chrs;
Branch: 1 branch
BranchTrueFalseTotal
unless length $chrs 0 25 25
137 25 $x = $obj_class -> from_hex($chrs);
138 } else {
139 0 $x = $obj_class -> bzero();
140 }
141  
142 # Warn about trailing garbage.
143  
144 25 ▶ if (CORE::length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 0 25 25
145 0 require Carp;
146 0 Carp::carp(sprintf("Illegal hexadecimal digit '%s' ignored",
147 substr($str, 0, 1)));
148 }
149  
150 25 return $x;
151 }
152  
153 # Internal function with the same semantics as CORE::oct(). This function is
154 # not used directly, but rather by other front-end functions.
155  
156 26 ▶sub _oct_core {
Subroutine _oct_core: called CC=9 SCAR=31.6
157 26 my $str = shift;
158  
159 26 $str =~ s/^\s*//;
160  
161 # Hexadecimal input.
162  
163 26 ▶ return _hex_core($str) if $str =~ /^0?[xX]/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^0?[xX]/ 0 26 26
164  
165 26 my $x;
166  
167 # Binary input.
168  
169 26 ▶ if ($str =~ /^0?[bB]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^0?[bB]/) 0 26 26
170  
171 # Strip off, clean, and parse as much as we can from the beginning.
172  
173 0 ▶ if ($str =~ s/ ^ ( 0? [bB] )? ( [01]* ( _ [01]+ )* ) //x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/ ^ ( 0? [bB] )? ( [01]* ( _ [01]+ )* ) //x) 0 0 0
174 0 my $chrs = $2;
175 0 $chrs =~ tr/_//d;
176 0 ▶ $chrs = '0' unless CORE::length $chrs;
Branch: 1 branch
BranchTrueFalseTotal
unless length $chrs 0 0 0
177 0 $x = $obj_class -> from_bin($chrs);
178 }
179  
180 # Warn about trailing garbage.
181  
182 0 ▶ if (CORE::length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 0 0 0
183 0 require Carp;
184 0 Carp::carp(sprintf("Illegal binary digit '%s' ignored",
185 substr($str, 0, 1)));
186 }
187  
188 0 return $x;
189 }
190  
191 # Octal input. Strip off, clean, and parse as much as we can from the
192 # beginning.
193  
194 26 ▶ if ($str =~ s/ ^ ( 0? [oO] )? ( [0-7]* ( _ [0-7]+ )* ) //x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ s/ ^ ( 0? [oO] )? ( [0-7]* ( _ [0-7]+ )* ) //x) 26 0 26
195 26 my $chrs = $2;
196 26 $chrs =~ tr/_//d;
197 26 ▶ $chrs = '0' unless CORE::length $chrs;
Branch: 1 branch
BranchTrueFalseTotal
unless length $chrs 0 26 26
198 26 $x = $obj_class -> from_oct($chrs);
199 }
200  
201 # Warn about trailing garbage. CORE::oct() only warns about 8 and 9, but it
202 # is more helpful to warn about all invalid digits.
203  
204 26 ▶ if (CORE::length($str)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 0 26 26
205 0 require Carp;
206 0 Carp::carp(sprintf("Illegal octal digit '%s' ignored",
207 substr($str, 0, 1)));
208 }
209  
210 26 return $x;
211 }
212  
213 {
214 14 my $proto = LEXICAL ? '_' : ';$';
215 1 ▶ eval '
Subroutine hex: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 1 0 1
216 sub hex(' . $proto . ') {' . <<'.';
217 my $str = @_ ? $_[0] : $_;
218 _hex_core($str);
219 }
220 .
221  
222 2 ▶ eval '
Subroutine oct: called CC=2 SCAR=7.1
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 0 2
223 sub oct(' . $proto . ') {' . <<'.';
224 my $str = @_ ? $_[0] : $_;
225 _oct_core($str);
226 }
227 .
228 }
229  
230 #############################################################################
231 # the following two routines are for Perl 5.9.4 or later and are lexical
232  
233 14 my ($prev_oct, $prev_hex, $overridden);
234  
235 40 ▶if (LEXICAL) { eval <<'.' }
Subroutine _hex: called CC=5 SCAR=16.4
Subroutine _oct: called CC=5 SCAR=16.4
Branch: 8 branches
BranchTrueFalseTotal
$hh->{'bigint'} ? : 1 39 40
$hh->{'bigfloat'} ? : 0 39 39
$hh->{'bigrat'} ? : 0 39 39
$prev_hex ? : 2 37 39
$hh->{'bigint'} ? : 1 39 40
$hh->{'bigfloat'} ? : 0 39 39
$hh->{'bigrat'} ? : 0 39 39
$prev_oct ? : 2 37 39
236 sub _hex(_) {
237 my $hh = (caller 0)[10];
238 return $$hh{bigint} ? bigint::_hex_core($_[0])
239 : $$hh{bigfloat} ? bigfloat::_hex_core($_[0])
240 : $$hh{bigrat} ? bigrat::_hex_core($_[0])
241 : $prev_hex ? &$prev_hex($_[0])
242 : CORE::hex($_[0]);
243 }
244  
245 sub _oct(_) {
246 my $hh = (caller 0)[10];
247 return $$hh{bigint} ? bigint::_oct_core($_[0])
248 : $$hh{bigfloat} ? bigfloat::_oct_core($_[0])
249 : $$hh{bigrat} ? bigrat::_oct_core($_[0])
250 : $prev_oct ? &$prev_oct($_[0])
251 : CORE::oct($_[0]);
252 }
253 .
254  
255 56 ▶sub _override {
Subroutine _override: called CC=2 SCAR=6.9
256 56 ▶ return if $overridden;
Branch: 1 branch
BranchTrueFalseTotal
if $overridden 42 14 56
257 14 $prev_oct = *CORE::GLOBAL::oct{CODE};
258 14 $prev_hex = *CORE::GLOBAL::hex{CODE};
259 14 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
260 14 *CORE::GLOBAL::oct = \&_oct;
261 14 *CORE::GLOBAL::hex = \&_hex;
262 14 $overridden = 1;
263 }
264  
265 14 ▶sub unimport {
Subroutine unimport: called CC=1 SCAR=0.0
266 14 delete $^H{bigint}; # no longer in effect
267 14 overload::remove_constant('binary', '', 'float', '', 'integer');
268 }
269  
270 56 ▶sub import {
Subroutine import: called CC=14 SCAR=28.4
271 56 my $class = shift;
272  
273 56 $^H{bigint} = 1; # we are in effect
274 56 delete $^H{bigfloat};
275 56 delete $^H{bigrat};
276  
277 # for newer Perls always override hex() and oct() with a lexical version:
278 56 if (LEXICAL) {
279 56 _override();
280 }
281  
282 56 my @import = ();
283 56 my @a = (); # unrecognized arguments
284 56 my $ver; # version? trace?
285  
286 56 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 39 36 75
287 39 my $param = shift;
288  
289 # Accuracy.
290  
291 39 ▶ if ($param =~ /^a(ccuracy)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^a(ccuracy)?$/) 2 37 39
292 2 push @import, 'accuracy', shift();
293 2 next;
294 }
295  
296 # Precision.
297  
298 37 ▶ if ($param =~ /^p(recision)?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^p(recision)?$/) 2 35 37
299 2 push @import, 'precision', shift();
300 2 next;
301 }
302  
303 # Rounding mode.
304  
305 35 ▶ if ($param eq 'round_mode') {
Branch: 1 branch
BranchTrueFalseTotal
if ($param eq "round_mode") 0 35 35
306 0 push @import, 'round_mode', shift();
307 0 next;
308 }
309  
310 # Backend library.
311  
312 35 ▶ if ($param =~ /^(l|lib|try|only)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(l|lib|try|only)$/) 9 26 35
313 9 ▶ push @import, $param eq 'l' ? 'lib' : $param;
Branch: 1 branch
BranchTrueFalseTotal
$param eq 'l' ? : 2 7 9
314 9 ▶ push @import, shift() if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 0 9
315 9 next;
316 }
317  
318 26 ▶ if ($param =~ /^(v|version)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(v|version)$/) 0 26 26
319 0 $ver = 1;
320 0 next;
321 }
322  
323 26 ▶ if ($param =~ /^(t|trace)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(t|trace)$/) 0 26 26
324 0 $obj_class .= "::Trace";
325 0 eval "require $obj_class";
326 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
327 0 next;
328 }
329  
330 26 ▶ if ($param =~ /^(PI|e|bexp|bpi|hex|oct)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param =~ /^(PI|e|bexp|bpi|hex|oct)\z/) 6 20 26
331 6 push @a, $param;
332 6 next;
333 }
334  
335 20 croak("Unknown option '$param'");
336 }
337  
338 36 eval "require $obj_class";
339 36 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 36 36
340 36 $obj_class -> import(@import);
341  
342 36 ▶ if ($ver) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ver) 0 36 36
343 0 printf "%-31s v%s\n", $class, $class -> VERSION();
344 0 printf " lib => %-23s v%s\n",
345 $obj_class -> config("lib"), $obj_class -> config("lib_version");
346 0 printf "%-31s v%s\n", $obj_class, $obj_class -> VERSION();
347 0 exit;
348 }
349  
350 36 $class -> export_to_level(1, $class, @a); # export inf, NaN, etc.
351  
352 overload::constant
353  
354 # This takes care each number written as decimal integer and within the
355 # range of what perl can represent as an integer, e.g., "314", but not
356 # "3141592653589793238462643383279502884197169399375105820974944592307".
357  
358 53 ▶ integer => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
359 #printf "Value '%s' handled by the 'integer' sub.\n", $_[0];
360 53 my $str = shift;
361 53 return $obj_class -> new($str);
362 },
363  
364 # This takes care of each number written with a decimal point and/or
365 # using floating point notation, e.g., "3.", "3.0", "3.14e+2" (decimal),
366 # "0b1.101p+2" (binary), "03.14p+2" and "0o3.14p+2" (octal), and
367 # "0x3.14p+2" (hexadecimal).
368  
369 53 ▶ float => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
370 #printf "# Value '%s' handled by the 'float' sub.\n", $_[0];
371 53 _float_constant(shift);
372 },
373  
374 # Take care of each number written as an integer (no decimal point or
375 # exponent) using binary, octal, or hexadecimal notation, e.g., "0b101"
376 # (binary), "0314" and "0o314" (octal), and "0x314" (hexadecimal).
377  
378 12 ▶ binary => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
379 #printf "# Value '%s' handled by the 'binary' sub.\n", $_[0];
380 12 my $str = shift;
381 12 ▶ return $obj_class -> new($str) if $str =~ /^0[XxBb]/;
Branch: 1 branch
BranchTrueFalseTotal
if $str =~ /^0[XxBb]/ 7 5 12
382 5 $obj_class -> from_oct($str);
383 36 };
384 }
385  
386 11 ▶sub inf () { $obj_class -> binf(); }
Subroutine inf: called CC=1 SCAR=0.0
Pod documented
387 10 ▶sub NaN () { $obj_class -> bnan(); }
Subroutine NaN: called CC=1 SCAR=0.0
Pod documented
388  
389 1 ▶sub PI () { $obj_class -> new(3); }
Subroutine PI: called CC=1 SCAR=0.0
Pod documented
390 1 ▶sub e () { $obj_class -> new(2); }
Subroutine e: called CC=1 SCAR=0.0
Pod documented
391  
392 1 ▶sub bpi ($) { $obj_class -> new(3); }
Subroutine bpi: called CC=1 SCAR=0.0
Pod documented
393  
394 2 ▶sub bexp ($$) {
Subroutine bexp: called CC=1 SCAR=0.0
Pod documented
395 2 my $x = $obj_class -> new(shift);
396 2 $x -> bexp(@_);
397 }
398  
399 14 1;
400  
401 __END__
« bignum.pm ↑ Summary bigrat.pm »