Java isNan 是怎么工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18442503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java isNan how it works?
提问by rascio
I was looking at the openjdk-1.7.0_25
source code and I have seen this method:
我正在查看openjdk-1.7.0_25
源代码,我已经看到了这种方法:
/**
* Returns {@code true} if the specified number is a
* Not-a-Number (NaN) value, {@code false} otherwise.
*
* @param v the value to be tested.
* @return {@code true} if the argument is NaN;
* {@code false} otherwise.
*/
static public boolean isNaN(float v) {
return (v != v);
}
I can't understand how it works, when this method can return true
?
我不明白它是如何工作的,这个方法true
什么时候可以返回?
采纳答案by Rohit Jain
That method can return truefor certain operations, for example:
该方法可以为某些操作返回true,例如:
System.out.println(Float.isNaN(0.0f / 0.0f));
System.out.println(Double.isNaN(Math.sqrt(-1)));
Basically, NaN
represents an undefined value. The value of 0.0 / 0.0
is NaN
, and Nan != NaN
. It may seem logical because Math.sqrt(-1)
also gives you NaN
.
基本上,NaN
代表一个未定义的值。值0.0 / 0.0
是NaN
,和Nan != NaN
。这似乎合乎逻辑,因为Math.sqrt(-1)
也给了你NaN
.
See the javadoc of Double.NaN
:
请参阅以下的 javadoc Double.NaN
:
It is equivalent to the value returned by
Double.longBitsToDouble(0x7ff8000000000000L)
它相当于返回的值
Double.longBitsToDouble(0x7ff8000000000000L)
And then Double.longBitsToDouble()
:
If the argument is any value in the range
0x7ff0000000000001L
through0x7fffffffffffffffL
or in the range0xfff0000000000001L
through0xffffffffffffffffL
, the result is aNaN
. No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values of the same type with different bit patterns.
如果参数是在范围内的任何值
0x7ff0000000000001L
通过0x7fffffffffffffffL
或在范围0xfff0000000000001L
通过0xffffffffffffffffL
,其结果是一个NaN
。Java 提供的 IEEE 754 浮点运算无法区分具有不同位模式的相同类型的两个 NaN 值。
回答by xyz
Because only NaN compares false with itself. So it will return true when you pass NaN
to the method.
因为只有 NaN 与自身比较 false。所以当你传递NaN
给方法时它会返回true 。
A comparison with a NaN always returns an unordered result even when comparing with itself. ... The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN.
与 NaN 的比较总是返回一个无序的结果,即使与它自己比较时也是如此。... 等式和不等式谓词是无信号的,因此返回 false 的 x = x 可用于测试 x 是否为安静的 NaN。
It not just about Java, It is also true for all languages following IEEE754 standard.
它不仅适用于 Java,对于遵循 IEEE754 标准的所有语言也是如此。
Related question : Why does Double.NaN==Double.NaN return false?
回答by axtavt
From Java Language Specification:
Floating-point equality testing is performed in accordance with the rules of the IEEE 754 standard:
If either operand is NaN, then the result of == is false but the result of != is true. Indeed, the test x!=x is true if and only if the value of x is NaN. (The methods Float.isNaN and Double.isNaN may also be used to test whether a value is NaN.)
Positive zero and negative zero are considered equal. Therefore, -0.0==0.0 is true, for example.
Otherwise, two distinct floating-point values are considered unequal by the equality operators. In particular, there is one value representing positive infinity and one value representing negative infinity; each compares equal only to itself, and each compares unequal to all other values.
浮点相等性测试按照 IEEE 754 标准的规则执行:
如果任一操作数为 NaN,则 == 的结果为假,但 != 的结果为真。实际上,当且仅当 x 的值为 NaN 时,测试 x!=x 才为真。(Float.isNaN 和 Double.isNaN 方法也可用于测试值是否为 NaN。)
正零和负零被认为是相等的。因此,例如,-0.0==0.0 为真。
否则,相等运算符将两个不同的浮点值视为不相等。特别是,有一个值代表正无穷大,一个值代表负无穷大;每个比较只等于它自己,每个比较不等于所有其他值。
回答by Suresh Atta
回答by Maroun
Simple.. Nan
is always !=NaN
, these values are not equal to anything. See this:
简单..Nan
总是!=NaN
,这些值不等于任何东西。看到这个:
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 returns true, including x!=x when x is NaN.
如前所述, NaN 是无序的,因此涉及一个或两个 NaN 的数值比较操作返回 false,任何涉及 NaN 的 != 比较都返回 true,包括 x!=x 当 x 是 NaN 时。
So testing if v != v
is sufficient to tell whether the value
is NaN
or not.
所以测试是否v != v
足以判断是否value
是NaN
。