php小數加減精度問題,比特幣計算精度問題


php小數加減精度問題,比特幣計算精度問題

在php開發時,有小數加減的場景。結果發現不能夠等於預想的值,
bccomp比較二個高精確度數字。
語法: int bccomp(string left operand, string right operand, int [scale]);
此函數比較二個高精確度的數字。輸入二個字符串,若二個字符串一樣大則返回 0;
若左邊的數字字符串 (left operand) 比右邊 (right operand) 的大則返回 +1;若左邊的數字字符串比右邊的小則返回 -1。
scale 是一個可有可無的選項,表示返回值的小數點后所需的位數。
-------------
不要相信浮點數結果精確到了最后一位,也永遠不要比較兩個浮點數是否相等。如果確實需要更高的精度,應該使用任意精度數學函數或者 gmp 函數。
BCMath 任意精度數學
http://www.php.net/manual/zh/book.bc.php
BC 數學 函數
bcadd — 2個任意精度數字的加法計算
bccomp — 比較兩個任意精度的數字
bcdiv — 2個任意精度的數字除法計算
bcmod — 對一個任意精度數字取模
bcmul — 2個任意精度數字乘法計算
bcpow — 任意精度數字的乘方
bcpowmod — Raise an arbitrary precision number to another, reduced by a specified modulus
bcscale — 設置所有bc數學函數的默認小數點保留位數
bcsqrt — 任意精度數字的二次方根
bcsub — 2個任意精度數字的減法

========================
GMP是The GNU MP Bignum Library,是一個開源的數學運算庫,它可以用於任意精度的數學運算,包括有符號整數、有理數和浮點數。它本身並沒有精度限制,只取決於機器的硬件情況。
本函數庫能處理的數值范圍只到長整數與倍浮點數的范圍。若要處理超過上述范圍的數值,要使用 bc 高精確度函數庫 。
本函數庫定義了圓周率的常量 m_pi 值為 3.14159265358979323846。
-------------
http://php.net/manual/zh/ref.gmp.php

gmp_abs — Absolute value
gmp_add — Add numbers
gmp_and — Bitwise AND
gmp_binomial — Calculates binomial coefficient
gmp_clrbit — Clear bit
gmp_cmp — Compare numbers
gmp_com — Calculates one's complement
gmp_div_q — Divide numbers
gmp_div_qr — Divide numbers and get quotient and remainder
gmp_div_r — Remainder of the division of numbers
gmp_div — 別名 gmp_div_q
gmp_divexact — Exact division of numbers
gmp_export — Export to a binary string
gmp_fact — Factorial
gmp_gcd — Calculate GCD
gmp_gcdext — Calculate GCD and multipliers
gmp_hamdist — Hamming distance
gmp_import — Import from a binary string
gmp_init — Create GMP number
gmp_intval — Convert GMP number to integer
gmp_invert — Inverse by modulo
gmp_jacobi — Jacobi symbol
gmp_kronecker — Kronecker symbol
gmp_lcm — Calculate GCD
gmp_legendre — Legendre symbol
gmp_mod — Modulo operation
gmp_mul — Multiply numbers
gmp_neg — Negate number
gmp_nextprime — Find next prime number
gmp_or — Bitwise OR
gmp_perfect_power — Perfect power check
gmp_perfect_square — Perfect square check
gmp_popcount — Population count
gmp_pow — Raise number into power
gmp_powm — Raise number into power with modulo
gmp_prob_prime — Check if number is "probably prime"
gmp_random_bits — Random number
gmp_random_range — Random number
gmp_random_seed — Sets the RNG seed
gmp_random — Random number
gmp_root — Take the integer part of nth root
gmp_rootrem — Take the integer part and remainder of nth root
gmp_scan0 — Scan for 0
gmp_scan1 — Scan for 1
gmp_setbit — Set bit
gmp_sign — Sign of number
gmp_sqrt — Calculate square root
gmp_sqrtrem — Square root with remainder
gmp_strval — Convert GMP number to string
gmp_sub — Subtract numbers
gmp_testbit — Tests if a bit is set
gmp_xor — Bitwise XOR

---------------
Example #1 gmp_add() example
<?php
$sum = gmp_add("123456789012345", "76543210987655");
echo gmp_strval($sum) . "\n";
?>
以上例程會輸出:
200000000000000

Example #1 gmp_sub() example
<?php
$sub = gmp_sub("281474976710656", "4294967296"); // 2^48 - 2^32
echo gmp_strval($sub) . "\n";
?>
以上例程會輸出:
281470681743360

=======================


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM