📄 math::bigint.3
字号:
.Vb 1\& $x\->bceil();.Ve.PPSet \f(CW$x\fR to the integer greater or equal than \f(CW$x\fR. This is a no-op in BigInt, butdoes change \f(CW$x\fR in BigFloat..Sh "\fIbgcd()\fP".IX Subsection "bgcd()".Vb 1\& bgcd(@values); # greatest common divisor (no OO style).Ve.Sh "\fIblcm()\fP".IX Subsection "blcm()".Vb 1\& blcm(@values); # lowest common multiplicator (no OO style).Ve.PPhead2 \fIlength()\fR.PP.Vb 2\& $x\->length();\& ($xl,$fl) = $x\->length();.Ve.PPReturns the number of digits in the decimal representation of the number.In list context, returns the length of the integer and fraction part. ForBigInt's, the length of the fraction part will always be 0..Sh "\fIexponent()\fP".IX Subsection "exponent()".Vb 1\& $x\->exponent();.Ve.PPReturn the exponent of \f(CW$x\fR as BigInt..Sh "\fImantissa()\fP".IX Subsection "mantissa()".Vb 1\& $x\->mantissa();.Ve.PPReturn the signed mantissa of \f(CW$x\fR as BigInt..Sh "\fIparts()\fP".IX Subsection "parts()".Vb 1\& $x\->parts(); # return (mantissa,exponent) as BigInt.Ve.Sh "\fIcopy()\fP".IX Subsection "copy()".Vb 1\& $x\->copy(); # make a true copy of $x (unlike $y = $x;).Ve.Sh "\fIas_int()\fP/\fIas_number()\fP".IX Subsection "as_int()/as_number()".Vb 1\& $x\->as_int();.Ve.PPReturns \f(CW$x\fR as a BigInt (truncated towards zero). In BigInt this is the same as\&\f(CW\*(C`copy()\*(C'\fR..PP\&\f(CW\*(C`as_number()\*(C'\fR is an alias to this method. \f(CW\*(C`as_number\*(C'\fR was introduced inv1.22, while \f(CW\*(C`as_int()\*(C'\fR was only introduced in v1.68..Sh "\fIbstr()\fP".IX Subsection "bstr()".Vb 1\& $x\->bstr();.Ve.PPReturns a normalized string representation of \f(CW$x\fR..Sh "\fIbsstr()\fP".IX Subsection "bsstr()".Vb 1\& $x\->bsstr(); # normalized string in scientific notation.Ve.Sh "\fIas_hex()\fP".IX Subsection "as_hex()".Vb 1\& $x\->as_hex(); # as signed hexadecimal string with prefixed 0x.Ve.Sh "\fIas_bin()\fP".IX Subsection "as_bin()".Vb 1\& $x\->as_bin(); # as signed binary string with prefixed 0b.Ve.Sh "\fIas_oct()\fP".IX Subsection "as_oct()".Vb 1\& $x\->as_oct(); # as signed octal string with prefixed 0.Ve.Sh "\fInumify()\fP".IX Subsection "numify()".Vb 1\& print $x\->numify();.Ve.PPThis returns a normal Perl scalar from \f(CW$x\fR. It is used automaticallywhenever a scalar is needed, for instance in array index operations..PPThis loses precision, to avoid this use \fIas_int()\fR instead..Sh "\fImodify()\fP".IX Subsection "modify()".Vb 1\& $x\->modify(\*(Aqbpowd\*(Aq);.Ve.PPThis method returns 0 if the object can be modified with the givenperation, or 1 if not..PPThis is used for instance by Math::BigInt::Constant..Sh "\fIupgrade()\fP/\fIdowngrade()\fP".IX Subsection "upgrade()/downgrade()"Set/get the class for downgrade/upgrade operations. Thuis is usedfor instance by bignum. The defaults are '', thus the followingoperation will create a BigInt, not a BigFloat:.PP.Vb 2\& my $i = Math::BigInt\->new(123);\& my $f = Math::BigFloat\->new(\*(Aq123.1\*(Aq);\&\& print $i + $f,"\en"; # print 246.Ve.Sh "\fIdiv_scale()\fP".IX Subsection "div_scale()"Set/get the number of digits for the default precision in divideoperations..Sh "\fIround_mode()\fP".IX Subsection "round_mode()"Set/get the current round mode..SH "ACCURACY and PRECISION".IX Header "ACCURACY and PRECISION"Since version v1.33, Math::BigInt and Math::BigFloat have full support foraccuracy and precision based rounding, both automatically after everyoperation, as well as manually..PPThis section describes the accuracy/precision handling in Math::Big* as itused to be and as it is now, complete with an explanation of all terms andabbreviations..PPNot yet implemented things (but with correct description) are marked with '!',things that need to be answered are marked with '?'..PPIn the next paragraph follows a short description of terms used here (becausethese may differ from terms used by others people or documentation)..PPDuring the rest of this document, the shortcuts A (for accuracy), P (forprecision), F (fallback) and R (rounding mode) will be used..Sh "Precision P".IX Subsection "Precision P"A fixed number of digits before (positive) or after (negative)the decimal point. For example, 123.45 has a precision of \-2. 0 means aninteger like 123 (or 120). A precision of 2 means two digits to the leftof the decimal point are zero, so 123 with P = 1 becomes 120. Note thatnumbers with zeros before the decimal point may have different precisions,because 1200 can have p = 0, 1 or 2 (depending on what the inital valuewas). It could also have p < 0, when the digits after the decimal pointare zero..PPThe string output (of floating point numbers) will be padded with zeros:.PP.Vb 9\& Initial value P A Result String\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& 1234.01 \-3 1000 1000\& 1234 \-2 1200 1200\& 1234.5 \-1 1230 1230\& 1234.001 1 1234 1234.0\& 1234.01 0 1234 1234\& 1234.01 2 1234.01 1234.01\& 1234.01 5 1234.01 1234.01000.Ve.PPFor BigInts, no padding occurs..Sh "Accuracy A".IX Subsection "Accuracy A"Number of significant digits. Leading zeros are not counted. Anumber may have an accuracy greater than the non-zero digitswhen there are zeros in it or trailing zeros. For example, 123.456 hasA of 6, 10203 has 5, 123.0506 has 7, 123.450000 has 8 and 0.000123 has 3..PPThe string output (of floating point numbers) will be padded with zeros:.PP.Vb 5\& Initial value P A Result String\& \-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\-\& 1234.01 3 1230 1230\& 1234.01 6 1234.01 1234.01\& 1234.1 8 1234.1 1234.1000.Ve.PPFor BigInts, no padding occurs..Sh "Fallback F".IX Subsection "Fallback F"When both A and P are undefined, this is used as a fallback accuracy whendividing numbers..Sh "Rounding mode R".IX Subsection "Rounding mode R"When rounding a number, different 'styles' or 'kinds'of rounding are possible. (Note that random rounding, as inMath::Round, is not implemented.).IP "'trunc'" 2.IX Item "'trunc'"truncation invariably removes all digits following therounding place, replacing them with zeros. Thus, 987.65 roundedto tens (P=1) becomes 980, and rounded to the fourth sigdigbecomes 987.6 (A=4). 123.456 rounded to the second place after thedecimal point (P=\-2) becomes 123.46..SpAll other implemented styles of rounding attempt to round to the\&\*(L"nearest digit.\*(R" If the digit D immediately to the right of therounding place (skipping the decimal point) is greater than 5, thenumber is incremented at the rounding place (possibly causing acascade of incrementation): e.g. when rounding to units, 0.9 roundsto 1, and \-19.9 rounds to \-20. If D < 5, the number is similarlytruncated at the rounding place: e.g. when rounding to units, 0.4rounds to 0, and \-19.4 rounds to \-19..SpHowever the results of other styles of rounding differ if thedigit immediately to the right of the rounding place (skipping thedecimal point) is 5 and if there are no digits, or no digits otherthan 0, after that 5. In such cases:.IP "'even'" 2.IX Item "'even'"rounds the digit at the rounding place to 0, 2, 4, 6, or 8if it is not already. E.g., when rounding to the first sigdig, 0.45becomes 0.4, \-0.55 becomes \-0.6, but 0.4501 becomes 0.5..IP "'odd'" 2.IX Item "'odd'"rounds the digit at the rounding place to 1, 3, 5, 7, or 9 ifit is not already. E.g., when rounding to the first sigdig, 0.45becomes 0.5, \-0.55 becomes \-0.5, but 0.5501 becomes 0.6..IP "'+inf'" 2.IX Item "'+inf'"round to plus infinity, i.e. always round up. E.g., whenrounding to the first sigdig, 0.45 becomes 0.5, \-0.55 becomes \-0.5,and 0.4501 also becomes 0.5..IP "'\-inf'" 2.IX Item "'-inf'"round to minus infinity, i.e. always round down. E.g., whenrounding to the first sigdig, 0.45 becomes 0.4, \-0.55 becomes \-0.6,but 0.4501 becomes 0.5..IP "'zero'" 2.IX Item "'zero'"round to zero, i.e. positive numbers down, negative ones up.E.g., when rounding to the first sigdig, 0.45 becomes 0.4, \-0.55becomes \-0.5, but 0.4501 becomes 0.5..IP "'common'" 2.IX Item "'common'"round up if the digit immediately to the right of the rounding placeis 5 or greater, otherwise round down. E.g., 0.15 becomes 0.2 and0.149 becomes 0.1..PPThe handling of A & P in \s-1MBI/MBF\s0 (the old core code shipped with Perlversions <= 5.7.2) is like this:.IP "Precision" 2.IX Item "Precision".Vb 3\& * ffround($p) is able to round to $p number of digits after the decimal\& point\& * otherwise P is unused.Ve.IP "Accuracy (significant digits)" 2.IX Item "Accuracy (significant digits)".Vb 10\& * fround($a) rounds to $a significant digits\& * only fdiv() and fsqrt() take A as (optional) paramater\& + other operations simply create the same number (fneg etc), or more (fmul)\& of digits\& + rounding/truncating is only done when explicitly calling one of fround\& or ffround, and never for BigInt (not implemented)\& * fsqrt() simply hands its accuracy argument over to fdiv.\& * the documentation and the comment in the code indicate two different ways\& on how fdiv() determines the maximum number of digits it should calculate,\& and the actual code does yet another thing\& POD:\& max($Math::BigFloat::div_scale,length(dividend)+length(divisor))\& Comment:\& result has at most max(scale, length(dividend), length(divisor)) digits\& Actual code:\& scale = max(scale, length(dividend)\-1,length(divisor)\-1);\& scale += length(divisor) \- length(dividend);\& So for lx = 3, ly = 9, scale = 10, scale will actually be 16 (10+9\-3).\& Actually, the \*(Aqdifference\*(Aq added to the scale is calculated from the\& number of "significant digits" in dividend and divisor, which is derived\& by looking at the length of the mantissa. Which is wrong, since it includes\& the + sign (oops) and actually gets 2 for \*(Aq+100\*(Aq and 4 for \*(Aq+101\*(Aq. Oops\& again. Thus 124/3 with div_scale=1 will get you \*(Aq41.3\*(Aq based on the strange\& assumption that 124 has 3 significant digits, while 120/7 will get you\& \*(Aq17\*(Aq, not \*(Aq17.1\*(Aq since 120 is thought to have 2 significant digits.\& The rounding after the division then uses the remainder and $y to determine\& wether it must round up or down.\& ? I have no idea which is the right way. That\*(Aqs why I used a slightly more\& ? simple scheme and tweaked the few failing testcases to match it..Ve.PPThis is how it works now:.IP "Setting/Accessing" 2.IX Item "Setting/Accessing".Vb 10\& * You can set the A global via C<< Math::BigInt\->accuracy() >> or\& C<< Math::BigFloat\->accuracy() >> or whatever class you are using.\& * You can also set P globally by using C<< Math::SomeClass\->precision() >>\& likewise.\& * Globals are classwide, and not inherited by subclasses.\& * to undefine A, use C<< Math::SomeCLass\->accuracy(undef); >>\& * to undefine P, use C<< Math::SomeClass\->precision(undef); >>\& * Setting C<< Math::SomeClass\->accuracy() >> clears automatically\& C<< Math::SomeClass\->precision() >>, and vice versa.\& * To be valid, A must be > 0, P can have any value.\& * If P is negative, this means round to the P\*(Aqth place to the right of the\& decimal point; positive values mean to the left of the decimal point.\& P of 0 means round to integer.\& * to find out the current global A, use C<< Math::SomeClass\->accuracy() >>\& * to find out the current global P, use C<< Math::SomeClass\->precision() >>\& * use C<< $x\->accuracy() >> respective C<< $x\->precision() >> for the local\& setting of C<< $x >>.\& * Please note that C<< $x\->accuracy() >> respective C<< $x\->precision() >>\& return eventually defined global A or P, when C<< $x >>\*(Aqs A or P is not\& set..Ve.IP "Creating numbers" 2.IX Item "Creating numbers".Vb 12\& * When you create a number, you can give the desired A or P via:\& $x = Math::BigInt\->new($number,$A,$P);\& * Only one of A or P can be defined, otherwise the result is NaN\& * If no A or P is give ($x = Math::BigInt\->new($number) form), then the\& globals (if set) will be used. Thus changing the global defaults later on\& will not change the A or P of previously created numbers (i.e., A and P of\& $x will be what was in effect when $x was created)\& * If given undef for A and P, B<no> rounding will occur, and the globals will\& B<not> be used. This is used by subclasses to create numbers without\& suffering rounding in the parent. Thus a subclass is able to have its own\& globals enforced upon creation of a number by using\& C<< $x = Math::BigInt\->new($number,undef,undef) >>:\&\& use Math::BigInt::SomeSubclass;\& use Math::BigInt;\&\& Math::BigInt\->accuracy(2);\& Math::BigInt::SomeSubClass\->accuracy(3);\& $x = Math::BigInt::SomeSubClass\->new(1234); \&\& $x is now 1230, and not 1200. A subclass might choose to implement\& this otherwise, e.g. falling back to the parent\*(Aqs A and P..Ve.IP "Usage" 2.IX Item "Usage".Vb 7\& * If A or P are enabled/defined, they are used to round the result of each\& operation according to the rules below\& * Negative P is ignored in Math::BigInt, since BigInts never have digits
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -