其他 NaN 值是什么?
保存双精度型非数字 (NaN) 值的常量。它相当于
Double.longBitsToDouble(0x7ff8000000000000L)
返回的值。
这似乎暗示还有其他人。如果是这样,我如何获得它们,这可以便携地完成吗?
需要明确的是,我想找到 double
值 x
使得
Double.doubleToRawLongBits(x) != Double.doubleToRawLongBits(Double.NaN)
和
Double.isNaN(x)
都为真。
The documentation for java.lang.Double.NaN
says that it is
A constant holding a Not-a-Number (NaN) value of type
double
. It is equivalent to the value returned byDouble.longBitsToDouble(0x7ff8000000000000L)
.
This seems to imply there are others. If so, how do I get hold of them, and can this be done portably?
To be clear, I would like to find the double
values x
such that
Double.doubleToRawLongBits(x) != Double.doubleToRawLongBits(Double.NaN)
and
Double.isNaN(x)
are both true.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要
doubleToRawLongBits
而不是doubleToLongBits
。doubleToRawLongBits
提取实际的二进制表示形式。doubleToLongBits
不会,它首先将所有NaN
转换为默认的NaN
。输出:
You need
doubleToRawLongBits
rather thandoubleToLongBits
.doubleToRawLongBits
extracts the actual binary representation.doubleToLongBits
doesn't, it converts allNaN
s to the defaultNaN
first.output:
Java 使用 IEEE 754 作为其浮点数,因此遵循其规则。
根据 NaN 的维基百科页面,它的定义如下:
因此有相当多的位模式都是 NaN 值。
Java uses IEEE 754 for its floating point numbers and therefore follows their rules.
According to the Wikipedia page on NaN it is defined like this:
So there are quite a few bit-patterns all of which are
NaN
values.IEEE 754 将 NaN 定义为所有指数位均为 1 且尾数为非零数的数字。
因此,对于您正在寻找的单精度数字:
Java 是这样处理的:
总而言之,您可以使用任何符合上述规则的 NaN(指数中的所有位都为 1,尾数!= 0)。
IEEE 754 defines a NaN as a number with all exponent bits which are
1
and a non zero number in the mantissa.So for a single-precision number you are looking for:
Java handles this like so:
To sum, you can use any NaN you want which conforms to the rules aforementioned (all bits 1 in exponent and mantissa != 0).