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");
51 $Math::BigInt::upgrade;
56 # this takes a binary/hexadecimal/octal constant string and returns it
57 # as string suitable for new. Basically it converts octal to decimal, and
58 # passes every thing else unmodified back.
61 return Math::BigInt->new($string) if $string =~ /^0[bx]/;
63 # so it must be an octal constant
64 Math::BigInt->from_oct($string);
69 # this takes a floating point constant string and returns it truncated to
70 # integer. For instance, '4.5' => '4', '1.234e2' => '123' etc
73 # some simple cases first
74 return $float if ($float =~ /^[+-]?[0-9]+$/); # '+123','-1','0' etc
76 if ($float =~ /^[+-]?[0-9]+\.?[eE]\+?[0-9]+$/); # 123e2, 123.e+2
77 return '0' if ($float =~ /^[+-]?[0]*\.[0-9]+$/); # .2, 0.2, -.1
78 if ($float =~ /^[+-]?[0-9]+\.[0-9]*$/) # 1., 1.23, -1.2 etc
83 my ($mis,$miv,$mfv,$es,$ev) = Math::BigInt::_split($float);
84 return $float if !defined $mis; # doesn't look like a number to me
86 my $sign = $$mis; $sign = '' if $sign eq '+';
89 # ignore fraction part entirely
90 if ($ec >= length($$miv)) # 123.23E-4
94 return $sign . substr ($$miv,0,length($$miv)-$ec); # 1234.45E-2 = 12
97 if ($ec >= length($$mfv))
100 return $sign.$$miv.$$mfv if $ec == 0; # 123.45E+2 => 12345
101 return $sign.$$miv.$$mfv.'E'.$ec; # 123.45e+3 => 12345e1
103 $mfv = substr($$mfv,0,$ec);
104 $sign.$$miv.$mfv; # 123.45e+1 => 1234
109 $^H{bigint} = undef; # no longer in effect
110 overload::remove_constant('binary','','float','','integer');
115 my $level = shift || 0;
116 my $hinthash = (caller($level))[10];
124 $^H{bigint} = 1; # we are in effect
127 my $lib = ''; my $lib_kind = 'try';
129 my @import = ( ':constant' ); # drive it w/ constant
130 my @a = @_; my $l = scalar @_; my $j = 0;
131 my ($ver,$trace); # version? trace?
132 my ($a,$p); # accuracy, precision
133 for ( my $i = 0; $i < $l ; $i++,$j++ )
135 if ($_[$i] =~ /^(l|lib|try|only)$/)
137 # this causes a different low lib to take care...
138 $lib_kind = $1; $lib_kind = 'lib' if $lib_kind eq 'l';
139 $lib = $_[$i+1] || '';
140 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
141 splice @a, $j, $s; $j -= $s; $i++;
143 elsif ($_[$i] =~ /^(a|accuracy)$/)
146 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
147 splice @a, $j, $s; $j -= $s; $i++;
149 elsif ($_[$i] =~ /^(p|precision)$/)
152 my $s = 2; $s = 1 if @a-$j < 2; # avoid "can not modify non-existant..."
153 splice @a, $j, $s; $j -= $s; $i++;
155 elsif ($_[$i] =~ /^(v|version)$/)
158 splice @a, $j, 1; $j --;
160 elsif ($_[$i] =~ /^(t|trace)$/)
163 splice @a, $j, 1; $j --;
165 else { die "unknown option $_[$i]"; }
168 $_lite = 0; # using M::BI::L ?
171 require Math::BigInt::Trace; $class = 'Math::BigInt::Trace';
175 # see if we can find Math::BigInt::Lite
176 if (!defined $a && !defined $p) # rounding won't work to well
178 eval 'require Math::BigInt::Lite;';
181 @import = ( ); # :constant in Lite, not MBI
182 Math::BigInt::Lite->import( ':constant' );
183 $_lite= 1; # signal okay
186 require Math::BigInt if $_lite == 0; # not already loaded?
187 $class = 'Math::BigInt'; # regardless of MBIL or not
189 push @import, $lib_kind => $lib if $lib ne '';
190 # Math::BigInt::Trace or plain Math::BigInt
191 $class->import(@import);
193 bigint->accuracy($a) if defined $a;
194 bigint->precision($p) if defined $p;
197 print "bigint\t\t\t v$VERSION\n";
198 print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $_lite;
199 print "Math::BigInt\t\t v$Math::BigInt::VERSION";
200 my $config = Math::BigInt->config();
201 print " lib => $config->{lib} v$config->{lib_version}\n";
204 # we take care of floating point constants, since BigFloat isn't available
205 # and BigInt doesn't like them:
206 overload::constant float => sub { Math::BigInt->new( _float_constant(shift) ); };
207 # Take care of octal/hexadecimal constants
208 overload::constant binary => sub { _binary_constant(shift) };
210 # if another big* was already loaded:
211 my ($package) = caller();
214 if (!defined *{"${package}::inf"})
216 $self->export_to_level(1,$self,@a); # export inf and NaN
220 sub inf () { Math::BigInt->binf(); }
221 sub NaN () { Math::BigInt->bnan(); }
229 bigint - Transparent BigInteger support for Perl
235 $x = 2 + 4.5,"\n"; # BigInt 6
236 print 2 ** 512,"\n"; # really is what you think it is
237 print inf + 42,"\n"; # inf
238 print NaN * 7,"\n"; # NaN
242 print 2 ** 256,"\n"; # a normal Perl scalar now
247 All operators (including basic math operations) are overloaded. Integer
248 constants are created as proper BigInts.
250 Floating point constants are truncated to integer. All results are also
255 bigint recognizes some options that can be passed while loading it via use.
256 The options can (currently) be either a single letter form, or the long form.
257 The following options exist:
263 This sets the accuracy for all math operations. The argument must be greater
264 than or equal to zero. See Math::BigInt's bround() function for details.
266 perl -Mbigint=a,2 -le 'print 12345+1'
268 Note that setting precision and accurary at the same time is not possible.
272 This sets the precision for all math operations. The argument can be any
273 integer. Negative values mean a fixed number of digits after the dot, and
274 are <B>ignored</B> since all operations happen in integer space.
275 A positive value rounds to this digit left from the dot. 0 or 1 mean round to
276 integer and are ignore like negative values.
278 See Math::BigInt's bfround() function for details.
280 perl -Mbignum=p,5 -le 'print 123456789+123'
282 Note that setting precision and accurary at the same time is not possible.
286 This enables a trace mode and is primarily for debugging bigint or
289 =item l, lib, try or only
291 Load a different math lib, see L<Math Library>.
293 perl -Mbigint=lib,GMP -e 'print 2 ** 512'
294 perl -Mbigint=try,GMP -e 'print 2 ** 512'
295 perl -Mbigint=only,GMP -e 'print 2 ** 512'
297 Currently there is no way to specify more than one library on the command
298 line. This means the following does not work:
300 perl -Mbignum=l,GMP,Pari -e 'print 2 ** 512'
302 This will be hopefully fixed soon ;)
306 This prints out the name and version of all modules used and then exits.
314 Math with the numbers is done (by default) by a module called
315 Math::BigInt::Calc. This is equivalent to saying:
317 use bigint lib => 'Calc';
319 You can change this by using:
321 use bignum lib => 'GMP';
323 The following would first try to find Math::BigInt::Foo, then
324 Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
326 use bigint lib => 'Foo,Math::BigInt::Bar';
328 Using C<lib> warns if none of the specified libraries can be found and
329 L<Math::BigInt> did fall back to one of the default libraries.
330 To supress this warning, use C<try> instead:
332 use bignum try => 'GMP';
334 If you want the code to die instead of falling back, use C<only> instead:
336 use bignum only => 'GMP';
338 Please see respective module documentation for further details.
340 =head2 Internal Format
342 The numbers are stored as objects, and their internals might change at anytime,
343 especially between math operations. The objects also might belong to different
344 classes, like Math::BigInt, or Math::BigInt::Lite. Mixing them together, even
345 with normal scalars is not extraordinary, but normal and expected.
347 You should not depend on the internal format, all accesses must go through
348 accessor methods. E.g. looking at $x->{sign} is not a good idea since there
349 is no guaranty that the object in question has such a hash key, nor is a hash
354 The sign is either '+', '-', 'NaN', '+inf' or '-inf'.
355 You can access it with the sign() method.
357 A sign of 'NaN' is used to represent the result when input arguments are not
358 numbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively
359 minus infinity. You will get '+inf' when dividing a positive number by 0, and
360 '-inf' when dividing any negative number by 0.
364 Since all numbers are now objects, you can use all functions that are part of
365 the BigInt API. You can only use the bxxx() notation, and not the fxxx()
372 A shortcut to return Math::BigInt->binf(). Useful because Perl does not always
373 handle bareword C<inf> properly.
377 A shortcut to return Math::BigInt->bnan(). Useful because Perl does not always
378 handle bareword C<NaN> properly.
382 Return the class that numbers are upgraded to, is in fact returning
383 C<$Math::BigInt::upgrade>.
389 print "in effect\n" if bigint::in_effect; # true
392 print "in effect\n" if bigint::in_effect; # false
395 Returns true or false if C<bigint> is in effect in the current scope.
397 This method only works on Perl v5.9.4 or later.
403 Math with the numbers is done (by default) by a module called
407 But a warning is in order. When using the following to make a copy of a number,
408 only a shallow copy will be made.
413 Using the copy or the original with overloaded math is okay, e.g. the
417 print $x + 1, " ", $y,"\n"; # prints 10 9
419 but calling any method that modifies the number directly will result in
420 B<both> the original and the copy being destroyed:
423 print $x->badd(1), " ", $y,"\n"; # prints 10 10
426 print $x->binc(1), " ", $y,"\n"; # prints 10 10
429 print $x->bmul(2), " ", $y,"\n"; # prints 18 18
431 Using methods that do not modify, but testthe contents works:
434 $z = 9 if $x->is_zero(); # works fine
436 See the documentation about the copy constructor and C<=> in overload, as
437 well as the documentation in BigInt for further details.
441 C<bigint> is just a thin wrapper around various modules of the Math::BigInt
442 family. Think of it as the head of the family, who runs the shop, and orders
443 the others to do the work.
445 The following modules are currently used by bigint:
447 Math::BigInt::Lite (for speed, and only if it is loadable)
452 Some cool command line examples to impress the Python crowd ;) You might want
453 to compare them to the results under -Mbignum or -Mbigrat:
455 perl -Mbigint -le 'print sqrt(33)'
456 perl -Mbigint -le 'print 2*255'
457 perl -Mbigint -le 'print 4.5+2*255'
458 perl -Mbigint -le 'print 3/7 + 5/7 + 8/3'
459 perl -Mbigint -le 'print 123->is_odd()'
460 perl -Mbigint -le 'print log(2)'
461 perl -Mbigint -le 'print 2 ** 0.5'
462 perl -Mbigint=a,65 -le 'print 2 ** 0.2'
463 perl -Mbignum=a,65,l,GMP -le 'print 7 ** 7777'
467 This program is free software; you may redistribute it and/or modify it under
468 the same terms as Perl itself.
472 Especially L<bigrat> as in C<perl -Mbigrat -le 'print 1/3+1/4'> and
473 L<bignum> as in C<perl -Mbignum -le 'print sqrt(2)'>.
475 L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well
476 as L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.
480 (C) by Tels L<http://bloodgate.com/> in early 2002 - 2007.