Linked Questions

79 votes
6 answers
30k views

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 ...
psihodelia's user avatar
  • 30.7k
56 votes
5 answers
398k views

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?
KodeWarrior's user avatar
  • 3,618
60 votes
3 answers
93k views

Can anyone explain how the modulo operator works in Python? I cannot understand why 3.5 % 0.1 = 0.1.
beruic's user avatar
  • 5,946
18 votes
12 answers
13k views

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,...
lurks's user avatar
  • 2,626
10 votes
6 answers
10k views

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)...
canyon289's user avatar
  • 3,595
18 votes
4 answers
8k views

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 % ...
Cerin's user avatar
  • 65.6k
3 votes
5 answers
11k views

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 ...
anon_nerd's user avatar
  • 1,291
4 votes
4 answers
1k views

Why modulo operator is not working as intended in C and Java?
DDC's user avatar
  • 872
0 votes
1 answer
1k views

-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 ...
user8098600's user avatar
4 votes
1 answer
2k views

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)
havin's user avatar
  • 203
2 votes
3 answers
559 views

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 ...
Flok's user avatar
  • 21
0 votes
2 answers
777 views

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 ...
numq's user avatar
  • 17
0 votes
2 answers
764 views

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 ...
joydip134's user avatar
  • 307
-7 votes
3 answers
649 views

I am familiar with the usual mod result but not for negative numbers. What is the logic?
Zaid renteria's user avatar
0 votes
0 answers
437 views

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 ...
kevin godfrey's user avatar

15 30 50 per page