內容

名稱

Math::BigFloat - 任意大小浮點數數學套件

語法

use Math::BigFloat;

# Configuration methods (may be used as class methods and instance methods)

Math::BigFloat->accuracy();     # get class accuracy
Math::BigFloat->accuracy($n);   # set class accuracy
Math::BigFloat->precision();    # get class precision
Math::BigFloat->precision($n);  # set class precision
Math::BigFloat->round_mode();   # get class rounding mode
Math::BigFloat->round_mode($m); # set global round mode, must be one of
                                # 'even', 'odd', '+inf', '-inf', 'zero',
                                # 'trunc', or 'common'
Math::BigFloat->config("lib");  # name of backend math library

# Constructor methods (when the class methods below are used as instance
# methods, the value is assigned the invocand)

$x = Math::BigFloat->new($str);               # defaults to 0
$x = Math::BigFloat->new('0x123');            # from hexadecimal
$x = Math::BigFloat->new('0o377');            # from octal
$x = Math::BigFloat->new('0b101');            # from binary
$x = Math::BigFloat->from_hex('0xc.afep+3');  # from hex
$x = Math::BigFloat->from_hex('cafe');        # ditto
$x = Math::BigFloat->from_oct('1.3267p-4');   # from octal
$x = Math::BigFloat->from_oct('01.3267p-4');  # ditto
$x = Math::BigFloat->from_oct('0o1.3267p-4'); # ditto
$x = Math::BigFloat->from_oct('0377');        # ditto
$x = Math::BigFloat->from_bin('0b1.1001p-4'); # from binary
$x = Math::BigFloat->from_bin('0101');        # ditto
$x = Math::BigFloat->from_ieee754($b, "binary64");  # from IEEE-754 bytes
$x = Math::BigFloat->bzero();                 # create a +0
$x = Math::BigFloat->bone();                  # create a +1
$x = Math::BigFloat->bone('-');               # create a -1
$x = Math::BigFloat->binf();                  # create a +inf
$x = Math::BigFloat->binf('-');               # create a -inf
$x = Math::BigFloat->bnan();                  # create a Not-A-Number
$x = Math::BigFloat->bpi();                   # returns pi

$y = $x->copy();        # make a copy (unlike $y = $x)
$y = $x->as_int();      # return as BigInt
$y = $x->as_float();    # return as a Math::BigFloat
$y = $x->as_rat();      # return as a Math::BigRat

# Boolean methods (these don't modify the invocand)

$x->is_zero();          # if $x is 0
$x->is_one();           # if $x is +1
$x->is_one("+");        # ditto
$x->is_one("-");        # if $x is -1
$x->is_inf();           # if $x is +inf or -inf
$x->is_inf("+");        # if $x is +inf
$x->is_inf("-");        # if $x is -inf
$x->is_nan();           # if $x is NaN

$x->is_positive();      # if $x > 0
$x->is_pos();           # ditto
$x->is_negative();      # if $x < 0
$x->is_neg();           # ditto

$x->is_odd();           # if $x is odd
$x->is_even();          # if $x is even
$x->is_int();           # if $x is an integer

# Comparison methods

$x->bcmp($y);           # compare numbers (undef, < 0, == 0, > 0)
$x->bacmp($y);          # compare absolutely (undef, < 0, == 0, > 0)
$x->beq($y);            # true if and only if $x == $y
$x->bne($y);            # true if and only if $x != $y
$x->blt($y);            # true if and only if $x < $y
$x->ble($y);            # true if and only if $x <= $y
$x->bgt($y);            # true if and only if $x > $y
$x->bge($y);            # true if and only if $x >= $y

# Arithmetic methods

$x->bneg();             # negation
$x->babs();             # absolute value
$x->bsgn();             # sign function (-1, 0, 1, or NaN)
$x->bnorm();            # normalize (no-op)
$x->binc();             # increment $x by 1
$x->bdec();             # decrement $x by 1
$x->badd($y);           # addition (add $y to $x)
$x->bsub($y);           # subtraction (subtract $y from $x)
$x->bmul($y);           # multiplication (multiply $x by $y)
$x->bmuladd($y,$z);     # $x = $x * $y + $z
$x->bdiv($y);           # division (floored), set $x to quotient
                        # return (quo,rem) or quo if scalar
