php關於精准計算的模塊 BCMath


Php: BCMath

bc是Binary Calculator的縮寫。bc*函數的參數都是操作數加上一個可選的 [int scale],比如string bcadd(string $left_operand, string $right_operand[, int $scale]),如果scale沒有提供,就用bcscale的缺省值。這里大數直接用一個由0-9組成的string表示,計算結果返回的也是一個 string。

bcadd — 將兩個高精度數字相加

  string bcadd(string left operand, string right operand [, int scale]);

bccomp — 比較兩個高精度數字,返回-1, 0, 1

  int bccomp(string left operand, string right operand [, int scale]);

bcdiv — 將兩個高精度數字相除

  string bcdiv(string left operand, string right operand [, int scale]);

bcmod — 求高精度數字余數

  string bcmod(string left operand, string modulus);

 bcmul — 將兩個高精度數字相乘

  string bcmul(string left operand, string right operand [, int scale]);

 bcpow — 求高精度數字乘方

  string bcpow(string x, string y [, int scale]);

 bcpowmod — 求高精度數字乘方求模,數論里非常常用

  string bcpowmod ( string x, string y, string modulus [, int scale]);

 bcscale — 配置默認小數點位數,相當於就是Linux bc中的”scale=”

  string bcscale(int scale);

 bcsqrt — 求高精度數字平方根

  string bcsqrt(string operand [, int scale]);

 bcsub — 將兩個高精度數字相減

  string bcsub(string left operand, string right operand [, int scale]);

可以參考手冊去查這些函數的應用。

bcmath的安裝
bcmath這個擴展在php的壓縮包中都是有的,需要重新編譯一下才能夠支持;
cd php-5.2.7/ext/bcmath
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install (make 可以得到so路徑)
so路徑拷貝到 extension路徑
vim php.ini
extension=bcmath.so
重啟apach


PHP code?

/*bcsub 函數說明
此函數將二個高精確度的數字相減,傳入二個字符串,以左邊的 (left operand) 減去右邊的 (right operand) 值。scale 是一個可有可無的選項,表示返回值的小數點后所需的位數。*/
//下面的代碼我是保證了小數點一位數字
$aa=bcsub(134.7,52.5,1);
echo $aa;//exit;
$bb=bcsub($aa,82.2,1);
echo $bb;//exit;


免責聲明!

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



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