-1

Why does the modulus of a negative number N where abs(N) is smaller than the divisor D become a small positive number instead of -N.

Say we use -3 % 5 as an example, why is this 2 as opposed to -3?

Vality
  • 6,577
  • 3
  • 27
  • 48
  • It depends on how you choose to define the modulo operation. Modern C++ (and I think C) agree with you; but mathematically it's simpler to define it to be always positive. – Mike Seymour Apr 28 '14 at 16:33
  • Hi There, please try and make your title a very short summary of your question, it is not appropriate to place an example in the title usually. – Vality Apr 28 '14 at 16:52

1 Answers1

0

At first -3%5 is not equal to 2. It is equal to -3. Additionally the -3( 5*0 + (-3)) is equal to 9. You entire calculation is wrong unfortunately. Modulus calculates with absolute value first and then make sign changes. Such as; -7%5=-2 in c++. Modulus divide 7 to 5 first then checks the sign and changes the sign negative or positive.

TacoTuesday
  • 67
  • 1
  • 11