note 112730 deleted from language.operators.arithmetic by salathe
| From: | salathe@php.net | Date: | Mon, 21 Sep 2020 14:58:09 +0000 |
| Subject: | note 112730 deleted from language.operators.arithmetic by salathe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215399@lists.php.net to get a copy of this message | ||
Note Submitter: Andrew
----
The % operator doesn't behave as many people with a maths background would expect, when dealing
with negative numbers. For example, -1 mod 8 = 7, but in PHP, -1 % 8 = -1.
The following function has the expected behaviour:
function mod($a, $n) {
return ($a % $n) + ($a < 0 ? $n : 0);
}
mod(-1, 8) returns 7 as expected.