TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Why Python's integer division floors (2010)

103 点作者 bshanks大约 1 年前

11 条评论

nwellnhof大约 1 年前
It should be noted that in C89, both behaviors of division and modulo are allowed and it's implementation-defined whether division results are truncated or rounded towards negative infinity. This has changed in C99 which only allows truncation towards zero.
评论 #39537397 未加载
评论 #39537107 未加载
评论 #39536638 未加载
Aardwolf大约 1 年前
They made the correct decision here!<p>Too bad they made the wrong decision in not supporting the expected floating point behavior of division through zero (python throwing errors, rather than return inf or nan)
评论 #39539685 未加载
评论 #39539106 未加载
评论 #39539768 未加载
taeric大约 1 年前
I confess I have grown increasingly in favor of how common lisp does this. The basic `&#x2F;` creates rationals. And &quot;floor&quot; is a multiple value return where the first value is the floor and the second is the remainder.<p>Granted, getting used to multiple value calls takes getting used to. I think I like it, but I also think I emphatically did not like it initially.
评论 #39542333 未加载
评论 #39541497 未加载
评论 #39541445 未加载
pansa2大约 1 年前
Note the top comment by “ark” - there’s really no perfect solution here.<p>In the floating-point case, you have to choose between negative remainders or potentially inexact results. And you definitely want integer division to work the same as float division.
评论 #39536745 未加载
评论 #39538279 未加载
评论 #39536128 未加载
aimor大约 1 年前
What do you do in Python when you want different behavior? Sometimes it&#x27;s desirable to fix, floor, ceil, or round depending on the situation.
评论 #39540118 未加载
评论 #39539892 未加载
评论 #39539838 未加载
dang大约 1 年前
Discussed at the time:<p><i>Why Python&#x27;s Integer Division Floors</i> - <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=1630394">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=1630394</a> - Aug 2010 (2 comments)
OscarCunningham大约 1 年前
Yes that makes perfect sense. So why is int(-1.5) == -1? It should be -2.
评论 #39536146 未加载
评论 #39536076 未加载
评论 #39536084 未加载
DonHopkins大约 1 年前
Forth-83 beat C99 by 16 years!<p>&gt;Python, unlike C, has the mod operator always return a positive number (twitter.com&#x2F;id_aa_carmack):<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=29729890">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=29729890</a><p><a href="https:&#x2F;&#x2F;twitter.com&#x2F;ID_AA_Carmack&#x2F;status&#x2F;1476294133975240712" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;ID_AA_Carmack&#x2F;status&#x2F;1476294133975240712</a><p>tzs on Dec 30, 2021 | next [–]<p>&gt;The submission is a tweet which doesn&#x27;t really have a title, so the submitter was forced to make up one. Unfortunately the assertion in the chosen title is not correct. In Python the mod operator returns a number with the same sign as the second argument:<p><pre><code> &gt;&gt;&gt; 10%3 1 &gt;&gt;&gt; (-10)%3 2 &gt;&gt;&gt; 10%(-3) -2 &gt;&gt;&gt; (-10)%(-3) -1 </code></pre> <a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=29732335">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=29732335</a><p>DonHopkins on Dec 30, 2021 | parent | context | favorite | on: Python, unlike C, has the mod operator always retu...<p>The FORTH-83 standard adopted floored division.<p>Signed Integer Division, by Robert L. Smith. Originally appearing in Dr. Dobb&#x27;s Journal September 1983:<p><a href="https:&#x2F;&#x2F;wiki.forth-ev.de&#x2F;doku.php&#x2F;projects:signed_integer_division" rel="nofollow">https:&#x2F;&#x2F;wiki.forth-ev.de&#x2F;doku.php&#x2F;projects:signed_integer_di...</a><p>Lots of other languages got it wrong:<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Modulo_operation#In_programming_languages" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Modulo_operation#In_programmin...</a><p>Symmetric division is the kind of thing that causes rockets ships to explode unexpectedly.<p>Symmetric division considered harmful:<p><a href="https:&#x2F;&#x2F;www.nimblemachines.com&#x2F;symmetric-division-considered-harmful&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.nimblemachines.com&#x2F;symmetric-division-considered...</a><p>&gt;Since its 1983 standard (Forth-83), Forth has implemented floored division as standard. Interestingly, almost all processor architectures natively implement symmetric division.<p>&gt;What is the difference between the two types? In floored division, the quotient is truncated toward minus infinity (remember, this is integer division we’re talking about). In symmetric division, the quotient is truncated toward zero, which means that depending on the sign of the dividend, the quotient can be truncated in different directions. This is the source of its evil.<p>&gt;I’ve thought about this a lot and have come to the conclusion that symmetric division should be considered harmful.<p>&gt;There are two reasons that I think this: symmetric division yields results different from arithmetic right shifts (which floor), and both the quotient and remainder have singularities around zero.<p>&gt;If you’re interested in the (gory) details, read on. [...]
vsuperpower2020大约 1 年前
So no good reasons, just mathematical ones.
notso411大约 1 年前
How many integer 2s go in to 7?<p>3 not 4..<p>Not a hard question to answer.
评论 #39538908 未加载
z_open大约 1 年前
Changing well known behavior for something no one is really going to need. The justification makes sense, but it breaks convention and the relationship with modulo doesn&#x27;t need to hold for negative numbers.
评论 #39535482 未加载
评论 #39535428 未加载
评论 #39535554 未加载
评论 #39536620 未加载