8 @EXPORT = qw( inf NaN );
13 ##############################################################################
15 # These are all alike, and thus faked by AUTOLOAD
17 my @faked = qw/round_mode accuracy precision div_scale/;
18 use vars qw/$VERSION $AUTOLOAD $_lite/; # _lite for testsuite
24 $name =~ s/.*:://; # split package
26 foreach my $n (@faked)
30 *{"bigint::$name"} = sub
36 return Math::BigInt->$name($_[0]);
38 return Math::BigInt->$name();
44 # delayed load of Carp and avoid recursion
46 Carp::croak ("Can't call bigint\-\>$name, not a valid method");
55 # $Math::BigInt::upgrade = $_[0];
57 return $Math::BigInt::upgrade;
62 # this takes a floating point constant string and returns it truncated to
63 # integer. For instance, '4.5' => '4', '1.234e2' => '123' etc
66 # some simple cases first
67 return $float if ($float =~ /^[+-]?[0-9]+$/); # '+123','-1','0' etc
69 if ($float =~ /^[+-]?[0-9]+\.?[eE]\+?[0-9]+$/); # 123e2, 123.e+2
70 return '0' if ($float =~ /^[+-]?[0]*\.[0-9]+$/); # .2, 0.2, -.1
71 if ($float =~ /^[+-]?[0-9]+\.[0-9]*$/) # 1., 1.23, -1.2 etc
76 my ($mis,$miv,$mfv,$es,$ev) = Math::BigInt::_split($float);
77 return $float if !defined $mis; # doesn't look like a number to me
79 my $sign = $$mis; $sign = '' if $sign eq '+';
82 # ignore fraction part entirely
83 if ($ec >= length($$miv)) # 123.23E-4
87 return $sign . substr ($$miv,0,length($$miv)-$ec); # 1234.45E-2 = 12
90 if ($ec >= length($$mfv))
93 return $sign.$$miv.$$mfv if $ec == 0; # 123.45E+2 => 12345
94 return $sign.$$miv.$$mfv.'E'.$ec; # 123.45e+3 => 12345e1
96 $mfv = substr($$mfv,0,$ec);
97 return $sign.$$miv.$mfv; # 123.45e+1 => 1234
107 my @import = ( ':constant' ); # drive it w/ constant
108 my @a = @_; my $l = scalar @_; my $j = 0;
109 my ($ver,$trace); # version? trace?
110 my ($a,$p); # accuracy, precision
111 for ( my $i = 0; $i < $l ; $i++,$j++ )
113 if ($_[$i] =~ /^(l|lib)$/)
115 # this causes a different low lib to take care...
116 $lib = $_[$i+1] || '';
117 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
118 splice @a, $j, $s; $j -= $s; $i++;
120 elsif ($_[$i] =~ /^(a|accuracy)$/)
123 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
124 splice @a, $j, $s; $j -= $s; $i++;
126 elsif ($_[$i] =~ /^(p|precision)$/)
129 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
130 splice @a, $j, $s; $j -= $s; $i++;
132 elsif ($_[$i] =~ /^(v|version)$/)
135 splice @a, $j, 1; $j --;
137 elsif ($_[$i] =~ /^(t|trace)$/)
140 splice @a, $j, 1; $j --;
142 else { die "unknown option $_[$i]"; }
145 $_lite = 0; # using M::BI::L ?
148 require Math::BigInt::Trace; $class = 'Math::BigInt::Trace';
152 # see if we can find Math::BigInt::Lite
153 if (!defined $a && !defined $p) # rounding won't work to well
155 eval 'require Math::BigInt::Lite;';
158 @import = ( ); # :constant in Lite, not MBI
159 Math::BigInt::Lite->import( ':constant' );
160 $_lite= 1; # signal okay
163 require Math::BigInt if $_lite == 0; # not already loaded?
164 $class = 'Math::BigInt'; # regardless of MBIL or not
166 # Math::BigInt::Trace or plain Math::BigInt
167 $class->import(@import, lib => $lib);
169 bigint->accuracy($a) if defined $a;
170 bigint->precision($p) if defined $p;
173 print "bigint\t\t\t v$VERSION\n";
174 print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
175 print "Math::BigInt\t\t v$Math::BigInt::VERSION";
176 my $config = Math::BigInt->config();
177 print " lib => $config->{lib} v$config->{lib_version}\n";
180 # we take care of floating point constants, since BigFloat isn't available
181 # and BigInt doesn't like them:
182 overload::constant float => sub { Math::BigInt->new( _constant(shift) ); };
184 $self->export_to_level(1,$self,@a); # export inf and NaN
187 sub inf () { Math::BigInt->binf(); }
188 sub NaN () { Math::BigInt->bnan(); }
196 bigint - Transparent BigInteger support for Perl
202 $x = 2 + 4.5,"\n"; # BigInt 6
203 print 2 ** 512,"\n"; # really is what you think it is
204 print inf + 42,"\n"; # inf
205 print NaN * 7,"\n"; # NaN
209 All operators (including basic math operations) are overloaded. Integer
210 constants are created as proper BigInts.
212 Floating point constants are truncated to integer. All results are also
217 bigint recognizes some options that can be passed while loading it via use.
218 The options can (currently) be either a single letter form, or the long form.
219 The following options exist:
225 This sets the accuracy for all math operations. The argument must be greater
226 than or equal to zero. See Math::BigInt's bround() function for details.
228 perl -Mbigint=a,2 -le 'print 12345+1'
232 This sets the precision for all math operations. The argument can be any
233 integer. Negative values mean a fixed number of digits after the dot, and
234 are <B>ignored</B> since all operations happen in integer space.
235 A positive value rounds to this digit left from the dot. 0 or 1 mean round to
236 integer and are ignore like negative values.
238 See Math::BigInt's bfround() function for details.
240 perl -Mbignum=p,5 -le 'print 123456789+123'
244 This enables a trace mode and is primarily for debugging bigint or
249 Load a different math lib, see L<MATH LIBRARY>.
251 perl -Mbigint=l,GMP -e 'print 2 ** 512'
253 Currently there is no way to specify more than one library on the command
254 line. This will be hopefully fixed soon ;)
258 This prints out the name and version of all modules used and then exits.
260 perl -Mbigint=v -e ''
264 Math with the numbers is done (by default) by a module called
265 Math::BigInt::Calc. This is equivalent to saying:
267 use bigint lib => 'Calc';
269 You can change this by using:
271 use bigint lib => 'BitVect';
273 The following would first try to find Math::BigInt::Foo, then
274 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
276 use bigint lib => 'Foo,Math::BigInt::Bar';
278 Please see respective module documentation for further details.
280 =head2 INTERNAL FORMAT
282 The numbers are stored as objects, and their internals might change at anytime,
283 especially between math operations. The objects also might belong to different
284 classes, like Math::BigInt, or Math::BigInt::Lite. Mixing them together, even
285 with normal scalars is not extraordinary, but normal and expected.
287 You should not depend on the internal format, all accesses must go through
288 accessor methods. E.g. looking at $x->{sign} is not a good idea since there
289 is no guaranty that the object in question has such a hash key, nor is a hash
294 The sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored seperately.
295 You can access it with the sign() method.
297 A sign of 'NaN' is used to represent the result when input arguments are not
298 numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
299 minus infinity. You will get '+inf' when dividing a positive number by 0, and
300 '-inf' when dividing any negative number by 0.
304 Since all numbers are now objects, you can use all functions that are part of
305 the BigInt API. You can only use the bxxx() notation, and not the fxxx()
310 But a warning is in order. When using the following to make a copy of a number,
311 only a shallow copy will be made.
316 Using the copy or the original with overloaded math is okay, e.g. the
320 print $x + 1, " ", $y,"\n"; # prints 10 9
322 but calling any method that modifies the number directly will result in
323 B<both> the original and the copy beeing destroyed:
326 print $x->badd(1), " ", $y,"\n"; # prints 10 10
329 print $x->binc(1), " ", $y,"\n"; # prints 10 10
332 print $x->bmul(2), " ", $y,"\n"; # prints 18 18
334 Using methods that do not modify, but testthe contents works:
337 $z = 9 if $x->is_zero(); # works fine
339 See the documentation about the copy constructor and C<=> in overload, as
340 well as the documentation in BigInt for further details.
344 C<bigint> is just a thin wrapper around various modules of the Math::BigInt
345 family. Think of it as the head of the family, who runs the shop, and orders
346 the others to do the work.
348 The following modules are currently used by bigint:
350 Math::BigInt::Lite (for speed, and only if it is loadable)
355 Some cool command line examples to impress the Python crowd ;) You might want
356 to compare them to the results under -Mbignum or -Mbigrat:
358 perl -Mbigint -le 'print sqrt(33)'
359 perl -Mbigint -le 'print 2*255'
360 perl -Mbigint -le 'print 4.5+2*255'
361 perl -Mbigint -le 'print 3/7 + 5/7 + 8/3'
362 perl -Mbigint -le 'print 123->is_odd()'
363 perl -Mbigint -le 'print log(2)'
364 perl -Mbigint -le 'print 2 ** 0.5'
365 perl -Mbigint=a,65 -le 'print 2 ** 0.2'
369 This program is free software; you may redistribute it and/or modify it under
370 the same terms as Perl itself.
374 Especially L<bigrat> as in C<perl -Mbigrat -le 'print 1/3+1/4'> and
375 L<bignum> as in C<perl -Mbignum -le 'print sqrt(2)'>.
377 L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
378 as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
382 (C) by Tels L<http://bloodgate.com/> in early 2002.