PDA

View Full Version : float to Binary (Ex. 15.375 = 1111.011b)



Noose
11-21-2007, 02:30 AM
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.

laserlight
11-21-2007, 02:39 AM
Search the Web for "binary" and "floating point".

matsp
11-21-2007, 03:28 AM
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

hk_mp5kpdw
11-21-2007, 04:50 AM
Start with a "number" set to .5
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.
Multiply the above "number" by half (0.5 becomes .25, .25 becomes .125)
Go back to step 2 until there is no more fraction or you're at the desired accuracy

DavidP
11-21-2007, 11:06 AM
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