📄 math::bigfloat.3
字号:
\&\f(CW\*(C`/^[+\-]\ed+E[+\-]?\ed+$/\*(C'\fR.IP "\(bu" 2\&\f(CW\*(C`/^[+\-]\ed*\e.\ed+E[+\-]?\ed+$/\*(C'\fR.PPall with optional leading and trailing zeros and/or spaces. Additionally,numbers are allowed to have an underscore between any two digits..PPEmpty strings as well as other illegal numbers results in 'NaN'..PP\&\fIbnorm()\fR on a BigFloat object is now effectively a no-op, since the numbers are always stored in normalized form. On a string, it creates a BigFloat object..Sh "Output".IX Subsection "Output"Output values are BigFloat objects (normalized), except for \fIbstr()\fR and \fIbsstr()\fR..PPThe string output will always have leading and trailing zeros stripped and dropa plus sign. \f(CW\*(C`bstr()\*(C'\fR will give you always the form with a decimal point,while \f(CW\*(C`bsstr()\*(C'\fR (s for scientific) gives you the scientific notation..PP.Vb 6\& Input bstr() bsstr()\& \*(Aq\-0\*(Aq \*(Aq0\*(Aq \*(Aq0E1\*(Aq\& \*(Aq \-123 123 123\*(Aq \*(Aq\-123123123\*(Aq \*(Aq\-123123123E0\*(Aq\& \*(Aq00.0123\*(Aq \*(Aq0.0123\*(Aq \*(Aq123E\-4\*(Aq\& \*(Aq123.45E\-2\*(Aq \*(Aq1.2345\*(Aq \*(Aq12345E\-4\*(Aq\& \*(Aq10E+3\*(Aq \*(Aq10000\*(Aq \*(Aq1E4\*(Aq.Ve.PPSome 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) return true or false, while others (\f(CW\*(C`bcmp()\*(C'\fR, \f(CW\*(C`bacmp()\*(C'\fR)return either undef, <0, 0 or >0 and are suited for sort..PPActual math is done by using the class defined with \f(CW\*(C`with =\*(C'\fR Class;> (whichdefaults to BigInts) to represent the mantissa and exponent..PPThe sign \f(CW\*(C`/^[+\-]$/\*(C'\fR is stored separately. The string 'NaN' is used to represent the result when input arguments are not numbers, as well as the result of dividing by zero..ie n .Sh """mantissa()""\fP, \f(CW""exponent()""\fP and \f(CW""parts()""".el .Sh "\f(CWmantissa()\fP, \f(CWexponent()\fP and \f(CWparts()\fP".IX Subsection "mantissa(), exponent() and parts()"\&\f(CW\*(C`mantissa()\*(C'\fR and \f(CW\*(C`exponent()\*(C'\fR return the said parts of the BigFloat as BigInts such that:.PP.Vb 4\& $m = $x\->mantissa();\& $e = $x\->exponent();\& $y = $m * ( 10 ** $e );\& print "ok\en" if $x == $y;.Ve.PP\&\f(CW\*(C`($m,$e) = $x\->parts();\*(C'\fR is just a shortcut giving you both of them..PPA zero is represented and returned as \f(CW0E1\fR, \fBnot\fR \f(CW0E0\fR (after Knuth)..PPCurrently the mantissa is reduced as much as possible, favouring higherexponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0).This might change in the future, so do not depend on it..Sh "Accuracy vs. Precision".IX Subsection "Accuracy vs. Precision"See also: Rounding..PPMath::BigFloat supports both precision (rounding to a certain place before orafter the dot) and accuracy (rounding to a certain number of digits). For afull documentation, examples and tips on these topics please see the largesection about rounding in Math::BigInt..PPSince things like \f(CWsqrt(2)\fR or \f(CW\*(C`1 / 3\*(C'\fR must presented with a limitedaccuracy lest a operation consumes all resources, each operation producesno more than the requested number of digits..PPIf there is no gloabl precision or accuracy set, \fBand\fR the operation inquestion was not called with a requested precision or accuracy, \fBand\fR theinput \f(CW$x\fR has no accuracy or precision set, then a fallback parameter willbe used. For historical reasons, it is called \f(CW\*(C`div_scale\*(C'\fR and can be accessedvia:.PP.Vb 2\& $d = Math::BigFloat\->div_scale(); # query\& Math::BigFloat\->div_scale($n); # set to $n digits.Ve.PPThe default value for \f(CW\*(C`div_scale\*(C'\fR is 40..PPIn case the result of one operation has more digits than specified,it is rounded. The rounding mode taken is either the default mode, or the onesupplied to the operation after the \fIscale\fR:.PP.Vb 7\& $x = Math::BigFloat\->new(2);\& Math::BigFloat\->accuracy(5); # 5 digits max\& $y = $x\->copy()\->bdiv(3); # will give 0.66667\& $y = $x\->copy()\->bdiv(3,6); # will give 0.666667\& $y = $x\->copy()\->bdiv(3,6,undef,\*(Aqodd\*(Aq); # will give 0.666667\& Math::BigFloat\->round_mode(\*(Aqzero\*(Aq);\& $y = $x\->copy()\->bdiv(3,6); # will also give 0.666667.Ve.PPNote that \f(CW\*(C`Math::BigFloat\->accuracy()\*(C'\fR and \f(CW\*(C`Math::BigFloat\->precision()\*(C'\fRset the global variables, and thus \fBany\fR newly created number will be subjectto the global rounding \fBimmediately\fR. This means that in the examples above, the\&\f(CW3\fR as argument to \f(CW\*(C`bdiv()\*(C'\fR will also get an accuracy of \fB5\fR..PPIt is less confusing to either calculate the result fully, and afterwardsround it explicitly, or use the additional parameters to the mathfunctions like so:.PP.Vb 4\& use Math::BigFloat; \& $x = Math::BigFloat\->new(2);\& $y = $x\->copy()\->bdiv(3);\& print $y\->bround(5),"\en"; # will give 0.66667\&\& or\&\& use Math::BigFloat; \& $x = Math::BigFloat\->new(2);\& $y = $x\->copy()\->bdiv(3,5); # will give 0.66667\& print "$y\en";.Ve.Sh "Rounding".IX Subsection "Rounding".IP "ffround ( +$scale )" 2.IX Item "ffround ( +$scale )"Rounds to the \f(CW$scale\fR'th place left from the '.', counting from the dot.The first digit is numbered 1..IP "ffround ( \-$scale )" 2.IX Item "ffround ( -$scale )"Rounds to the \f(CW$scale\fR'th place right from the '.', counting from the dot..IP "ffround ( 0 )" 2.IX Item "ffround ( 0 )"Rounds to an integer..IP "fround ( +$scale )" 2.IX Item "fround ( +$scale )"Preserves accuracy to \f(CW$scale\fR digits from the left (aka significant digits)and pads the rest with zeros. If the number is between 1 and \-1, thesignificant digits count from the first non-zero after the '.'.IP "fround ( \-$scale ) and fround ( 0 )" 2.IX Item "fround ( -$scale ) and fround ( 0 )"These are effectively no-ops..PPAll rounding functions take as a second parameter a rounding mode from one ofthe following: 'even', 'odd', '+inf', '\-inf', 'zero', 'trunc' or 'common'..PPThe default rounding mode is 'even'. By using\&\f(CW\*(C`Math::BigFloat\->round_mode($round_mode);\*(C'\fR you can get and set the defaultmode for subsequent rounding. The usage of \f(CW\*(C`$Math::BigFloat::$round_mode\*(C'\fR isno longer supported.The second parameter to the round functions then overrides the defaulttemporarily..PPThe \f(CW\*(C`as_number()\*(C'\fR function returns a BigInt from a Math::BigFloat. It uses\&'trunc' as rounding mode to make it equivalent to:.PP.Vb 2\& $x = 2.5;\& $y = int($x) + 2;.Ve.PPYou can override this by passing the desired rounding mode as parameter to\&\f(CW\*(C`as_number()\*(C'\fR:.PP.Vb 2\& $x = Math::BigFloat\->new(2.5);\& $y = $x\->as_number(\*(Aqodd\*(Aq); # $y = 3.Ve.SH "METHODS".IX Header "METHODS"Math::BigFloat supports all methods that Math::BigInt supports, except itcalculates non-integer results when possible. Please see Math::BigIntfor a full description of each method. Below are just the most importantdifferences:.Sh "accuracy".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.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!.Sh "\fIbexp()\fP".IX Subsection "bexp()".Vb 1\& $x\->bexp($accuracy); # calculate e ** X.Ve.PPCalculates the expression \f(CW\*(C`e ** $x\*(C'\fR where \f(CW\*(C`e\*(C'\fR is Euler's number..PPThis method was added in v1.82 of Math::BigInt (April 2007)..Sh "\fIbnok()\fP".IX Subsection "bnok()".Vb 1\& $x\->bnok($y); # x over y (binomial coefficient n over k).Ve.PPCalculates the binomial coefficient n over k, also called the \*(L"choose\*(R"function. The result is equivalent to:.PP.Vb 3\& ( n ) n!\& | \- | = \-\-\-\-\-\-\-\& ( k ) k!(n\-k)!.Ve.PPThis method was added in v1.84 of Math::BigInt (April 2007)..Sh "\fIbpi()\fP".IX Subsection "bpi()".Vb 1\& print Math::BigFloat\->bpi(100), "\en";.Ve.PPCalculate \s-1PI\s0 to N digits (including the 3 before the dot). The result isrounded according to the current rounding mode, which defaults to \*(L"even\*(R"..PPThis method was added in v1.87 of Math::BigInt (June 2007)..Sh "\fIbcos()\fP".IX Subsection "bcos()".Vb 2\& my $x = Math::BigFloat\->new(1);\& print $x\->bcos(100), "\en";.Ve.PPCalculate the cosinus of \f(CW$x\fR, modifying \f(CW$x\fR in place..PPThis method was added in v1.87 of Math::BigInt (June 2007)..Sh "\fIbsin()\fP".IX Subsection "bsin()".Vb 2\& my $x = Math::BigFloat\->new(1);\& print $x\->bsin(100), "\en";.Ve.PPCalculate the sinus of \f(CW$x\fR, modifying \f(CW$x\fR in place..PPThis method was added in v1.87 of Math::BigInt (June 2007)..Sh "\fIbatan2()\fP".IX Subsection "batan2()".Vb 3\& my $y = Math::BigFloat\->new(2);\& my $x = Math::BigFloat\->new(3);\& print $y\->batan2($x), "\en";.Ve.PP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -