Java是如何工作的? [英] Java isNan how it works?

查看:180
本文介绍了Java是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



<$ 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.NaN


相当于Double.longBitsToDouble(0x7ff8000000000000L)


返回的值然后 Double.longBitsToDouble() $ b


如果参数是 0x7ff0000000000001L 0x7fffffffffffffffL ,或者在 0xfff0000000000001L 0xffffffffffffffffL ,结果是 NaN 。 Java提供的IEEE 754浮点操作不能区分具有不同位模式的相同类型的两个NaN值。


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:

It is equivalent to the value returned by Double.longBitsToDouble(0x7ff8000000000000L)

And then Double.longBitsToDouble():

If the argument is any value in the range 0x7ff0000000000001L through 0x7fffffffffffffffL or in the range 0xfff0000000000001L through 0xffffffffffffffffL, the result is a NaN. No IEEE 754 floating-point operation provided by Java can distinguish between two NaN values of the same type with different bit patterns.

这篇关于Java是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