float to Binary (Ex. 15.375 = 1111.011b)

This is a discussion on float to Binary (Ex. 15.375 = 1111.011b) within the A Brief History of Cprogramming.com forums, part of the Community Boards category; I was wondering if someone could shed some light on this. Some calculators output their binary numbers this way, Ex. ...

  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
    21,594
    Search the Web for "binary" and "floating point".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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,799
    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,738
    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, 01: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, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21