为什么 float('inf') == float('inf') 返回 True,但 float('inf') - float('inf') x27;) == float('inf') 返回 False?

发布于 01-13 06:05 字数 670 浏览 4 评论 0原文

为什么在 Python 中会发生这种情况?

  • float('inf') == float('inf') 返回 True
  • float('inf') + float('inf') == float ('inf') 返回 True
  • float('inf') * float('inf') == float('inf') 返回 正确
  • float('inf') - float('inf') == float('inf') 返回 False
  • float('inf') / float( 'inf') == float('inf') 返回 False

如果我考虑限制,我最好的猜测与此操作的结果有关:

limx→+∞(f(x) ▢ g(x)) 其中 limx→+ ∞ f(x) = +∞ 和 limx→+∞ g(x) = +∞,如果 ▢ 是 + 或 *,则返回 +∞,但未定义 (它可以返回每个值)如果 ▢是 - 或 /。

不过我很困惑。

Why does this happen in Python?

  • float('inf') == float('inf') returns True,
  • float('inf') + float('inf') == float('inf') returns True,
  • float('inf') * float('inf') == float('inf') returns True,
  • float('inf') - float('inf') == float('inf') returns False,
  • float('inf') / float('inf') == float('inf') returns False.

My best guess, if I think about limits, is related with the result of this operation:

limx→+∞(f(x) ▢ g(x)) where limx→+∞ f(x) = +∞ and limx→+∞ g(x) = +∞, which returns +∞ if ▢ is + or *, but it is not defined (it could return every value) if ▢ is - or /.

I am very puzzled, though.

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

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

发布评论

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

评论(3

何以笙箫默2025-01-20 06:05:16

在进行比较之前

float('inf') - float('inf') == float('inf')

的结果

float('inf') - float('inf')

,先计算 。 结果是 NaN

它是 NaN,因为无穷大的数量可能不同。 Stack Overflow 姊妹网站 Math.SE 对此进行了解释,被称为希尔伯特酒店悖论:

从外行人的角度来看,想象一下我有无数个酒店房间,每个房间的编号为 1, 2, 3, 4, ...

那么我就把它们全部给你。我将一无所有,所以 ∞−∞=0

另一方面,如果我把所有奇数都给你,那么我还剩下无穷多个。所以 ∞−∞=∞。

现在假设我给你除了前七个之外的所有内容。那么 ∞−∞=7。
虽然这并不能解释为什么这是不确定的,但希望您能同意它是不确定的!

表示不确定的最佳数字是 NaN。将 NaN 与任何内容进行比较始终是False,甚至将 NaN 与自身进行比较也是如此。

除了这个相当“逻辑”的解释之外,我们发现 Python 使用 IEEE754 表示用于浮点计算。

您通常需要购买 IEEE754 规范,但幸运的是我们看到一些草案版本在线。相关章节恕我直言是7.2:

对于以浮点格式生成结果的操作,表示操作的默认结果
无效操作异常应为安静的 NaN [...]

[...]

d) 加法或减法或 fusedMultiplyAdd:无穷大的幅度减法,例如:
加法(+∞,−∞)

Before the comparison of

float('inf') - float('inf') == float('inf')

can be made, the result of

float('inf') - float('inf')

will be calculated. That result is NaN.

It is NaN because the amounts of infinity may differ. It's explained on the Stack Overflow sister site Math.SE, known as the Hilbert hotel paradox:

From a layman's perspective, imagine that I have an infinite number of hotel rooms, each numbered 1, 2, 3, 4, ...

Then I give you all of them. I would have none left, so ∞−∞=0

On the other hand, if I give you all of the odd-numbered ones, then I still have an infinite number left. So ∞−∞=∞.

Now suppose that I give you all of them except for the first seven. Then ∞−∞=7.
While this doesn't explain why this is indeterminate, hopefully you can agree that it is indeterminate!

The best number to represent indeterminate is NaN. Comparing NaN to anything is always False, even comparing NaN against itself.

Besides that quite "logical" explanation, we find that Python uses IEEE754 representation for floating point calculation.

You'd typically need to buy the IEEE754 specification, but luckily we see some draft version online. The relevant chapter IMHO is 7.2:

