File Coverage

blib/lib/bigint.pm
Criterion Covered Total %
statement 133 172 77.3
branch 48 82 58.5
condition 4 20 20.0
subroutine 30 33 90.9
pod 15 15 100.0
total 230 322 71.4


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