除以无穷大

发布于 2024-08-08 06:14:26 字数 208 浏览 10 评论 0原文

我不是数学家,但我认为除以无穷大要么是糟糕的数学,要么至少是不切实际的。

我只花了半个小时调试我的 javascript,它在 Firefox 中运行得很好,但在 IE 中却出现错误。我终于意识到这是因为在某些情况下,我要求 IE 除以无穷大。

所以,我解决了这个问题,但我很好奇为什么 Firefox 对此没问题。诚然,这可能更像是一个数学问题,而不是编程问题。 ;)

I'm not a mathematician, but I assume dividing by infinity is either bad math, or, at the very least, impractical.

I just spend a half hour debugging my javascript that was working perfectly fine in Firefox but was giving me an error in IE. I finally realized it was because in certain scenarios, I was asking IE to divide by infinity.

So, I fixed that, but I'm curious as to why Firefox was OK with that. Admittedly, this might be more of a mathematics question than programming. ;)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

落花随流水 2024-08-15 06:14:26

除以无穷大是可以的——它为零。

除非你将无穷大除以无穷大,这是未定义的(至少一般来说)

Dividing by infinity is fine - it's zero.

Unless you're dividing infinity by infinity, which is undefined (in general at least)

浅忆 2024-08-15 06:14:26

我认为这是 IE 中的一个错误。根据 IEEE 数学规则,n/Inf=0(对于 n!=0)。因此,如果 IE 对此发出嘎嘎声,那么这是 IE 的问题。另一方面,在普通数学运算中使用 Inf 是有问题的,如果您的代码一开始就不依赖它,情况会更好。

为什么它很危险?好吧,对于初学者来说,根据 IEEE 754:

1/0 = Inf
1/-0 = -Inf

但我们知道 0 = -0,因此

Inf = -Inf

这显然是不可取的。无论如何,IEEE 并没有打算让 Inf 成为一个普通的数字。 Inf 和 NaN 的存在是为了在计算机中进行浮点算术,其固有的有限精度和溢出问题类似于普通世界的算术,其中,举个最简单的例子,实数集在加法下是闭合的(将两个实数相加总是会导致另一个实数)。如果您使用有限精度浮点数执行此操作,则会遇到将两个数字相加导致溢出的情况。为了避免将其视为错误条件,IEEE 引入了两个额外的符号 Inf 和 NaN,以及一组它们的行为规则。现在,数学运算的结果始终是数字、Inf 或 NaN,结果是否有意义取决于用户。

I think that's a bug in IE. According to the rules of IEEE math, n/Inf=0 (for n!=0). So if IE croaks over this, it's a problem in IE. On the other hand, using Inf in ordinary mathematical operations is problematic, and you'd be better off if your code didn't rely on it in the first place.

Why is it dangerous? Well, for starters, according to IEEE 754:

1/0 = Inf
1/-0 = -Inf

but we know that 0 = -0, hence

Inf = -Inf

which is obviously undesirable. IEEE did not mean to make Inf an ordinary number anyway. Inf and NaN exist to make floating point arithmetics in computers with its inherent limited precision and overflow issues resemble ordinary-world arithmetics, where, to take the simplest example, the set of real numbers is closed under addition (adding two real numbers always results in another real number). If you do that with finite-precision floats, you'll get cases where adding two numbers results in an overflow. To avoid having to treat this as an error condition, IEEE introduced two extra symbols, Inf and NaN, and a set of rules for their behaviour. Now the result of a mathematical operation is always a number, or Inf, or NaN, and whether the result makes any sense or not is left to the user.

给我一枪 2024-08-15 06:14:26

从数学角度来看,将数字除以无穷大是完全合理的。它是一个无限接近零的数字,但实际上并未达到零。

从软件的角度来看,这要困难得多,因为无穷大不是一个离散可表示的值。我的猜测是,您的行为差异是基于设计决策的。

在 Firefox 中,他们可能选择返回零值,因为出于所有实际目的,此结果将满足您的需要。

另一方面,在 IE 中,开发人员似乎有意识地决定不允许您执行他们知道无法给出离散答案的计算。

Dividing a number by infinity is perfectly reasonable from a mathematical perspective. It is a number infinitely close to zero, without actually reaching zero.

From a software perspective, this is much more difficult, as infinity is not a discretely representable value. My guess would be that your difference in behavior is based on design decisions.

In Firefox, they probably chose to return a value of zero, since for all practical purposes, this result will work for what you need.

In IE, on the other hand, it appears the developers made the conscious decision to not allow you to perform a calculation that they knew they could not give a discrete answer for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文