简体   繁体   English

如何将二进制浮点数转换为十进制数

[英]How to convert binary floating point number to decimal number

I have used that site: http://sandbox.mc.edu/~bennet/cs110/flt/dtof.html they say that 2.625 is 0_100_0101 based on that the exponent is 4 and mantisa is 5/16 . 我使用过该网站: http0_100_0101他们说2.6250_100_0101基于指数是4 ,mantisa是5/16 So the number is (5/16) * 2^4 and this is definetly not 2.625 . 所以这个数字是(5/16) * 2^4 ,这绝对不是2.625

So how it should be. 那应该是怎么回事。

You are interpreting your data in a wrong way. 您正在以错误的方式解释数据。

0 is the sign, so the number is positive 100 is the exponent which is in fact 1 (2^1). 0是符号,所以数字是正数100是指数,实际上是1(2 ^ 1)。 The exponent is signed and the possible values are: 指数已签名,可能的值为:

000 (the number 0, and denormalised numbers) 000(数字0和非规范化数字)
001 -2 001 -2
010 -1 010 -1
011 0 011 0
100 1 100 1
101 2 101 2
110 3 110 3
111 (infinities and NANs) 111(无穷大和南方)

PS NAN : not a number (error codes and stuff) PS NAN:不是数字(错误代码和东西)

0101 is your mantissa. 0101是你的尾数。

What this in fact means is that your number is 1.0101 (there's a hidden bit for extra precision since every number must start with "1." that "1" is not actually stored). 这实际上意味着你的数字是1.0101(因为每个数字必须以“1”开头,因为“1”实际上没有存储),所以有一个隐藏的位用于额外的精度。

This gives you (1+5/16)*2 = 2.625 这给你(1 + 5/16)* 2 = 2.625

The most significant '1' is not encoded. 最重要的'1'未编码。 So you need to add that on when converting. 所以你需要在转换时添加它。

In this case, the digits are '0101', so adding a '1' would give '1.0101'. 在这种情况下,数字为'0101',因此添加'1'将给出'1.0101'。 Then the exponent is 4, but needs to be offset by 3, so the actual multiplier is just 2^1. 那么指数是4,但需要偏移3,所以实际乘数只有2 ^ 1。

That gives a result of '10.101' which is indeed 2.625 这给出了'10 .101'的结果,这确实是2.625

[Expanding on JasonD's answer] [扩展JasonD的答案]

I think it's safe to assume that you understand, or rather take for granted, that a float can have a negative exponent. 我认为可以安全地假设您理解浮游可以具有负指数,或者更确切地说理所当然。

But if you come to think about it, how does that actually happen? 但如果你开始考虑它,那实际上是怎么发生的呢? That's where the 'bias' comes to play, and I think that's the missing link in your understanding. 这就是“偏见”发挥作用的地方,我认为这是你理解中缺失的环节。 In your link, they mentioned the following law for calculating the bias: 在您的链接中,他们提到了以下用于计算偏差的法律:

2^(k-1) - 1 2 ^(k-1) - 1

Where k is the number of bits in the exponent field. 其中k是指数字段中的位数。 In your example, k was 3 bits, so the bias is 3. This way you can encode any exponent in the range [-3,4] (inclusive). 在您的示例中,k为3位,因此偏差为3.这样,您可以编码[-3,4](包括)范围内的任何指数。

So now it's hopefully clear that when you're decoding the number, you have to 'unbias' the exponent first. 所以现在有希望清楚的是,当你解码数字时,你必须首先“取消”指数。 So your 2^4 is actually 2^1 as JasonD stated. 所以你的2 ^ 4实际上是J ^ D所说的2 ^ 1。

The Floating Point Number is 45 (Base 16) ie 01000101 (Base 2).. 浮点数为45(Base 16),即01000101(Base 2)..

0101 is the mantissa part and 100 is the exponent one... 0101是尾数部分,100是指数1 ...

On restoring the leading one the mantissa is renewed to 1.0101... 在恢复前导时,尾数更新为1.0101 ......

Subtracting the bias from the exponent, which is 3 in case of 8-bit representation, the exponent becomes 1... 从指数中减去偏差,在8位表示的情况下为3,指数变为1 ......

The Binary representation is 1.0101 * 2^1... 二进制表示是1.0101 * 2 ^ 1 ...

De-normalizing we get 10.101, which is the binary representation for 2.625... 去标准化我们得到10.101,这是2.625的二进制表示...

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

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