Python integer division and modulation with negative operands

Questions arise when I enter these expressions in Python 3.3.0

-10 // 3 # -4 -10 % 3 # 2 10 // -3 # -4 10 % -3 # -2 -10 // -3 # 3 

It seems like it takes an approximate floating point (-3.33)? and is rounded in any direction in integer division, but in a modular operation it does something completely different. It seems that it returns the remainder +/- 1 and only switches the sign depending on where the negative operand is. I am completely confused even looking at the other answers on this site! I hope someone can explain this to me! The book tells you: remember this magic formula a = (a // b) (b) + (a% b), but it does not seem to purify the water for me at all.

- Thanks in advance!

Edit: These are just my personal assessments of what is happening (above), I know I'm completely off!

+6
source share
4 answers

Integer division simply takes half the number obtained at the end.

 10/3 -> floor(3.33) -> 3 -10/3 -> floor(-3.33) -> -4 

(Why is he flooring)


An operation with a module, on the other hand, performs a mathematical definition .

+3
source
  • Magic formula: a = (a // b) * b + (a % b)
  • a: -10
  • b: 3
  • a // b: -4
  • a % b: 2

    Replace in the magic formula: -10 = -4 * 3 + 2 = -12 + 2 = -10

  • a: 10

  • b: -3
  • a // b: -4
  • a % b: -2

    In the magic formula: 10 = -4 * -3 - 2 = 12 - 2 = 10

So the magic formula seems right.

If you define a // b as floor(a / b) (which it is), a % b should be a - floor(a / b) * b . Let's get a look:

  • a: -10
  • b: 3
  • a % b = a - floor(a / b) * b = -10 - floor(-3.33) * 3 = -10 + 4 * 3 = 2

The fact that a // b always overlapping is pretty easy to remember (please read the first link of Cthulhu, this is an explanation of the creator of Python). For negative a in a % b .. try to imagine a table of numbers that starts at 0 and has columns b :

 b = 3: 0 1 2 3 4 5 6 7 8 9 10 11 ... 

If a is the number in the cell, a % b will be the column number:

 aa % b _______________ 0 1 2 0 1 2 3 4 5 0 1 2 6 7 8 0 1 2 9 10 11 0 1 2 

Now add the table back to the negatives:

  aa % b __________________ -12 -11 -10 0 1 2 -9 -8 -7 0 1 2 -6 -5 -4 0 1 2 -3 -2 -1 0 1 2 0 1 2 0 1 2 3 4 5 0 1 2 6 7 8 0 1 2 9 10 11 0 1 2 

-10 % 3 will be 2 . A negative a in a % b would appear under such conditions. a % b with negative b not very much.

+4
source

A simple rule: for a % b = c , if c not equal to zero, then it must have the same sign as b .

And apply the magic formula:

10 % -3 = -2 => 10 // -3 = (10 - (-2)) / (-3) = -4

-10 % 3 = 2 => -10 // 3 = (-10 - 2) / 3 = -4

-10 % -3 = -1 => -10 // -3 = (-10 - (-1)) / (-3) = 3

+2
source

OK, so I did a few digging, and I think that the problem is not in Python, but in the Modulo function. I base this answer on http://mathforum.org/library/drmath/view/52343.html

10% 3 Uses the highest multiple of 3, which is less than MANY 10. In this case, 9. 10 - 9 = 1

-10% 3 does the same. He is still looking for a few out of 3, which is less than -10. In this case, -12. (-10) - (-12) = 2

+1
source

All Articles