11 ##############################################################################
13 # These are all alike, and thus faked by AUTOLOAD
15 my @faked = qw/round_mode accuracy precision div_scale/;
16 use vars qw/$VERSION $AUTOLOAD $_lite/; # _lite for testsuite
22 $name =~ s/.*:://; # split package
24 foreach my $n (@faked)
28 *{"bigrat::$name"} = sub
34 Math::BigInt->$name($_[0]);
35 Math::BigFloat->$name($_[0]);
37 return Math::BigInt->$name();
43 # delayed load of Carp and avoid recursion
45 Carp::croak ("Can't call bigrat\-\>$name, not a valid method");
54 # $Math::BigInt::upgrade = $_[0];
55 # $Math::BigFloat::upgrade = $_[0];
57 return $Math::BigInt::upgrade;
64 # see also bignum->import() for additional comments
67 my $lib = 'Calc'; my $upgrade = 'Math::BigFloat';
69 my @import = ( ':constant' ); # drive it w/ constant
70 my @a = @_; my $l = scalar @_; my $j = 0;
72 my ($ver,$trace); # version? trace?
73 for ( my $i = 0; $i < $l ; $i++,$j++ )
75 if ($_[$i] eq 'upgrade')
77 # this causes upgrading
78 $upgrade = $_[$i+1]; # or undef to disable
79 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
80 splice @a, $j, $s; $j -= $s;
82 elsif ($_[$i] =~ /^(l|lib)$/)
84 # this causes a different low lib to take care...
85 $lib = $_[$i+1] || '';
86 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
87 splice @a, $j, $s; $j -= $s;
89 elsif ($_[$i] =~ /^(v|version)$/)
92 splice @a, $j, 1; $j --;
94 elsif ($_[$i] =~ /^(t|trace)$/)
97 splice @a, $j, 1; $j --;
101 die ("unknown option $_[$i]");
105 $_lite = 0; # using M::BI::L ?
108 require Math::BigInt::Trace; $class = 'Math::BigInt::Trace';
109 $upgrade = 'Math::BigFloat::Trace';
113 # see if we can find Math::BigInt::Lite
114 if (!defined $a && !defined $p) # rounding won't work to well
116 eval 'require Math::BigInt::Lite;';
119 @import = ( ); # :constant in Lite, not MBI
120 Math::BigInt::Lite->import( ':constant' );
121 $_lite= 1; # signal okay
124 require Math::BigInt if $_lite == 0; # not already loaded?
125 $class = 'Math::BigInt'; # regardless of MBIL or not
127 # Math::BigInt::Trace or plain Math::BigInt
128 $class->import(@import, upgrade => $upgrade, lib => $lib);
130 require Math::BigFloat;
131 Math::BigFloat->import( upgrade => 'Math::BigRat', ':constant' );
132 require Math::BigRat;
135 print "bigrat\t\t\t v$VERSION\n";
136 print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
137 print "Math::BigInt\t\t v$Math::BigInt::VERSION";
138 my $config = Math::BigInt->config();
139 print " lib => $config->{lib} v$config->{lib_version}\n";
140 print "Math::BigFloat\t\t v$Math::BigFloat::VERSION\n";
141 print "Math::BigRat\t\t v$Math::BigRat::VERSION\n";
152 bigrat - Transparent BigNumber/BigRational support for Perl
158 $x = 2 + 4.5,"\n"; # BigFloat 6.5
159 print 1/3 + 1/4,"\n"; # produces 7/12
163 All operators (inlcuding basic math operations) are overloaded. Integer and
164 floating-point constants are created as proper BigInts or BigFloats,
167 Other than L<bignum>, this module upgrades to Math::BigRat, meaning that
168 instead of 2.5 you will get 2+1/2 as output.
172 C<bigrat> is just a thin wrapper around various modules of the Math::BigInt
173 family. Think of it as the head of the family, who runs the shop, and orders
174 the others to do the work.
176 The following modules are currently used by bignum:
178 Math::BigInt::Lite (for speed, and only if it is loadable)
185 Math with the numbers is done (by default) by a module called
186 Math::BigInt::Calc. This is equivalent to saying:
188 use bigrat lib => 'Calc';
190 You can change this by using:
192 use bigrat lib => 'BitVect';
194 The following would first try to find Math::BigInt::Foo, then
195 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
197 use bigrat lib => 'Foo,Math::BigInt::Bar';
199 Please see respective module documentation for further details.
203 The sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored seperately.
205 A sign of 'NaN' is used to represent the result when input arguments are not
206 numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
207 minus infinity. You will get '+inf' when dividing a positive number by 0, and
208 '-inf' when dividing any negative number by 0.
212 Since all numbers are not objects, you can use all functions that are part of
213 the BigInt or BigFloat API. It is wise to use only the bxxx() notation, and not
214 the fxxx() notation, though. This makes you independed on the fact that the
215 underlying object might morph into a different class than BigFloat.
219 perl -Mbigrat -le 'print sqrt(33)'
220 perl -Mbigrat -le 'print 2*255'
221 perl -Mbigrat -le 'print 4.5+2*255'
222 perl -Mbigrat -le 'print 3/7 + 5/7 + 8/3'
223 perl -Mbigrat -le 'print 12->is_odd()';
227 This program is free software; you may redistribute it and/or modify it under
228 the same terms as Perl itself.
232 Especially L<bignum>.
234 L<Math::BigFloat>, L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
235 as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
239 (C) by Tels L<http://bloodgate.com/> in early 2002.