# 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. ## 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. Search the Web for "binary" and "floating point".

3. Originally Posted by Noose
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

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

4. 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

Popular pages Recent additions