为什么这个比较给了我“假”?我查看了源代码,Float.NaN 被定义为
/**
* A constant holding a Not-a-Number (NaN) value of type
* <code>float</code>. It is equivalent to the value returned by
* <code>Float.intBitsToFloat(0x7fc00000)</code>.
*/
public static final float NaN = 0.0f / 0.0f;
编辑:令人惊讶的是,如果我这样做:
System.out.println("FC " + (Float.compare(Float.NaN, Float.NaN)));
它给了我 0
。所以 Float.compare()
确实认为 NaN is 等于它自己!
最佳答案
使用 Float.isNaN
检查 NaN 值。
关于java - float .NaN == float .NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9341653/