8 @EXPORT = qw( inf NaN );
12 ##############################################################################
14 # These are all alike, and thus faked by AUTOLOAD
16 my @faked = qw/round_mode accuracy precision div_scale/;
17 use vars qw/$VERSION $AUTOLOAD $_lite/; # _lite for testsuite
23 $name =~ s/.*:://; # split package
25 foreach my $n (@faked)
29 *{"bigrat::$name"} = sub
35 Math::BigInt->$name($_[0]);
36 Math::BigFloat->$name($_[0]);
37 return Math::BigRat->$name($_[0]);
39 return Math::BigInt->$name();
45 # delayed load of Carp and avoid recursion
47 Carp::croak ("Can't call bigrat\-\>$name, not a valid method");
56 # $Math::BigInt::upgrade = $_[0];
57 # $Math::BigFloat::upgrade = $_[0];
59 return $Math::BigInt::upgrade;
66 # see also bignum->import() for additional comments
69 my $lib = 'Calc'; my $upgrade = 'Math::BigFloat';
71 my @import = ( ':constant' ); # drive it w/ constant
72 my @a = @_; my $l = scalar @_; my $j = 0;
74 my ($ver,$trace); # version? trace?
75 for ( my $i = 0; $i < $l ; $i++,$j++ )
77 if ($_[$i] eq 'upgrade')
79 # this causes upgrading
80 $upgrade = $_[$i+1]; # or undef to disable
81 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
82 splice @a, $j, $s; $j -= $s;
84 elsif ($_[$i] =~ /^(l|lib)$/)
86 # this causes a different low lib to take care...
87 $lib = $_[$i+1] || '';
88 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
89 splice @a, $j, $s; $j -= $s; $i++;
91 elsif ($_[$i] =~ /^(a|accuracy)$/)
94 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
95 splice @a, $j, $s; $j -= $s; $i++;
97 elsif ($_[$i] =~ /^(p|precision)$/)
100 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
101 splice @a, $j, $s; $j -= $s; $i++;
103 elsif ($_[$i] =~ /^(v|version)$/)
106 splice @a, $j, 1; $j --;
108 elsif ($_[$i] =~ /^(t|trace)$/)
111 splice @a, $j, 1; $j --;
115 die ("unknown option $_[$i]");
119 $_lite = 0; # using M::BI::L ?
122 require Math::BigInt::Trace; $class = 'Math::BigInt::Trace';
123 $upgrade = 'Math::BigFloat::Trace';
127 # see if we can find Math::BigInt::Lite
128 if (!defined $a && !defined $p) # rounding won't work to well
130 eval 'require Math::BigInt::Lite;';
133 @import = ( ); # :constant in Lite, not MBI
134 Math::BigInt::Lite->import( ':constant' );
135 $_lite= 1; # signal okay
138 require Math::BigInt if $_lite == 0; # not already loaded?
139 $class = 'Math::BigInt'; # regardless of MBIL or not
141 # Math::BigInt::Trace or plain Math::BigInt
142 $class->import(@import, upgrade => $upgrade, lib => $lib);
144 require Math::BigFloat;
145 Math::BigFloat->import( upgrade => 'Math::BigRat', ':constant' );
146 require Math::BigRat;
148 bigrat->accuracy($a) if defined $a;
149 bigrat->precision($p) if defined $p;
152 print "bigrat\t\t\t v$VERSION\n";
153 print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
154 print "Math::BigInt\t\t v$Math::BigInt::VERSION";
155 my $config = Math::BigInt->config();
156 print " lib => $config->{lib} v$config->{lib_version}\n";
157 print "Math::BigFloat\t\t v$Math::BigFloat::VERSION\n";
158 print "Math::BigRat\t\t v$Math::BigRat::VERSION\n";
161 $self->export_to_level(1,$self,@a); # export inf and NaN
164 sub inf () { Math::BigInt->binf(); }
165 sub NaN () { Math::BigInt->bnan(); }
173 bigrat - Transparent BigNumber/BigRational support for Perl
179 $x = 2 + 4.5,"\n"; # BigFloat 6.5
180 print 1/3 + 1/4,"\n"; # produces 7/12
184 All operators (inlcuding basic math operations) are overloaded. Integer and
185 floating-point constants are created as proper BigInts or BigFloats,
188 Other than L<bignum>, this module upgrades to Math::BigRat, meaning that
189 instead of 2.5 you will get 2+1/2 as output.
193 C<bigrat> is just a thin wrapper around various modules of the Math::BigInt
194 family. Think of it as the head of the family, who runs the shop, and orders
195 the others to do the work.
197 The following modules are currently used by bignum:
199 Math::BigInt::Lite (for speed, and only if it is loadable)
206 Math with the numbers is done (by default) by a module called
207 Math::BigInt::Calc. This is equivalent to saying:
209 use bigrat lib => 'Calc';
211 You can change this by using:
213 use bigrat lib => 'BitVect';
215 The following would first try to find Math::BigInt::Foo, then
216 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
218 use bigrat lib => 'Foo,Math::BigInt::Bar';
220 Please see respective module documentation for further details.
224 The sign is either '+', '-', 'NaN', '+inf' or '-inf'.
226 A sign of 'NaN' is used to represent the result when input arguments are not
227 numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
228 minus infinity. You will get '+inf' when dividing a positive number by 0, and
229 '-inf' when dividing any negative number by 0.
233 Since all numbers are not objects, you can use all functions that are part of
234 the BigInt or BigFloat API. It is wise to use only the bxxx() notation, and not
235 the fxxx() notation, though. This makes you independed on the fact that the
236 underlying object might morph into a different class than BigFloat.
240 But a warning is in order. When using the following to make a copy of a number,
241 only a shallow copy will be made.
246 If you want to make a real copy, use the following:
250 Using the copy or the original with overloaded math is okay, e.g. the
254 print $x + 1, " ", $y,"\n"; # prints 10 9
256 but calling any method that modifies the number directly will result in
257 B<both> the original and the copy beeing destroyed:
260 print $x->badd(1), " ", $y,"\n"; # prints 10 10
263 print $x->binc(1), " ", $y,"\n"; # prints 10 10
266 print $x->bmul(2), " ", $y,"\n"; # prints 18 18
268 Using methods that do not modify, but testthe contents works:
271 $z = 9 if $x->is_zero(); # works fine
273 See the documentation about the copy constructor and C<=> in overload, as
274 well as the documentation in BigInt for further details.
278 bignum recognizes some options that can be passed while loading it via use.
279 The options can (currently) be either a single letter form, or the long form.
280 The following options exist:
286 This sets the accuracy for all math operations. The argument must be greater
287 than or equal to zero. See Math::BigInt's bround() function for details.
289 perl -Mbigrat=a,50 -le 'print sqrt(20)'
293 This sets the precision for all math operations. The argument can be any
294 integer. Negative values mean a fixed number of digits after the dot, while
295 a positive value rounds to this digit left from the dot. 0 or 1 mean round to
296 integer. See Math::BigInt's bfround() function for details.
298 perl -Mbigrat=p,-50 -le 'print sqrt(20)'
302 This enables a trace mode and is primarily for debugging bignum or
303 Math::BigInt/Math::BigFloat.
307 Load a different math lib, see L<MATH LIBRARY>.
309 perl -Mbigrat=l,GMP -e 'print 2 ** 512'
311 Currently there is no way to specify more than one library on the command
312 line. This will be hopefully fixed soon ;)
316 This prints out the name and version of all modules used and then exits.
322 perl -Mbigrat -le 'print sqrt(33)'
323 perl -Mbigrat -le 'print 2*255'
324 perl -Mbigrat -le 'print 4.5+2*255'
325 perl -Mbigrat -le 'print 3/7 + 5/7 + 8/3'
326 perl -Mbigrat -le 'print 12->is_odd()';
330 This program is free software; you may redistribute it and/or modify it under
331 the same terms as Perl itself.
335 Especially L<bignum>.
337 L<Math::BigFloat>, L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
338 as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
342 (C) by Tels L<http://bloodgate.com/> in early 2002 - 2005.