8 @EXPORT = qw( inf NaN );
13 ##############################################################################
15 # These are all alike, and thus faked by AUTOLOAD
17 my @faked = qw/round_mode accuracy precision div_scale/;
18 use vars qw/$VERSION $AUTOLOAD $_lite/; # _lite for testsuite
24 $name =~ s/.*:://; # split package
26 foreach my $n (@faked)
30 *{"bigrat::$name"} = sub
36 Math::BigInt->$name($_[0]);
37 Math::BigFloat->$name($_[0]);
38 return Math::BigRat->$name($_[0]);
40 return Math::BigInt->$name();
46 # delayed load of Carp and avoid recursion
48 Carp::croak ("Can't call bigrat\-\>$name, not a valid method");
53 $Math::BigInt::upgrade;
58 # this takes a binary/hexadecimal/octal constant string and returns it
59 # as string suitable for new. Basically it converts octal to decimal, and
60 # passes every thing else unmodified back.
63 return Math::BigInt->new($string) if $string =~ /^0[bx]/;
65 # so it must be an octal constant
66 Math::BigInt->from_oct($string);
71 $^H{bigrat} = undef; # no longer in effect
72 overload::remove_constant('binary','','float','','integer');
77 my $level = shift || 0;
78 my $hinthash = (caller($level))[10];
86 # see also bignum->import() for additional comments
88 $^H{bigrat} = 1; # we are in effect
91 my $lib = ''; my $lib_kind = 'try'; my $upgrade = 'Math::BigFloat';
93 my @import = ( ':constant' ); # drive it w/ constant
94 my @a = @_; my $l = scalar @_; my $j = 0;
96 my ($ver,$trace); # version? trace?
97 for ( my $i = 0; $i < $l ; $i++,$j++ )
99 if ($_[$i] eq 'upgrade')
101 # this causes upgrading
102 $upgrade = $_[$i+1]; # or undef to disable
103 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
104 splice @a, $j, $s; $j -= $s;
106 elsif ($_[$i] =~ /^(l|lib|try|only)$/)
108 # this causes a different low lib to take care...
109 $lib_kind = $1; $lib_kind = 'lib' if $lib_kind eq 'l';
110 $lib = $_[$i+1] || '';
111 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
112 splice @a, $j, $s; $j -= $s; $i++;
114 elsif ($_[$i] =~ /^(a|accuracy)$/)
117 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
118 splice @a, $j, $s; $j -= $s; $i++;
120 elsif ($_[$i] =~ /^(p|precision)$/)
123 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
124 splice @a, $j, $s; $j -= $s; $i++;
126 elsif ($_[$i] =~ /^(v|version)$/)
129 splice @a, $j, 1; $j --;
131 elsif ($_[$i] =~ /^(t|trace)$/)
134 splice @a, $j, 1; $j --;
138 die ("unknown option $_[$i]");
142 $_lite = 0; # using M::BI::L ?
145 require Math::BigInt::Trace; $class = 'Math::BigInt::Trace';
146 $upgrade = 'Math::BigFloat::Trace';
150 # see if we can find Math::BigInt::Lite
151 if (!defined $a && !defined $p) # rounding won't work to well
153 eval 'require Math::BigInt::Lite;';
156 @import = ( ); # :constant in Lite, not MBI
157 Math::BigInt::Lite->import( ':constant' );
158 $_lite= 1; # signal okay
161 require Math::BigInt if $_lite == 0; # not already loaded?
162 $class = 'Math::BigInt'; # regardless of MBIL or not
164 push @import, $lib_kind => $lib if $lib ne '';
165 # Math::BigInt::Trace or plain Math::BigInt
166 $class->import(@import, upgrade => $upgrade);
168 require Math::BigFloat;
169 Math::BigFloat->import( upgrade => 'Math::BigRat', ':constant' );
170 require Math::BigRat;
172 bigrat->accuracy($a) if defined $a;
173 bigrat->precision($p) if defined $p;
176 print "bigrat\t\t\t v$VERSION\n";
177 print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
178 print "Math::BigInt\t\t v$Math::BigInt::VERSION";
179 my $config = Math::BigInt->config();
180 print " lib => $config->{lib} v$config->{lib_version}\n";
181 print "Math::BigFloat\t\t v$Math::BigFloat::VERSION\n";
182 print "Math::BigRat\t\t v$Math::BigRat::VERSION\n";
186 # Take care of octal/hexadecimal constants
187 overload::constant binary => sub { _binary_constant(shift) };
189 # if another big* was already loaded:
190 my ($package) = caller();
193 if (!defined *{"${package}::inf"})
195 $self->export_to_level(1,$self,@a); # export inf and NaN
199 sub inf () { Math::BigInt->binf(); }
200 sub NaN () { Math::BigInt->bnan(); }
208 bigrat - Transparent BigNumber/BigRational support for Perl
214 print 2 + 4.5,"\n"; # BigFloat 6.5
215 print 1/3 + 1/4,"\n"; # produces 7/12
219 print 1/3,"\n"; # 0.33333...
224 All operators (including basic math operations) are overloaded. Integer and
225 floating-point constants are created as proper BigInts or BigFloats,
228 Other than L<bignum>, this module upgrades to Math::BigRat, meaning that
229 instead of 2.5 you will get 2+1/2 as output.
233 C<bigrat> is just a thin wrapper around various modules of the Math::BigInt
234 family. Think of it as the head of the family, who runs the shop, and orders
235 the others to do the work.
237 The following modules are currently used by bignum:
239 Math::BigInt::Lite (for speed, and only if it is loadable)
246 Math with the numbers is done (by default) by a module called
247 Math::BigInt::Calc. This is equivalent to saying:
249 use bigrat lib => 'Calc';
251 You can change this by using:
253 use bignum lib => 'GMP';
255 The following would first try to find Math::BigInt::Foo, then
256 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
258 use bigrat lib => 'Foo,Math::BigInt::Bar';
260 Using C<lib> warns if none of the specified libraries can be found and
261 L<Math::BigInt> did fall back to one of the default libraries.
262 To supress this warning, use C<try> instead:
264 use bignum try => 'GMP';
266 If you want the code to die instead of falling back, use C<only> instead:
268 use bignum only => 'GMP';
270 Please see respective module documentation for further details.
274 The sign is either '+', '-', 'NaN', '+inf' or '-inf'.
276 A sign of 'NaN' is used to represent the result when input arguments are not
277 numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
278 minus infinity. You will get '+inf' when dividing a positive number by 0, and
279 '-inf' when dividing any negative number by 0.
283 Since all numbers are not objects, you can use all functions that are part of
284 the BigInt or BigFloat API. It is wise to use only the bxxx() notation, and not
285 the fxxx() notation, though. This makes you independed on the fact that the
286 underlying object might morph into a different class than BigFloat.
292 A shortcut to return Math::BigInt->binf(). Useful because Perl does not always
293 handle bareword C<inf> properly.
297 A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always
298 handle bareword C<NaN> properly.
302 Return the class that numbers are upgraded to, is in fact returning
303 C<$Math::BigInt::upgrade>.
309 print "in effect\n" if bigrat::in_effect; # true
312 print "in effect\n" if bigrat::in_effect; # false
315 Returns true or false if C<bigrat> is in effect in the current scope.
317 This method only works on Perl v5.9.4 or later.
323 Math with the numbers is done (by default) by a module called
327 But a warning is in order. When using the following to make a copy of a number,
328 only a shallow copy will be made.
333 If you want to make a real copy, use the following:
337 Using the copy or the original with overloaded math is okay, e.g. the
341 print $x + 1, " ", $y,"\n"; # prints 10 9
343 but calling any method that modifies the number directly will result in
344 B<both> the original and the copy being destroyed:
347 print $x->badd(1), " ", $y,"\n"; # prints 10 10
350 print $x->binc(1), " ", $y,"\n"; # prints 10 10
353 print $x->bmul(2), " ", $y,"\n"; # prints 18 18
355 Using methods that do not modify, but testthe contents works:
358 $z = 9 if $x->is_zero(); # works fine
360 See the documentation about the copy constructor and C<=> in overload, as
361 well as the documentation in BigInt for further details.
365 bignum recognizes some options that can be passed while loading it via use.
366 The options can (currently) be either a single letter form, or the long form.
367 The following options exist:
373 This sets the accuracy for all math operations. The argument must be greater
374 than or equal to zero. See Math::BigInt's bround() function for details.
376 perl -Mbigrat=a,50 -le 'print sqrt(20)'
378 Note that setting precision and accurary at the same time is not possible.
382 This sets the precision for all math operations. The argument can be any
383 integer. Negative values mean a fixed number of digits after the dot, while
384 a positive value rounds to this digit left from the dot. 0 or 1 mean round to
385 integer. See Math::BigInt's bfround() function for details.
387 perl -Mbigrat=p,-50 -le 'print sqrt(20)'
389 Note that setting precision and accurary at the same time is not possible.
393 This enables a trace mode and is primarily for debugging bignum or
394 Math::BigInt/Math::BigFloat.
398 Load a different math lib, see L<MATH LIBRARY>.
400 perl -Mbigrat=l,GMP -e 'print 2 ** 512'
402 Currently there is no way to specify more than one library on the command
403 line. This means the following does not work:
405 perl -Mbignum=l,GMP,Pari -e 'print 2 ** 512'
407 This will be hopefully fixed soon ;)
411 This prints out the name and version of all modules used and then exits.
419 perl -Mbigrat -le 'print sqrt(33)'
420 perl -Mbigrat -le 'print 2*255'
421 perl -Mbigrat -le 'print 4.5+2*255'
422 perl -Mbigrat -le 'print 3/7 + 5/7 + 8/3'
423 perl -Mbigrat -le 'print 12->is_odd()';
424 perl -Mbignum=l,GMP -le 'print 7 ** 7777'
428 This program is free software; you may redistribute it and/or modify it under
429 the same terms as Perl itself.
433 Especially L<bignum>.
435 L<Math::BigFloat>, L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
436 as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
440 (C) by Tels L<http://bloodgate.com/> in early 2002 - 2007.