Big Integers

Zend\Math\BigInteger\BigInteger offers a class to manage arbitrary length integers. PHP supports integer numbers with a maximum value of PHP_INT_MAX, a value defined by your processor architecture and available memory. If you need to manage integers bigger than PHP_INT_MAX, you need to use external libraries or PHP extensions such as GMP or BC Math.

Zend\Math\BigInteger\BigInteger is able to manage big integers using either the GMP or the BC Math extensions as adapters.

Methods available

The mathematical functions implemented in Zend\Math\BigInteger\BigInteger are:

Examples

The example below illustrates using the BC Math adapter to calculate the sum of two random integers with 100 digits each.

use Zend\Math\BigInteger\BigInteger;
use Zend\Math\Rand;

$bigInt = BigInteger::factory('bcmath');

$x = Rand::getString(100, '0123456789');
$y = Rand::getString(100, '0123456789');

$sum = $bigInt->add($x, $y);
$len = strlen($sum);

printf("%{$len}s +\n%{$len}s =\n%s\n%s\n", $x, $y, str_repeat('-', $len), $sum);

Note that the big integers are managed using strings; even the result of the sum is represented as a string.

Next is an example using the BC Math adapter to generate the binary representation of a negative big integer containing 100 digits.

use Zend\Math\BigInteger\BigInteger;
use Zend\Math\Rand;

$bigInt = BigInteger::factory('bcmath');

$digits = 100;
$x = '-' . Rand::getString($digits, '0123456789');

$byte = $bigInt->intToBin($x);

printf(
    "The binary representation of a big integer with %d digits:\n%s\nis (in Base64 format): %s\n",
    $digits
    $x,
    base64_encode($byte)
);
printf("Length in bytes: %d\n", strlen($byte));

$byte = $bigInt->intToBin($x, true);

printf(
    "The two's complement binary representation of the big integer with %d digits:\n"
    . "%s\nis (in Base64 format): %s\n",
    $digits,
    $x,
    base64_encode($byte)
);
printf("Length in bytes: %d\n", strlen($byte));

The above generates the binary representation of the big integer number using the default binary format, and the two's complement representation (specified with the true parameter in the intToBin function).