简体   繁体   English

使用 IEEE 754 浮点到二进制

[英]Floating point to binary using IEEE 754

Hello there I need to store 0.2730 using IEEE format.您好,我需要使用 IEEE 格式存储 0.2730。 Now what I did was to set the sing at 0 because the number is positive.现在我所做的是将 sing 设置为 0,因为这个数字是正数。 Now I thought that that since what's before the point is 0 then I don't have to do any conversion to it because 0 in binary it's just 0 so the exponent would end up being 0 and I just needed to convert 127 to binary.现在我想,既然点之前是 0,那么我不必对它进行任何转换,因为二进制中的 0 它只是 0,所以指数最终会是 0,我只需要将 127 转换为二进制。 When I looked for the Mantissa I tried to do the pattern when I multiply the decimal part by 2 and take out the number before the I can't find a pattern nor it reaches 0 so what I did was just multiply until I found 23 bits.当我寻找尾数时,我试图做模式,当我将小数部分乘以 2 并在我找不到模式也没有达到 0 之前取出数字所以我所做的只是乘以直到找到 23 位. Now when I looked up the real value it says that the exponent should be 125 and that the number is actually 0 0111 0001 0111 1000 1101 0101 000. Now I don't know why I have to subtract 2 from the exponent since I think I don't have to move the point nor when to stop when doing the multiplication by 2 pattern.现在,当我查看实际值时,它说指数应该是 125,而这个数字实际上是 0 0111 0001 0111 1000 1101 0101 000。现在我不知道为什么我必须从指数中减去 2,因为我认为我在进行乘以 2 模式时,不必移动点或何时停止。 I left a picture of my work in case it helps.我留下了我工作的照片以防万一。 Thanks in advance and hope you are doing well在此先感谢您,希望您一切顺利

在此处输入图像描述

IEEE-754 binary32 encoding IEEE-754 binary32 编码

The sign of 0.2730 is positive, so the sign bit in IEEE-754 binary32 format will be zero. 0.2730 的符号为正,因此 IEEE-754 binary32 格式的符号位将为零。

Next, represent the number with an explicit scaling by a power of 2: 0.2730 = 0.2730 • 2 0 .接下来,用 2 的幂表示具有显式缩放的数字: 0.2730 = 0.2730 • 2 0 In this, we call the part before the “•” (0.2730) the significand , and the power that 2 is raised to is the exponent .在此,我们将“•” (0.2730) 之前的部分称为有效数字 ,而 2 的幂是指数

Then, adjust this representation to make the significand be at least 1 and less than 2. The allowed adjustments are to adjust the exponent by increments of one and multiply or divide the significand by 2 accordingly: 0.2730 • 2 0 = 0.5460 • 2 −1 = = 1.0920 • 2 −2 .然后,调整此表示以使有效数字至少为 1 且小于 2。允许的调整是将指数调整为增量 1,并相应地将有效数字乘以或除以 2:0.2730 • 2 0 = 0.5460 • 2 -1 = = 1.0920 • 2 -2

This is called the normalized form.这称为规范化形式。 The exponent in this form, −2 is used for the encoding.这种形式的指数 -2 用于编码。 To encode it in the exponent bits, we add the fixed bias of the format, 127, and write the result in 8-bit binary.为了在指数位中对其进行编码,我们添加了格式的固定偏差 127,并将结果写入 8 位二进制。 −2 + 127 = 125, and 125 in binary is 01111101. −2 + 127 = 125,二进制的 125 是 01111101。