$x->btdiv($y);          # division (truncated), set $x to quotient
                        # return (quo,rem) or quo if scalar
$x->bmod($y);           # modulus (x % y)
$x->btmod($y);          # modulus (truncated)
$x->bmodinv($mod);      # modular multiplicative inverse
$x->bmodpow($y,$mod);   # modular exponentiation (($x ** $y) % $mod)
$x->bpow($y);           # power of arguments (x ** y)
$x->blog();             # logarithm of $x to base e (Euler's number)
$x->blog($base);        # logarithm of $x to base $base (e.g., base 2)
$x->bexp();             # calculate e ** $x where e is Euler's number
$x->bnok($y);           # x over y (binomial coefficient n over k)
$x->bsin();             # sine
$x->bcos();             # cosine
$x->batan();            # inverse tangent
$x->batan2($y);         # two-argument inverse tangent
$x->bsqrt();            # calculate square root
$x->broot($y);          # $y'th root of $x (e.g. $y == 3 => cubic root)
$x->bfac();             # factorial of $x (1*2*3*4*..$x)

$x->blsft($n);          # left shift $n places in base 2
$x->blsft($n,$b);       # left shift $n places in base $b
                        # returns (quo,rem) or quo (scalar context)
$x->brsft($n);          # right shift $n places in base 2
$x->brsft($n,$b);       # right shift $n places in base $b
                        # returns (quo,rem) or quo (scalar context)

# Bitwise methods

$x->band($y);           # bitwise and
$x->bior($y);           # bitwise inclusive or
$x->bxor($y);           # bitwise exclusive or
$x->bnot();             # bitwise not (two's complement)

# Rounding methods
$x->round($A,$P,$mode); # round to accuracy or precision using
                        # rounding mode $mode
$x->bround($n);         # accuracy: preserve $n digits
$x->bfround($n);        # $n > 0: round to $nth digit left of dec. point
                        # $n < 0: round to $nth digit right of dec. point
$x->bfloor();           # round towards minus infinity
$x->bceil();            # round towards plus infinity
$x->bint();             # round towards zero

# Other mathematical methods

$x->bgcd($y);            # greatest common divisor
$x->blcm($y);            # least common multiple

# Object property methods (do not modify the invocand)

$x->sign();              # the sign, either +, - or NaN
$x->digit($n);           # the nth digit, counting from the right
$x->digit(-$n);          # the nth digit, counting from the left
$x->length();            # return number of digits in number
($xl,$f) = $x->length(); # length of number and length of fraction
                         # part, latter is always 0 digits long
                         # for Math::BigInt objects
$x->mantissa();          # return (signed) mantissa as BigInt
$x->exponent();          # return exponent as BigInt
$x->parts();             # return (mantissa,exponent) as BigInt
$x->sparts();            # mantissa and exponent (as integers)
$x->nparts();            # mantissa and exponent (normalised)
$x->eparts();            # mantissa and exponent (engineering notation)
$x->dparts();            # integer and fraction part
$x->fparts();            # numerator and denominator
$x->numerator();         # numerator
$x->denominator();       # denominator

# Conversion methods (do not modify the invocand)

$x->bstr();         # decimal notation, possibly zero padded
$x->bsstr();        # string in scientific notation with integers
$x->bnstr();        # string in normalized notation
$x->bestr();        # string in engineering notation
$x->bdstr();        # string in decimal notation
$x->bfstr();        # string in fractional notation

$x->as_hex();       # as signed hexadecimal string with prefixed 0x
$x->as_bin();       # as signed binary string with prefixed 0b
$x->as_oct();       # as signed octal string with prefixed 0
$x->to_ieee754($format); # to bytes encoded according to IEEE 754-2008

# Other conversion methods

$x->numify();           # return as scalar (might overflow or underflow)

