gpt4 book ai didi

java - Infinity 等于 Java 中的 Infinity

转载 作者:行者123 更新时间:2023-12-01 07:49:51 24 4
gpt4 key购买 nike

我想测试无穷大是否等于Java中的无穷大:

Double.POSITIVE_INFINITY == Double.POSITIVE_INFINITY

当结果证明是真的时,我感到很惊讶。我的问题是两个无限值如何相等?

最佳答案

因为 Double.POSITIVE_INFINITY 代表一个特定的数字,因此使用 == 将其与自身进行比较应该返回 true。

此行为在 JLS Sec 15.21.1 中明确指定:

Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:

  • ...

  • Otherwise, two distinct floating-point values are considered unequal by the equality operators.

    In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares unequal to all other values.

关于java - Infinity 等于 Java 中的 Infinity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40038118/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com