Next, write the significand in binary to at least 24 digits: 1.0920 = 1.00010111100011010100111 11101111100111011… 2 .接下来,将二进制的有效数字写入至少 24 位:1.0920 = 1.00010111100011010100111 11101111100111011… 2 The bold shows the first 24 digits.粗体显示前 24 位数字。 We can only use 24 digits in the IEEE-754 binary32 format, and we see the remaining part is above half of the 24 th digit, so we will round up, producing 1.00010111100011010101000 2 .我们只能使用 IEEE-754 binary32 格式的 24 位数字,并且我们看到剩余部分在第 24数字的一半以上,因此我们将四舍五入,产生 1.00010111100011010101000 2 That is our significand in binary.那是我们二进制的有效数字。 (For information on doing this, see “Decimal to binary” below.) (有关执行此操作的信息,请参阅下面的“十进制到二进制”。)

To encode the significand, we use the 23 digits after the “.”为了对有效数字进行编码,我们使用“.”后面的 23 位数字。 (The leading digit is known to be 1, because we normalized the number to make it so, so it does not need to be included in the bits that primarily encode the significand.) Those bits are 00010111100011010101000. (已知前导数字是 1,因为我们对数字进行了规范化以使其如此,所以它不需要包含在主要对有效数字进行编码的位中。)这些位是 00010111100011010101000。

Then we put the sign, exponent, and significand bits together: 0 01111101 00010111100011010101000.然后我们将符号、指数和有效位放在一起:0 01111101 00010111100011010101000。

Decimal to binary十进制转二进制

To convert the decimal numeral 1.0920 to binary:要将十进制数字 1.0920 转换为二进制:

  • Write the leading “1” and remove it, giving.0920.写下开头的“1”并将其删除,给出.0920。 Multiply by two, giving 0.1840.乘以 2,得到 0.1840。 Also write “.”还要写“。” since we start at this point.因为我们从这一点开始。
  • Write the leading “0” and remove it, giving.1840.写入前导“0”并将其删除,给出.1840。 Multiply by two, giving 0.3680.乘以 2,得到 0.3680。
  • Write the leading “0” and remove it, giving.3680.写入前导“0”并将其删除,给出.3680。 Multiply by two, giving 0.7360.乘以 2,得到 0.7360。
  • Write the leading “0” and remove it, giving.7360.写入前导“0”并将其删除,给出.7360。 Multiply by two, giving 1.4720.乘以 2,得到 1.4720。
  • Write the leading “1” and remove it, giving.4720.写下开头的“1”并将其删除,给出.4720。 Multiply by two, giving 0.9440.乘以 2,得到 0.9440。

Continue as long as desired.根据需要继续。

It's hard to see what's in your picture, but here's the binary lay-out for your number 0.2730 both as a single-precision and double-precision IEEE-754 representation.很难看清图片中的内容,但这里是数字0.2730的二进制布局,作为单精度和双精度 IEEE-754 表示。 Hopefully you can use it to double-check your answer.希望您可以使用它来仔细检查您的答案。 If you've a mismatch, feel free to ask a more specific question.如果您不匹配,请随时提出更具体的问题。

Here's 0.2730 in single-precision (32 bit) IEEE-754 representation:这是0.2730的单精度(32 位)IEEE-754 表示:

                  3  2          1         0
                  1 09876543 21098765432109876543210
                  S ---E8--- ----------F23----------
          Binary: 0 01111101 00010111100011010101000
             Hex: 3E8B C6A8
       Precision: SP
            Sign: Positive
        Exponent: -2 (Stored: 125, Bias: 127)
       Hex-float: +0x1.178d5p-2
           Value: +0.273 (NORMAL)

And here's the same, this time in double-precision (64 bit) IEEE754 representation:这也是一样的,这次是双精度(64 位)IEEE754 表示:

                  6    5          4         3         2         1         0
                  3 21098765432 1098765432109876543210987654321098765432109876543210
                  S ----E11---- ------------------------F52-------------------------
          Binary: 0 01111111101 0001011110001101010011111101111100111011011001000110
             Hex: 3FD1 78D4 FDF3 B646
       Precision: DP
            Sign: Positive
        Exponent: -2 (Stored: 1021, Bias: 1023)
       Hex-float: +0x1.178d4fdf3b646p-2
           Value: +0.273 (NORMAL)

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

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