浮点序列化,字典序比较==浮点比较

发布于 2024-10-21 13:32:08 字数 222 浏览 7 评论 0原文

我正在寻找一种序列化浮点的方法,以便在序列化形式中,字典比较与浮点比较相同。我认为可以通过以下形式存储它:

| signed bit (1 for positive) | exponent | significand |

指数和有效数将被序列化为大端,补码将被取为负数。

这行得通吗?我不介意它是否因 NaN 而中断,但 INF 比较工作会很好。

I'm looking for a way to serialize floating points so that in their serialized form a lexicographical comparison is the same as a floating point comparison. I think it is possible by storing it in the form:

| signed bit (1 for positive) | exponent | significand |

The exponent and the significand would be serialized as big-endian and the complement would be taken for negative numbers.

Would this work? I don't mind if it breaks for NaN, but having INF comparison working would be nice.

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

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

发布评论

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

评论(1

自由范儿 2024-10-28 13:32:08

IEEE 数字的格式经过专门设计,以便可以使用“普通”整数比较。但是,这只适用于比较两个相同符号的数字时。

你的建议是当数字为负时对其进行补充,这是合理的,所以这会起作用。

这适用于 +-Inf:s 和次正规数。然而,NaN:s 不起作用,或者更确切地说,它们将被视为比 inf:s“更大”。

唯一有问题的情况是“-零”(即符号=1、指数=0、尾数=0)。根据 IEEE,零 == -零。您必须决定是否要将 -Zero 作为零发出,将它们视为不同的,或者向比较例程添加特殊代码。

The format of IEEE numbers are specifically designed so that "plain" integer comparison could be used. However, this only applies when two numbers of the same sign is compared.

Your suggestion to complement the numbers when they are negative is sound, so this will work.

This will work for +-Inf:s and for subnormal numbers. NaN:s, however, will not work, or rather, they will be considered "larger" than inf:s.

The only problematic case is "-Zero" (i.e. sign=1, exponent=0, and mantissa=0). Accoring to IEEE, Zero == -Zero. You have to decide if you want to emit -Zero as Zero, treat them as different, or add special code to the comparison routine.

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