For operations producing results in floating-point format, the default result of an operation that signals the
invalid operation exception shall be a quiet NaN [...]

[...]

d) addition or subtraction or fusedMultiplyAdd: magnitude subtraction of infinities, such as:
addition(+∞, −∞)

无声无音无过去2025-01-20 06:05:16

我不认为这是 python 的问题。

无穷大 ​​- 无穷大 未定义(数学上),因此它不等于无穷大。


>>> np.inf - np.inf
nan
>>> np.inf == np.inf
True

这是有道理的,我们正在比较相等的对象。

>>> (np.inf - np.inf) == np.inf
False

这也是有道理的,我们正在比较不同的对象。

python 在您发布的示例中给您的所有答案都是正确的。

I don't think that's a problem with python.

infinity - infinity is undefined (mathematically), therefore it is not be equal to infinity.


>>> np.inf - np.inf
nan
>>> np.inf == np.inf
True

makes sense, we're comparing objects that are equal.

>>> (np.inf - np.inf) == np.inf
False

Also makes sense, we're comparing different objects.

All the answers python gave you, on the examples you posted, are right.

对风讲故事2025-01-20 06:05:16

请参阅@thomas-weller 的回答,但此外,问题归结为这一点。因为无穷大有多种大小,所以我们在对它们进行减法和除法时会遇到问题。

抛开最近的证据不谈,一些被认为具有不同大小的无限集合实际上具有相同的大小(数学家测量无穷大并发现它们相等)。

整数列表是无限的,实数列表也是无限的。然而,任意两个相邻整数之间的实数列表也是无限的。

float("inf") 没有机会说“这是无限”与“这是无限无限”,所以我们不知道它是什么。请注意,我将从这里开始使用两个引用:

  • INF === 无限
  • INFINF === 无限无限

现在,我们将在哪里:

float('inf') == float('inf') = => True

看起来合理的是: (INF 或 INFINF) 等于 (INF 或 INFINF)

float('inf') + float('inf') == float( 'inf')

看起来合理的是:(INF 或 INFINF) + (INF 或 INFINF) 等于 (INF 或 INFINF)

float('inf') * float('inf') == float('inf')

这似乎是合理的:(INF 或 INFINF) * (INF 或 INFINF) 等于 (INF 或 INFINF)

float('inf') - float('inf') == float('inf')

现在我们有点坚持。从概念上讲,我们有很多潜在的答案。给定 float('inf') - float('inf')0 是合理的,-INFINFINFINF 也是如此代码>.这个结果是未定义的,因此nan。事实上,正如 @thomas-weller 指出的那样,几乎任何答案都可能是合理的。

float('inf') / float('inf') == float('inf')

与上面相同的问题。由于我们不知道 float("inf") 有多大,所以这个结果是未定义的。

See answer by @thomas-weller but additionally, the issue boils down to this. Because there are multiple sizes of infinite we run into a problem when subtracting and dividing them.

Setting aside recent proof that some infinite sets thought to be of different size are in fact the the same size (Mathematicians Measure Infinities and Find They’re Equal).

The list of whole numbers is infinite and the list of real numbers is also infinite. However, the list of real numbers between any two adjacent whole numbers is also infinite.

float("inf") does not give a chance to say "this is infinite" vs "this is infinitely infinite" so we don't know which it is. Note I'm going to use two references from here on:

  • INF === infinite
  • INFINF === infinitely infinite

Now then where does that leave us:

float('inf') == float('inf') ==> True

It seems reasonable that: (INF or INFINF) is equal to (INF or INFINF)

float('inf') + float('inf') == float('inf')

It seems reasonable that: (INF or INFINF) + (INF or INFINF) is equal to (INF or INFINF)

float('inf') * float('inf') == float('inf')

It seems reasonable that: (INF or INFINF) * (INF or INFINF) is equal to (INF or INFINF)

float('inf') - float('inf') == float('inf')

Now we have a bit of a stickler. We conceptually have lots of potential answers. Given float('inf') - float('inf') then 0 is plausible as is -INFINF and INFINF. This result is undefined and as such nan. In fact, as @thomas-weller points out almost any answer might be plausible.

float('inf') / float('inf') == float('inf')

Same issue as above. This result is undefined given that we don't know how "big" any of the float("inf") are.

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