22/7 and the Approximation of Irrational Numbers

Irrational numbers are somewhat difficult to work with. Unfortunately, they’re also quite useful and crop up both in pure and applied mathematics, and tons of places you may not expect.

An irrational number is any real number that cannot be expressed exactly as a fraction. Think \(\pi , e, \) and \( \sqrt{2}\) .

When written in decimal form, they result in an infinite sequence of numbers with no apparent pattern. If we round or truncate this number, we lose accuracy and introduce some level of error into any calculation. This isn’t inherently a problem in most situations, so long as our approximation is sufficiently close to the actual value.

Let’s take \(\pi\) for example:

\[ \pi = 3.141592653589793238462643383279\dots\]

We have the common approximation of \( 3.14\), the slightly more accurate \( 3.1415\) — if you’re feeling fancy — or even \( 3.141592653589793\) , which is used by NASA.

But none of these are the true value of pi…

Sometimes you’ll see the fraction \(\frac{22}{7}\) used as an approximation of pi. This is surprisingly close to the actual value.

\[\frac{22}{7} = 3.14285\dots\]

\[\pi – \frac{22}{7} = -0.00126\dots\]

Better still, we have

\[\frac{355}{113} = 3.1415929\dots\]

so \(\pi – \frac{355}{113} = -0.0000002667 \) — pretty close!

This problem isn’t limited to irrational numbers either; if we have a long rational number that we wish to write in a more concise form without losing accuracy, we can approximate it using a fraction.

But how do we come up with these approximations? Can they be improved? The math behind it, while strange-looking, is simpler than you might think!

Some funny looking fractions

One of the first theorems you’ll encounter when studying university-level mathematics is commonly known as the Division Algorithm.

Theorem. The Division Algorithm.

Let \( a\) and \( b \) be integers with \( b>0\). Then there exist integers \( q\) and \( r\) such that \[ a = qb + r,\]

where \( 0\le r < b \).

You can find a proof of this theorem here.

It may look a little confusing, but what it really tells us is that we can rewrite the expression \( \frac{a}{b}\) as \(q + \frac{r}{b}\) — in other words — we can replace an improper fraction with a mixed number.

(Yes, this may seem like an obvious statement, but mathematicians like to be rigorous!)

Example 1.

Let’s start with an example using a rational number. Take the fraction \(\frac{13}{8}\).

The theorem above tells us that we can rewrite this as \(\frac{13}{8} = 1 + \frac{5}{8}\)

But we can also rewrite \(\frac{5}{8}\) as \(\frac{1}{\frac{8}{5}}\), giving us another improper fraction, \(\frac{8}{5}\), so we can apply the theorem again:

\[\frac{8}{5} = 1 + \frac{3}{5}\]

If we combine everything we’ve got so far, we get:

\[ \frac{13}{8} =1 + \frac{5}{8} \\ = 1 + \frac{1}{\frac{8}{5}} \\ = 1 + \frac{1}{ 1 + \frac{3}{5}} \]

It’s a strange way to write it, but the process is mathematically simple: flip the fraction to make it an improper one, then rewrite in a mixed number form.

We can continue this process, rewriting \(\frac{3}{5}\) as \(\frac{1}{\frac{5}{3}}\)

\[\vdots \\ =1 + \frac{1}{ 1 + \frac{1}{\frac{5}{3}}} \\ = 1 + \frac{1}{ 1 + \frac{1}{1 + \frac{2}{3}}} \\ \vdots \]

And then rewrite \(\frac{2}{3}\) as \(\frac{1}{\frac{3}{2}}\)

\[\vdots \\ = 1 + \frac{1}{ 1 + \frac{1}{1 + \frac{1}{\frac{3}{2}}}} \\ = 1 + \frac{1}{ 1 + \frac{1}{1 + \frac{1}{1 + \frac{1}{2}}}}\]

All of that is to say, we can rewrite \(\frac{13}{8}\) as \(1 + \frac{1}{ 1 + \frac{1}{1 + \frac{1}{1 + \frac{1}{2}}}}\). This is known as a continued fraction expansion.

