1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Decimal to floating point

  1. Feb 1, 2010 #1
    1. Convert -45 and 3.54625 32-bit ( single precision) to floating point representation.



    I know how to convert positive and even decimal numbers, but I don't know how to do these. Is there somebody who knows? Help would be appreciated.
     
  2. jcsd
  3. Feb 1, 2010 #2

    berkeman

    User Avatar

    Staff: Mentor

    What reference are you using for floating point representation?
     
  4. Feb 1, 2010 #3
    What do you mean?
     
  5. Feb 1, 2010 #4

    berkeman

    User Avatar

    Staff: Mentor

    I mean what spec, or what rules? I can go to wikipedia to look it up, but I was hoping you could point me to what you are using. What textbook, or what other learning materials? How are you supposed to know how to do any conversions involving floating point?

    I've done a few conversions involving floating point, and I have to look up the conversion rules each time I do.
     
  6. Feb 1, 2010 #5
    Dosen't matter how. I tried to to convert it to binary first and then go from there, using the formular I found, but that didn't work.
     
  7. Feb 1, 2010 #6

    berkeman

    User Avatar

    Staff: Mentor

    So which one of the IEEE 754 formats are you supposed to use?

    http://en.wikipedia.org/wiki/Floating_point

    Or is it when they say
    that it's confusing? I don't know myself what they mean by a decimal number represneted by 32-bit single precision...
     
  8. Feb 1, 2010 #7
    The floating point is gonna look like this: 0 1111 1111 0110 1010 0100 0000 0000 0000

    32 digits. The single precision is easy. I know how to do that once I know how to convert it. I saw that on wikipedia too. It's just not clear to me.
     
  9. Feb 1, 2010 #8

    vela

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper
    Education Advisor

    Negative numbers are easy if you know how to do positive numbers. You just flip the sign bit.

    Floating point representation is essentially just scientific notation using base 2. To convert to base 2, first convert the integer part. Then multiply the fraction by 2. The integer part of the result is the next bit. To get successive bits, repeat the process of discarding the integer part and multiplying the resulting fraction by 2.

    For example, the binary representation of 3.671875 starts with 11. After the binary point, the bits are

    2x0.671875 = 1.34375
    2x0.34375 = 0.6875
    2x0.6875 = 1.375
    2x0.375 = 0.75
    2x0.75 = 1.5
    2x0.5 = 1.0

    So 3.671875 in decimal has the binary representation 11.1010112. Now you want to move the binary point all the way to the left, yielding [itex]0.11101011_2\times2^2[/itex].

    In IEEE 754 single-precision format, you first have the sign bit, 0 for positive and 1 for negative. Add an offset of 127 to the exponent and store the result in the next 8 bits. You then drop the first bit of the mantissa, because you know it's always 1, and store the rest in the remaining 23 bits. For our example, you'd have

    0 10000001 11010110000000000000000

    The first bit is 0 indicating the number is positive. The 8-bit exponent field equals 129, which is 2+127. The remaining bits are from the binary representation except for the leading one after the binary point.
     
  10. Feb 2, 2010 #9
    Thank you.
    Just multiply it by 2? Why did you take the 0.75 from 1.375 and not 0.375?
     
  11. Feb 2, 2010 #10

    Mark44

    Staff: Mentor

    Take another look at what Vela wrote, starting with "Then multiply the fraction by 2. The integer part of the result is the next bit. To get successive bits, repeat the process of discarding the integer part and multiplying the resulting fraction by 2."
     
  12. Feb 2, 2010 #11

    vela

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper
    Education Advisor

    Multiplying by 2 just shifts the binary point over by one spot (just like multiplying by 10 shifts the decimal point over by one spot), so it causes the leading bit in the fraction to become the integer part.

    You could also multiply a fraction by a power of two to turn it into an integer and then find the binary representation of the integer. For example, 0.671875x26 = 43, which is represented by 1010112. So 0.671875 = 43x2-6 = 0.1010112. The main complication with this approach are that you can't turn most fractions neatly into an integer by multiplying by a power of two, so you need to choose the power of 2 to get the number of bits you want and toss the remaining part of the fraction, and then you still have to find the (possibly large) integer's binary representation.

    I think you missed the line between the 1.375 to the 0.75.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Decimal to floating point
  1. Floating point format (Replies: 1)

Loading...