說明

Math::BigFloat 提供對任意精度的浮點數支援。也提供 Perl 運算子的覆寫。

如果您將大型浮點數宣告為

$x = Math::BigFloat -> new('12_3.456_789_123_456_789E-2');

使用超載運算子的運算會保留引數,這正是您所預期的。

輸入

這些常式的輸入值可以是任何看起來像數字的純量數字或字串。Perl 接受為文字數字常數的任何內容,都應該會被此模組接受。

有效字串輸入的一些範例

Input string                Resulting value

123                         123
1.23e2                      123
12300e-2                    123

67_538_754                  67538754
-4_5_6.7_8_9e+0_1_0         -4567890000000

0x13a                       314
0x13ap0                     314
0x1.3ap+8                   314
0x0.00013ap+24              314
0x13a000p-12                314

0o472                       314
0o1.164p+8                  314
0o0.0001164p+20             314
0o1164000p-10               314

0472                        472     Note!
01.164p+8                   314
00.0001164p+20              314
01164000p-10                314

0b100111010                 314
0b1.0011101p+8              314
0b0.00010011101p+12         314
0b100111010000p-3           314

0x1.921fb5p+1               3.14159262180328369140625e+0
0o1.2677025p1               2.71828174591064453125
01.2677025p1                2.71828174591064453125
0b1.1001p-4                 9.765625e-2

輸出

輸出值通常是 Math::BigFloat 物件。

布林運算子 is_zero()is_one()is_inf() 等會傳回 true 或 false。

比較運算子 bcmp()bacmp()) 會傳回 -1、0、1 或 undef。

方法

Math::BigFloat 支援 Math::BigInt 支援的所有方法,但它會在可能的情況下計算非整數結果。請參閱 Math::BigInt 以取得每個方法的完整說明。以下是最重要的差異

組態方法

accuracy()
$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

設定或取得全域或區域精確度,亦即結果有多少個有效數字。如果您設定全域精確度,這也會套用至 new()!

警告!精確度會「黏住」,例如一旦您在 CLASS->accuracy($A) 的影響下建立一個數字,使用該數字進行數學運算的所有結果也會四捨五入。

在多數情況下,您可能應該使用 "Math::BigInt 中的 round()""Math::BigInt 中的 bround()""Math::BigInt 中的 bfround()" 明確四捨五入結果,或將所需的精確度傳遞給數學運算作為其他參數

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
precision()
$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

注意:您可能想改用 "accuracy()"。使用 "accuracy()" 設定每個結果應有的數字數量,使用 "precision()" 設定四捨五入的位置!

建構函式方法

from_hex()
$x -> from_hex("0x1.921fb54442d18p+1");
$x = Math::BigFloat -> from_hex("0x1.921fb54442d18p+1");

將輸入詮釋為十六進位字串。字首(「0x」、「x」,不分大小寫)為選用項目。任何兩個數字之間都可以放一個底線字元(「_」)。如果輸入無效,會傳回 NaN。指數使用十進位數字表示,以 2 為底。

如果以執行個體方法呼叫,值會指定給被呼叫者。

from_oct()
$x -> from_oct("1.3267p-4");
$x = Math::BigFloat -> from_oct("1.3267p-4");

將輸入詮釋為八進位字串。任何兩個數字之間都可以放一個底線字元(「_」)。如果輸入無效,會傳回 NaN。指數使用十進位數字表示,以 2 為底。

如果以執行個體方法呼叫,值會指定給被呼叫者。

from_bin()
$x -> from_bin("0b1.1001p-4");
$x = Math::BigFloat -> from_bin("0b1.1001p-4");

將輸入詮釋為十六進位字串。字首(「0b」或「b」,不分大小寫)為選用項目。任何兩個數字之間都可以放一個底線字元(「_」)。如果輸入無效,會傳回 NaN。指數使用十進位數字表示,以 2 為底。

如果以執行個體方法呼叫,值會指定給被呼叫者。

from_ieee754()

將輸入詮釋為 IEEE754-2008 中所述編碼的值。輸入可以是位元組字串、十六進位字串或二進位字串。輸入假設為大端序位元組順序。

# both $dbl and $mbf are 3.141592...
$bytes = "\x40\x09\x21\xfb\x54\x44\x2d\x18";
$dbl = unpack "d>", $bytes;
$mbf = Math::BigFloat -> from_ieee754($bytes, "binary64");
bpi()
print Math::BigFloat->bpi(100), "\n";

計算 PI 至 N 位數(包括小數點前的 3 位)。結果會根據目前的捨入模式四捨五入,預設為「偶數」。

這個方法新增於 Math::BigInt 的 v1.87(2007 年 6 月)。

算術方法

bmuladd()
$x->bmuladd($y,$z);

將 $x 乘以 $y,然後將 $z 加到結果中。

這個方法新增於 Math::BigInt 的 v1.87(2007 年 6 月)。

bdiv()
$q = $x->bdiv($y);
($q, $r) = $x->bdiv($y);

在標量上下文中,將 $x 除以 $y,並將結果返回到給定的或預設的準確度/精度。在列表上下文中,執行取整除法(F 除法),傳回整數 $q 和餘數 $r,使得 $x = $q * $y + $r。餘數(模數)等於 $x->bmod($y) 傳回的值。

bmod()
$x->bmod($y);

傳回 $x 模 $y。當 $x 為有限值,且 $y 為有限值且非零時,結果與取整除法(F 除法)後的餘數相同。如果 $x 和 $y 都是整數,則結果與 Perl 的 % 算子傳回的結果相同。

bexp()
$x->bexp($accuracy);            # calculate e ** X

計算表達式 e ** $x,其中 e 是歐拉數。

此方法已新增至 Math::BigInt 的 v1.82(2007 年 4 月)。

bnok()
$x->bnok($y);   # x over y (binomial coefficient n over k)

計算二項式係數 n over k,也稱為「選擇」函數。結果等於

( n )      n!
| - |  = -------
( k )    k!(n-k)!

此方法已新增至 Math::BigInt 的 v1.84(2007 年 4 月)。

bsin()
my $x = Math::BigFloat->new(1);
print $x->bsin(100), "\n";

計算 $x 的正弦,修改 $x 的值。

這個方法新增於 Math::BigInt 的 v1.87(2007 年 6 月)。

bcos()
my $x = Math::BigFloat->new(1);
print $x->bcos(100), "\n";

計算 $x 的餘弦,修改 $x 的值。

這個方法新增於 Math::BigInt 的 v1.87(2007 年 6 月)。

batan()
my $x = Math::BigFloat->new(1);
print $x->batan(100), "\n";

計算 $x 的反正切,修改 $x 的值。另請參閱 "batan2()"

這個方法新增於 Math::BigInt 的 v1.87(2007 年 6 月)。

batan2()
my $y = Math::BigFloat->new(2);
my $x = Math::BigFloat->new(3);
print $y->batan2($x), "\n";

計算 $y 除以 $x 的反正切,修改 $y 的值。另請參閱 "batan()"

這個方法新增於 Math::BigInt 的 v1.87(2007 年 6 月)。

as_float()

當 Math::BigFloat 遇見它不知道如何處理的物件時,就會呼叫此方法。例如,假設 $x 是 Math::BigFloat 或其子類別,而 $y 已定義,但不是 Math::BigFloat 或其子類別。如果您執行

$x -> badd($y);

$y 需要轉換成 $x 可以處理的物件。首先檢查 $y 是否是 $x 可以升級的物件。如果是,則不會再進行其他嘗試。接下來是查看 $y 是否支援 as_float() 方法。as_float() 方法預期傳回一個與 $x 相同類別、其子類別的物件,或一個 ref($x)->new() 可以解析以建立物件的字串。

在 Math::BigFloat 中,as_float() 的效果與 copy() 相同。

to_ieee754()

使用 IEEE 754-2008 中指定的格式,將尾數編碼為位元組字串。請注意,編碼值是該值的最近可能表示形式。此值可能與尾數中的值不完全相同。

# $x = 3.1415926535897932385
$x = Math::BigFloat -> bpi(30);

$b = $x -> to_ieee754("binary64");  # encode as 8 bytes
$h = unpack "H*", $b;               # "400921fb54442d18"

# 3.141592653589793115997963...
$y = Math::BigFloat -> from_ieee754($h, "binary64");

IEEE 754-2008 中的所有二進位格式都可接受。為了方便,會辨識一些別名:將「half」視為「binary16」,將「single」視為「binary32」,將「double」視為「binary64」,將「quadruple」視為「binary128」,將「octuple」視為「binary256」,將「sexdecuple」視為「binary512」。

另請參閱 https://en.wikipedia.org/wiki/IEEE_754

ACCURACY AND PRECISION

另請參閱:捨入

Math::BigFloat 支援精確度(在小數點前或後捨入到某個位數)和準確度(捨入到某個數字位數)。如需這些主題的完整文件、範例和提示,請參閱 Math::BigInt 中有關捨入的大部分區段。

由於像 sqrt(2)1 / 3 之類的東西必須以有限的準確度呈現,否則運算會消耗所有資源,因此每個運算產生的數字位數不會超過所要求的數字位數。

如果沒有設定全域精確度或準確度,所討論的運算沒有以要求的精確度或準確度呼叫,輸入 $x 沒有設定精確度或準確度,則會使用後備參數。基於歷史原因,它稱為 div_scale,可透過以下方式存取

$d = Math::BigFloat->div_scale();       # query
Math::BigFloat->div_scale($n);          # set to $n digits

div_scale 的預設值為 40。

如果某個運算的結果數字位數多於指定數字位數,則會將其捨入。採用的捨入模式可能是預設模式,或是在 scale 之後提供給運算的模式

$x = Math::BigFloat->new(2);
Math::BigFloat->accuracy(5);              # 5 digits max
$y = $x->copy()->bdiv(3);                 # gives 0.66667
$y = $x->copy()->bdiv(3,6);               # gives 0.666667
$y = $x->copy()->bdiv(3,6,undef,'odd');   # gives 0.666667
Math::BigFloat->round_mode('zero');
$y = $x->copy()->bdiv(3,6);               # will also give 0.666667

請注意,Math::BigFloat->accuracy()Math::BigFloat->precision() 會設定全域變數,因此任何新建立的數字立即會受到全域捨入的影響。這表示在上述範例中,作為 bdiv() 參數的 3 也會獲得5 的準確度。

比較不會造成混淆的方法是完全計算結果,然後明確地將其捨入,或使用數學函數的附加參數,如下所示

use Math::BigFloat;
$x = Math::BigFloat->new(2);
$y = $x->copy()->bdiv(3);
print $y->bround(5),"\n";               # gives 0.66667

or

use Math::BigFloat;
$x = Math::BigFloat->new(2);
$y = $x->copy()->bdiv(3,5);             # gives 0.66667
print "$y\n";

捨入

bfround ( +$scale )

從小數點「.」向左捨入到第 $scale 位,從小數點開始算起。第一個數字編號為 1。

bfround ( -$scale )

將小數點右邊第 $scale 位數四捨五入,從小數點開始算。

bfround ( 0 )

四捨五入為整數。

bround ( +$scale )

保留從左邊開始的 $scale 位數的準確度(又稱有效數字),並用零填充其餘部分。如果數字介於 1 和 -1 之間,則有效數字從小數點後的第一個非零數字開始計算。

bround ( -$scale ) 和 bround ( 0 )

這些實際上沒有運算。

所有捨入函數都將以下其中一種捨入模式作為第二個參數:'even'、'odd'、'+inf'、'-inf'、'zero'、'trunc' 或 'common'。

預設捨入模式為 'even'。透過使用 Math::BigFloat->round_mode($round_mode);,您可以取得和設定後續捨入的預設模式。不再支援使用 $Math::BigFloat::$round_mode。然後,捨入函數的第二個參數會暫時覆寫預設值。

