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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:39:24  来源:igfitidea点击:

Java isNan how it works?

javafloating-pointnan

提问by rascio

I was looking at the openjdk-1.7.0_25source 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, NaNrepresents an undefined value. The value of 0.0 / 0.0is NaN, and Nan != NaN. It may seem logical because Math.sqrt(-1)also gives you NaN.

基本上,NaN代表一个未定义的值。值0.0 / 0.0NaN,和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():

然后Double.longBitsToDouble()

If the argument is any value in the range 0x7ff0000000000001Lthrough 0x7fffffffffffffffLor in the range 0xfff0000000000001Lthrough 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.

如果参数是在范围内的任何值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 NaNto 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。

Source

来源

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?

相关问题:为什么 Double.NaN==Double.NaN 返回 false?

回答by axtavt

From Java Language Specification:

Java 语言规范

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

As far as I know NaN valueis not equal to anything.So when you passed float v,It's never not equal to it self.

据我所知NaN 值不等于任何东西。所以当你通过时float v,它永远不等于它自己。

回答by Maroun

Simple.. Nanis 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 != vis sufficient to tell whether the valueis NaNor not.

所以测试是否v != v足以判断是否valueNaN