bigfloat.pm

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