java floats 可以按字节表示排序吗?

发布于 2024-08-12 04:53:08 字数 306 浏览 3 评论 0原文

我在 Hadoop 中工作,我需要提供一个比较器来将对象排序为原始网络顺序字节数组。对于我来说,使用整数很容易做到这一点——我只需按顺序比较每个字节即可。我还需要对浮点数执行此操作。我认为,但我找不到参考,Java 使用的浮点数的 IEEE 754 格式可以通过将每个字节作为带符号的 8 位值进行比较来排序。

谁能证实或反驳这一点?

编辑:表示形式为 IEEE 754 32 位浮点。我实际上有一个(更大的)字节缓冲区以及该缓冲区内的偏移量和长度。我发现已经存在一些实用方法,可以轻松地将其转换为浮点数,所以我想这个问题没有实际意义。我仍然很好奇是否有人知道答案。

I'm working in Hadoop, and I need to provide a comparator to sort objects as raw network order byte arrays. This is easy for me to do with integers -- I just compare each byte in order. I also need to do this for floats. I think, but I can't find a reference, that the IEEE 754 format for floats used by Java can be sorted by just comparing each byte as a signed 8 bit value.

Can anyone confirm or refute this?

Edit: the representation is IEEE 754 32 bit floating point. I actually have a (larger) byte buffer and an offset and length within that buffer. I found some utility methods already there that make it easy to turn this into a float, so I guess this question is moot. I'm still curious if anyone knows the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

左岸枫 2024-08-19 04:53:08

正浮点数与被视为 2 补码整数的位表示形式具有相同的顺序。负浮点数则不然。

例如,-2.0f 的位表示为 0xc0000000,-1.0f 的位表示为 0xbf800000。如果您尝试对表示进行比较,您会得到 -2.0f > -1.0f,这是不正确的。

还有 NaN 的问题(它将无序与所有浮点数据进行比较,而表示则不然),但您可能不关心它们。

Positive floats have the same ordering as their bit representations viewed as 2s complement integers. Negative floats do not.

For example, the bit representation of -2.0f is 0xc0000000 and -1.0f is 0xbf800000. If you try to use a comparison on the representations, you get -2.0f > -1.0f, which is incorrect.

There's also the issue of NaNs (which compare unordered against all floating point data, whereas the representations do not), but you may not care about them.

您的好友蓝忘机已上羡 2024-08-19 04:53:08

这几乎有效:

int bits = Float.floatToIntBits(x);
bits ^= (bits >> 31) & Integer.MAX_VALUE;

这里负浮点数的位 0-30 被反转(因为您想要与原始符号/幅度表示形式相反的顺序,同时保留符号位。)

注意事项:

  • NaN 包含在排序中(最好如果涉及 NaN,则考虑结果未定义。)
  • +0 现在比较为大于 -0(内置关系运算符认为它们相等。)

但它适用于所有其他值,包括非正规值和无穷大。

This almost works:

int bits = Float.floatToIntBits(x);
bits ^= (bits >> 31) & Integer.MAX_VALUE;

Here negative floats have bits 0-30 inverted (because you want the opposite order to what the original sign/magnitude representation would give you, whilst preserving the sign bit.)

Caveats:

  • NaNs are included in the ordering (best to consider the results undefined if NaNs are involved.)
  • +0 now compares as greater than -0 (the built-in relational operators consider them equal.)

It works for all other values though, including denormals and infinities.

月下伊人醉 2024-08-19 04:53:08

使用 Float.toIntBits(float) 并比较整数。

编辑:这仅适用于正数,包括正无穷大,但不适用于 NaN。对于负数,您必须颠倒顺序。正数当然大于负数。

Use Float.toIntBits(float) and compare the integers.

Edit: This works only for positive numbers, including positive infinity, but not NaN. For negative numbers you have to reverse the ordering. And positive numbers are of course greater than negative numbers.

枕梦 2024-08-19 04:53:08

好吧,如果您通过网络传输数据,那么当您传输 int 和 float 时,您应该有某种形式的语义表示。由于它是与机器无关的信息,因此数据类型宽度也应该在某个地方定义或由规范预定义(即 32 位或 64 位浮点数)。因此,您真正应该做的是将字节累积到适当的数据类型中,然后使用自然语言数据类型进行比较。

为了真正准确地给出答案,我们需要查看您的传输和接收代码,以查看您是否通过某种修饰的 I/O 流或类似的东西自动装箱原语。为了得到更好的答案,请提供更详细的信息。

well, if you are transmitting data over the network, you should have some form of semantic representation for when you are transmitting an int and when you are transmitting a float. Since it is machine agnostic information, data type width should also be defined in some place or predefined by the spec (i.e 32 bit or 64 bit floats). So, what you should really do is accumulate your bytes into the appropriate data type, then use the natural language data types to do the comparison.

To be really accurate with an answer tho, we would need to see your transmit and receive code to see if you are autoboxing primitives via some kind of decorated i/o stream or some such thing. For a better answer, please provide better detail.

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