I have couple line of C code testing the modulo operator as follows:
// line 1
printf("%d\n", 5 % (-3)); => output: 2
// line 2
printf("%d\n", -5 % 3); => output: -2
I know that the sign of the modulo depends on the sign of the numerator, but I am curious why not otherwise?
5/(-3) = -1;
(-5)/3 = -1;
If that is agreed then let's calculate the remainder
Remainder = Dividend - ( Divisor * Factor)
5 - (-3 * -1) = 2
-5 - (3 * -1) = -2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With