其他 NaN 值是什么?

发布于 2024-08-19 10:52:58 字数 539 浏览 7 评论 0原文

java.lang 的文档。 Double.NaN 说它是

保存双精度型非数字 (NaN) 值的常量。它相当于 Double.longBitsToDouble(0x7ff8000000000000L) 返回的值。

这似乎暗示还有其他人。如果是这样,我如何获得它们,这可以便携地完成吗?

需要明确的是,我想找到 doublex 使得

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 by Double.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千鲤 2024-08-26 10:52:58

您需要 doubleToRawLongBits 而不是 doubleToLongBits

doubleToRawLongBits 提取实际的二进制表示形式。 doubleToLongBits 不会,它首先将所有 NaN 转换为默认的 NaN

double n = Double.longBitsToDouble(0x7ff8000000000000L); // default NaN
double n2 = Double.longBitsToDouble(0x7ff8000000000100L); // also a NaN, but M != 0

System.out.printf("%X\n", Double.doubleToLongBits(n));
System.out.printf("%X\n", Double.doubleToRawLongBits(n));
System.out.printf("%X\n", Double.doubleToLongBits(n2));
System.out.printf("%X\n", Double.doubleToRawLongBits(n2));

输出:

7FF8000000000000
7FF8000000000000
7FF8000000000000
7FF8000000000100

You need doubleToRawLongBits rather than doubleToLongBits.

doubleToRawLongBits extracts the actual binary representation. doubleToLongBits doesn't, it converts all NaNs to the default NaN first.

double n = Double.longBitsToDouble(0x7ff8000000000000L); // default NaN
double n2 = Double.longBitsToDouble(0x7ff8000000000100L); // also a NaN, but M != 0

System.out.printf("%X\n", Double.doubleToLongBits(n));
System.out.printf("%X\n", Double.doubleToRawLongBits(n));
System.out.printf("%X\n", Double.doubleToLongBits(n2));
System.out.printf("%X\n", Double.doubleToRawLongBits(n2));

output:

7FF8000000000000
7FF8000000000000
7FF8000000000000
7FF8000000000100
放低过去 2024-08-26 10:52:58

Java 使用 IEEE 754 作为其浮点数,因此遵循其规则。

根据 NaN 的维基百科页面,它的定义如下:

IEEE 浮点标准单精度 NaN 的逐位示例:x111 1111 1axx xxxx xxxx xxxx xxxx xxxx 其中 x 表示 不照顾。


因此有相当多的位模式都是 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:

A bit-wise example of a IEEE floating-point standard single precision NaN: x111 1111 1axx xxxx xxxx xxxx xxxx xxxx where x means don't care.

So there are quite a few bit-patterns all of which are NaN values.

假扮的天使 2024-08-26 10:52:58

IEEE 754 将 NaN 定义为所有指数位均为 1 且尾数为非零数的数字。

因此,对于您正在寻找的单精度数字:

S     E            M
x  11111111   xxxxxx....xxx (with M != 0)

Java 是这样处理的:

Double n = Double.longBitsToDouble(0x7ff8000000000000L); // default NaN
Double n2 = Double.longBitsToDouble(0x7ff8000000000100L); // also a NaN, but M != 0

System.out.println(n.isNaN()); // true
System.out.println(n2.isNaN()); // true
System.out.println(n2 != Double.doubleToLongBits(Double.NaN)); // true

总而言之,您可以使用任何符合上述规则的 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:

S     E            M
x  11111111   xxxxxx....xxx (with M != 0)

Java handles this like so:

Double n = Double.longBitsToDouble(0x7ff8000000000000L); // default NaN
Double n2 = Double.longBitsToDouble(0x7ff8000000000100L); // also a NaN, but M != 0

System.out.println(n.isNaN()); // true
System.out.println(n2.isNaN()); // true
System.out.println(n2 != Double.doubleToLongBits(Double.NaN)); // true

To sum, you can use any NaN you want which conforms to the rules aforementioned (all bits 1 in exponent and mantissa != 0).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文