为非常大的数字计算%b - php

I have to calculate a % b for two very large numbers. I can not use the default modulo operator, because a and b are larger then PHP_INT_MAX, so I have to handle them as "strings".

I know that there exists special math libraries like BC or GMP but I can't use them, because my app probably will hosted on a shared host, where these are not enabled. I have to write a function in php that will do the job. The function will take two strings (the two number) as parameters and have to return a % b, but I don't know how to start? How to solve this problem?

Since PHP 4.0.4, libbcmath is bundled with PHP. You don't need any external libraries for this extension. These functions are only available if PHP was configured with --enable-bcmath .

The Windows version of PHP has built-in support for this extension. You do not need to load any additional extensions in order to use these functions. You should be able to enable these functions yourself, without any action on the part of the hosting company.

You can use fmod for values larger than MAX_INT

Read more about it here

http://php.net/manual/en/function.fmod.php

Depending on your processor, if using 64 bit machine 2^63-1 and if 32 bit machine 2^31-1 should give you the length of your decimal your machine can compute. above that you will get wrong values. You can do the same by splitting your number into chunks. Example: my number is 18 decimal long thus, split into chunks of 9/7/2 = 18. calculate the mod of the first chunk. Append the mod of the first one to the front of the second chunk. Example: result of the first mod = 23, thus 23XXXXXXX. find the mod of the resulting 23XXXXXXX. add the mod to the last chunk. Example: mod = 15 then 15XX.

$string = '123456789123456789'; // 18 decimal long
$chunk[0] = '123456789'; // 9 decimal long
$chunk[1] = '1234567'; // 7 decimal long
$chunk[2] = '89'; // 2 decimal long
$modulus = null;
foreach($chunk as $value){
$modulus = (int)($modulus.$value) % 45;
}

The result $modulus above should be same as

$modulus = $tring % 45 Better late than even. Hope this will help. anyone with similar approach?

I though of this solution: $n represents a huge number, $m the (not so huge) modulus.

function getModulus($n, $m)
{
    $a = str_split($n);
    $r = 0;

    foreach($a as $v)
    {
        $r = ((($r * 10) + intval($v)) % $m);
    }

    return $r;
}

Hope it helps someone,