as_number() 函數會從 Math::BigFloat 傳回 BigInt。它使用 'trunc' 作為捨入模式,使其等於

$x = 2.5;
$y = int($x) + 2;

您可以透過將所需的捨入模式傳遞為 as_number() 的參數來覆寫此設定

$x = Math::BigFloat->new(2.5);
$y = $x->as_number('odd');      # $y = 3

數值文字

use Math::BigFloat ':constant' 之後,給定範圍內的所有數值文字都會轉換為 Math::BigFloat 物件。此轉換會在編譯時發生。

例如,

perl -MMath::BigFloat=:constant -le 'print 2e-150'

會列印 2e-150 的確切值。請注意,如果不轉換常數,則會使用 Perl 純量計算表達式 2e-150,這會導致不準確的結果。

請注意,字串不會受到影響,因此

use Math::BigFloat qw/:constant/;

$y = "1234567890123456789012345678901234567890"
        + "123456789123456789";

不會提供您預期的結果。您需要在至少一個運算元周圍加上明確的 Math::BigFloat->new()。您還應該引用大型常數以防止精度損失

use Math::BigFloat;

$x = Math::BigFloat->new("1234567889123456789123456789123456789");

如果不加引號,Perl 會在編譯時將大型數字轉換為浮點常數,然後在執行時將結果轉換為 Math::BigFloat 物件,這會導致不準確的結果。

十六進位、八進位和二進位浮點文字

Perl(和此模組)接受十六進位、八進位和二進位浮點文字,但在 v5.32.0 之前的 Perl 版本中請小心使用,因為某些版本的 Perl 會在不提示的情況下提供錯誤的結果。以下是撰寫數字十進位數 314 的一些不同方式範例。

十六進位浮點文字

0x1.3ap+8         0X1.3AP+8
0x1.3ap8          0X1.3AP8
0x13a0p-4         0X13A0P-4

八進位浮點文字(加上 "0" 前綴)

01.164p+8         01.164P+8
01.164p8          01.164P8
011640p-4         011640P-4

八進位浮點文字(加上 "0o" 前綴)(需要 v5.34.0)

0o1.164p+8        0O1.164P+8
0o1.164p8         0O1.164P8
0o11640p-4        0O11640P-4

二進位浮點文字

0b1.0011101p+8    0B1.0011101P+8
0b1.0011101p8     0B1.0011101P8
0b10011101000p-2  0B10011101000P-2

數學函式庫

預設使用 Math::BigInt::Calc 模組來處理數字的數學運算。這等同於說

use Math::BigFloat lib => "Calc";

你可以使用以下方式來變更

use Math::BigFloat lib => "GMP";

注意:一般用途的套件不應明確指定要使用的函式庫;讓腳本作者決定哪個最好。

注意:當無法載入所要求的函式庫時,關鍵字「lib」會發出警告。若要抑制警告,請改用「try」

use Math::BigFloat try => "GMP";

如果你的腳本處理的數字很大,而 Calc 對它們來說太慢,你也可以載入其中一個函式庫,如果無法使用任何一個函式庫,程式碼就會終止

use Math::BigFloat only => "GMP,Pari";

以下會先嘗試尋找 Math::BigInt::Foo,然後是 Math::BigInt::Bar,如果也失敗,則改用 Math::BigInt::Calc

use Math::BigFloat lib => "Foo,Math::BigInt::Bar";

請參閱各個底層函式庫文件以取得進一步的詳細資料。

請參閱 Math::BigInt 以取得有關使用不同底層函式庫的更多詳細資料。

使用 Math::BigInt::Lite

出於向後相容性的原因,仍然可以要求使用 Math::BigFloat 的不同儲存類別

use Math::BigFloat with => 'Math::BigInt::Lite';

不過,這個要求會被忽略,因為目前的程式碼現在使用底層數學函式庫來直接儲存數字部分。

匯出

Math::BigFloat 預設不匯出任何內容,但可以匯出 bpi() 方法

use Math::BigFloat qw/bpi/;

print bpi(10), "\n";

注意事項

