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]);
38 return Math::BigInt->$name();
44 # delayed load of Carp and avoid recursion
46 Carp::croak ("Can't call bigrat\-\>$name, not a valid method");
55 # $Math::BigInt::upgrade = $_[0];
56 # $Math::BigFloat::upgrade = $_[0];
58 return $Math::BigInt::upgrade;
65 # see also bignum->import() for additional comments
68 my $lib = 'Calc'; my $upgrade = 'Math::BigFloat';
70 my @import = ( ':constant' ); # drive it w/ constant
71 my @a = @_; my $l = scalar @_; my $j = 0;
73 my ($ver,$trace); # version? trace?
74 for ( my $i = 0; $i < $l ; $i++,$j++ )
76 if ($_[$i] eq 'upgrade')
78 # this causes upgrading
79 $upgrade = $_[$i+1]; # or undef to disable
80 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
81 splice @a, $j, $s; $j -= $s;
83 elsif ($_[$i] =~ /^(l|lib)$/)
85 # this causes a different low lib to take care...
86 $lib = $_[$i+1] || '';
87 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
88 splice @a, $j, $s; $j -= $s;
90 elsif ($_[$i] =~ /^(v|version)$/)
93 splice @a, $j, 1; $j --;
95 elsif ($_[$i] =~ /^(t|trace)$/)
98 splice @a, $j, 1; $j --;
102 die ("unknown option $_[$i]");
106 $_lite = 0; # using M::BI::L ?
109 require Math::BigInt::Trace; $class = 'Math::BigInt::Trace';
110 $upgrade = 'Math::BigFloat::Trace';
114 # see if we can find Math::BigInt::Lite
115 if (!defined $a && !defined $p) # rounding won't work to well
117 eval 'require Math::BigInt::Lite;';
120 @import = ( ); # :constant in Lite, not MBI
121 Math::BigInt::Lite->import( ':constant' );
122 $_lite= 1; # signal okay
125 require Math::BigInt if $_lite == 0; # not already loaded?
126 $class = 'Math::BigInt'; # regardless of MBIL or not
128 # Math::BigInt::Trace or plain Math::BigInt
129 $class->import(@import, upgrade => $upgrade, lib => $lib);
131 require Math::BigFloat;
132 Math::BigFloat->import( upgrade => 'Math::BigRat', ':constant' );
133 require Math::BigRat;
136 print "bigrat\t\t\t v$VERSION\n";
137 print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
138 print "Math::BigInt\t\t v$Math::BigInt::VERSION";
139 my $config = Math::BigInt->config();
140 print " lib => $config->{lib} v$config->{lib_version}\n";
141 print "Math::BigFloat\t\t v$Math::BigFloat::VERSION\n";
142 print "Math::BigRat\t\t v$Math::BigRat::VERSION\n";
145 $self->export_to_level(1,$self,@a); # export inf and NaN
148 sub inf () { Math::BigInt->binf(); }
149 sub NaN () { Math::BigInt->bnan(); }
157 bigrat - Transparent BigNumber/BigRationale support for Perl
163 $x = 2 + 4.5,"\n"; # BigFloat 6.5
164 print 1/3 + 1/4,"\n"; # produces 7/12
168 All operators (inlcuding basic math operations) are overloaded. Integer and
169 floating-point constants are created as proper BigInts or BigFloats,
172 Other than L<bignum>, this module upgrades to Math::BigRat, meaning that
173 instead of 2.5 you will get 2+1/2 as output.
177 C<bigrat> is just a thin wrapper around various modules of the Math::BigInt
178 family. Think of it as the head of the family, who runs the shop, and orders
179 the others to do the work.
181 The following modules are currently used by bignum:
183 Math::BigInt::Lite (for speed, and only if it is loadable)
190 Math with the numbers is done (by default) by a module called
191 Math::BigInt::Calc. This is equivalent to saying:
193 use bigrat lib => 'Calc';
195 You can change this by using:
197 use bigrat lib => 'BitVect';
199 The following would first try to find Math::BigInt::Foo, then
200 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
202 use bigrat lib => 'Foo,Math::BigInt::Bar';
204 Please see respective module documentation for further details.
208 The sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored seperately.
210 A sign of 'NaN' is used to represent the result when input arguments are not
211 numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
212 minus infinity. You will get '+inf' when dividing a positive number by 0, and
213 '-inf' when dividing any negative number by 0.
217 Since all numbers are not objects, you can use all functions that are part of
218 the BigInt or BigFloat API. It is wise to use only the bxxx() notation, and not
219 the fxxx() notation, though. This makes you independed on the fact that the
220 underlying object might morph into a different class than BigFloat.
224 perl -Mbigrat -le 'print sqrt(33)'
225 perl -Mbigrat -le 'print 2*255'
226 perl -Mbigrat -le 'print 4.5+2*255'
227 perl -Mbigrat -le 'print 3/7 + 5/7 + 8/3'
228 perl -Mbigrat -le 'print 12->is_odd()';
232 This program is free software; you may redistribute it and/or modify it under
233 the same terms as Perl itself.
237 Especially L<bignum>.
239 L<Math::BigFloat>, L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
240 as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
244 (C) by Tels L<http://bloodgate.com/> in early 2002.