Home > Backend Development > PHP Tutorial > PHP int 超大溢出整数的 加减运算函数,如果有更好的方法欢迎探讨

PHP int 超大溢出整数的 加减运算函数,如果有更好的方法欢迎探讨

WBOY
Release: 2016-06-13 12:38:29
Original
1247 people have browsed it

【分享】PHP int 超大溢出整数的 加减运算函数,如果有更好的方法欢迎探讨
分享一个溢出整数加减的运算函数,刚刚写的,对于溢出的整数可以用这个来进行加减运算。
遗憾的几点是:

<br />
一代码太多;<br />
二只有加减运算,乘除取余都没有;<br />
Copy after login



其实还有一个更简便的方式就是用SQL数据库的:SELECT n1+n2;
<br />
mysql> SELECT 11234123413241341234123412341234+1;<br />
+------------------------------------+<br />
| 11234123413241341234123412341234+1 |<br />
+------------------------------------+<br />
|   11234123413241341234123412341235 |<br />
+------------------------------------+<br />
1 row in set (0.00 sec)<br />
<br />
<br />
mysql> SELECT 11234123413241341234123412341234*12341234123;<br />
+----------------------------------------------+<br />
| 11234123413241341234123412341234*12341234123 |<br />
+----------------------------------------------+<br />
|   138642947209487270472850788378836360727782 |<br />
+----------------------------------------------+<br />
1 row in set (0.00 sec)<br />
<br />
<br />
Copy after login


如果有更好的方法,请随时回帖或者发个信息给我。欢迎探讨。

<br>
/* big int operate [by fuzb 20130826] */<br>
function bigintO($num1,$op,$num2)<br>
{<br>
    $arr = array();<br>
    $endop = '';<br>
    $num1o = $num1;<br>
    $num2o = $num2;<br>
    if($num1 
    {<br>
        $c1 = -1;<br>
        $num1 = preg_replace('/^(-)/','',$num1);<br>
<br>
    } else {<br>
        $c1 = 1;<br>
    }<br>
<br>
    if($num2 
    {<br>
        $c2 = -1;<br>
        $num2 = preg_replace('/^(-)/','',$num2);<br>
    } else {<br>
        $c2 = 1;<br>
    }<br>
<br>
    $len1 = strlen($num1);<br>
    $len2 = strlen($num2);<br>
    $len = max(strlen($num1),strlen($num2));<br>
    if($len1 
    if($len2 
<br>
<br>
    if($op == '+')<br>
    {<br>
        if($c1 == $c2)<br>
        { <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template