浮点序列化,字典比较==浮点比较 [英] Floating point serialization, lexicographical comparison == floating point comparison

查看:153
本文介绍了浮点序列化,字典比较==浮点比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  |有符号位(1为正)|指数|有效位数

指数和有效位数将序列化为大尾数,补数将取负数。



这会有效吗?

解决方案

IEEE数字的格式被特别设计,使得可以使用简单整数比较。但是,这仅适用于比较两个相同符号的数字。



您的建议是,当数字为负数时,这是正确的。 p>

这将适用于+ -Inf:s和子正常数字。



唯一有问题的情况是-Zero(null)。即sign = 1,exponent = 0,尾数= 0)。达到IEEE,零== - 零。您必须决定是否要将零发送为零,将它们视为不同,或向比较例程中添加特殊代码。


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.

解决方案

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.

这篇关于浮点序列化,字典比较==浮点比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