为什么 Double.NaN==Double.NaN 返回 false? [英] Why does Double.NaN==Double.NaN return false?

查看:40
本文介绍了为什么 Double.NaN==Double.NaN 返回 false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was just studying OCPJP questions and I found this strange code:

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

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

When I ran the code, I got:

false
true

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

How is the output false when we're comparing two things that look the same as each other? What does NaN mean?

推荐答案

NaN 的意思是非数字".

NaN means "Not a Number".

Java 语言规范 (JLS) 第三版说:

溢出的运算产生有符号无穷大,下溢的运算产生非规范化值或有符号零,而没有数学上确定结果的运算产生 NaN.所有以 NaN 作为操作数的数值运算都会产生 NaN 作为结果.如前所述,NaN 是无序的,因此涉及一个或两个 NaN 的数值比较运算返回 false 并且任何涉及 NaN 的 != 比较返回 true,当x为NaN时,包括x!=x.

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.

这篇关于为什么 Double.NaN==Double.NaN 返回 false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