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?

anon_nerd
  • 1,251
  • 1
  • 10
  • 16

5 Answers5

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.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • 3
    Guido explains this in some more detail in this blog post: [Why Python's Integer Division Floors](http://python-history.blogspot.ch/2010/08/why-pythons-integer-division-floors.html). – Lukas Graf Sep 28 '12 at 02:11
0

Lets write it out as N=kM+R.

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

OneThreeSeven
  • 422
  • 5
  • 13
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

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.

Derek W
  • 9,708
  • 5
  • 58
  • 67
  • 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. – nneonneo Sep 28 '12 at 04:09
  • 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](http://en.wikipedia.org/wiki/Modular_arithmetic) – Derek W Sep 28 '12 at 15:24
  • Ah. You never did define `b`, so I made an assumption. – nneonneo Sep 28 '12 at 15:59
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.

Y. Chang
  • 595
  • 1
  • 5
  • 18