是什么决定整数m%n的符号? [英] What determines the sign of m % n for integers?

查看:84
本文介绍了是什么决定整数m%n的符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python中的模数令人困惑.

The modulo in Python is confusing.

在Python中,%运算符正在计算余数:

In Python, % operator is calculating the remainder:

>>> 9 % 5
4

但是:

>>> -9 % 5
1

为什么是结果1?而不是-4?

Why is the result 1? and not -4?

推荐答案

由于在python中,符号与分母匹配.

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.

这篇关于是什么决定整数m%n的符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