I’ve this PHP function which does not work for negative numbers:
function isOdd($num)
{
return $num % 2 == 1;
}
but it works for positive number.
I have this Perl routine which does the exact same thing and works for negative number also
sub isOdd()
{
my ($num) = @_;
return $num % 2 == 1;
}
Did I make any mistake in translating the function ? or is it PHP bug ?
In PHP the sign of the result of
x % yis the sign of dividend which isxbutin Perl it is the sign of the divisor which is
y.So in PHP the result of
$num % 2can be be either1,-1or0.So fix your function compare the result with
0: