What determines the sign of m % n for integers?

后端 未结 5 1392
自闭症患者
自闭症患者 2020-12-20 15:36

The modulo in Python is confusing.

In Python, % operator is calculating the remainder:

>>> 9 % 5
4

However:

5条回答
  •  猫巷女王i
    2020-12-20 16:08

    Because in python, the sign matches the denominator.

    >>> 9 % -5
    -1
    >>> -9 % 5
    1
    

    For an explanation of why it was implemented this way, read the blog post by Guido.

提交回复
热议问题