Sub topic 5
Converting Fractions Between Decimal and Binary
This chapter assumes you can already convert whole numbers between decimal and binary, and that you know powers of two, including negative exponents such as 2^-3 = 1/8. The goal is to extend place value to the right of the binary point. You will then convert in both directions, predict which decimals are exact in binary, and measure the error when they are not.
Place value after the binary point
In a positional system, each digit to the right of the point has a weight that is the base raised to a negative power. In binary the weights are halved at each step.
| Position after point | 1 | 2 | 3 | 4 | 5 | 6 |
|---|---|---|---|---|---|---|
| Weight | 2^-1 | 2^-2 | 2^-3 | 2^-4 | 2^-5 | 2^-6 |
| Decimal value | 0.5 | 0.25 | 0.125 | 0.0625 | 0.03125 | 0.015625 |
Binary fraction to decimal
Add the weights of the positions that hold a 1.
For 1010.011 in binary:
- The integer part 1010 is 8 + 2 = 10.
- The fraction part .011 is 0 + 1/4 + 1/8 = 0.375.
- The result is 10.375.
The same idea works in any base. In octal, 0.54 is 5/8 + 4/64 = 0.6875. In hexadecimal, 0.B is 11/16 = 0.6875.
Decimal fraction to binary
Convert the integer part and the fraction part separately.
- Convert the integer part by repeated division by 2, reading remainders from last to first.
- Convert the fraction part by repeated multiplication by 2. At each step, the integer digit (0 or 1) of the product is the next bit. Remove that digit and keep the new fraction.
- Stop when the fraction becomes 0, or when you have enough bits.
Worked example: 0.6875
| Step | Fraction | x 2 | Bit | New fraction |
|---|---|---|---|---|
| 1 | 0.6875 | 1.375 | 1 | 0.375 |
| 2 | 0.375 | 0.75 | 0 | 0.75 |
| 3 | 0.75 | 1.5 | 1 | 0.5 |
| 4 | 0.5 | 1.0 | 1 | 0 |
Reading the bits top to bottom gives 0.6875 = 0.1011 in binary. The process stopped because the fraction reached 0.
Mixed numbers
For 10.375, the integer part 10 is 1010. The fraction 0.375 gives 0.75 (bit 0), 1.5 (bit 1), 1.0 (bit 1), so it is 0.011. The answer is 1010.011, which matches the earlier conversion back.
Why the method works
A binary fraction is 0.b1 b2 b3... = b1/2 + b2/4 + b3/8 + .... Multiplying by 2 gives b1 + b2/2 + b3/4 + ..., so the integer part of the product is exactly b1. The leftover is 0.b2 b3..., and you repeat.
When the expansion does not terminate
Many short decimals have no finite binary form. Take 0.1:
- 0.1 x 2 = 0.2, bit 0
- 0.2 x 2 = 0.4, bit 0
- 0.4 x 2 = 0.8, bit 0
- 0.8 x 2 = 1.6, bit 1, keep 0.6
- 0.6 x 2 = 1.2, bit 1, keep 0.2
The fraction 0.2 appeared before, so the bits from that point repeat forever. The result is 0.0 0011 0011 0011..., written 0.0(0011) with the repeating block in parentheses.
The rule is this. Write the fraction as p/q in lowest terms. It terminates in binary if and only if q is a power of 2. If q has any other prime factor, the expansion repeats.
- 0.375 = 3/8 terminates, with 3 bits.
- 0.1 = 1/10 = 1/(2 x 5) repeats, because of the factor 5.
- 1/3 = 0.(01) repeats with period 2.
- 1/7 = 0.(001) repeats with period 3.
A useful bound for decimals: a terminating decimal with d places is N/10^d. If it terminates in binary, it needs at most d fractional bits. For example, 0.125 has 3 decimal places and needs exactly 3 bits (0.001). The decimal 0.1 has 1 place but never terminates, because 5 does not divide 1.
A converter you can run
The program below uses Python's exact Fraction type so that no float error enters the conversion. It detects repeats by remembering each fraction it has seen.
from fractions import Fraction
def frac_to_binary(x, max_bits=20):
x = Fraction(x)
if not 0 <= x < 1:
raise ValueError("x must satisfy 0 <= x < 1")
seen = {}
bits = []
while x != 0 and len(bits) < max_bits:
if x in seen:
start = seen[x]
return "0." + "".join(bits[:start]) + "(" + "".join(bits[start:]) + ")"
seen[x] = len(bits)
x *= 2
if x >= 1:
bits.append("1")
x -= 1
else:
bits.append("0")
return "0." + "".join(bits) + ("" if x == 0 else "...")
def binary_to_fraction(s):
whole, _, frac = s.partition(".")
value = Fraction(int(whole or "0", 2))
for i, b in enumerate(frac, start=1):
value += Fraction(int(b), 2 ** i)
return value
print(frac_to_binary("0.6875")) # 0.1011
print(frac_to_binary("0.1")) # 0.0(0011)
print(frac_to_binary("1/3")) # 0.(01)
v = binary_to_fraction("1010.011")
print(v, float(v)) # 83/8 10.375Pass strings such as "0.1" to Fraction. Passing the float 0.1 would give the exact value of the nearest double, which is not 1/10.
Octal and hexadecimal fractions
Because 8 = 2^3 and 16 = 2^4, you convert between these bases and binary by grouping bits. Start at the point and group outward, padding with zeros on the right of the fraction.
- 0.1011 in binary, padded to 0.101 100, is 0.54 in octal.
- 0.1011 in binary is 0.B in hexadecimal.
- 0.1 in decimal is 0.00011001100110011... in binary. Grouped by 4 bits as 0001 1001 1001 1001, it is 0.1999... in hexadecimal.
Padding must go on the right of the fraction part. Padding on the left would change the value.
Truncation and rounding error
When a fraction does not terminate, or you only have k fractional bits, you must cut it off.
- Truncating after k bits gives an error of less than 2^-k, and the stored value is always at or below the true value.
- Rounding to the nearest k-bit value gives an error of at most 2^-(k+1).
For 0.1 with 4 fractional bits, truncation gives 0.0001 = 0.0625 (error 0.0375). Rounding gives 0.0010 = 0.125 (error 0.025), since 0.1 x 16 = 1.6 rounds to 2. The fixed-point format in the next chapters stores exactly this kind of rounded integer.
Common errors
- Multiplying the whole number, including the integer part, by 2 at each step. Only the fraction is carried forward.
- Reading the bits in reverse. For fractions the first bit generated is the most significant, the opposite of the division method for integers.
- Forgetting to stop at a repeated fraction and writing a long string of bits as if it were exact.
- Treating 0.1 + 0.2 and 0.3 as equal bit patterns. Their binary expansions are all cut off, so they differ.
Exercise
Convert 6.4 to binary. Give the exact repeating form, then truncate to 4 fractional bits and compute the decimal value you get and the absolute error.
Show answer
The integer part 6 is 110. For 0.4: 0.4 x 2 = 0.8 (bit 0); 0.8 x 2 = 1.6 (bit 1, keep 0.6); 0.6 x 2 = 1.2 (bit 1, keep 0.2); 0.2 x 2 = 0.4 (bit 0, back to 0.4). So 0.4 = 0.(0110) and 6.4 = 110.(0110). Truncated to 4 fractional bits it is 110.0110 = 6 + 1/4 + 1/8 = 6.375. The error is 6.4 - 6.375 = 0.025, which is below 2^-4 = 0.0625.
Exercise
Without converting, decide which of these decimals terminate in binary and give the number of fractional bits for those that do: 0.3125, 0.2, 0.0625, 3/12.
Show answer
0.3125 = 5/16 terminates and needs 4 bits (0.0101). 0.2 = 1/5 does not terminate, because of the factor 5. 0.0625 = 1/16 terminates and needs 4 bits (0.0001). 3/12 = 1/4 terminates and needs 2 bits (0.01).