使用字节表示法对浮点值进行排序 [英] Sorting floating-point values using their byte-representation

查看:73
本文介绍了使用字节表示法对浮点值进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有一个8字节的数据段并在其中写入一个双精度浮点值,那么在什么条件下通过数字比较和按字节顺序的字典比较可以达成一致?

If have an 8-byte section of data and write a double-precision floating-point value to it, under what conditions will comparison by numerical comparison and lexicographic sorting of the bytes agree?

我相信,如果数字为正,且表示形式为big-endian,则浮点值的数字顺序将与字节的字典顺序相匹配.

I believe that if the number is positive, and the representation is big-endian, then numerical ordering of the floating-point values will match the lexicographic ordering of the bytes.

这个想法是,它会首先按指数排序,然后按尾数排序.甚至非规范化" IEEE表示也不会引起任何问题.

The idea is that it would first sort on the exponent, then on the mantissa. Even the "denormalized" IEEE representation shouldn't cause any problems.

这是真的吗?

(我正在使用Node的 Buffer :: writeDoubleBE ,但是没关系.)

(I'm using Node's Buffer::writeDoubleBE, but that shouldn't matter.)

我认为一个简单的修改可以将其扩展为负数:将所有正数与0x8000...和负数与0xffff...进行异或.这应该翻转两个符号位(因此负数先出现),然后颠倒负数的顺序.有人看到这个问题吗?

I think a simple modification can extend this to negative numbers: XOR all positive numbers with 0x8000... and negative numbers with 0xffff.... This should flip the sign bit on both (so negative numbers go first), and then reverse the ordering on negative numbers. Does anyone see a problem with this?

推荐答案

您的方法:

我认为一个简单的修改可以将其扩展为负数:对所有正数与0x8000 ...和负数与0xffff ...进行XOR.然后颠倒负数的顺序.有人看到这个问题吗?

I think a simple modification can extend this to negative numbers: XOR all positive numbers with 0x8000... and negative numbers with 0xffff.... This should flip the sign bit on both (so negative numbers go first), and then reverse the ordering on negative numbers. Does anyone see a problem with this?

绝对是答案. 此外,例如,它被用于 dBase 中,并进行克隆以对浮点列进行排序,而且我猜想它将会是新一代的数据库.

is definitely the answer. Moreover, it was used, for example, in dBase and clones to organize sorting on a float column, and I guess it's followed by newer DB generations.

此外,对于二进制表示,它与IEEE-754的总顺序"相同. (但不是十进制的,后者要复杂得多.)

Also, it is identical to the "total order" according to IEEE-754 for binary representations. (But not for decimal ones, the latter is much more complex.)

更新:如@Sneftel所建议:在转换为位字符串之前,您可能会发现用+0替换-0很有用.

UPDATE: as suggested by @Sneftel: you could find replacing -0 with +0 as useful before converting to bit string.

这篇关于使用字节表示法对浮点值进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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