本文介绍了在其位重新presentations比较花车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我想要一个函数,需要两个浮点数( X ),我想对它们进行比较使用不是他们的浮动重新presentation而是他们按位重新presentation作为32位 unsigned int类型。也就是说,如 -495.5 若干具有重presentation 0b11000011111001011100000000000000 0xC3E5C000位浮动,和我有一个 unsigned int类型与再$ p相同的位$ psentation(相当于十进制值 3286614016 ,我不关心)。有我,来执行℃的操作没有简单的方法; = 使用始载于各自的信息,这些浮标unsigned int类型同行?

Say I want a function that takes two floats (x and y), and I want to compare them using not their float representation but rather their bitwise representation as a 32-bit unsigned int. That is, a number like -495.5 has bit representation 0b11000011111001011100000000000000 or 0xC3E5C000 as a float, and I have an unsigned int with the same bit representation (corresponding to a decimal value 3286614016, which I don't care about). Is there any easy way for me to perform an operation like <= on these floats using only the information contained in their respective unsigned int counterparts?

推荐答案

您必须做一个比较签订,除非你确保所有的原始值均为阳性。您必须使用整数类型,其大小与原浮点类型相同。每个芯片可以具有不同的内部格式,因此从不同的芯片作为整数进行比较的值是最有可能得到误导的结果。

You must do a signed compare unless you ensure that all the original values were positive. You must use an integer type that is the same size as the original floating point type. Each chip may have a different internal format, so comparing values from different chips as integers is most likely to give misleading results.

大多数浮动格式是这个样子:

Most float formats look something like this:

sxxxmmmm

s是符号位

xxx是一个指数

MMMM是尾数

s is a sign bit
xxx is an exponent
mmmm is the mantissa

转口货值为presented后会有类似

the value represented will then be something like

1mmm&所述;&下; (XXX-K)

1mmm << (xxx-k)

1mmm因为有一个隐含的领先的1位,除非值为零。

1mmm because there is an implied leading 1 bit unless the value is zero.

如果XXX&LT; 10k,则这将是一个右移位。 k是邻近但不等于一半可能是用xxx pssed前$ P $的最大值。它被调整为尾数的大小

if xxx < k then it will be a right shift. k is near but not equal to half the largest value that could be expressed by xxx. it is adjusted for the size of the mantissa.

所有的说,无论NaN的,因为同样大小的有符号整数将产生有意义的结果比较浮点值。他们设计的方式,使浮点比较是不超过整数比较昂贵。有编译器优化来关闭NaN的检查,以使比较是直的整数比较,如果芯片的浮点格式支持它。

All to say that, disregarding NaN, comparing floating point values as signed integers of the same size will yield meaningful results. They are designed that way so that floating point comparisons are no more costly than integer comparisons. There are compiler optimizations to turn off NaN checks so that the comparisons are straight integer comparisons if the floating point format of the chip supports it.

作为一个整数,楠大于无穷比有限值更大。如果您尝试一个无符号的比较,所有的负值会比正值越大,就像投为无符号整数。

As an integer, NaN is greater than infinity is greater than finite values. If you try an unsigned compare, all the negative values will be larger than the positive values, just like signed integers cast to unsigned.

这篇关于在其位重新presentations比较花车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 14:26