1N/A############################################################################## 1N/A# These are all alike, and thus faked by AUTOLOAD 1N/A *{
"bignum::$name"} =
sub 1N/A # delayed load of Carp and avoid recursion 1N/A Carp::
croak (
"Can't call bignum\-\>$name, not a valid method");
1N/A# $Math::BigInt::upgrade = $_[0]; 1N/A# $Math::BigFloat::upgrade = $_[0]; 1N/A my @
import = (
':constant' );
# drive it w/ constant 1N/A my @a = @_;
my $l =
scalar @_;
my $j =
0;
1N/A my ($a,$p);
# accuracy, precision 1N/A for (
my $i =
0; $i < $l ; $i++,$j++ )
1N/A if ($_[$i]
eq 'upgrade')
1N/A # this causes upgrading 1N/A my $s =
2; $s =
1 if @a-$j <
2;
# avoid "can not modify non-existant..." 1N/A splice @a, $j, $s; $j -= $s; $i++;
1N/A elsif ($_[$i]
eq 'downgrade')
1N/A # this causes downgrading 1N/A my $s =
2; $s =
1 if @a-$j <
2;
# avoid "can not modify non-existant..." 1N/A splice @a, $j, $s; $j -= $s; $i++;
1N/A elsif ($_[$i] =~ /^(l|
lib)$/)
1N/A # this causes a different low lib to take care... 1N/A my $s =
2; $s =
1 if @a-$j <
2;
# avoid "can not modify non-existant..." 1N/A splice @a, $j, $s; $j -= $s; $i++;
1N/A my $s =
2; $s =
1 if @a-$j <
2;
# avoid "can not modify non-existant..." 1N/A splice @a, $j, $s; $j -= $s; $i++;
1N/A my $s =
2; $s =
1 if @a-$j <
2;
# avoid "can not modify non-existant..." 1N/A splice @a, $j, $s; $j -= $s; $i++;
1N/A splice @a, $j,
1; $j --;
1N/A splice @a, $j,
1; $j --;
1N/A else {
die "unknown option $_[$i]"; }
1N/A # see if we can find Math::BigInt::Lite 1N/A if (!
defined $a && !
defined $p)
# rounding won't work to well 1N/A eval 'require Math::BigInt::Lite;';
1N/A $
class =
'Math::BigInt';
# regardless of MBIL or not 1N/A # Math::BigInt::Trace or plain Math::BigInt 1N/A print "bignum\t\t\t v$VERSION\n";
1N/A print "Math::BigInt::Lite\t v$Math::BigInt::Lite::VERSION\n" if $
_lite;
1N/A print "Math::BigInt\t\t v$Math::BigInt::VERSION";
1N/A print " lib => $config->{lib} v$config->{lib_version}\n";
1N/A print "Math::BigFloat\t\t v$Math::BigFloat::VERSION\n";
1N/Abignum - Transparent BigNumber support for Perl 1N/A $x = 2 + 4.5,"\n"; # BigFloat 6.5 1N/A print 2 ** 512 * 0.1,"\n"; # really is what you think it is 1N/A print inf * inf,"\n"; # prints inf 1N/A print NaN * 3,"\n"; # prints NaN 1N/AAll operators (including basic math operations) are overloaded. Integer and 1N/Afloating-point constants are created as proper BigInts or BigFloats, 1N/Aat the top of your script, Math::BigFloat and Math::BigInt will be loaded 1N/Aand any constant number will be converted to an object (Math::BigFloat for 1N/Afloats like 3.1415 and Math::BigInt for integers like 1234). 1N/ASo, the following line: 1N/Acreates actually a Math::BigInt and stores a reference to in $x. 1N/AThis happens transparently and behind your back, so to speak. 1N/AYou can see this with the following: 1N/A perl -Mbignum -le 'print ref(1234)' 1N/ADon't worry if it says Math::BigInt::Lite, bignum and friends will use Lite 1N/Aif it is installed since it is faster for some operations. It will be 1N/Aautomatically upgraded to BigInt whenever neccessary: 1N/A perl -Mbignum -le 'print ref(2**255)' 1N/AThis also means it is a bad idea to check for some specific package, since 1N/Athe actual contents of $x might be something unexpected. Due to the 1N/Atransparent way of bignum C<ref()> should not be neccessary, anyway. 1N/ASince Math::BigInt and BigFloat also overload the normal math operations, 1N/Athe following line will still work: 1N/A perl -Mbignum -le 'print ref(1234+1234)' 1N/ASince numbers are actually objects, you can call all the usual methods from 1N/A perl -Mbignum -le '$x = 1234; print $x->bdec()' 1N/A perl -Mbignum -le 'print 1234->binc();' 1N/A perl -Mbignum -le 'print 1234->binc->badd(6);' 1N/A perl -Mbignum -le 'print +(1234)->binc()' 1N/A(Note that print doesn't do what you expect if the expression starts with 1N/AYou can even chain the operations together as usual: 1N/A perl -Mbignum -le 'print 1234->binc->badd(6);' 1N/AUnder bignum (or bigint or bigrat), Perl will "upgrade" the numbers 1N/Aappropriately. This means that: 1N/A perl -Mbignum -le 'print 1234+4.5' 1N/Awill work correctly. These mixed cases don't do always work when using 1N/AMath::BigInt or Math::BigFloat alone, or at least not in the way normal Perl 1N/AIf you do want to work with large integers like under C<use integer;>, try 1N/A perl -Mbigint -le 'print 1234.5+4.5' 1N/AThere is also C<use bigrat;> which gives you big rationals: 1N/A perl -Mbigrat -le 'print 1234+4.1' 1N/Aas you expect or may even have bugs. 1N/AYou might get errors like this: 1N/A Can't use an undefined value as an ARRAY reference at 1N/AThis means somewhere a routine got a BigFloat/Lite but expected a BigInt (or 1N/Areport it so that we can fix it. 1N/AYou might consider using just Math::BigInt or Math::BigFloat, since they 1N/Aallow you finer control over what get's done in which module/space. For 1N/Ainstance, simple loop counters will be Math::BigInts under C<use bignum;> and 1N/Athis is slower than keeping them as Perl scalars: 1N/A perl -Mbignum -le 'for ($i = 0; $i < 10; $i++) { print ref($i); }' 1N/APlease note the following does not work as expected (prints nothing), since 1N/Aoverloading of '..' is not yet possible in Perl (as of v5.8.0): 1N/A perl -Mbignum -le 'for (1..2) { print ref($_); }' 1N/Abignum recognizes some options that can be passed while loading it via use. 1N/AThe options can (currently) be either a single letter form, or the long form. 1N/AThe following options exist: 1N/AThis sets the accuracy for all math operations. The argument must be greater 1N/Athan or equal to zero. See Math::BigInt's bround() function for details. 1N/A perl -Mbignum=a,50 -le 'print sqrt(20)' 1N/AThis sets the precision for all math operations. The argument can be any 1N/Ainteger. Negative values mean a fixed number of digits after the dot, while 1N/Aa positive value rounds to this digit left from the dot. 0 or 1 mean round to 1N/Ainteger. See Math::BigInt's bfround() function for details. 1N/A perl -Mbignum=p,-50 -le 'print sqrt(20)' 1N/AThis enables a trace mode and is primarily for debugging bignum or 1N/ALoad a different math lib, see L<MATH LIBRARY>. 1N/A perl -Mbignum=l,GMP -e 'print 2 ** 512' 1N/ACurrently there is no way to specify more than one library on the command 1N/Aline. This will be hopefully fixed soon ;) 1N/AThis prints out the name and version of all modules used and then exits. 1N/A perl -Mbignum=v -e '' 1N/ABeside import() and AUTOLOAD() there are only a few other methods. 1N/ASince all numbers are now objects, you can use all functions that are part of 1N/Athe BigInt or BigFloat API. It is wise to use only the bxxx() notation, and not 1N/Athe fxxx() notation, though. This makes it possible that the underlying object 1N/Amight morph into a different class than BigFloat. 1N/ABut a warning is in order. When using the following to make a copy of a number, 1N/Aonly a shallow copy will be made. 1N/AUsing the copy or the original with overloaded math is okay, e.g. the 1N/A print $x + 1, " ", $y,"\n"; # prints 10 9 1N/Abut calling any method that modifies the number directly will result in 1N/AB<both> the original and the copy beeing destroyed: 1N/A print $x->badd(1), " ", $y,"\n"; # prints 10 10 1N/A print $x->binc(1), " ", $y,"\n"; # prints 10 10 1N/A print $x->bmul(2), " ", $y,"\n"; # prints 18 18 1N/AUsing methods that do not modify, but testthe contents works: 1N/A $z = 9 if $x->is_zero(); # works fine 1N/ASee the documentation about the copy constructor and C<=> in overload, as 1N/Awell as the documentation in BigInt for further details. 1N/AA shortcut to return Math::BigInt->binf(). Usefull because Perl does not always 1N/Ahandle bareword C<inf> properly. 1N/AA shortcut to return Math::BigInt->bnan(). Usefull because Perl does not always 1N/Ahandle bareword C<NaN> properly. 1N/AReturn the class that numbers are upgraded to, is in fact returning 1N/AC<$Math::BigInt::upgrade>. 1N/AMath with the numbers is done (by default) by a module called 1N/AMath::BigInt::Calc. This is equivalent to saying: 1N/A use bignum lib => 'Calc'; 1N/AYou can change this by using: 1N/A use bignum lib => 'BitVect'; 1N/AThe following would first try to find Math::BigInt::Foo, then 1N/AMath::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: 1N/A use bignum lib => 'Foo,Math::BigInt::Bar'; 1N/APlease see respective module documentation for further details. 1N/A=head2 INTERNAL FORMAT 1N/AThe numbers are stored as objects, and their internals might change at anytime, 1N/Aespecially between math operations. The objects also might belong to different 1N/Aclasses, like Math::BigInt, or Math::BigFLoat. Mixing them together, even 1N/Awith normal scalars is not extraordinary, but normal and expected. 1N/AYou should not depend on the internal format, all accesses must go through 1N/Aaccessor methods. E.g. looking at $x->{sign} is not a bright idea since there 1N/Ais no guaranty that the object in question has such a hashkey, nor is a hash 1N/AThe sign is either '+', '-', 'NaN', '+inf' or '-inf' and stored seperately. 1N/AYou can access it with the sign() method. 1N/AA sign of 'NaN' is used to represent the result when input arguments are not 1N/Anumbers or as a result of 0/0. '+inf' and '-inf' represent plus respectively 1N/Aminus infinity. You will get '+inf' when dividing a positive number by 0, and 1N/A'-inf' when dividing any negative number by 0. 1N/AC<bignum> is just a thin wrapper around various modules of the Math::BigInt 1N/Afamily. Think of it as the head of the family, who runs the shop, and orders 1N/Athe others to do the work. 1N/AThe following modules are currently used by bignum: 1N/A Math::BigInt::Lite (for speed, and only if it is loadable) 1N/ASome cool command line examples to impress the Python crowd ;) 1N/A perl -Mbignum -le 'print sqrt(33)' 1N/A perl -Mbignum -le 'print 2*255' 1N/A perl -Mbignum -le 'print 4.5+2*255' 1N/A perl -Mbignum -le 'print 3/7 + 5/7 + 8/3' 1N/A perl -Mbignum -le 'print 123->is_odd()' 1N/A perl -Mbignum -le 'print log(2)' 1N/A perl -Mbignum -le 'print 2 ** 0.5' 1N/A perl -Mbignum=a,65 -le 'print 2 ** 0.2' 1N/AThis program is free software; you may redistribute it and/or modify it under 1N/Athe same terms as Perl itself. 1N/AEspecially L<bigrat> as in C<perl -Mbigrat -le 'print 1/3+1/4'>. 1N/AL<Math::BigFloat>, L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as well 1N/Aas L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.