Thread: float to Binary (Ex. 15.375 = 1111.011b)

  1. #1
    Registered User Noose's Avatar
    Join Date
    Jul 2003
    Posts
    11

    float to Binary (Ex. 15.375 = 1111.011b)

    I was wondering if someone could shed some light on this. Some calculators output their binary numbers this way, Ex. 15 = 1111.b, 15.375 = 1111.011b. How would you create a binary string "1111.011b" from a float. Not the actual writing of the string and not so much the integer part but the fraction part. Im looking for the process, not a function call.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Search the Web for "binary" and "floating point".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Noose View Post
    I was wondering if someone could shed some light on this. Some calculators output their binary numbers this way, Ex. 15 = 1111.b, 15.375 = 1111.011b. How would you create a binary string "1111.011b" from a float. Not the actual writing of the string and not so much the integer part but the fraction part. Im looking for the process, not a function call.
    Essentially, the opposite of the code posted - and no, don't expect me to write that one too!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. Start with a "number" set to .5
    2. If the fractional part of the value we are converting to binary is greater than (or equal to) this "number", output a 1 and subtract the "number" from the fraction. If not, just output a 0.
    3. Multiply the above "number" by half (0.5 becomes .25, .25 becomes .125)
    4. Go back to step 2 until there is no more fraction or you're at the desired accuracy
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Place Values in the Decimal System:

    100 10 1 [Decimal Point] 1/10 1/100 1/1000

    Place Values in the Binary System:

    4 2 1 [Decimal Point] 1/2 1/4 1/8
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-26-2007, 05:48 PM
  2. Class won't call
    By Aalmaron in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2006, 04:57 PM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM