in reply to Re^3: Best way to make sure a number is an even multiple of another?
in thread Best way to make sure a number is an even multiple of another?
That ensures that what you are subtracting from $m is not larger than $m, and the result is the same, modulo $m.$n + ($m - $n % $m) % $m;
In Perl, modulo works as it is supposed to. :-) The result is between zero and the right operand. In Java, it's broken. This allows us to simplify the expression a bit further:
$n + -$n%$m;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Best way to make sure a number is an even multiple of another?
by hv (Prior) on Oct 22, 2004 at 10:44 UTC |