不要試圖在切換函式庫之間插入一些運算

require Math::BigFloat;
my $matter = Math::BigFloat->bone() + 4;    # load BigInt and Calc
Math::BigFloat->import( lib => 'Pari' );    # load Pari, too
my $anti_matter = Math::BigFloat->bone()+4; # now use Pari

這會建立物件,其數字儲存在兩個不同的後端函式庫中,當你將它們一起使用時,會發生非常糟糕的事

my $flash_and_bang = $matter + $anti_matter;    # Don't do this!
字串化、bstr()

stringify 和 bstr() 現在都捨棄開頭的「+」。舊程式碼會傳回「+1.23」,新程式碼會傳回「1.23」。請參閱 Math::BigInt 中的文件以了解原因和詳細資料。

brsft()

以下可能不會印出你預期的內容

my $c = Math::BigFloat->new('3.14159');
print $c->brsft(3,10),"\n";     # prints 0.00314153.1415

它會印出商數和餘數,因為 print 在清單內容中呼叫 brsft()。此外,$c->brsft() 會修改 $c,所以要小心。你可能想要使用

print scalar $c->copy()->brsft(3,10),"\n";
# or if you really want to modify $c
print scalar $c->brsft(3,10),"\n";

代替。

修改和 =

小心

$x = Math::BigFloat->new(5);
$y = $x;

它不會執行你所想的,例如製作 $x 的副本。它只會在同一個物件中建立第二個參考,並將其儲存在 $y 中。因此,任何修改 $x 的動作都會修改 $y(除了過載的數學運算子),反之亦然。請參閱 Math::BigInt 以取得詳細資料以及如何避免這種情況。

precision() 與 accuracy()

一個常見的陷阱是在您想要將結果四捨五入到特定位數時使用 "precision()"

use Math::BigFloat;

Math::BigFloat->precision(4);           # does not do what you
                                        # think it does
my $x = Math::BigFloat->new(12345);     # rounds $x to "12000"!
print "$x\n";                           # print "12000"
my $y = Math::BigFloat->new(3);         # rounds $y to "0"!
print "$y\n";                           # print "0"
$z = $x / $y;                           # 12000 / 0 => NaN!
print "$z\n";
print $z->precision(),"\n";             # 4

"accuracy()" 替換 "precision()" 可能也不是您想要的

use Math::BigFloat;

Math::BigFloat->accuracy(4);          # enables global rounding:
my $x = Math::BigFloat->new(123456);  # rounded immediately
                                      #   to "12350"
print "$x\n";                         # print "123500"
my $y = Math::BigFloat->new(3);       # rounded to "3
print "$y\n";                         # print "3"
print $z = $x->copy()->bdiv($y),"\n"; # 41170
print $z->accuracy(),"\n";            # 4

您想要使用的是

use Math::BigFloat;

my $x = Math::BigFloat->new(123456);    # no rounding
print "$x\n";                           # print "123456"
my $y = Math::BigFloat->new(3);         # no rounding
print "$y\n";                           # print "3"
print $z = $x->copy()->bdiv($y,4),"\n"; # 41150
print $z->accuracy(),"\n";              # undef

除了計算您預期的內容之外,最後一個範例也不會以準確度或精確度設定「污染」結果,這會影響任何後續運算。

BUGS

請將任何錯誤或功能要求回報至 bug-math-bigint at rt.cpan.org,或透過 https://rt.cpan.org/Ticket/Create.html?Queue=Math-BigInt 上的網路介面(需要登入)。我們將會收到通知,然後當我進行變更時,您會自動收到關於您錯誤進度的通知。

支援

您可以使用 perldoc 指令尋找此模組的文件。

perldoc Math::BigFloat

您也可以在以下位置尋找資訊

授權

此程式是自由軟體;您可以在與 Perl 相同的條款下重新散布或修改它。

另請參閱

Math::BigIntMath::BigInt 以及後端 Math::BigInt::FastCalcMath::BigInt::GMPMath::BigInt::Pari

實用程式 bignumbigintbigrat

作者