binary - 指数偏差如何使比较更容易

标签 binary

我正在阅读 this关于 float 中的 exponent bias 的文章,内容如下:

n IEEE 754 floating point numbers, the exponent is biased in the engineering sense of the word – the value stored is offset from the actual value by the exponent bias. Biasing is done because exponents have to be signed values in order to be able to represent both tiny and huge values, but two's complement, the usual representation for signed values, would make comparison harder. To solve this problem the exponent is biased before being stored, by adjusting its value to put it within an unsigned range suitable for comparison. By arranging the fields so that the sign bit is in the most significant bit position, the biased exponent in the middle, then the mantissa in the least significant bits, the resulting value will be ordered properly, whether it's interpreted as a floating point or integer value. This allows high speed comparisons of floating point numbers using fixed point hardware.

我还从维基百科的 article 中找到了这个解释关于偏移二进制:

This has the consequence that the "zero" value is represented by a 1 in the most significant bit and zero in all other bits, and in general the effect is conveniently the same as using two's complement except that the most significant bit is inverted. It also has the consequence that in a logical comparison operation, one gets the same result as with a two's complement numerical comparison operation, whereas, in two's complement notation a logical comparison will agree with two's complement numerical comparison operation if and only if the numbers being compared have the same sign. Otherwise the sense of the comparison will be inverted, with all negative values being taken as being larger than all positive values.

我不太明白他们这里说的是什么比较。有人可以用一个简单的例子来解释吗?

最佳答案

这里的‘比较’指的是通常的按大小比较数字:5 > 4等,假设 float 用as存储

[sign bit] [unbiased exponent] [mantissa]

例如,如果指数是一个 2 的补码 3 位二进制数,而尾数是一个 4 位无符号二进制数,您将有

1 010 1001 = 4.5
1 110 0111 = 0.21875

您可以看到第一个比第二个大,但要计算出来,计算机必须计算 1.001 x 2^20.111 x 2^(-2 ) 然后比较结果 float 。这对于浮点硬件来说已经很复杂了,如果这台计算机没有这样的硬件,那么......

所以数字存储为

[sign bit] [biased exponent] [mantissa]

使用相同的 3 位二进制数作为指数(这次有偏差;参见 a related question)和无符号 4 位尾数,我们有

1 101 1001 = 4.5
1 001 0111 = 0.21875

但是现在比较很容易!您可以将这两个数字视为整数 1101100110010111 并看到第一个数字明显更大:即使对于计算机来说也是显而易见的,因为整数比较很容易。这就是使用有偏指数的原因。

关于binary - 指数偏差如何使比较更容易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37096796/

相关文章:

ASCII 和二进制之间的转换器和压缩器

Python ASCII 到二进制

binary - 可执行文件究竟是如何工作的?

c - 二进制浮点加法

javascript - javascript 中 undefined 是如何转换成二进制的?

c - 字符转二进制(ASCII)的递归函数

json - 将二进制数据发布到 RESTful 应用程序

c++ - 二进制数。检查以确保二进制输入是二进制时出错

python - 黑白图像到二进制数组

binary - 将 APNS 二进制协议(protocol)升级为增强的基于 HTTP/2 的 API