How to convert decimal fractions to binary.


We all know how to convert decimal integral numbers to binary (don't we?) by the simple method of dividing succesively by 2 and using the remainders but what happens when we are trying to convert a decimal number which has a fractional part?

First we can see that it is obvious that the integral part of a decimal number will always be represented by an integral binary (with no fractional part) while the fractional part of a decimal number will always be represented by a fractional part of a binary number (with no integral part).  An integral number will be integral in any integral base and a fractional number will be fractional in any integral base.  This means we can split a decimal number into its integral and fractional parts, convert them separately to binary and add them up again in binary.  This means we can forget about the integral part and concentrate on converting the fractional part.  Some decimal fractions may convert into binary exactly (like .5625) but others may not (like .5624) and we may get an infinite number of binary digits in the binary fraction.

One way of converting a decimal fraction to binary fraction is to first multiply it by a power of 2 so that it becomes an integral number, then convert to binary and then divide by the same power of 2 (which now just means displacing the binary point to the left).

Examples:

- Convert .5625 to binary.

   - Multiply .5625 * 2^4 = 9
   - Convert 9 to binary = 1001
   - Divide 1001 / 2^4 = .1001

- Convert .5625 to binary
   - Multiply .5625 * 2^10 = 576
   - Convert 576 to binary = 1001000000
   - Divide 1001000000 / 2^10 = .1001000000
   (Same thing, more precision)

- Convert .5624 to binary
   - Multiply .5624 * 2^20 = 589719.1424
   - Convert 589719 to binary = 10001111111110010111
   - Divide 10001111111110010111 / 2^20 = .10001111111110010111
   (The more precision you want, the higher the power of 2 to use.
   Use the same power as the number of significant digits you want in the result.)

If we have a number which has integral and fractional parts we can convert them to binary separately and then add them together or we can just multiply the entire number by a power of two, convert to binary and then divide by the same power of two.

- Convert 165650.435 to binary
   - 165650 = 101000011100010010
   - Multiply .435 by 2^20 = 456130.56
   - Convert 456131 to binary: 1101111010111000010
   - Divide by 2^20: .01101111010111000010
   - We now add integral and fractional parts and the answer is:
   101000011100010010.01101111010111000010
   which we can round off to:
   101000011100010010.01101111010111
   Converting this back to decimal we get 165650.43499755859375

Or, done another way:

- Convert 165650.435 to binary
   - Multiply 165650.435 by 2^20 = 173697070531
   - Convert 173697070531 to binary: 10100001110001001001101111010111000011
   - Divide by 2^20: 101000011100010010.01101111010111000011
   which we can round off to:  101000011100010010.01101111010111
   Converting this back to decimal we get 165650.43499755859375

Roughly three decimal digits are equivalent in precision to 10 binary digits so, if we truncate to ten fractional digits we get.
101000011100010010.0110111101 = 165650.4345703125

The advantage of doing the integral and the fractional parts separately is that we do not need to handle so many digits at the same time.  If the total number of digits is low then we can do it all in one go but if the total number of digits is high then it may be more convenient to convert the integral and the fractional parts separately.

So it is a good rule of thumb to use 10 binary digits for every 3 decimal digits.