Advertisement
Madotsuki

IEEE floating point conversion

Feb 12th, 2014
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.23 KB | None | 0 0
  1. Converting a number from base 10 to 32-bit floating point integer:
  2.  
  3. Let's use an example number.
  4. 1701.125
  5.  
  6. The structure of a floating point number is this:
  7.  
  8. 0 1 .... 8 9 ... 31
  9. x xxxxxxxx xxxxxxxx
  10. ^ ^ ^
  11. Sign Exponential Fractional
  12.  
  13. Sign: Either a 0 or 1. 0 = positive, 1 = negative
  14.  
  15. We'll cover over exponential and fractional when we get there.
  16.  
  17. ================================================================
  18. 1) Convert DEC to binary format.
  19. ================================================================
  20. 1001.125 is 11010100101.001 in binary format.
  21.  
  22. ================================================================
  23. 2) Move the decimal spot so that it's in scientific notation.
  24. ================================================================
  25. This process will help find the exponential bit, which is going to be
  26. an 8-bit binary number.
  27.  
  28. So in the case of...
  29. 11010100101.001
  30.  
  31. The decimal point will move 10 times, so that it'll be:
  32. 1.1010100101001 * 2^10
  33. You must remember how much you've moved it by, it plays a role next.
  34.  
  35. ================================================================
  36. 3) Add that number to 127, then take the binary form of it.
  37. ================================================================
  38. So 10 + 127 = 137.
  39.  
  40. 137 will now need to be converted to binary form, which is:
  41.  
  42. 10001001
  43.  
  44. You have now found the exponential bits. This goes inside slots 1-8
  45. in the floating point number.
  46.  
  47. ================================================================
  48. 4) All the numbers have after the decimal belong in the fractional.
  49. ================================================================
  50. Our binary value is...
  51. 1.1010100101001 * 2^10
  52.  
  53. So 1010100101001 goes inside the fractional significand, starting from
  54. bit 9.
  55.  
  56. After the number finishes, the rest of the bits will be 0 until you reach
  57. bit 32.
  58.  
  59. So in this case, the fractional bit will contain:
  60. 10101001010010000000000
  61.  
  62. ================================================================
  63. 5) Put them together...
  64. ================================================================
  65.  
  66. Sign: 0
  67. Exponential: 10001001
  68. Fractional: 10101001010010000000000
  69.  
  70. Final 32-bit floating point:
  71. 01000100110101001010010000000000
  72.  
  73. In Hex format:
  74. 44D4A400
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement