for instance,-10 % 3 = 2, this does not make any sense as the definition for % is remainder. Thanks
Asked
Active
Viewed 153 times
0
-
In addition to the answer @COLDSPEED referenced, it's worth noting that % is the _modulo_ operator if you're looking for more information. You can also see [this answer](https://stackoverflow.com/questions/4432208/how-does-work-in-python) for general information about how it works in python. – Seth Rothschild Mar 09 '18 at 04:23
1 Answers
0
-10 = 3(-4)+2, hence the remainder is 2 mathematically.
Alternatively notice that that 10 % 3 is equal to 1. Hence the remainder of -10 % 3 should be -1 which is equal to 3-1 = 2 modulo 3.
By the rule that a % b follows the sign of b and |a%b| < b, 2 is returned as the answer.

Siong Thye Goh
- 3,518
- 10
- 23
- 31
-
1This really doesn't answer the question. Please see the marked duplicates above, which explain _why_ the modulo operator works that way. – cs95 Mar 09 '18 at 03:07
-
Thanks, as you suggested, there is two hidden rules applied for ' % ', am I understanding correct? – Tony Mar 09 '18 at 22:55
-
yup, as pointed out by the accepted answer shared by coldspeed. I miss out the rule out a%b follows the sign of b back then. – Siong Thye Goh Mar 09 '18 at 23:03