2017-09-25 36 views
1

在php: Modulus%给出$ x的余数除以$ y。Modulus%给2代替0

我已经tryed验证码:

<?php 
print(100000000165 % 5); 

结果是2 ,因为它应该是0

+1

在我的系统中它是'0'。你使用32位或64位版本的PHP? –

+0

您可能正在运行一个32位PHP,其中最大的整数约为20亿。过去,数字环绕,100000000165变成1215752357。 – cHao

回答

1

这是因为你在32位系统上工作。

32bit php中最大的整数是2147483647。这意味着之后(从2147483648开始)这是一个溢出和包装。

你的数量比更大,所以结果是:(100000000165 % 2147483648) % 5 =>1215752357 % 5 =>2


增加:您可以通过建立自己的模函数和处理花车

$largeNumberThatBreaksInteger = 10000000000000000000165; 
$modulus = $largeNumberThatBreaksInteger/PHP_INT_MAX - (int)($largeNumberThatBreaksInteger/PHP_INT_MAX) * PHP_INT_MAX; 
// results in something like -9.9981352879506E+21. So you can compare it with an epsilon and know if it's 0 or not. 

Dealing with floats and epsilon