We also have some specific notation to describe this form: \( [1; 1, 1, 1, 2] \). These values refer to the terms being added in each successive fraction, as shown below:

It’s an interesting and unusual form for sure, but in this case it doesn’t help us find a rational approximation of our number because we already have one: \( \frac{13}{8} \) ! Fortunately, we can apply this same procedure to any real number, rational or irrational. Let’s see another example with \(\sqrt{3} = 1.7320508075\dots\) and we’ll take a look at how we can use this continued fraction form to derive a rational approximation.

Example 2.

\[ \sqrt{3} = 1 + (\sqrt{3} – 1 ) \\ = 1 + \frac{1}{\frac{1}{\sqrt{3}-1}} \]

This time, we have the additional step of rationalising the denominator of \(\frac{1}{\sqrt{3}-1}\) , by multiplying it by \(\frac{\sqrt{3} + 1}{\sqrt{3} + 1}\):

\[ \frac{1}{\sqrt{3}-1}\ \times \frac{\sqrt{3} + 1}{\sqrt{3} + 1}\ = \frac{1 + \sqrt{3}}{2} \]

Therefore:

\[\vdots\\ = 1 + \frac{1}{\frac{1 + \sqrt{3}}{2}} \\ \vdots\]

Then we can use the same substitution as before: \(\sqrt{3} = 1 + (\sqrt{3} – 1 )\)

\[\vdots \\ = 1 + \frac{1}{\frac{1 + (1+(\sqrt{3}-1))}{2} } \\ = 1 + \frac{1}{ \frac{2+(\sqrt{3}-1)}{2}} \\ = 1 + \frac{1}{ 1 + \frac{\sqrt{3}-1}{2} } \\ = 1 + \frac{1}{1 + \frac{1}{\frac{2}{\sqrt{3}-1}} } \\ = 1 + \frac{1}{1 + \frac{1}{2\frac{1}{\sqrt{3}-1}} } \\ \vdots \]

Rationalising the denominator, the same as before, we get:

\[\vdots \\ = 1 + \frac{1}{1 + \frac{1}{2\frac{\sqrt{3}+1}{2}} } \\ = 1 + \frac{1}{1 + \frac{1}{\sqrt{3}+1} } \\ = 1 + \frac{1}{1 + \frac{1}{2 + (\sqrt{3}-1)} } \]

You may notice, that we’re now back where we started, with a \(\sqrt{3}-1\) term. This means that if we carry on this procedure, we’ll end up with a repeating pattern: \( 1, 2, 1, 2, 1, 2, \dots \) . We can describe this with the following notation:

\[ [1; 1, 2, 1, 2, 1, 2, \dots ] = [1; \overline{1, 2}] \]

Now let’s find an approximation of \(\sqrt{3}\) …

Continued Fraction \(\rightarrow\) Rational Approximation

To start finding rational approximations, we use the following recurrence relation:

Let \(\alpha = [a_0 ; a_1 , a_2 , a_3 , \dots ]\). Then the following recurrence relation generates the convergents of \(\alpha\) . These are rational approximations that get closer and closer to the original value.

\[ C_n = \frac{P_n}{Q_n} = \frac{a_n P_{n-1} + P_{n-2}}{a_n Q_{n-1} + Q_{n-2}} \]

Using the initial values \( C_0 = \frac{a_0}{1} , C_1 = \frac{a_0 a_1 + 1}{a_1} \)

We keep the terms of \(C_n\) as fractions, since the numerator gives us the \(P_n\) term and the denominator gives us the \(Q_n\) term. The sequence of rational numbers, \(C_n\) converges to our original irrational number (as described by the continued fraction form \(\alpha\) ).

This means, we have a sequence of approximations that get more accurate the further along the sequence you go!

Back to \(\sqrt{3}\)

As we saw, we have the continued fraction expansion of \(\sqrt{3}\) as described by \( [1; \overline{1, 2} ] \).

