【分享】PHP int 超大溢出整數的 加減運算函數,如果有更好的方法歡迎探討
分享一個溢出整數加減的運算函數,剛剛寫的,對於溢出的整數可以用這個來進行加減運算。
遺憾的幾點是:
一代碼太多;
二隻有加減運算,乘除取餘都沒有;
其實還有一個更簡便的方式就是用SQL資料庫的:SELECT n1+n2;
mysql> SELECT 11234123413241341234123412341234+1;
+------------------------------------+
| 11234123413241341234123412341234+1 |
+------------------------------------+
| 11234123413241341234123412341235 |
+------------------------------------+
1 row in set (0.00 sec)
mysql> SELECT 11234123413241341234123412341234*12341234123;
+----------------------------------------------+
| 11234123413241341234123412341234*12341234123 |
+----------------------------------------------+
| 138642947209487270472850788378836360727782 |
+----------------------------------------------+
1 row in set (0.00 sec)
如果有更好的方法,請隨時回帖或者發個資訊給我。歡迎探討。
/* big int operate [by fuzb 20130826] */
function bigintO($num1,$op,$num2)
{
$arr = array();
$endop = '';
$num1o = $num1;
$num2o = $num2;
if($num1 < 0)
{
$c1 = -1;
$num1 = preg_replace('/^(-)/','',$num1);
} else {
$c1 = 1;
}
if($num2 < 0)
{
$c2 = -1;
$num2 = preg_replace('/^(-)/','',$num2);
} else {
$c2 = 1;
}
$len1 = strlen($num1);
$len2 = strlen($num2);
$len = max(strlen($num1),strlen($num2));
if($len1 < $len) $num1 = str_pad('0',$len - $len1).$num1;
if($len2 < $len) $num2 = str_pad('0',$len - $len2).$num2;
if($op == '+')
{
if($c1 == $c2)
{