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   458513 use strict;
  15         87  
  15         378  
4 15     15   66 use warnings;
  15         22  
  15         438  
5              
6 15     15   81 use Carp qw< carp croak >;
  15         22  
  15         807  
7              
8             our $VERSION = '0.66';
9              
10 15     15   76 use Exporter;
  15         37  
  15         1007  
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   8063 use overload;
  15         7023  
  15         77  
16              
17             my $obj_class = "Math::BigRat";
18              
19             ##############################################################################
20              
21             sub accuracy {
22 5     5 1 9910 my $self = shift;
23 5         39 $obj_class -> accuracy(@_);
24             }
25              
26             sub precision {
27 5     5 1 1189 my $self = shift;
28 5         17 $obj_class -> precision(@_);
29             }
30              
31             sub round_mode {
32 5     5 1 1236 my $self = shift;
33 5         19 $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 22 my $level = shift || 0;
43 6         37 my $hinthash = (caller($level))[10];
44 6         25 $hinthash->{bigrat};
45             }
46              
47             sub _float_constant {
48 12     12   27 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         17 my $nstr;
55              
56 12 0 33     104 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         1773 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   5191 use constant LEXICAL => $] > 5.009004;
  15         30  
  15         9108  
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   43 my $str = shift;
108              
109             # Strip off, clean, and parse as much as we can from the beginning.
110              
111 24         28 my $x;
112 24 50       185 if ($str =~ s/ ^ ( 0? [xX] )? ( [0-9a-fA-F]* ( _ [0-9a-fA-F]+ )* ) //x) {
113 24         226 my $chrs = $2;
114 24         42 $chrs =~ tr/_//d;
115 24 50       55 $chrs = '0' unless CORE::length $chrs;
116 24         77 $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       17939 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         128 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   46 my $str = shift;
137              
138 24         63 $str =~ s/^\s*//;
139              
140             # Hexadecimal input.
141              
142 24 50       239 return _hex_core($str) if $str =~ /^0?[xX]/;
143              
144 24         29 my $x;
145              
146             # Binary input.
147              
148 24 50       50 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       90 if ($str =~ s/ ^ ( 0? [oO] )? ( [0-7]* ( _ [0-7]+ )* ) //x) {
174 24         47 my $chrs = $2;
175 24         33 $chrs =~ tr/_//d;
176 24 50       54 $chrs = '0' unless CORE::length $chrs;
177 24         68 $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       8349 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         112 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   32320 if (LEXICAL) { eval <<'.' }
  109 100   109   1035  
  109 100       1908  
  109 100       1001  
    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   140 return if $overridden;
236 15         36 $prev_oct = *CORE::GLOBAL::oct{CODE};
237 15         22 $prev_hex = *CORE::GLOBAL::hex{CODE};
238 15     15   111 no warnings 'redefine';
  15         31  
  15         13294  
239 15         37 *CORE::GLOBAL::oct = \&_oct;
240 15         30 *CORE::GLOBAL::hex = \&_hex;
241 15         26 $overridden = 1;
242             }
243              
244             sub unimport {
245 14     14   2205 $^H{bigrat} = undef; # no longer in effect
246 14         56 overload::remove_constant('binary', '', 'float', '', 'integer');
247             }
248              
249             sub import {
250 58     58   63165 my $class = shift;
251              
252 58         181 $^H{bigrat} = 1; # we are in effect
253 58         128 $^H{bigint} = undef;
254 58         101 $^H{bigfloat} = undef;
255              
256             # for newer Perls always override hex() and oct() with a lexical version:
257 58         71 if (LEXICAL) {
258 58         106 _override();
259             }
260              
261 58         90 my @import = ();
262 58         68 my @a = (); # unrecognized arguments
263 58         67 my $ver; # version?
264              
265 58         147 while (@_) {
266 41         54 my $param = shift;
267              
268             # Accuracy.
269              
270 41 100       116 if ($param =~ /^a(ccuracy)?$/) {
271 4         10 push @import, 'accuracy', shift();
272 4         9 next;
273             }
274              
275             # Precision.
276              
277 37 100       63 if ($param =~ /^p(recision)?$/) {
278 2         6 push @import, 'precision', shift();
279 2         16 next;
280             }
281              
282             # Rounding mode.
283              
284 35 50       67 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       112 if ($param =~ /^(l|lib|try|only)$/) {
292 9 100       27 push @import, $param eq 'l' ? 'lib' : $param;
293 9 50       20 push @import, shift() if @_;
294 9         22 next;
295             }
296              
297 26 50       52 if ($param =~ /^(v|version)$/) {
298 0         0 $ver = 1;
299 0         0 next;
300             }
301              
302 26 50       48 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       61 if ($param =~ /^(PI|e|bexp|bpi|hex|oct)\z/) {
310 6         8 push @a, $param;
311 6         14 next;
312             }
313              
314 20         1908 croak("Unknown option '$param'");
315             }
316              
317 38         2016 eval "require $obj_class";
318 38 50       659507 die $@ if $@;
319 38         174 $obj_class -> import(@import);
320              
321 38 50       188334 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         1347 $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   60222 my $str = shift;
340 91         252 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   9506 _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   14108 my $str = shift;
360 10 100       94 return $obj_class -> new($str) if $str =~ /^0[XxBb]/;
361 3         15 $obj_class -> from_oct($str);
362 38         303 };
363             }
364              
365 11     11 1 6195 sub inf () { $obj_class -> binf(); }
366 10     10 1 7161 sub NaN () { $obj_class -> bnan(); }
367              
368             # This should depend on the current accuracy/precision. Fixme!
369 1     1 1 8763 sub PI () { $obj_class -> new('3.141592653589793238462643383279502884197'); }
370 1     1 1 71 sub e () { $obj_class -> new('2.718281828459045235360287471352662497757'); }
371              
372             sub bpi ($) {
373 1     1 1 1959 my $up = Math::BigFloat -> upgrade(); # get current upgrading, if any ...
374 1         11 Math::BigFloat -> upgrade(undef); # ... and disable
375 1         9 my $x = Math::BigFloat -> bpi(@_);
376 1         330 Math::BigFloat -> upgrade($up); # reset the upgrading
377 1         11 return $obj_class -> new($x);
378             }
379              
380             sub bexp ($$) {
381 1     1 1 7386 my $up = Math::BigFloat -> upgrade(); # get current upgrading, if any ...
382 1         14 Math::BigFloat -> upgrade(undef); # ... and disable
383 1         10 my $x = Math::BigFloat -> new(shift);
384 1         169 $x -> bexp(@_);
385 1         581 Math::BigFloat -> upgrade($up); # reset the upgrading
386 1         9 return $obj_class -> new($x);
387             }
388              
389             1;
390              
391             __END__