简体   繁体   English

标准化二进制浮点值

[英]Normalizing Binary Floating Point Values

I'm a little confused on how to normalize numbers in C. I know that if you have something like the floating-point binary value 1101.101, it is normalized as 1.101101 x 2^3 by moving the decimal point 3 positions to the left. 我对如何在C中标准化数字有些困惑。我知道,如果您具有类似浮点二进制值1101.101的内容,则可以通过将小数点3位置向左移动来将其标准化为1.101101 x 2 ^ 3。 However, I am not sure how to do this in code. 但是,我不确定如何在代码中执行此操作。

So if I have 10010000 exp 0 and I want to get 01001000 exp 1, the fraction field is "001" and the final result is 010001. 因此,如果我有10010000 exp 0,而我想获得01001000 exp 1,则分数字段为“ 001”,最终结果为010001。

If I have an 8 bit number and the leading 2 bits are 00, I want to move it until I find a 1, or if I have a leading bit of 01 then it is already normalized. 如果我有一个8位数字,而前2位是00,我想将其移动直到找到1,或者如果我有一个前导位01,则它已经被标准化。 Similary for a leading 2 bits of 11 I move it to the left one. 对于11的前2位,我将其移到左边。

So if my number was 32 bits, 1 for the sign, 8 for the exponent, 23 for the mantissa, would I do something like: 因此,如果我的数字是32位,符号1,指数8,尾数23,我会做些什么:

if ( (result >> 24) == "11") ) // although here I know I'm comparing an int with a string
   {
   //code to set result = to the new number
   }
if ( (result >> 24) == "01" ) )
   {
      return result; 
   }

And then several more if statements for the other 2 leading bit numbers (00, 10)? 然后再有几个if语句用于其他2个前导位数(00,10)? I am not sure if my logic/code is right. 我不确定我的逻辑/代码是否正确。

Thanks. 谢谢。

No it is not correct. 不,这是不正确的。

The floating point numbers are represented in IEEE 754 Floating point format , you cannot just shift the number to normalize it. 浮点数以IEEE 754浮点格式表示 ,您不能只移动数字以对其进行规范化。

If you want to represent the number in this format: 如果要以这种格式表示数字:

(+/-)1.M x 2 E

Then you will have to extract sign bit, mantissa M and exponent E separately first. 然后,您必须先分别提取符号位,尾数M和指数E

Then do binary operations (shifting, AND, OR, etc.) to represent it in the way you want. 然后执行二进制操作(移位,AND,OR等)以所需的方式表示它。

IEEE 754单精度格式


Like you say, you need to extract the sign, mantissa and exponent . 就像您说的那样,您需要提取符号,尾数指数 The diagram above taken from Wikipedia shows how it is laid out for the most common single precision floating point format, IEEE 754. 上面的摘自Wikipedia的图表显示了如何针对最常见的单精度浮点格式IEEE 754进行布局。

To extract each part we need to do this in three steps detailed below. 要提取每个部分,我们需要按照以下三个步骤进行操作。 I don't have the C code but I will show the steps you need to take. 我没有C代码,但我将显示您需要采取的步骤。 After extracting the 3 parts just place them in the bit positions shown in the diagram above. 提取3个部分后,只需将它们放在上图所示的位位置即可。

1. Sign 1.签收

If it's an unsigned number this is always 0. 如果是无符号数字,则始终为0。

If it's signed it's the MSB. 如果已签名,则为MSB。

2. Exponent 2.指数

If the number is signed and negative you will need to flip all the bits and add 1 to turn it positive. 如果该数字是带正负号的,则需要翻转所有位并加1使其变为正数。 If not you can leave it as it is. 如果没有,您可以保留原样。

To extract the exponent we must know where the binary point is meant to be. 要提取指数,我们必须知道二进制点的位置。 Let the position of the binary point be b (In your example its 3). 设二进制点的位置为b (在您的示例中为3)。

Let the first bit from the MSB that is 1 be p (In your example its 6). 让从MSB是1个是p的第一个位(在您的例子其6)。

Letting the exponent be e : 令指数为e

e = 127 - (b - p) e = 127-(b-p)

3. Mantissa 3.尾数

This will be equal to the bits from position p-1 down to bit 0. 这将等于从位置p-1到位0的位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM