模数 % 用 2 代替 0
Modulus % give 2 in place of 0
在php中:
模数 % 给出 $x 除以 $y 的余数。
我试过这个代码:
<?php
print(100000000165 % 5);
结果是 2
因为它应该是 0
发生这种情况是因为您在 32 位系统上工作。
32位的最大整数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.
在php中: 模数 % 给出 $x 除以 $y 的余数。
我试过这个代码:
<?php
print(100000000165 % 5);
结果是 2 因为它应该是 0
发生这种情况是因为您在 32 位系统上工作。
32位的最大整数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.