phppythonmathmodulusfmod

Wrong modulus result on PHP but right result on Python


I got the problem when operating this number on PHP

ex:

72 ** 79 % 3337

When i use

echo 72 ** 79 % 3337 // Result is int(0)

Then i try to split into this one

$num = number_format(72 ** 79, 0, '', '');
echo $num % 3337;   // Result is 1069

Then again i try using fmod() and bcmod()

$num = number_format(72 ** 79, 0, '', '');
echo fmod($num, 3337);        // Result is 2255
echo bcmod($num, 3337);       // Result is 2255

But, the result that i want is 285 and when using Python the answer is right.

Why does this happen? Any Solution?


Solution

  • You are overflowing floating point resolution, so you need to do all the math with bcmath, including the exponentiation:

    $num = bcpow(72, 79);
    echo bcmod($num, 3337);
    

    Output

    285
    

    Demo on 3v4l.org