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';
126 # print STDERR "Loading $class";
130 # see if we can find Math::BigInt::Lite
131 if (!defined $a && !defined $p) # rounding won't work to well
133 eval 'require Math::BigInt::Lite;';
136 @import = ( ); # :constant in Lite, not MBI
137 Math::BigInt::Lite->import( ':constant' );
138 $_lite= 1; # signal okay
141 require Math::BigInt if $_lite == 0; # not already loaded?
142 $class = 'Math::BigInt'; # regardless of MBIL or not
144 # Math::BigInt::Trace or plain Math::BigInt
145 $class->import(@import, upgrade => $upgrade, lib => $lib);
149 require Math::BigFloat::Trace; $class = 'Math::BigFloat::Trace';
150 $downgrade = 'Math::BigInt::Trace';
151 # print STDERR "Loading $class";
155 require Math::BigFloat; $class = 'Math::BigFloat';
157 $class->import(':constant','downgrade',$downgrade);
159 bignum->accuracy($a) if defined $a;
160 bignum->precision($p) if defined $p;
163 print "bignum\t\t\t v$VERSION\n";
164 print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
165 print "Math::BigInt\t\t v$Math::BigInt::VERSION";
166 my $config = Math::BigInt->config();
167 print " lib => $config->{lib} v$config->{lib_version}\n";
168 print "Math::BigFloat\t\t v$Math::BigFloat::VERSION\n";
179 bignum - Transparent BigNumber support for Perl
185 $x = 2 + 4.5,"\n"; # BigFloat 6.5
186 print 2 ** 512 * 0.1; # really is what you think it is
190 All operators (including basic math operations) are overloaded. Integer and
191 floating-point constants are created as proper BigInts or BigFloats,
196 bignum recognizes some options that can be passed while loading it via use.
197 The options can (currently) be either a single letter form, or the long form.
198 The following options exist:
204 This sets the accuracy for all math operations. The argument must be greater
205 than or equal to zero. See Math::BigInt's bround() function for details.
207 perl -Mbignum=a,50 -le 'print sqrt(20)'
211 This sets the precision for all math operations. The argument can be any
212 integer. Negative values mean a fixed number of digits after the dot, while
213 a positive value rounds to this digit left from the dot. 0 or 1 mean round to
214 integer. See Math::BigInt's bfround() function for details.
216 perl -Mbignum=p,-50 -le 'print sqrt(20)'
220 This enables a trace mode and is primarily for debugging bignum or
221 Math::BigInt/Math::BigFloat.
225 Load a different math lib, see L<MATH LIBRARY>.
227 perl -Mbignum=l,GMP -e 'print 2 ** 512'
229 Currently there is no way to specify more than one library on the command
230 line. This will be hopefully fixed soon ;)
234 This prints out the name and version of all modules used and then exits.
236 perl -Mbignum=v -e ''
240 Math with the numbers is done (by default) by a module called
241 Math::BigInt::Calc. This is equivalent to saying:
243 use bignum lib => 'Calc';
245 You can change this by using:
247 use bignum lib => 'BitVect';
249 The following would first try to find Math::BigInt::Foo, then
250 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
252 use bignum lib => 'Foo,Math::BigInt::Bar';
254 Please see respective module documentation for further details.
256 =head2 INTERNAL FORMAT
258 The numbers are stored as objects, and their internals might change at anytime,
259 especially between math operations. The objects also might belong to different
260 classes, like Math::BigInt, or Math::BigFLoat. Mixing them together, even
261 with normal scalars is not extraordinary, but normal and expected.
263 You should not depend on the internal format, all accesses must go through
264 accessor methods. E.g. looking at $x->{sign} is not a bright idea since there
265 is no guaranty that the object in question has such a hashkey, nor is a hash
270 The sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored seperately.
271 You can access it with the sign() method.
273 A sign of 'NaN' is used to represent the result when input arguments are not
274 numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
275 minus infinity. You will get '+inf' when dividing a positive number by 0, and
276 '-inf' when dividing any negative number by 0.
280 Since all numbers are now objects, you can use all functions that are part of
281 the BigInt or BigFloat API. It is wise to use only the bxxx() notation, and not
282 the fxxx() notation, though. This makes it possible that the underlying object
283 might morph into a different class than BigFloat.
287 C<bignum> is just a thin wrapper around various modules of the Math::BigInt
288 family. Think of it as the head of the family, who runs the shop, and orders
289 the others to do the work.
291 The following modules are currently used by bignum:
293 Math::BigInt::Lite (for speed, and only if it is loadable)
299 Some cool command line examples to impress the Python crowd ;)
301 perl -Mbignum -le 'print sqrt(33)'
302 perl -Mbignum -le 'print 2*255'
303 perl -Mbignum -le 'print 4.5+2*255'
304 perl -Mbignum -le 'print 3/7 + 5/7 + 8/3'
305 perl -Mbignum -le 'print 123->is_odd()'
306 perl -Mbignum -le 'print log(2)'
307 perl -Mbignum -le 'print 2 ** 0.5'
308 perl -Mbignum=a,65 -le 'print 2 ** 0.2'
312 This program is free software; you may redistribute it and/or modify it under
313 the same terms as Perl itself.
317 Especially L<bigrat> as in C<perl -Mbigrat -le 'print 1/3+1/4'>.
319 L<Math::BigFloat>, L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
320 as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
324 (C) by Tels L<http://bloodgate.com/> in early 2002.