java - 为什么 Double.NaN==Double.NaN 返回 false?

标签 java floating-point nan scjp ocpjp

我只是在研究 OCPJP 问题,发现了这个奇怪的代码:

public static void main(String a[]) {
    System.out.println(Double.NaN==Double.NaN);
    System.out.println(Double.NaN!=Double.NaN);
}

当我运行代码时,我得到了:

false
true

当我们比较两个看起来相同的东西时,输出 false 怎么样? NaN 是什么意思?

最佳答案

NaN 表示“不是数字”。

Java Language Specification (JLS) Third Edition says :

An operation that overflows produces a signed infinity, an operation that underflows produces a denormalized value or a signed zero, and an operation that has no mathematically definite result produces NaN. All numeric operations with NaN as an operand produce NaN as a result. As has already been described, NaN is unordered, so a numeric comparison operation involving one or two NaNs returns false and any != comparison involving NaN returns true, including x!=x when x is NaN.

关于java - 为什么 Double.NaN==Double.NaN 返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8819738/

相关文章:

java - png 图像文件到视频(无损)

java - 如何使用 cloudant java 客户端列出响应查询的所有对象?

java - 在本地主机上通过 Java EE 读取电子邮件 Outlook

go - 如何将 float64 数字截断为特定精度?

Javascript 随机数生成器出现 NaN

python - 将组的最后一个值替换为 NaN

java - 当只允许插入/删除/替换操作时,确定一个字符串是否可以转换为另一个字符串

OpenCV 从二进制创建浮点 Mat

java - java中的 float 比较以识别模式

javascript - 当我刚刚访问数组时,为什么会出现 NaN ?