算法与计算机科学hillelwayne.com
1/0 = 0:除零的来龙去脉
1/0 = 0
2018 — 2024,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇文章讨论编程语言 Pony 将整数除零(1/0)定义为 0 这一设计选择,并围绕其理由与争议展开分析。文中涉及部分函数、错误处理,以及整数除零与浮点除零(后者仍得到 Infinity)的区别。
为什么经久不衰:它触及一个看似简单却牵涉类型系统、错误处理与数学直觉的经典设计取舍,因此多年后仍值得拿出来讨论。
HN 评论精选
除零几乎总是未处理的 bug,返回 0 会掩盖错误;应抛错,若确需 0 应显式处理。
我对“1/0 = 0”的不满是:它本质上是在掩盖程序里几乎总是 bug 的情况。程序执行除以零,几乎肯定不是你想要的,而是你没能预见和处理的边界情况;既然没计划,返回的结果几乎肯定也不是你想给用户的。遇到这种未预料的边界情况,通常更好的做法是抛错:防止数据/状态进一步损坏,避免用户拿到一个她以为可信的坏结果,并清楚暴露问题。直接返回 0 恰恰相反。如果你是那 0.1% 预见到并确实想让程序把 1/0 当作 0 的人,那就显式检查这个情况并让它按你的意愿行为。Pony 作者可以任意设计语言,但在这个案例中,他们伤害用户远多于帮助用户。
whack查看英文原文
My problem with "1/0 = 0" is that it's essentially masking what's almost always a bug in your program. If you have a program that's performing divide-by-zeroes, that's almost surely something you did not intend for. It's a corner case that you failed to anticipate and plan for. And because you didn't plan for it, whatever result you get for 1/0 is almost surely a result that you wouldn't want to have returned to the user. When encountering such unanticipated corner cases, it's almost always better to throw an error. That prevents any further data/state corruption from happening. It prevents your user from getting back a bad result which she thinks she can trust and rely on. It highlights the problem very clearly, so that you know you have a problem, and that you have to fix it. Simply returning a 0 does the exact opposite. If you're one of the 0.1% who did anticipate all this, and correctly intended for your program to treat 1/0 as 0, then just check for this case explicitly and make it behave the way you wanted. The authors of pony are welcome to design their language any way they want. But in this case, they are hurting their users far more than they are helping.
Pony 核心成员回应:这是有意但不满意的权衡,仅限整数除法,浮点仍得 Infinity,未来会引入可报错的部分函数。
我是 Pony 核心团队成员。之后会写得更详细。先简单说几点:1)团队里没人对最终走到这一步感到满意;理解这个决定需要了解 Pony 如何处理部分函数(可能产生除零等错误的函数),以及由此带来的易用性问题。2)这只适用于整数除法,其中除零未定义;1.0/0.0 会使用可用的 Infinity 值。3)Pony 即将引入部分函数数学,包括把 1/0 视为部分函数(即会产生错误),以及处理 +、-、* 的整数上溢/下溢。4)即使没有这些操作,也很容易自己定义一个在除零时返回错误的除法。
spooneybarger查看英文原文
Hi, I'm on the Pony core team. I will be writing in more detail about this decision. A few short notes until then: 1) no one on the team has ever been happy with ending up here, understanding why the decision was made involved understand how partial functions (one that can produce errors like division by zero) are handled in Pony and interesting ergonomic issues that can result that is a large part of what my post will be about. 2) this applies only to integer division wherein division by zero is undefined. 1.0/0.0 uses the available Infinity value. 3) Partial function math is coming to Pony that includes 1/0 as partial (ie error producing) as well as handling integer overflow and underflow for operations: `+`, `-`, and `*`. 4) It's very straightforward even without that those operations to define your own division that will be partial (ie return an error) for division by zero. Edit to include link to the comment below because it contains a good bit of the decisions that have to make when dealing with integer math: https://news.ycombinator.com/item?id=17736637 Which is part of what I will touch on in my post about the decision process that Pony went through to reach to state we are currently at (which I want to highlight, is one we intend to improve- see #3 above).
真正麻烦的是 0/0:沿不同路径逼近,x/y 的极限可以是 0、无穷甚至 17,所以不存在单一值。
有人说“这很简单,1/0 就是 +Infinity”。所以真正有趣的是 0/0。x/y 在 x 和 y 都趋于零时的极限取决于你沿 xy 平面上哪条路径逼近奇点:沿一种逼近极限是 0,沿另一种发散到无穷,沿又一种极限是 17。我没开玩笑!去 GeoGebra 3D 输入 x/y 旋转图像,x=0、y=0 处的“值”是整个 z 轴。换个视角,用 WolframAlpha 画 z=x/y 并开启等高线模式,注意等高线从 z 轴辐射出去;每条线都是一种不同 z 值的逼近,沿每条线取极限会得到不同值。
phyzome查看英文原文
Some people say "oh, that's easy, 1/0 is +Infinity". So the real fun is at 0/0. The limit of x/y as x and y go to zero depends on which path across the xy plane you take towards the singularity. Along one approach, the limit is 0, along another approach the limit diverges to infinity, along yet another the limit is 17. I'm not kidding! Go to https://www.geogebra.org/3d and enter "x/y" and spin the graph around. The "value" at x=0 y=0 is the entirety of the z-axis. For another perspective, try http://www.wolframalpha.com/input/?i=z%3Dx%2Fy and turn on contour mode for the 3D plot. Notice how the contour lines radiate out from the z-axis; each of those is an "approach" to the singularity at a different z-value, and taking the limit along each line leads to a different value.
把 1/0 定义为 0 会破坏除法与乘法的对称性,以及依赖 (x/y)*y=x 的代码。
> 但 Pony 做了不健全的事吗?绝对没有。定义 1/0 = 0 完全没问题。什么都不会破坏。 它实际上破坏了除法与乘法之间的对称性,以及许多假设 (x / y) * y 等于 x 的代码。这里有一个简单且不实用的例子,但现实世界中并非不可能意外或有意出现这种简化代码,当 y = 0 时可能产生非常不精确的结果。
luso_brazilian查看英文原文
> But is Pony doing something unsound? Absolutely not. It is totally fine to define 1/0 = 0. Nothing breaks It actually does break something, the symmetry between division and multiplication and the many pieces of code that assume that (x / y) * y equals x. Here is a naive and non practical example, but it is not impossible to find a real world example where this simplified code manifests itself accidentally or by design function do_something_with_x(x, y) { let ratio = x / y; if (do_something_with_ratio(ratio)) { return x * y; } else return null; } Again, this is a naive example but one that could manifest itself with a very imprecise result when y = 0
历次讨论
同类还值得读
位运算技巧集
Bit Twiddling Hacks
斯坦福页面上的经典位运算技巧合集,收录了 popcount、前导/尾随零、位反转、交换、舍入等大量技巧,并附有解释和性能讨论。
算法与计算机科学上首页 8 次 · 2011–2024graphics.stanford.edu编辑精选如何不按平均评分排序
How Not To Sort By Average Rating
经典文章解释了为什么按平均评分排序不可靠:只有少量评价的条目(比如仅一条五星)会轻易排到最前,而评价数量的差异被忽略。
算法与计算机科学上首页 7 次 · 2009–2024evanmiller.org编辑精选数据结构可视化
Data Structure Visualizations
旧金山大学 David Galles 教授制作的数据结构与算法交互式可视化网站,涵盖 B 树、红黑树、排序、图算法等,可逐步操作观察其运行过程。
算法与计算机科学上首页 6 次 · 2011–2021cs.usfca.edu编辑精选1996 年以来的计算机科学最佳论文奖
Best Paper Awards in Computer Science since 1996
Jeff Huang 维护的一个汇总页面,收录了自 1996 年以来计算机科学各主要会议的“最佳论文奖”获奖名单,并按年份和会议整理,方便读者一站式浏览历届获奖论文。
算法与计算机科学上首页 6 次 · 2010–2018jeffhuang.com编辑精选算法(Jeff Erickson)
Algorithms, by Jeff Erickson
伊利诺伊大学 Jeff Erickson 教授的算法课教材,免费在线开放。
算法与计算机科学上首页 5 次 · 2018–2022jeffe.cs.illinois.edu编辑精选