问题描述
<$ p
如果指定的数字是
*非数字(NaN)值,则返回{@code true};否则返回{@code false} 。
*
* @param v要测试的值。
* @return {@code true}如果参数是NaN;
* {@code false}否则。
* /
static public boolean isNaN(float v){
return(v!= v);
$ b $ p
$ b
我不明白它是如何工作的,当这个方法可以返回 true
?
对于某些操作,例如:
$ b $ pre $ System.out.println(Float.isNaN(0.0f / 0.0f));
System.out.println(Double.isNaN(Math.sqrt(-1)));
基本上, NaN
代表一个未定义的值。 0.0 / 0.0
的值是 NaN
和 Nan!= NaN
。这似乎是合乎逻辑的,因为 Math.sqrt(-1)
也给你 NaN
。
查看:
返回的值然后 Double.longBitsToDouble()$
$ b
I was looking at the openjdk-1.7.0_25
source code and I have seen this method:
/**
* 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
?
That method can return true for certain operations, for example:
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
.
See the javadoc of Double.NaN
:
And then Double.longBitsToDouble()
:
这篇关于Java是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!