Home » Php » Arithmetic with Arbitrarily Large Integers in PHP

# Arithmetic with Arbitrarily Large Integers in PHP

Questions:

Ok, so PHP isn’t the best language to be dealing with arbitrarily large integers in, considering that it only natively supports 32-bit signed integers. What I’m trying to do though is create a class that could represent an arbitrarily large binary number and be able to perform simple arithmetic operations on two of them (add/subtract/multiply/divide).

My target is dealing with 128-bit integers.

There’s a couple of approaches I’m looking at, and problems I see with them. Any input or commentary on what you would choose and how you might go about it would be greatly appreciated.

Approach #1: Create a 128-bit integer class that stores its integer internally as four 32-bit integers. The only problem with this approach is that I’m not sure how to go about handling overflow/underflow issues when manipulating individual chunks of the two operands.

Approach #2: Use the bcmath extension, as this looks like something it was designed to tackle. My only worry in taking this approach is the scale setting of the bcmath extension, because there can’t be any rounding errors in my 128-bit integers; they must be precise. I’m also worried about being able to eventually convert the result of the bcmath functions into a binary string (which I’ll later need to shove into some mcrypt encryption functions).

Approach #3: Store the numbers as binary strings (probably LSB first). Theoretically I should be able to store integers of any arbitrary size this way. All I would have to do is write the four basic arithmetic functions to perform add/sub/mult/div on two binary strings and produce a binary string result. This is exactly the format I need to hand over to mcrypt as well, so that’s an added plus. This is the approach I think has the most promise at the moment, but the one sticking point I’ve got is that PHP doesn’t offer me any way to manipulate the individual bits (that I know of). I believe I’d have to break it up into byte-sized chunks (no pun intended), at which point my questions about handling overflow/underflow from Approach #1 apply.

The PHP GMP extension will be better for this. As an added bonus, you can use it to do your decimal-to-binary conversion, like so:

``````gmp_strval(gmp_init(\$n, 10), 2);
``````

There are already various classes available for this so you may wish to look at them before writing your own solution (if indeed writing your own solution is still needed).

As far as I can tell, the bcmath extension is the one you’ll want. The data in the PHP manual is a little sparse, but you out to be able to set the precision to be exactly what you need by using the bcscale() function, or the optional third parameter in most of the other bcmath functions. Not too sure on the binary strings thing, but a bit of googling tells me you ought to be able to do with by making use of the pack() function.

I implemented the following PEMDAS complaint BC evaluator which may be useful to you.

``````function BC(\$string, \$precision = 32)
{
{
if (is_array(\$string) === true)
{
if ((count(\$string = array_slice(\$string, 1)) == 3) && (bcscale(\$precision) === true))
{
\$callback = array('^' => 'pow', '*' => 'mul', '/' => 'div', '%' => 'mod', '+' => 'add', '-' => 'sub');

if (array_key_exists(\$operator = current(array_splice(\$string, 1, 1)), \$callback) === true)
{
\$x = 1;
\$result = @call_user_func_array('bc' . \$callback[\$operator], \$string);

if ((strcmp('^', \$operator) === 0) && ((\$i = fmod(array_pop(\$string), 1)) > 0))
{
\$y = BC(sprintf('((%1\$s * %2\$s ^ (1 - %3\$s)) / %3\$s) - (%2\$s / %3\$s) + %2\$s', \$string = array_shift(\$string), \$x, \$i = pow(\$i, -1)));

do
{
\$x = \$y;
\$y = BC(sprintf('((%1\$s * %2\$s ^ (1 - %3\$s)) / %3\$s) - (%2\$s / %3\$s) + %2\$s', \$string, \$x, \$i));
}

while (BC(sprintf('%s > %s', \$x, \$y)));
}

if (strpos(\$result = bcmul(\$x, \$result), '.') !== false)
{
\$result = rtrim(rtrim(\$result, '0'), '.');

if (preg_match(sprintf('~[.]{%u}\$~', \$precision), \$result) > 0)
{
\$result = bcadd(\$result, (strncmp('-', \$result, 1) === 0) ? -1 : 1, 0);
}

else if (preg_match(sprintf('~[.]{%u}\$~', \$precision - 1), \$result) > 0)
{
\$result = bcmul(\$result, 1, 0);
}
}

return \$result;
}

return intval(version_compare(call_user_func_array('bccomp', \$string), 0, \$operator));
}

\$string = array_shift(\$string);
}

\$string = str_replace(' ', '', str_ireplace('e', ' * 10 ^ ', \$string));

while (preg_match('~[(]([^()]++)[)]~', \$string) > 0)
{
\$string = preg_replace_callback('~[(]([^()]++)[)]~', __FUNCTION__, \$string);
}

foreach (array('\^', '[\*/%]', '[\+-]', '[<>]=?|={1,2}') as \$operator)
{
while (preg_match(sprintf('~(?<![0-9])(%1\$s)(%2\$s)(%1\$s)~', '[+-]?(?:[0-9]++(?:[.][0-9]*+)?|[.][0-9]++)', \$operator), \$string) > 0)
{
\$string = preg_replace_callback(sprintf('~(?<![0-9])(%1\$s)(%2\$s)(%1\$s)~', '[+-]?(?:[0-9]++(?:[.][0-9]*+)?|[.][0-9]++)', \$operator), __FUNCTION__, \$string, 1);
}
}
}

return (preg_match('~^[+-]?[0-9]++(?:[.][0-9]++)?\$~', \$string) > 0) ? \$string : false;
}
``````

It automatically deals with rounding errors, just set the precision to whatever digits you need.