简体   繁体   中英

Floating point to binary using IEEE 754

Hello there I need to store 0.2730 using IEEE format. Now what I did was to set the sing at 0 because the number is positive. 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. 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. 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. I left a picture of my work in case it helps. Thanks in advance and hope you are doing well

在此处输入图像描述

IEEE-754 binary32 encoding

The sign of 0.2730 is positive, so the sign bit in IEEE-754 binary32 format will be zero.

Next, represent the number with an explicit scaling by a power of 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 .

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 .

This is called the normalized form. The exponent in this form, −2 is used for the encoding. To encode it in the exponent bits, we add the fixed bias of the format, 127, and write the result in 8-bit binary. −2 + 127 = 125, and 125 in binary is 01111101.

Next, write the significand in binary to at least 24 digits: 1.0920 = 11101111100111011… 2 . 11101111100111011… 2The bold shows the first 24 digits. 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 . 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 “.” (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.

Then we put the sign, exponent, and significand bits together: 0 01111101 00010111100011010101000.

Decimal to binary

To convert the decimal numeral 1.0920 to binary:

  • Write the leading “1” and remove it, giving.0920. Multiply by two, giving 0.1840. Also write “.” since we start at this point.
  • Write the leading “0” and remove it, giving.1840. Multiply by two, giving 0.3680.
  • Write the leading “0” and remove it, giving.3680. Multiply by two, giving 0.7360.
  • Write the leading “0” and remove it, giving.7360. Multiply by two, giving 1.4720.
  • Write the leading “1” and remove it, giving.4720. Multiply by two, giving 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. 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:

                  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:

                  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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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