Linked Questions
45 questions linked to/from How does the modulo (%) operator work on negative numbers in Python?
79 votes
6 answers
30k views
C and Python - different behaviour of the modulo (%) operation [duplicate]
I have found that the same mod operation produces different results depending on what language is being used. In Python: -1 % 10 produces 9 In C it produces -1 ! Which one is the right modulo? How ...
56 votes
5 answers
398k views
Modulo operator in Python [duplicate]
What does modulo in the following piece of code do? from math import * 3.14 % 2 * pi How do we calculate modulo on a floating point number?
60 votes
3 answers
93k views
Python modulo on floats [duplicate]
Can anyone explain how the modulo operator works in Python? I cannot understand why 3.5 % 0.1 = 0.1.
18 votes
12 answers
13k views
Modulus operation with negatives values - weird thing? [duplicate]
Can you please tell me how much is (-2) % 5? According to my Python interpreter is 3, but do you have a wise explanation for this? I've read that in some languages the result can be machine-dependent,...
10 votes
6 answers
10k views
Modulo for negative dividends in Python [duplicate]
Been looking through other answers and I still don't understand the modulo for negative numbers in python For example the answer by df x == (x/y)*y + (x%y) so it makes sense that (-2)%5 = -2 - (-2/5)...
18 votes
4 answers
8k views
Difference Between Modulus Implementation in Python Vs Java [duplicate]
I've noticed differing implementations of the modulus operator in Python and Java. For example, in Python: >>> print -300 % 800 >>> 500 Whereas in Java: System.out.println(-300 % ...
3 votes
5 answers
11k views
modulus of negative numbers in Python [duplicate]
23 % -5 = -2 23 % 5 = 3 Can someone explain to me how I can understand this because I have an exam tomorrow. I want to say its because -5 * -5 =25 then 25 -2 = 23 which is how they get the 23. Is this ...
4 votes
4 answers
1k views
Why -1%26 = -1 in Java and C, and why it is 25 in Python? [duplicate]
Why modulo operator is not working as intended in C and Java?
0 votes
1 answer
1k views
Modulus of negative number [duplicate]
-14%3=? C gives me -2 and python gives me 1. I have also tried many languages, all of them are having either giving 1or -2. Then I checked again, and found out that the compilers are giving -2 and ...
4 votes
1 answer
2k views
Python Negative Number modulo positive number [duplicate]
How come -20 % 3 = 1? Just confused with the formulae used for negative number % positive number. (I have seen many question related in quora but still not clear with formula used)
2 votes
3 answers
559 views
How does modulo work with a negative dividend that is also less than the divisor? [duplicate]
To be clear, I understand the behavior of modulo in the following 3 scenarios. I understand why 16 % 3 = 1. I understand why -16 % 3 = 2. (This one can be tricky, I realize) I understand why 3 % 16 ...
0 votes
2 answers
777 views
modulo operation with negative numbers in python [duplicate]
I found this definition for modulo mod(a, n) = a - n * floor(a / n) Doing an integer division and then multiplying it again means finding the biggest number smaller than a that is dividable by n ...
0 votes
2 answers
764 views
What should be result for -1%5 in python [duplicate]
I am trying to write a list rotation function in python. I came across with the the following code : def move(p, U): q = [] for i in range(0,len(p)): q.append(p[(i-U)%len(p)]) return q This ...
-7 votes
3 answers
649 views
Why in python -11%5 evaluate to 4? [duplicate]
I am familiar with the usual mod result but not for negative numbers. What is the logic?
0 votes
0 answers
437 views
Why are python and java giving different answers for the same modulus operation? [duplicate]
So my understanding of the modulus operator is that it returns the remainder of the division, after dividing the dividend with a divisor. and of course, this dividend and divisor could be either an ...