PHP上模量结果错误,Python上结果正确

问题描述 投票:0回答:1

在PHP上操作此数字时出现问题

ex:

72 ** 79%3337

当我使用时

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

然后我尝试将其拆分为一个

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

然后再次尝试使用fmod()和bcmod()

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

但是,我想要的结果是285,而当使用Python时,答案是正确的。

为什么会发生?有解决方案吗?

php python math modulus fmod
1个回答
0
投票

您正在溢出浮点分辨率,所以您需要使用bcmath进行数学运算,包括幂运算:$num = bcpow(72, 79); echo bcmod($num, 3337);

输出

285

Demo on 3v4l.org
© www.soinside.com 2019 - 2024. All rights reserved.