为什么Double.NaN == Double.NaN返回false? [英] Why does Double.NaN==Double.NaN return false?
问题描述
$ 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 returnstrue
, includingx!=x
whenx
is NaN.
这篇关于为什么Double.NaN == Double.NaN返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!