3
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 correct?

3
  • 2
    This isn't relate to Python so much as it is to mathematics in general. mathforum.org/library/drmath/view/52343.html Commented Sep 28, 2012 at 1:59
  • 5
    @WaleedKhan, the answer will be different for different languages so it very much is a Python question. Commented Sep 28, 2012 at 2:08
  • You can use math.fmod to get the same behavior as in C or Java. Commented Jan 28, 2017 at 16:42

5 Answers 5

8

In Python, the sign of the remainder is the same as the sign of the denominator (which differs from languages like C, where it is the same as the sign of the numerator).

Mathematically, you are always guaranteed that if a, b = divmod(n, d), then a*d + b == n.

Note that 23//5 == 4 and 23//-5 == -5 (Python always does floor division). Thus, we have 4*5 + 3 == 23 and -5*-5 - 2 == 23, as you have said.

Sign up to request clarification or add additional context in comments.

1 Comment

Guido explains this in some more detail in this blog post: Why Python's Integer Division Floors.
0

Lets write it out as N=kM+R.

We have 23 = -5*(-5) - 2, and 23 = 4*5 + 3.

Comments

0

The simplest way of looking at problem for your purposes is to consider the definition that:

a mod n = R where the remainder R must satisfy 0<= R

So for mod -5 arithmetic, 0<= R < -4 i.e. R can be one of 0, -1, -2, -3, -4

that is you effectively subtract (or add) n from a until you bring R into the range above:

So

23 % 5 is (23-4*5) = 23-20 = 3

but

23 % -5 is (23+5*(-5)) = 23-25 = -2

Comments

0

Well, 23 % 5 = 3 since 4*5 = 20 and when you divide 23 by 20 you obtain a remainder of 3. You can think of it as the closet you can go without going over.

As for 23 % -5, well that answer differs from one programming language to another.

For Python it's -2 because it will always return the value of the divisor and it's because 5*5 = 25 and when you divide 23 by 25 in Python you obtain a remainder of -2 (since it must be negative because the divisor was negative) so we have 25 - 2 = 23.

It's worth noting that the formal mathematical definition states that b is a positive integer.

3 Comments

Actually, no. The formal mathematical definition says that certain numbers are equivalent modulo the modulus. The equivalence classes are commonly taken to be [0], ... [n-1], but they don't have to be. [42] and [-99] are perfectly good equivalence classes mod 2.
I think you misunderstood what I was saying. In the form a(mod b), a can be any integer value. Hence, [2] is congruent to [-3] modulo 5. It's the b that must be a positive integer in the formal mathematical definition. Article on Modular arithmetic
Ah. You never did define b, so I made an assumption.
0

% in Python uses "Modulo operation" ; it's different from taking the reminder of a division operation such that.

a - int(a/n) * n

although it is sometimes equivalent in some computer languages.

The math expression can be found explict here: http://en.wikipedia.org/wiki/Modulo_operation

So obviously, in Python "%" operation uses the following expression:

mod(a, n) = a - n * floor(a / n)

Therefore,

23%-5 = mod(23,-5) = 23 - (-5) * floor(23/-5) = 23 - (-5) * -5 = -2

and

23%5 = mod(23, 5) = 23 - 5 * floor(23/5) = 23 - 5 * 4 = 3

In addition, you my find it's interesting that

-23%5 = mod(-23,5) = (-23) - 5 * floor(-23/5) = -23 - 5 * (-5) = 2

since floor() action will take the integer value toward negative infinity.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.