bigrat.pm

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