Following the formula above, we take \( C_0 = \frac{a_0}{1} = \frac{1}{1}\) and \( C_1 = \frac{a_0 a_1 + 1}{a_1} = \frac{1 \cdot 1 + 1}{1} = \frac{2}{1} \)

Then we repeat as follows:

\[ C_2 = \frac{a_2 P_1 + P_0}{a_2 Q_1 + Q_0} = \frac{2\cdot 2 + 1}{2\cdot 1 +1} = \frac{5}{3} =1.\overline{6}\]

\[ C_3 = \frac{a_3 P_2 + P_1}{a_3 Q_2 + Q_1} = \frac{1\cdot 5 + 2}{1\cdot 3 +1} = \frac{7}{4} = 1.75\]

\[ C_4 = \frac{ a_4 P_3 + P_2}{a_4 Q_3 + Q_2} = \frac{2\cdot 7 + 5}{2\cdot 4 +3} = \frac{19}{11} = 1.\overline{72}\]

\[ C_5 = \frac{ a_5 P_4 + P_3}{a_5 Q_4 + Q_3} = \frac{1\cdot 19 + 7}{1\cdot 11 +4} = \frac{26}{15} = 1.7\overline{3} \\ \\ \vdots \\ \\ \sqrt{3} =1.7320508075 \dots \]

As you can see, our approximations get closer and closer to the actual value.

Why do it by hand when a computer can do it for you?

Full code implementation and example usage…
from fractions import Fraction
from mpmath import mp


def to_continued_fraction(x, max_terms=20):
    """
    Convert a number to its continued fraction representation.
    Returns a list of integers [a0, a1, a2, ...] such that:
        x ≈ a0 + 1/(a1 + 1/(a2 + ...))
    """

    terms = []
    for _ in range(max_terms):
        integer_part = int(x)
        terms.append(integer_part)
        frac_part = x - integer_part
        if frac_part == 0:
            break
        x = 1 / frac_part
    return terms

def from_continued_fraction(terms):
    """
    Convert a list of continued fraction terms back to a Fraction.
    Computes the 'convergent' - the rational approximation.
    """
    result = Fraction(terms[-1])
    for term in reversed(terms[:-1]):
        result = term + 1 / result
    return result



###   Example usage: approximating 13/8   ###

x = Fraction(13, 8)
cf = to_continued_fraction(x)
print("Continued fraction terms:", cf)

approximation = from_continued_fraction(cf)
print("Reconstructed fraction:", approximation, "\n")



###   Example usage: approximating sqrt 3   ###

mp.dps = 50  # 50 decimal places

# Example: sqrt(3) to high precision
sqrt3 = mp.sqrt(3)

# Get continued fraction terms
cf = to_continued_fraction(sqrt3, max_terms=15)
print("Continued fraction for sqrt(3):", cf)

# Get the best approximation
approx = from_continued_fraction(cf)
print("Best rational approximation:", approx, " = ", float(approx))

print("Error: ", float(approx) - sqrt3)
>>> Continued fraction terms: [1, 1, 1, 1, 2]
>>> Reconstructed fraction: 13/8 

>>> Continued fraction for sqrt(3): [1, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]
>>> Best rational approximation: 13775/7953  =  1.73205079844084
>>> Error:  -0.0000000091280373153290773879067434947866620653338185218744

That’s enough fractions for one day

Believe it or not, this is one of those things in math that’s actually kind of useful. Continued fractions don’t just give us better approximations of irrational numbers — they also open some doors in Number Theory. Continued fraction expansions give us the tools we need to solve equations of the form \(x^2 − Dy^2 = 1\) (known as Pell’s Equation).

Outside of pure math, rational approximations pop up more than you’d think: data compression, digital signal processing, and anywhere we need to store or transmit numbers accurately, but with a restricted size.

So, while 22/7 is a fan favourite among mathematicians, it’s really just the tip of the iceberg. With a bit of creative thinking (and a few layers of fractions), we can get surprisingly close to perfect — and most of the time, that’s more than enough.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *