note 102169 modified in language.operators.arithmetic by danbrown
| From: | danbrown@php.net | Date: | Mon, 31 Jan 2011 14:25:19 +0000 |
| Subject: | note 102169 modified in language.operators.arithmetic by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-176536@lists.php.net to get a copy of this message | ||
if you want $n % $m >= 0 and $n % $m == ($n + $m) % $m to hold everywhere except for overflows:
<?php
function modulo($n, $m)
{
$r = $n % $m;
return $r < 0 ? $r + abs($m) : $r; // replace abs($m) with $m if you know that $m > 0
}
?>
--was--
if you want $n % $m >= 0 and $n % $m == ($n + $m) % $m to hold everywhere except for overflows:
<?php
function modulo($n, $m)
{
$r = $n % $m;
return $r < 0 ? $r + abs($m) : $r; // replace abs($m) with $m if you know that $m > 0
}
http://php.net/manual/en/language.operators.arithmetic.php