Home>Article>Backend Development> PHP calculates two particularly large integers example code

PHP calculates two particularly large integers example code

不言
不言 Original
2018-05-07 14:22:09 1233browse

This article mainly introduces the example codes for PHP calculation of two particularly large integers. It has a certain reference value. Now I share it with you. Friends in need can refer to it.

No more nonsense. , the specific code is as follows:

function getIntAdd($a,$b){ $c = ''; $bCount = strlen($b); $aCount = strlen($a); $count = max($bCount,$aCount); $aDiff = $count - $aCount; $bDiff = $count - $bCount; for($i = $count - 1;$i >= 0;$i--){ $aVal = $count - $i <= $aCount ? intval($a[$i - $aDiff]) : 0; $bVal = $count - $i <= $bCount ? intval($b[$i - $bDiff]) : 0; $v = $aVal + $bVal; if(strlen($c) > 0 && strlen($c) >= $count - $i){ $c = ($v + intval($c[0])).substr($c,1,strlen($c) - 1); }else{ $c = $v.$c.''; } } return $c; } $a = '23490234328490289048902384908392849238'; $b = '234320498324982390489328498230984982399290384902384'; $c = getIntAdd($a,$b); print_r($c);

Related recommendations:

PHP Calculate Time Difference

PHP Calculation Cosine Similarity Algorithm Example

The above is the detailed content of PHP calculates two particularly large integers example code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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