我只是在研究 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 returnstrue
, includingx!=x
whenx
is NaN.
关于java - 为什么 Double.NaN==Double.NaN 返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8819738/