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

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

问题描述

我只是在研究OCPJP的问题,我发现这个奇怪的代码:
$ b $ pre $ 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语言规范(JLS)第三版说
$ b


一个溢出的操作产生一个有符号的无穷大,一个下溢的操作产生非规格化的值或有符号的零,并且没有数学上确定的结果的操作产生NaN。以NaN作为操作数的所有数字操作都会产生NaN。正如已经描述的那样,NaN是无序的,所以涉及一个或两个NaN的数字比较操作返回 false 和任何!= 比较涉及NaN返回 true ,包括 x!= x x 是NaN。


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

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

解决方案

NaN means "Not a Number".

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.

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

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