Numerical AnalysisNumbers on a Computer

Binary Fractions

Convert a decimal fraction to base 2 by doubling and keeping the whole-number parts: 0.1 becomes 0.000110011… and never ends.

Not started Jump to practice

Computers store numbers in base 2. Whole numbers convert easily, but fractions hold a surprise that explains why a computer cannot store $0.1$ exactly. The story is the same one you met with Repeating Decimals as Fractions, told in base 2, and it is the first step toward measuring the errors a computer makes.

After the binary point, the places are worth $\frac{1}{2}, \frac{1}{4}, \frac{1}{8}, \frac{1}{16}, \ldots$ So $0.101_2 = \frac{1}{2} + \frac{1}{8} = 0.625$.

Key idea

To write a fraction $0 \le x < 1$ in base 2, double it: the whole-number part (0 or 1) is the next binary digit; keep the fraction part and repeat. A fraction in lowest terms ends in base 2 exactly when its denominator is a power of 2; otherwise its digits repeat forever.

Why it works

If $x = 0.b_1b_2b_3\ldots_2$, then $2x = b_1.b_2b_3\ldots_2$: doubling moves the binary point one place right, so the whole part of $2x$ is $b_1$ and the fraction part holds the remaining digits. For $x = \frac{a}{n}$, every fraction part that appears has the form $\frac{r}{n}$ with $0 \le r < n$. There are only $n$ choices, so either some step gives $0$ (the expansion ends) or a fraction part comes back and the digits repeat from there. Ending means $x = \frac{k}{2^{j}}$, so in lowest terms the denominator must divide a power of 2.

Worked example: A fraction that ends

Write $0.625$ in base 2.
  1. $2 \times 0.625 = 1.25$: digit $1$, keep $0.25$.
  2. $2 \times 0.25 = 0.5$: digit $0$, keep $0.5$.
  3. $2 \times 0.5 = 1$: digit $1$, nothing left; it ends.
  4. Check: $0.101_2 = \frac{1}{2} + \frac{1}{8} = \frac{5}{8} = 0.625$. The denominator $8 = 2^{3}$, so three digits are enough.

Answer: $0.101_2$

Worked example: A fraction that repeats

Write $0.1$ in base 2.
  1. $0.1 \to 0.2$ (digit $0$), $0.2 \to 0.4$ (digit $0$), $0.4 \to 0.8$ (digit $0$).
  2. $0.8 \to 1.6$ (digit $1$, keep $0.6$), $0.6 \to 1.2$ (digit $1$, keep $0.2$).
  3. We are back at $0.2$, so the digits $0011$ repeat forever: $$0.1 = 0.0\overline{0011}_2 = 0.000110011001\ldots_2$$
  4. Why: $0.1 = \frac{1}{10}$, and $10 = 2 \times 5$ has the factor $5$.

Answer: $0.0\overline{0011}_2$

Repeated doubling of 0.1. The highlighted rows hold the same fraction, so the digits between them repeat forever.

Worked example: From repeating binary back to a fraction

Write $y = 0.\overline{01}_2 = 0.010101\ldots_2$ as a fraction.
  1. Multiplying by $2^{2} = 4$ shifts the point two places: $4y = 1.\overline{01}_2$.
  2. Subtract: $4y - y = 1$, so $3y = 1$ and $y = \frac{1}{3}$.

Answer: $\frac{1}{3}$

Common mistake

Expecting every short decimal to have a short binary form. $0.5$, $0.25$ and $0.375$ end in binary, but $0.1$, $0.2$ and $0.3$ never do, so a computer must cut them off and round. Quick check: write the decimal as a fraction in lowest terms and look at the denominator. Any factor other than 2 (such as the 5 in 10) means the binary digits go on forever.
This is why $0.1 + 0.2$ is not exactly $0.3$ on a computer: none of the three is stored exactly. The next topic, Floating-Point Numbers, measures how far off they are.

Practice

Pass with 3 right answers, the last 2 in a row. Each right answer earns 2 XP.

Loading practice…

Builds on

Leads to

Sources and further reading

  • Tobin A. Driscoll and Richard J. Braun, Fundamentals of Numerical Computation (University of Delaware, free online edition): chapter "Introduction", section "Floating-point numbers"
  • Autar Kaw, Holistic Numerical Methods (University of South Florida): Introduction to Numerical Methods, "Binary Representation of Numbers"
@teddyrookbook GitHub