📄 math::bigint.3
字号:
\& $zero = Math::BigInt\->bzero(); \& $nan = Math::BigInt\->bnan();.Ve.Sp\&\f(CW\*(C`bnorm()\*(C'\fR on a BigInt object is now effectively a no-op, since the numbers are always stored in normalized form. If passed a string, creates a BigInt object from the input..IP "Output" 2.IX Item "Output"Output values are BigInt objects (normalized), except for the methods whichreturn a string (see \s-1SYNOPSIS\s0)..SpSome routines (\f(CW\*(C`is_odd()\*(C'\fR, \f(CW\*(C`is_even()\*(C'\fR, \f(CW\*(C`is_zero()\*(C'\fR, \f(CW\*(C`is_one()\*(C'\fR,\&\f(CW\*(C`is_nan()\*(C'\fR, etc.) return true or false, while others (\f(CW\*(C`bcmp()\*(C'\fR, \f(CW\*(C`bacmp()\*(C'\fR)return either undef (if NaN is involved), <0, 0 or >0 and are suited for sort..SH "METHODS".IX Header "METHODS"Each of the methods below (except \fIconfig()\fR, \fIaccuracy()\fR and \fIprecision()\fR)accepts three additional parameters. These arguments \f(CW$A\fR, \f(CW$P\fR and \f(CW$R\fRare \f(CW\*(C`accuracy\*(C'\fR, \f(CW\*(C`precision\*(C'\fR and \f(CW\*(C`round_mode\*(C'\fR. Please see the section about\&\*(L"\s-1ACCURACY\s0 and \s-1PRECISION\s0\*(R" for more information..Sh "\fIconfig()\fP".IX Subsection "config()".Vb 1\& use Data::Dumper;\&\& print Dumper ( Math::BigInt\->config() );\& print Math::BigInt\->config()\->{lib},"\en";.Ve.PPReturns a hash containing the configuration, e.g. the version number, libloaded etc. The following hash keys are currently filled in with theappropriate information..PP.Vb 10\& key Description\& Example\& ============================================================\& lib Name of the low\-level math library\& Math::BigInt::Calc\& lib_version Version of low\-level math library (see \*(Aqlib\*(Aq)\& 0.30\& class The class name of config() you just called\& Math::BigInt\& upgrade To which class math operations might be upgraded\& Math::BigFloat\& downgrade To which class math operations might be downgraded\& undef\& precision Global precision\& undef\& accuracy Global accuracy\& undef\& round_mode Global round mode\& even\& version version number of the class you used\& 1.61\& div_scale Fallback accuracy for div\& 40\& trap_nan If true, traps creation of NaN via croak()\& 1\& trap_inf If true, traps creation of +inf/\-inf via croak()\& 1.Ve.PPThe following values can be set by passing \f(CW\*(C`config()\*(C'\fR a reference to a hash:.PP.Vb 2\& trap_inf trap_nan\& upgrade downgrade precision accuracy round_mode div_scale.Ve.PPExample:.PP.Vb 1\& $new_cfg = Math::BigInt\->config( { trap_inf => 1, precision => 5 } );.Ve.Sh "\fIaccuracy()\fP".IX Subsection "accuracy()".Vb 3\& $x\->accuracy(5); # local for $x\& CLASS\->accuracy(5); # global for all members of CLASS\& # Note: This also applies to new()!\&\& $A = $x\->accuracy(); # read out accuracy that affects $x\& $A = CLASS\->accuracy(); # read out global accuracy.Ve.PPSet or get the global or local accuracy, aka how many significant digits theresults have. If you set a global accuracy, then this also applies to \fInew()\fR!.PPWarning! The accuracy \fIsticks\fR, e.g. once you created a number under theinfluence of \f(CW\*(C`CLASS\->accuracy($A)\*(C'\fR, all results from math operations withthat number will also be rounded..PPIn most cases, you should probably round the results explicitly using one of\&\fIround()\fR, \fIbround()\fR or \fIbfround()\fR or by passing the desired accuracyto the math operation as additional parameter:.PP.Vb 4\& my $x = Math::BigInt\->new(30000);\& my $y = Math::BigInt\->new(7);\& print scalar $x\->copy()\->bdiv($y, 2); # print 4300\& print scalar $x\->copy()\->bdiv($y)\->bround(2); # print 4300.Ve.PPPlease see the section about \*(L"\s-1ACCURACY\s0 \s-1AND\s0 \s-1PRECISION\s0\*(R" for further details..PPValue must be greater than zero. Pass an undef value to disable it:.PP.Vb 2\& $x\->accuracy(undef);\& Math::BigInt\->accuracy(undef);.Ve.PPReturns the current accuracy. For \f(CW\*(C`$x\-\*(C'\fR\fIaccuracy()\fR> it will return either thelocal accuracy, or if not defined, the global. This means the return valuerepresents the accuracy that will be in effect for \f(CW$x:\fR.PP.Vb 9\& $y = Math::BigInt\->new(1234567); # unrounded\& print Math::BigInt\->accuracy(4),"\en"; # set 4, print 4\& $x = Math::BigInt\->new(123456); # $x will be automatically rounded!\& print "$x $y\en"; # \*(Aq123500 1234567\*(Aq\& print $x\->accuracy(),"\en"; # will be 4\& print $y\->accuracy(),"\en"; # also 4, since global is 4\& print Math::BigInt\->accuracy(5),"\en"; # set to 5, print 5\& print $x\->accuracy(),"\en"; # still 4\& print $y\->accuracy(),"\en"; # 5, since global is 5.Ve.PPNote: Works also for subclasses like Math::BigFloat. Each class has it's ownglobals separated from Math::BigInt, but it is possible to subclassMath::BigInt and make the globals of the subclass aliases to the ones fromMath::BigInt..Sh "\fIprecision()\fP".IX Subsection "precision()".Vb 2\& $x\->precision(\-2); # local for $x, round at the second digit right of the dot\& $x\->precision(2); # ditto, round at the second digit left of the dot\&\& CLASS\->precision(5); # Global for all members of CLASS\& # This also applies to new()!\& CLASS\->precision(\-5); # ditto\&\& $P = CLASS\->precision(); # read out global precision \& $P = $x\->precision(); # read out precision that affects $x.Ve.PPNote: You probably want to use \fIaccuracy()\fR instead. With accuracy youset the number of digits each result should have, with precision youset the place where to round!.PP\&\f(CW\*(C`precision()\*(C'\fR sets or gets the global or local precision, aka at which digitbefore or after the dot to round all results. A set global precision alsoapplies to all newly created numbers!.PPIn Math::BigInt, passing a negative number precision has no effect since nonumbers have digits after the dot. In Math::BigFloat, it will round allresults to P digits after the dot..PPPlease see the section about \*(L"\s-1ACCURACY\s0 \s-1AND\s0 \s-1PRECISION\s0\*(R" for further details..PPPass an undef value to disable it:.PP.Vb 2\& $x\->precision(undef);\& Math::BigInt\->precision(undef);.Ve.PPReturns the current precision. For \f(CW\*(C`$x\-\*(C'\fR\fIprecision()\fR> it will return either thelocal precision of \f(CW$x\fR, or if not defined, the global. This means the returnvalue represents the prevision that will be in effect for \f(CW$x:\fR.PP.Vb 4\& $y = Math::BigInt\->new(1234567); # unrounded\& print Math::BigInt\->precision(4),"\en"; # set 4, print 4\& $x = Math::BigInt\->new(123456); # will be automatically rounded\& print $x; # print "120000"!.Ve.PPNote: Works also for subclasses like Math::BigFloat. Each class has itsown globals separated from Math::BigInt, but it is possible to subclassMath::BigInt and make the globals of the subclass aliases to the ones fromMath::BigInt..Sh "\fIbrsft()\fP".IX Subsection "brsft()".Vb 1\& $x\->brsft($y,$n);.Ve.PPShifts \f(CW$x\fR right by \f(CW$y\fR in base \f(CW$n\fR. Default is base 2, used are usually 10 and2, but others work, too..PPRight shifting usually amounts to dividing \f(CW$x\fR by \f(CW$n\fR ** \f(CW$y\fR and truncating theresult:.PP.Vb 4\& $x = Math::BigInt\->new(10);\& $x\->brsft(1); # same as $x >> 1: 5\& $x = Math::BigInt\->new(1234);\& $x\->brsft(2,10); # result 12.Ve.PPThere is one exception, and that is base 2 with negative \f(CW$x:\fR.PP.Vb 2\& $x = Math::BigInt\->new(\-5);\& print $x\->brsft(1);.Ve.PPThis will print \-3, not \-2 (as it would if you divide \-5 by 2 and truncate theresult)..Sh "\fInew()\fP".IX Subsection "new()".Vb 1\& $x = Math::BigInt\->new($str,$A,$P,$R);.Ve.PPCreates a new BigInt object from a scalar or another BigInt object. Theinput is accepted as decimal, hex (with leading '0x') or binary (with leading\&'0b')..PPSee Input for more info on accepted input formats..Sh "\fIfrom_oct()\fP".IX Subsection "from_oct()".Vb 1\& $x = Math::BigInt\->from_oct("0775"); # input is octal.Ve.Sh "\fIfrom_hex()\fP".IX Subsection "from_hex()".Vb 1\& $x = Math::BigInt\->from_hex("0xcafe"); # input is hexadecimal.Ve.Sh "\fIfrom_bin()\fP".IX Subsection "from_bin()".Vb 1\& $x = Math::BigInt\->from_oct("0x10011"); # input is binary.Ve.Sh "\fIbnan()\fP".IX Subsection "bnan()".Vb 1\& $x = Math::BigInt\->bnan();.Ve.PPCreates a new BigInt object representing NaN (Not A Number).If used on an object, it will set it to NaN:.PP.Vb 1\& $x\->bnan();.Ve.Sh "\fIbzero()\fP".IX Subsection "bzero()".Vb 1\& $x = Math::BigInt\->bzero();.Ve.PPCreates a new BigInt object representing zero.If used on an object, it will set it to zero:.PP.Vb 1\& $x\->bzero();.Ve.Sh "\fIbinf()\fP".IX Subsection "binf()".Vb 1\& $x = Math::BigInt\->binf($sign);.Ve.PPCreates a new BigInt object representing infinity. The optional argument iseither '\-' or '+', indicating whether you want infinity or minus infinity.If used on an object, it will set it to infinity:.PP.Vb 2\& $x\->binf();\& $x\->binf(\*(Aq\-\*(Aq);.Ve.Sh "\fIbone()\fP".IX Subsection "bone()".Vb 1\& $x = Math::BigInt\->binf($sign);.Ve.PPCreates a new BigInt object representing one. The optional argument iseither '\-' or '+', indicating whether you want one or minus one.If used on an object, it will set it to one:.PP.Vb 2\& $x\->bone(); # +1\& $x\->bone(\*(Aq\-\*(Aq); # \-1.Ve.Sh "\fIis_one()\fP/\fIis_zero()\fP/\fIis_nan()\fP/\fIis_inf()\fP".IX Subsection "is_one()/is_zero()/is_nan()/is_inf()".Vb 6\& $x\->is_zero(); # true if arg is +0\& $x\->is_nan(); # true if arg is NaN\& $x\->is_one(); # true if arg is +1\& $x\->is_one(\*(Aq\-\*(Aq); # true if arg is \-1\& $x\->is_inf(); # true if +inf\& $x\->is_inf(\*(Aq\-\*(Aq); # true if \-inf (sign is default \*(Aq+\*(Aq).Ve.PPThese methods all test the BigInt for being one specific value and returntrue or false depending on the input. These are faster than doing somethinglike:.PP.Vb 1\& if ($x == 0).Ve.Sh "\fIis_pos()\fP/\fIis_neg()\fP/\fIis_positive()\fP/\fIis_negative()\fP".IX Subsection "is_pos()/is_neg()/is_positive()/is_negative()".Vb 2\& $x\->is_pos(); # true if > 0\& $x\->is_neg(); # true if < 0.Ve.PPThe methods return true if the argument is positive or negative, respectively.\&\f(CW\*(C`NaN\*(C'\fR is neither positive nor negative, while \f(CW\*(C`+inf\*(C'\fR counts as positive, and\&\f(CW\*(C`\-inf\*(C'\fR is negative. A \f(CW\*(C`zero\*(C'\fR is neither positive nor negative..PPThese methods are only testing the sign, and not the value..PP\&\f(CW\*(C`is_positive()\*(C'\fR and \f(CW\*(C`is_negative()\*(C'\fR are aliases to \f(CW\*(C`is_pos()\*(C'\fR and\&\f(CW\*(C`is_neg()\*(C'\fR, respectively. \f(CW\*(C`is_positive()\*(C'\fR and \f(CW\*(C`is_negative()\*(C'\fR wereintroduced in v1.36, while \f(CW\*(C`is_pos()\*(C'\fR and \f(CW\*(C`is_neg()\*(C'\fR were only introducedin v1.68..Sh "\fIis_odd()\fP/\fIis_even()\fP/\fIis_int()\fP".IX Subsection "is_odd()/is_even()/is_int()".Vb 3\& $x\->is_odd(); # true if odd, false for even\& $x\->is_even(); # true if even, false for odd\& $x\->is_int(); # true if $x is an integer.Ve.PPThe return true when the argument satisfies the condition. \f(CW\*(C`NaN\*(C'\fR, \f(CW\*(C`+inf\*(C'\fR,\&\f(CW\*(C`\-inf\*(C'\fR are not integers and are neither odd nor even..PPIn BigInt, all numbers except \f(CW\*(C`NaN\*(C'\fR, \f(CW\*(C`+inf\*(C'\fR and \f(CW\*(C`\-inf\*(C'\fR are integers..Sh "\fIbcmp()\fP".IX Subsection "bcmp()".Vb 1\& $x\->bcmp($y);.Ve.PPCompares \f(CW$x\fR with \f(CW$y\fR and takes the sign into account.Returns \-1, 0, 1 or undef..Sh "\fIbacmp()\fP".IX Subsection "bacmp()".Vb 1\& $x\->bacmp($y);.Ve.PP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -