關於PHP精確計算模組

碼魘發表於2019-02-16

PHP精確計算的BC函式


簡介:
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]);

相關文章