RollyGuy has asked for the wisdom of the Perl Monks concerning the following question:
A coworker of mine pointed out that C and Perl think differently when it comes to the modulus operator. For all positive number, the two agree, but when the modulus of a negative number is taken, there are differences.
Using a C-based calculator, I have:
> (-7)%4
= -3.000000 (0000000000000000)
Using perl, I have:
ANS1 = 1
So, which is mathematically correct? I think that maybe the C version is correct because modulus give us the residue (or remainder) of a division function. -7/4 gives -(1 3/4) as an answer, so -3 seems to be the correct remainder. Any thoughts?
Using a C-based calculator, I have:
> (-7)%4
= -3.000000 (0000000000000000)
Using perl, I have:
Output:my $val = (-7)%4; print "ANS1 = $val\n";
ANS1 = 1
So, which is mathematically correct? I think that maybe the C version is correct because modulus give us the residue (or remainder) of a division function. -7/4 gives -(1 3/4) as an answer, so -3 seems to be the correct remainder. Any thoughts?
Back to
Seekers of Perl Wisdom