📄 bigint.pm
字号:
3 # perl -Mbigint -wle 'print 3.2 + 0' 3 # perl -Mbigint -wle 'print exp(1) + 0' 2 # perl -Mbigint -wle 'print exp(1)' 2 # perl -Minteger -wle 'print exp(1)' 2.71828182845905 # perl -Minteger -wle 'print exp(1) + 0' 2In practice this makes seldom a difference as B<parts and results> ofexpressions will be truncated anyway, but this can, for instance, affect thereturn value of subroutines: sub three_integer { use integer; return 3.2; } sub three_bigint { use bigint; return 3.2; } print three_integer(), " ", three_bigint(),"\n"; # prints "3.2 3"=head2 Optionsbigint recognizes some options that can be passed while loading it via use.The options can (currently) be either a single letter form, or the long form.The following options exist:=over 2=item a or accuracyThis sets the accuracy for all math operations. The argument must be greaterthan or equal to zero. See Math::BigInt's bround() function for details. perl -Mbigint=a,2 -le 'print 12345+1'Note that setting precision and accurary at the same time is not possible.=item p or precisionThis sets the precision for all math operations. The argument can be anyinteger. Negative values mean a fixed number of digits after the dot, andare <B>ignored</B> since all operations happen in integer space.A positive value rounds to this digit left from the dot. 0 or 1 mean round tointeger and are ignore like negative values.See Math::BigInt's bfround() function for details. perl -Mbignum=p,5 -le 'print 123456789+123'Note that setting precision and accurary at the same time is not possible.=item t or traceThis enables a trace mode and is primarily for debugging bigint orMath::BigInt.=item hexOverride the built-in hex() method with a version that can handle bigintegers. Note that under Perl v5.9.4 or ealier, this will be globaland cannot be disabled with "no bigint;".=item octOverride the built-in oct() method with a version that can handle bigintegers. Note that under Perl v5.9.4 or ealier, this will be globaland cannot be disabled with "no bigint;".=item l, lib, try or onlyLoad a different math lib, see L<Math Library>. perl -Mbigint=lib,GMP -e 'print 2 ** 512' perl -Mbigint=try,GMP -e 'print 2 ** 512' perl -Mbigint=only,GMP -e 'print 2 ** 512'Currently there is no way to specify more than one library on the commandline. This means the following does not work: perl -Mbignum=l,GMP,Pari -e 'print 2 ** 512'This will be hopefully fixed soon ;)=item v or versionThis prints out the name and version of all modules used and then exits. perl -Mbigint=v=back=head2 Math LibraryMath with the numbers is done (by default) by a module calledMath::BigInt::Calc. This is equivalent to saying: use bigint lib => 'Calc';You can change this by using: use bignum lib => 'GMP';The following would first try to find Math::BigInt::Foo, thenMath::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc: use bigint lib => 'Foo,Math::BigInt::Bar';Using C<lib> warns if none of the specified libraries can be found andL<Math::BigInt> did fall back to one of the default libraries.To supress this warning, use C<try> instead: use bignum try => 'GMP';If you want the code to die instead of falling back, use C<only> instead: use bignum only => 'GMP';Please see respective module documentation for further details.=head2 Internal FormatThe numbers are stored as objects, and their internals might change at anytime,especially between math operations. The objects also might belong to differentclasses, like Math::BigInt, or Math::BigInt::Lite. Mixing them together, evenwith normal scalars is not extraordinary, but normal and expected.You should not depend on the internal format, all accesses must go throughaccessor methods. E.g. looking at $x->{sign} is not a good idea since thereis no guaranty that the object in question has such a hash key, nor is a hashunderneath at all.=head2 SignThe sign is either '+', '-', 'NaN', '+inf' or '-inf'.You can access it with the sign() method.A sign of 'NaN' is used to represent the result when input arguments are notnumbers or as a result of 0/0. '+inf' and '-inf' represent plus respectivelyminus infinity. You will get '+inf' when dividing a positive number by 0, and'-inf' when dividing any negative number by 0.=head2 MethodsSince all numbers are now objects, you can use all functions that are part ofthe BigInt API. You can only use the bxxx() notation, and not the fxxx()notation, though. =over 2=item inf()A shortcut to return Math::BigInt->binf(). Useful because Perl does not alwayshandle bareword C<inf> properly.=item NaN()A shortcut to return Math::BigInt->bnan(). Useful because Perl does not alwayshandle bareword C<NaN> properly.=item e # perl -Mbigint=e -wle 'print e'Returns Euler's number C<e>, aka exp(1). Note that under bigint, this istruncated to an integer, and hence simple '2'.=item PI # perl -Mbigint=PI -wle 'print PI'Returns PI. Note that under bigint, this is truncated to an integer, and hencesimple '3'.=item bexp() bexp($power,$accuracy);Returns Euler's number C<e> raised to the appropriate power, tothe wanted accuracy.Note that under bigint, the result is truncated to an integer.Example: # perl -Mbigint=bexp -wle 'print bexp(1,80)'=item bpi() bpi($accuracy);Returns PI to the wanted accuracy. Note that under bigint, this is truncatedto an integer, and hence simple '3'.Example: # perl -Mbigint=bpi -wle 'print bpi(80)'=item upgrade()Return the class that numbers are upgraded to, is in fact returningC<$Math::BigInt::upgrade>.=item in_effect() use bigint; print "in effect\n" if bigint::in_effect; # true { no bigint; print "in effect\n" if bigint::in_effect; # false }Returns true or false if C<bigint> is in effect in the current scope.This method only works on Perl v5.9.4 or later.=back=head2 MATH LIBRARYMath with the numbers is done (by default) by a module called=head2 CaveatBut a warning is in order. When using the following to make a copy of a number,only a shallow copy will be made. $x = 9; $y = $x; $x = $y = 7;Using the copy or the original with overloaded math is okay, e.g. thefollowing work: $x = 9; $y = $x; print $x + 1, " ", $y,"\n"; # prints 10 9but calling any method that modifies the number directly will result inB<both> the original and the copy being destroyed: $x = 9; $y = $x; print $x->badd(1), " ", $y,"\n"; # prints 10 10 $x = 9; $y = $x; print $x->binc(1), " ", $y,"\n"; # prints 10 10 $x = 9; $y = $x; print $x->bmul(2), " ", $y,"\n"; # prints 18 18 Using methods that do not modify, but testthe contents works: $x = 9; $y = $x; $z = 9 if $x->is_zero(); # works fineSee the documentation about the copy constructor and C<=> in overload, aswell as the documentation in BigInt for further details.=head1 CAVAETS=over 2=item in_effect()This method only works on Perl v5.9.4 or later.=item hex()/oct()C<bigint> overrides these routines with versions that can also handlebig integer values. Under Perl prior to version v5.9.4, however, thiswill not happen unless you specifically ask for it with the twoimport tags "hex" and "oct" - and then it will be global and cannot bedisabled inside a scope with "no bigint": use bigint qw/hex oct/; print hex("0x1234567890123456"); { no bigint; print hex("0x1234567890123456"); }The second call to hex() will warn about a non-portable constant.Compare this to: use bigint; # will warn only under Perl older than v5.9.4 print hex("0x1234567890123456");=back=head1 MODULES USEDC<bigint> is just a thin wrapper around various modules of the Math::BigIntfamily. Think of it as the head of the family, who runs the shop, and ordersthe others to do the work.The following modules are currently used by bigint: Math::BigInt::Lite (for speed, and only if it is loadable) Math::BigInt=head1 EXAMPLESSome cool command line examples to impress the Python crowd ;) You might wantto compare them to the results under -Mbignum or -Mbigrat: perl -Mbigint -le 'print sqrt(33)' perl -Mbigint -le 'print 2*255' perl -Mbigint -le 'print 4.5+2*255' perl -Mbigint -le 'print 3/7 + 5/7 + 8/3' perl -Mbigint -le 'print 123->is_odd()' perl -Mbigint -le 'print log(2)' perl -Mbigint -le 'print 2 ** 0.5' perl -Mbigint=a,65 -le 'print 2 ** 0.2' perl -Mbignum=a,65,l,GMP -le 'print 7 ** 7777'=head1 LICENSEThis program is free software; you may redistribute it and/or modify it underthe same terms as Perl itself.=head1 SEE ALSOEspecially L<bigrat> as in C<perl -Mbigrat -le 'print 1/3+1/4'> andL<bignum> as in C<perl -Mbignum -le 'print sqrt(2)'>.L<Math::BigInt>, L<Math::BigRat> and L<Math::Big> as wellas L<Math::BigInt::BitVect>, L<Math::BigInt::Pari> and L<Math::BigInt::GMP>.=head1 AUTHORS(C) by Tels L<http://bloodgate.com/> in early 2002 - 2007.=cut
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -