note 103525 deleted from language.operators.arithmetic by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:57:58 +0000 |
| Subject: | note 103525 deleted from language.operators.arithmetic by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215394@lists.php.net to get a copy of this message | ||
Note Submitter: php at richardneill dot org
----
For larger numbers (above PHP_INT_MAX), use fmod() rather than %.
The other operators (+-*/) work correctly with floats and integer overflow, but % uses integer wrap.
Eg.
<?php
var_dump(0xffffffff % 2);
//Prints int(-1) which is WRONG
var_dump(intval(fmod(0xffffffff,2)));
//Prints int(1) which is the right answer
?>
(The reason this matters is that PHP's float is actually a double, and can accurately represent
integers up to 52-bits, even on 32-bit systems)