简体   繁体   English

如何将 IEEE 单精度浮点数转换为十进制值

[英]How to convert an IEEE single precision floating point to a decimal value

So I am trying to convert 46bfc000 (which is a floating-point number in IEEE single precision) into a decimal value.所以我试图将 46bfc000(IEEE 单精度中的浮点数)转换为十进制值。

I can get a approximate value, but not the exact value.我可以得到一个近似值,但不能得到准确值。 So here is my work for my approximate value:所以这是我的近似值的工作:

1) Convert into binary: 0100 0110 1011 1111 1100 0000 0000 0000 1)转换成二进制: 0100 0110 1011 1111 1100 0000 0000 0000

2) Find b-exp : 141-127 2)找到 b-exp : 141-127

3) Convert what is after the decimal value : 2^-1 + 2^-5... = .552726746 3)转换十进制值后的内容:2^-1 + 2^-5... = .552726746

4) Now follow this equation format : (1)sign bit * (1. + value in step 3) * 2^b-exp 4)现在遵循这个等式格式:(1)符号位*(1。+步骤3中的值)* 2^b-exp

5) Calculate : +1 X (1.5527226746) X 2^14 = 25439.87501 5)计算:+1 X (1.5527226746) X 2^14 = 25439.87501

Now I know that the exact value is: 24544. But I am wondering if there is a way for me to get the exact number, or is it impossible to convert a IEEE single precision binary to a decimal value?现在我知道确切的值是:24544。但我想知道是否有办法让我得到确切的数字,或者是否不可能将 IEEE 单精度二进制转换为十进制值?

I have figured out the equation to get out the exact number of the binary representation, it is: sign * 2^b-exp * mantissa我已经找到了得到二进制表示的确切数字的等式,它是:符号 * 2^b-exp * 尾数

Edit: To get the right mantissa, you need to ONLY calculate it starting at the fractional part of the binary.编辑:要获得正确的尾数,您只需要从二进制的小数部分开始计算它。 So for example, if your fractional is 011 1111...例如,如果您的小数是 011 1111 ...

Then you would do (1*2^-0) + (1*2^-1) + (1*2^-2)...然后你会做 (1*2^-0) + (1*2^-1) + (1*2^-2)...

Keep doing this for all the numbers and you'll get your mantissa.继续对所有数字执行此操作,您将获得尾数。

Instead of calculating all those bits behind the comma, which is heck of a job, IMO, just scale everything by 2^23 and subtract 23 more from the exponent for compensation.而不是计算逗号后面的所有那些位,这是一项非常艰巨的工作,IMO,只需将所有内容按 2^23 缩放并从指数中减去 23 以进行补偿。

This is explained in my article about floating point for Delphi .这在我关于 Delphi 的浮点数的文章中进行了解释。

First decode:第一次解码:

0 - 1000 1101 - 011 1111 1100 0000 0000 0000

Insert hidden bit:插入隐藏位:

0 - 1000 1101 - 1011 1111 1100 0000 0000 0000

In hex:十六进制:

0 - 8D - BFC000

0x8D = 141 , minus bias of 127 , that becomes 14 . 0x8D = 141 ,减去127偏差,变成14

I like to scale things, so the calculation is:我喜欢缩放东西,所以计算是:

sign * full_mantissa * (exp - bias - len)

where full_mantissa is the mantissa, including hidden bit, as integer ;其中 full_mantissa 是尾数,包括隐藏位,为整数 bias = 127 and len = 23 (the number of mantissa bits).偏差 = 127 和 len = 23(尾数位数)。

So then it becomes:那么就变成了:

1 * 0xBFC000 * 2^(14-23) = 0xBFC000 / 0x200 = 0x5FE0 = 24544

because 2^(14-23) = 2^-9 = 1 / 2^9 = 1 / 0x200 .因为2^(14-23) = 2^-9 = 1 / 2^9 = 1 / 0x200

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

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