File Coverage

blib/lib/bigrat.pm
Criterion Covered Total %
statement 122 156 78.2
branch 44 72 61.1
condition 4 20 20.0
subroutine 28 31 90.3
pod 13 13 100.0
total 211 292 72.2


line stmt bran cond sub pod time code
1             package bigrat;
2              
3 15     15   425250 use strict;
  15         89  
  15         363  
4 15     15   60 use warnings;
  15         25  
  15         423  
5              
6 15     15   70 use Carp qw< carp croak >;
  15         30  
  15         794  
7              
8             our $VERSION = '0.65';
9              
10 15     15   72 use Exporter;
  15         23  
  15         910  
11             our @ISA = qw( Exporter );
12             our @EXPORT_OK = qw( PI e bpi bexp hex oct );
13             our @EXPORT = qw( inf NaN );
14              
15 15     15   7160 use overload;
  15         6037  
  15         74  
16              
17             my $obj_class = "Math::BigRat";
18              
19             ##############################################################################
20              
21             sub accuracy {
22 5     5 1 11664 my $self = shift;
23 5         20 $obj_class -> accuracy(@_);
24             }
25              
26             sub precision {
27 5     5 1 1742 my $self = shift;
28 5         16 $obj_class -> precision(@_);
29             }
30              
31             sub round_mode {
32 5     5 1 1599 my $self = shift;
33 5         16 $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 in_effect {
42 6   50 6 1 16 my $level = shift || 0;
43 6         33 my $hinthash = (caller($level))[10];
44 6         22 $hinthash->{bigrat};
45             }
46              
47             sub _float_constant {
48 12     12   23 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 12         14 my $nstr;
55              
56 12 0 33     69 if (
      33        
      0        
      33        
      0        
      0        
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 12         1522 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         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         0 return $obj_class -> bnan();
96             }
97              
98             #############################################################################
99             # the following two routines are for "use bigrat qw/hex oct/;":
100              
101 15     15   4503 use constant LEXICAL => $] > 5.009004;
  15         23  
  15         8371  
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             sub _hex_core {
107 24     24   45 my $str = shift;
108              
109             # Strip off, clean, and parse as much as we can from the beginning.
110              
111 24         27 my $x;
112 24 50       152 if ($str =~ s/ ^ ( 0? [xX] )? ( [0-9a-fA-F]* ( _ [0-9a-fA-F]+ )* ) //x) {
113 24         177 my $chrs = $2;
114 24         34 $chrs =~ tr/_//d;
115 24 50       58 $chrs = '0' unless CORE::length $chrs;
116 24         90 $x = $obj_class -> from_hex($chrs);
117             } else {
118 0         0 $x = $obj_class -> bzero();
119             }
120              
121             # Warn about trailing garbage.
122              
123 24 50       15820 if (CORE::length($str)) {
124 0         0 require Carp;
125 0         0 Carp::carp(sprintf("Illegal hexadecimal digit '%s' ignored",
126             substr($str, 0, 1)));
127             }
128              
129 24         111 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             sub _oct_core {
136 24     24   37 my $str = shift;
137              
138 24         67 $str =~ s/^\s*//;
139              
140             # Hexadecimal input.
141              
142 24 50       148 return _hex_core($str) if $str =~ /^0?[xX]/;
143              
144 24         31 my $x;
145              
146             # Binary input.
147              
148 24 50       52 if ($str =~ /^0?[bB]/) {
149              
150             # Strip off, clean, and parse as much as we can from the beginning.
151              
152 0 0       0 if ($str =~ s/ ^ ( 0? [bB] )? ( [01]* ( _ [01]+ )* ) //x) {
153 0         0 my $chrs = $2;
154 0         0 $chrs =~ tr/_//d;
155 0 0       0 $chrs = '0' unless CORE::length $chrs;
156 0         0 $x = $obj_class -> from_bin($chrs);
157             }
158              
159             # Warn about trailing garbage.
160              
161 0 0       0 if (CORE::length($str)) {
162 0         0 require Carp;
163 0         0 Carp::carp(sprintf("Illegal binary digit '%s' ignored",
164             substr($str, 0, 1)));
165             }
166              
167 0         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 50       94 if ($str =~ s/ ^ ( 0? [oO] )? ( [0-7]* ( _ [0-7]+ )* ) //x) {
174 24         41 my $chrs = $2;
175 24         35 $chrs =~ tr/_//d;
176 24 50       52 $chrs = '0' unless CORE::length $chrs;
177 24         66 $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 50       6905 if (CORE::length($str)) {
184 0         0 require Carp;
185 0         0 Carp::carp(sprintf("Illegal octal digit '%s' ignored",
186             substr($str, 0, 1)));
187             }
188              
189 24         108 return $x;
190             }
191              
192             {
193             my $proto = LEXICAL ? '_' : ';$';
194 0 0   0 1 0 eval '
  0         0  
195             sub hex(' . $proto . ') {' . <<'.';
196             my $str = @_ ? $_[0] : $_;
197             _hex_core($str);
198             }
199             .
200              
201 0 0   0 1 0 eval '
  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             my ($prev_oct, $prev_hex, $overridden);
213              
214 109 100   109   26940 if (LEXICAL) { eval <<'.' }
  109 100   109   941  
  109 100       1624  
  109 100       913  
    100          
    100          
    100          
    100          
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             sub _override {
235 58 100   58   135 return if $overridden;
236 15         32 $prev_oct = *CORE::GLOBAL::oct{CODE};
237 15         24 $prev_hex = *CORE::GLOBAL::hex{CODE};
238 15     15   107 no warnings 'redefine';
  15         35  
  15         11865  
239 15         41 *CORE::GLOBAL::oct = \&_oct;
240 15         30 *CORE::GLOBAL::hex = \&_hex;
241 15         25 $overridden = 1;
242             }
243              
244             sub unimport {
245 14     14   1775 $^H{bigrat} = undef; # no longer in effect
246 14         50 overload::remove_constant('binary', '', 'float', '', 'integer');
247             }
248              
249             sub import {
250 58     58   71737 my $class = shift;
251              
252 58         184 $^H{bigrat} = 1; # we are in effect
253 58         140 $^H{bigint} = undef;
254 58         106 $^H{bigfloat} = undef;
255              
256             # for newer Perls always override hex() and oct() with a lexical version:
257 58         67 if (LEXICAL) {
258 58         114 _override();
259             }
260              
261 58         108 my @import = ();
262 58         68 my @a = (); # unrecognized arguments
263 58         62 my $ver; # version?
264              
265 58         140 while (@_) {
266 41         55 my $param = shift;
267              
268             # Accuracy.
269              
270 41 100       110 if ($param =~ /^a(ccuracy)?$/) {
271 4         8 push @import, 'accuracy', shift();
272 4         10 next;
273             }
274              
275             # Precision.
276              
277 37 100       83 if ($param =~ /^p(recision)?$/) {
278 2         4 push @import, 'precision', shift();
279 2         18 next;
280             }
281              
282             # Rounding mode.
283              
284 35 50       64 if ($param eq 'round_mode') {
285 0         0 push @import, 'round_mode', shift();
286 0         0 next;
287             }
288              
289             # Backend library.
290              
291 35 100       361 if ($param =~ /^(l|lib|try|only)$/) {
292 9 100       27 push @import, $param eq 'l' ? 'lib' : $param;
293 9 50       23 push @import, shift() if @_;
294 9         21 next;
295             }
296              
297 26 50       74 if ($param =~ /^(v|version)$/) {
298 0         0 $ver = 1;
299 0         0 next;
300             }
301              
302 26 50       43 if ($param =~ /^(t|trace)$/) {
303 0         0 $obj_class .= "::Trace";
304 0         0 eval "require $obj_class";
305 0 0       0 die $@ if $@;
306 0         0 next;
307             }
308              
309 26 100       70 if ($param =~ /^(PI|e|bexp|bpi|hex|oct)\z/) {
310 6         8 push @a, $param;
311 6         11 next;
312             }
313              
314 20         2184 croak("Unknown option '$param'");
315             }
316              
317 38         1999 eval "require $obj_class";
318 38 50       561167 die $@ if $@;
319 38         172 $obj_class -> import(@import);
320              
321 38 50       168639 if ($ver) {
322 0         0 printf "%-31s v%s\n", $class, $class -> VERSION();
323 0         0 printf " lib => %-23s v%s\n",
324             $obj_class -> config("lib"), $obj_class -> config("lib_version");
325 0         0 printf "%-31s v%s\n", $obj_class, $obj_class -> VERSION();
326 0         0 exit;
327             }
328              
329 38         1239 $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             integer => sub {
338             #printf "Value '%s' handled by the 'integer' sub.\n", $_[0];
339 91     91   52390 my $str = shift;
340 91         249 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             float => sub {
349             #printf "# Value '%s' handled by the 'float' sub.\n", $_[0];
350 12     12   7790 _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             binary => sub {
358             #printf "# Value '%s' handled by the 'binary' sub.\n", $_[0];
359 10     10   12967 my $str = shift;
360 10 100       61 return $obj_class -> new($str) if $str =~ /^0[XxBb]/;
361 3         12 $obj_class -> from_oct($str);
362 38         291 };
363             }
364              
365 11     11 1 6784 sub inf () { $obj_class -> binf(); }
366 10     10 1 7751 sub NaN () { $obj_class -> bnan(); }
367              
368             # This should depend on the current accuracy/precision. Fixme!
369 1     1 1 7442 sub PI () { $obj_class -> new('3.141592653589793238462643383279502884197'); }
370 1     1 1 76 sub e () { $obj_class -> new('2.718281828459045235360287471352662497757'); }
371              
372             sub bpi ($) {
373 1     1 1 1317 my $up = Math::BigFloat -> upgrade(); # get current upgrading, if any ...
374 1         10 Math::BigFloat -> upgrade(undef); # ... and disable
375 1         9 my $x = Math::BigFloat -> bpi(@_);
376 1         279 Math::BigFloat -> upgrade($up); # reset the upgrading
377 1         8 return $obj_class -> new($x);
378             }
379              
380             sub bexp ($$) {
381 1     1 1 6393 my $up = Math::BigFloat -> upgrade(); # get current upgrading, if any ...
382 1         14 Math::BigFloat -> upgrade(undef); # ... and disable
383 1         8 my $x = Math::BigFloat -> new(shift);
384 1         149 $x -> bexp(@_);
385 1         493 Math::BigFloat -> upgrade($up); # reset the upgrading
386 1         8 return $obj_class -> new($x);
387             }
388              
389             1;
390              
391             __END__