note 112730 added to language.operators.arithmetic
| From: | Andrew | Date: | Wed, 17 Jul 2013 14:10:55 +0000 |
| Subject: | note 112730 added to language.operators.arithmetic | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-195655@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 94.76.225.145
Probable Submitter: 62.172.216.8
----
Manual Page -- http://php.net/manual/en/language.operators.arithmetic.php
Edit -- https://master.php.net/note/edit/112730
Del: integrated -- https://master.php.net/note/delete/112730/integrated
Del: useless -- https://master.php.net/note/delete/112730/useless
Del: bad code -- https://master.php.net/note/delete/112730/bad+code
Del: spam -- https://master.php.net/note/delete/112730/spam
Del: non-english -- https://master.php.net/note/delete/112730/non-english
Del: in docs -- https://master.php.net/note/delete/112730/in+docs
Del: other reasons-- https://master.php.net/note/delete/112730
Reject -- https://master.php.net/note/reject/112730
Search -- https://master.php.net/manage/user-notes.php