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 *{"bignum::$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 bignum\-\>$name, not a valid method");
54 # $Math::BigInt::upgrade = $_[0];
55 # $Math::BigFloat::upgrade = $_[0];
57 return $Math::BigInt::upgrade;
66 my $upgrade = 'Math::BigFloat';
67 my $downgrade = 'Math::BigInt';
69 my @import = ( ':constant' ); # drive it w/ constant
70 my @a = @_; my $l = scalar @_; my $j = 0;
71 my ($ver,$trace); # version? trace?
72 my ($a,$p); # accuracy, precision
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; $i++;
82 elsif ($_[$i] eq 'downgrade')
84 # this causes downgrading
85 $downgrade = $_[$i+1]; # or undef to disable
86 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
87 splice @a, $j, $s; $j -= $s; $i++;
89 elsif ($_[$i] =~ /^(l|lib)$/)
91 # this causes a different low lib to take care...
92 $lib = $_[$i+1] || '';
93 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
94 splice @a, $j, $s; $j -= $s; $i++;
96 elsif ($_[$i] =~ /^(a|accuracy)$/)
99 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
100 splice @a, $j, $s; $j -= $s; $i++;
102 elsif ($_[$i] =~ /^(p|precision)$/)
105 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
106 splice @a, $j, $s; $j -= $s; $i++;
108 elsif ($_[$i] =~ /^(v|version)$/)
111 splice @a, $j, 1; $j --;
113 elsif ($_[$i] =~ /^(t|trace)$/)
116 splice @a, $j, 1; $j --;
118 else { die "unknown option $_[$i]"; }
121 $_lite = 0; # using M::BI::L ?
124 require Math::BigInt::Trace; $class = 'Math::BigInt::Trace';
125 $upgrade = 'Math::BigFloat::Trace';
129 # see if we can find Math::BigInt::Lite
130 if (!defined $a && !defined $p) # rounding won't work to well
132 eval 'require Math::BigInt::Lite;';
135 @import = ( ); # :constant in Lite, not MBI
136 Math::BigInt::Lite->import( ':constant' );
137 $_lite= 1; # signal okay
140 require Math::BigInt if $_lite == 0; # not already loaded?
141 $class = 'Math::BigInt'; # regardless of MBIL or not
143 # Math::BigInt::Trace or plain Math::BigInt
144 $class->import(@import, upgrade => $upgrade, lib => $lib);
148 require Math::BigFloat::Trace; $class = 'Math::BigFloat::Trace';
149 $downgrade = 'Math::BigInt::Trace';
153 require Math::BigFloat; $class = 'Math::BigFloat';
155 $class->import(':constant','downgrade',$downgrade);
157 bignum->accuracy($a) if defined $a;
158 bignum->precision($p) if defined $p;
161 print "bignum\t\t\t v$VERSION\n";
162 print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
163 print "Math::BigInt\t\t v$Math::BigInt::VERSION";
164 my $config = Math::BigInt->config();
165 print " lib => $config->{lib} v$config->{lib_version}\n";
166 print "Math::BigFloat\t\t v$Math::BigFloat::VERSION\n";
177 bignum - Transparent BigNumber support for Perl
183 $x = 2 + 4.5,"\n"; # BigFloat 6.5
184 print 2 ** 512 * 0.1; # really is what you think it is
188 All operators (including basic math operations) are overloaded. Integer and
189 floating-point constants are created as proper BigInts or BigFloats,
194 bignum recognizes some options that can be passed while loading it via use.
195 The options can (currently) be either a single letter form, or the long form.
196 The following options exist:
202 This sets the accuracy for all math operations. The argument must be greater
203 than or equal to zero. See Math::BigInt's bround() function for details.
205 perl -Mbignum=a,50 -le 'print sqrt(20)'
209 This sets the precision for all math operations. The argument can be any
210 integer. Negative values mean a fixed number of digits after the dot, while
211 a positive value rounds to this digit left from the dot. 0 or 1 mean round to
212 integer. See Math::BigInt's bfround() function for details.
214 perl -Mbignum=p,-50 -le 'print sqrt(20)'
218 This enables a trace mode and is primarily for debugging bignum or
219 Math::BigInt/Math::BigFloat.
223 Load a different math lib, see L<MATH LIBRARY>.
225 perl -Mbignum=l,GMP -e 'print 2 ** 512'
227 Currently there is no way to specify more than one library on the command
228 line. This will be hopefully fixed soon ;)
232 This prints out the name and version of all modules used and then exits.
234 perl -Mbignum=v -e ''
238 Math with the numbers is done (by default) by a module called
239 Math::BigInt::Calc. This is equivalent to saying:
241 use bignum lib => 'Calc';
243 You can change this by using:
245 use bignum lib => 'BitVect';
247 The following would first try to find Math::BigInt::Foo, then
248 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
250 use bignum lib => 'Foo,Math::BigInt::Bar';
252 Please see respective module documentation for further details.
254 =head2 INTERNAL FORMAT
256 The numbers are stored as objects, and their internals might change at anytime,
257 especially between math operations. The objects also might belong to different
258 classes, like Math::BigInt, or Math::BigFLoat. Mixing them together, even
259 with normal scalars is not extraordinary, but normal and expected.
261 You should not depend on the internal format, all accesses must go through
262 accessor methods. E.g. looking at $x->{sign} is not a bright idea since there
263 is no guaranty that the object in question has such a hashkey, nor is a hash
268 The sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored seperately.
269 You can access it with the sign() method.
271 A sign of 'NaN' is used to represent the result when input arguments are not
272 numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
273 minus infinity. You will get '+inf' when dividing a positive number by 0, and
274 '-inf' when dividing any negative number by 0.
278 Since all numbers are now objects, you can use all functions that are part of
279 the BigInt or BigFloat API. It is wise to use only the bxxx() notation, and not
280 the fxxx() notation, though. This makes it possible that the underlying object
281 might morph into a different class than BigFloat.
285 C<bignum> is just a thin wrapper around various modules of the Math::BigInt
286 family. Think of it as the head of the family, who runs the shop, and orders
287 the others to do the work.
289 The following modules are currently used by bignum:
291 Math::BigInt::Lite (for speed, and only if it is loadable)
297 Some cool command line examples to impress the Python crowd ;)
299 perl -Mbignum -le 'print sqrt(33)'
300 perl -Mbignum -le 'print 2*255'
301 perl -Mbignum -le 'print 4.5+2*255'
302 perl -Mbignum -le 'print 3/7 + 5/7 + 8/3'
303 perl -Mbignum -le 'print 123->is_odd()'
304 perl -Mbignum -le 'print log(2)'
305 perl -Mbignum -le 'print 2 ** 0.5'
306 perl -Mbignum=a,65 -le 'print 2 ** 0.2'
310 This program is free software; you may redistribute it and/or modify it under
311 the same terms as Perl itself.
315 Especially L<bigrat> as in C<perl -Mbigrat -le 'print 1/3+1/4'>.
317 L<Math::BigFloat>, L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
318 as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
322 (C) by Tels L<http://bloodgate.com/> in early 2002.