Working With Large Numbers in PHP

Working with large numbers in PHP

For some reason, there are two standard libraries in PHP handling the arbitrary length/precision numbers: BC Math and GMP. I personally prefer GMP, as it's fresher and has richer API.

Based on GMP I've implemented Decimal2 class for storing and processing currency amounts (like USD 100.25). A lot of mod calculations there w/o any problems. Tested with very large numbers.

Extremely Large Integers in PHP

PHP uses floats, or signed 32-bit ints to store numbers. Clearly this is not enough. You can use arbitrary precision arithmetic to store these large numbers. See the PHP book on BC Math for more information:

http://php.net/manual/en/book.bc.php

Working with large numbers in php is outputing 0

You can use GMP library for this (https://www.php.net/manual/en/ref.gmp.php):

echo "calculation of Ga og Gb<br>";
echo "<br>user A shareable value<br>";
$calculatedSecretKeyA = gmp_mod ( gmp_pow ($gen, $saltA), $mod);
echo $gen . "^" . $saltA . " mod " . $mod . " = " . $calculatedSecretKeyA;

Output:

calculation of Ga og Gb

user A shareable value
877^517 mod 80182923 = 79127908

How to work with big numbers in PHP?

You can go for BCMath to work with big numbers.

PHP gives incorrect results when formatting long numbers

Your number is actually too big for php standard integers. php uses 64 bit integers which can hold values within range -9223372036854775808 (PHP_INT_MIN)
to +9223372036854775807 (PHP_INT_MAX).

Your number is about 87 bits long which simply is too much.

If you really need such big numbers you should use the php BC math types, explained in the manual: http://php.net/manual/en/ref.bc.php

If you just want to format a string formed like a huge number then use something like this:

function number_format_string($number) {
return strrev(implode(',', str_split(strrev($number), 3)));
}

$x = '100000000000000000000000000';

$x = number_format_string($x);
echo "The number is: $x\n";

// Output: The number is: 100,000,000,000,000,000,000,000,000

Edit:
Added strrev() to function because the string needs to be reversed before splitting it up (thanks to @ceeee for the hint). This ensures that the delimiter is placed at right position when length of input is not divisible by 3. Generated string needs to be reversed afterwards again.

Working example can be found at http://sandbox.onlinephpfunctions.com/code/c10fc9b9e2c65a27710fb6be3a0202ad492e3e9a

Large numbers in PHP

From the official PHP documentation on pow:

base raised to the power of exp. If both arguments are non-negative
integers and the result can be represented as an integer, the result
will be returned with integer type, otherwise it will be returned as a
float.

$largenumber = pow(142,142);
var_dump($largenumber); //float(INF)

pow returns a float, which decbin tries to convert to an int and where you exceed the maximum integer value.

Handling large numbers in PHP

If you're converting to an int for sanity purposes (so it appears), perhaps you could just adjust it to evaluate it purely on it's numeric basis instead of int datatype:

if(ctype_digit($name) && ($found = array_key_exists($id = $name, $albums)))
return ($found ? $id : false);
//etc

Actually, should this work too?

if(ctype_digit($name) && ($found = array_key_exists($name, $albums)))
return ($found ? $name: false);
//etc

Dealing with large numbers in php

Why do you need number_format($k,0,'','');??

Just:

echo bcmul(PHP_INT_MAX ,PHP_INT_MAX);

How do Divide a H U G E Number in PhP

BC Math can calculate with strings. I am guessing you want to have the remainer of the division only? Then bcmod would be your choice. Otherwise pick bcdiv.

Note: works only php 7.2+, before this functions cuts with int precision as well.



Related Topics



Leave a reply



Submit