在这种情况下,"a!= a"可以返回"true"? [英] In which case could "a != a" return "true"?
本文介绍了在这种情况下,"a!= a"可以返回"true"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
java .lang.Math#min(double,double):
public static double min(double a, double b) {
if (a != a) return a; // a is NaN
if (a == 0.0d && b == 0.0d && Double.doubleToLongBits(b) == negativeZeroDoubleBits) return b;
return (a <= b) ? a : b;
}
在哪种情况下a != a
可以返回true
?看来是a
是NaN的时候,但是我无法想象一个例子.您能提供一个吗?
In which case could a != a
return true
? It seems that it's when a
is NaN, but I can't imagine an example. Could you please provide one?
推荐答案
一个简单的例子是
double d = Double.NaN; // or
double d = 0.0/0.0; // or
double d = Double.POSITIVE_INFINITY + Double.NEGATIVE_INFINITY;
if (Double.isNaN(a)) { // tests if a != a
// do something
BTW Double.compare()确实将NaN视为相等
BTW Double.compare() does see NaN as equal
if (Double.compare(d, d) == 0) // always.
对于多个线程,这对于任何类型和值都是可行的.例如
With multiple threads this is possible for any type and value. e.g.
if (a != /* another thread changes 'a' */ a) {
// a thread changed a while you were looking at it.
这篇关于在这种情况下,"a!= a"可以返回"true"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文