开发者

Floating point serialization, lexicographical comparison == floating point comparison

开发者 https://www.devze.com 2023-02-15 21:33 出处:网络
I\'m looking for a way to serialize floating p开发者_运维技巧oints so that in their serialized form a lexicographical comparison is the same as a floating point comparison. I think it is possible by s

I'm looking for a way to serialize floating p开发者_运维技巧oints 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号