整数除以负数 [英] Integer division by negative number

查看:540
本文介绍了整数除以负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

整数除法-1/5应该返回什么?我对此行为完全感到困惑.我认为从数学上讲应该为0,但是python和ruby返回-1.

What should integer division -1 / 5 return? I am totally confused by this behaviour. I think mathematically it should be 0, but python and ruby are returning -1.

为什么不同的语言在这里表现不同?请有人解释.谢谢.

Why are different languages behaving differently here? Please someone explain. Thanks.

| Language  | Code           | Result |
|-----------+----------------+--------|
| ruby      | -1 / 5         |     -1 |
| python    | -1 / 5         |     -1 |
| c         | -1 / 5         |      0 |
| clojure   | (int (/ -1 5)) |      0 |
| emacslisp | (/ -1 5)       |      0 |
| bash      | expr -1 / 5    |      0 |

推荐答案

简短答案:语言设计者可以选择在进行整数除法时其语言将向零,负无穷大还是正无穷大取整. .不同的语言做出了不同的选择.

Short answer: Language designers get to choose if their language will round towards zero, negative infinity, or positive infinity when doing integer division. Different languages have made different choices.

长答案: Python和Ruby的语言作者都认为,向负无穷大舍入比向零四舍五入更有意义(就像C一样). python的创建者在此处上写了一篇有关其推理的博客文章. .我在下面摘录了很多内容.

Long answer: The language authors of Python and Ruby both decided that rounding towards negative infinity makes more sense than rounding towards zero (like C does). The creator of python wrote a blog post about his reasoning here. I've excerpted much of it below.

今天(再次)被要求解释为什么Python中的整数除法返回结果的底数,而不是像C一样向零截断.

I was asked (again) today to explain why integer division in Python returns the floor of the result instead of truncating towards zero like C.

对于正数,不足为奇:

>>> 5//2
2

但是,如果其中一个操作数为负数,则结果将是下限的,即从零舍入(朝负无穷大):

But if one of the operands is negative, the result is floored, i.e., rounded away from zero (towards negative infinity):

>>> -5//2
-3
>>> 5//-2
-3

这使某些人感到不安,但是有一个很好的数学原因. 整数除法运算(//)及其同级,取模 运算(%),一起学习并满足数学要求 关系(所有变量都是整数):

This disturbs some people, but there is a good mathematical reason. The integer division operation (//) and its sibling, the modulo operation (%), go together and satisfy a nice mathematical relationship (all variables are integers):

a/b = q with remainder r

如此

b*q + r = a and 0 <= r < b
(assuming a and b are >= 0).

如果您想将关系扩展为负a(保持b 正数),您有两种选择:如果将q截断为零,则r 将变为负,使得不变变为0≤abs(r)≤1. 否则,您可以将q趋向负无穷大,然后 不变保持为0< = r< b. [更新:解决了此问题]

If you want the relationship to extend for negative a (keeping b positive), you have two choices: if you truncate q towards zero, r will become negative, so that the invariant changes to 0 <= abs(r) < otherwise, you can floor q towards negative infinity, and the invariant remains 0 <= r < b. [update: fixed this para]

在数学数论中,数学家总是喜欢后者 选择(例如,参见维基百科).对于Python,我做出了相同的选择 因为模的一些有趣的应用 a的符号不有趣的操作.考虑采取 POSIX时间戳(自1970年初以来的秒数),并将其转换为 一天中的时间.由于一天有24 * 3600 = 86400秒, 这个计算只是t%86400.但是如果我们要表达时间 在1970年之前使用负数,截断为零"规则 会给出毫无意义的结果!使用下限规则,一切都可以解决 很好.

In mathematical number theory, mathematicians always prefer the latter choice (see e.g. Wikipedia). For Python, I made the same choice because there are some interesting applications of the modulo operation where the sign of a is uninteresting. Consider taking a POSIX timestamp (seconds since the start of 1970) and turning it into the time of day. Since there are 24*3600 = 86400 seconds in a day, this calculation is simply t % 86400. But if we were to express times before 1970 using negative numbers, the "truncate towards zero" rule would give a meaningless result! Using the floor rule it all works out fine.

这篇关于整数除以负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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