Float.NaN == Float.NaN
为什么这个比较给我“假”?我查看了源代码,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 等于它自己!
Why does this comparison give me 'false'? I looked at the source and Float.NaN is defined as
/**
* 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;
EDIT: surprisingly, if I do this:
System.out.println("FC " + (Float.compare(Float.NaN, Float.NaN)));
it gives me 0
. So Float.compare()
does think that NaN is equal to itself!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Float.isNaN 检查 NaN 值。
Use
Float.isNaN
to check for NaN values.因为 Java 实现了 IEEE-754 浮点标准,该标准保证任何与
NaN
的比较都将返回 false(!=
除外,它返回 true),这意味着,您无法检查按照您通常的方式,浮点数是否为 NaN,因此您可以将这两个数字重新解释为整数并进行比较,或者使用更聪明的解决方案:
Because Java implements the IEEE-754 floating point standard which guarantees that any comparison against
NaN
will return false (except!=
which returns true)That means, you can't check in your usual ways whether a floating point number is NaN, so you could either reinterpret both numbers as ints and compare them or use the much cleverer solution:
我需要说的是:维基百科关于 NaN。
写得很清楚了。有趣的是,通用标准的浮点 NaN 是这样表达 NaN 的:
s 是符号(负或正),1 是指数,x 被视为有效负载。
查看有效负载,NaN 不等于任何 NaN,并且作为开发人员,您很少会对有效负载的这些信息感兴趣(例如复数)。
另一件事是,在标准中他们有信号并且相当 NaN。信号 NaN (sNaN) 意味着 NaN 应该引发类似异常的反应。它应该用来大声说出你的方程有问题。安静的 NaN (qNaN) 是静默传递的 NaN。
创建信号的 sNaN 将转换为 qNaN,以便在后续操作中不会进一步产生任何更多信号。请记住,某些系统将 i^0 = 1 定义为 NaN^0 = 1 成立的常量。所以在某些情况下人们会用 NaN 进行计算。
所以最后我会这样: qNaN != sNaN 但这是内部的,用户无法观察到(你无法检查)。将付款和符号混合在一起(是的,你可以有负数和正数 NaN),在我看来,总是返回 NaN != NaN 看起来是一个更明智的选择,我终于学会了欣赏 ->我再也不会抱怨或怀疑 NaN 的不等式了。赞扬那些为我们提供这么好的标准的周到的人们!
顺便说一下:Java 使用正数 NaN,有效负载为 0(所有 x 均为零)。
All I need to say is: Wikipedia About NaN.
It is written quite clearly. The interesting part is that the floating point NaN of the common standard expresses a NaN this way:
The s is the sign (negative or positive), the 1's are the exponent and the x is regarded as a payload.
Looking at the payload a NaN is not equal any NaN and there are rare chances that these information of the payload are interesting for you as a developer (e.g. complex numbers).
Another thing is that in the standard they have signalling and quite NaN. A signalling NaN (sNaN) means an NaN that should raises a reaction like a exception. It should be used to say out loud that you have a problem in your equation. A quiet NaN (qNaN) is a NaN that is silently passed on.
A sNaN that created a signal is converted to a qNaN to not further produce any more signals in subsequent operations. Remember some system define i^0 = 1 as a constant that NaN^0 = 1 holds true. So there are cases where people calculate with NaN.
So in the end I would go with this: qNaN != sNaN but this is internal and is not observable for the user (you cant check that). Mix along the payment and the sign (yes you can have negative and positive NaN) and it appears to me that always return NaN != NaN looks like a much wiser choice that I finally learned to appreciate -> I will never ever complain or wonder about the inequality of NaN again. Praise the folks who were that thoughtful giving us such a good standard!
By the way: Java uses a positive NaN with a payload of 0 (all x are zeros).