浮点序列化,字典序比较==浮点比较
我正在寻找一种序列化浮点的方法,以便在序列化形式中,字典比较与浮点比较相同。我认为可以通过以下形式存储它:
| 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.