sp0*_*00m 4 java numbers equality nan
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;
}
Run Code Online (Sandbox Code Playgroud)
在哪种情况下可以a != a返回true?似乎a是NaN 的时候,但我无法想象一个例子.你能提供一个吗?
Pet*_*rey 10
一个简单的例子是
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
Run Code Online (Sandbox Code Playgroud)
BTW Double.compare()确实看到NaN相等
if (Double.compare(d, d) == 0) // always.
Run Code Online (Sandbox Code Playgroud)
对于任何类型和值,使用多个线程.例如
if (a != /* another thread changes 'a' */ a) {
// a thread changed a while you were looking at it.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
289 次 |
| 最近记录: |