2017-09-25 37 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