TIL Parity check with %
· One min read
The result of modulo operator (%) varies between programming languages especially when one of the operand is negative.
For example, here's what Python outputs:
>>> 29 % 2
1
>>> -29 % 2
1
>>> 29 % (-2)
-1
And here's the output from Java
jshell> 29 % 2
$1 ==> 1
jshell> -29 % 2
$2 ==> -1
jshell> 29 % (-2)
$3 ==> 1
Both give different results. Hence, to check if an integer is odd, it's better to define the function as
bool is_odd(int n) {
return n % 2 != 0;
}
than
bool is_odd(int n) {
// !!! incorrect for n < 0 !!!
return n % 2 == 1;
}
See Wikipedia entry on this: https://en.wikipedia.org/wiki/Modulo#Common_pitfalls