Shelves / Arithmetic and Numbers
AN·12
The Real Number Line
Where the book's numbers fit together: why the fractions, though dense, leave gaps; the proof that √2 is not a fraction, and which other numbers aren't; decimals that neither end nor repeat; the real numbers as the line with no gaps, pinned down by shrinking intervals; why there are more real numbers than fractions (Cantor's diagonal argument); and what a computer's floating-point numbers are in comparison: a finite, unevenly spaced sprinkle of fractions on the line.
Start here
What this chapter answers
Is every point on the number line a fraction? How can we be sure isn't one, when is so close? Which other numbers aren't fractions, and what do their decimals look like? If fractions are everywhere on the line (there's always one between any two), how can they leave gaps? What exactly is a real number? Are there "more" real numbers than fractions, even though both go on for ever? And what does a computer actually store when it stores ?
The number line in one picture
Draw a square with sides of length 1, and swing its diagonal down onto the number line with a compass, starting at 0. By Pythagoras's theorem the diagonal has length (its square is ), so the compass lands at a point a little past 1.41. Every fraction is a point on the line, and between any two of them are infinitely many more, so it seems the compass must land on one. This chapter proves that it doesn't: the line has a point there that no fraction names. The real numbers are what you get when every such point is given a name.
Numbers that aren't fractions
√2 is irrational
A number that can be written as a fraction of whole numbers is rational; one that can't is irrational.
The argument doesn't depend on how hard anyone has searched: it shows that no fraction, of any size, squares to 2. That's why the excellent approximations of chapter 10 (, whose square is ) can never become exact. The lab measures how close fractions get.
Which other roots are irrational
Unique factorisation (chapter 5) gives a general test. In a perfect square every prime appears an even number of times (). If , then ; the left side has every prime an even number of times, so the right side must too, which forces every prime in to appear an even number of times: is a perfect square. So is either a whole number or irrational: , but , , and are irrational. The same works for cube roots and higher.
Sums and products of irrationals can go either way: is rational, is rational, but is irrational, and a rational number (other than 0) times an irrational one is always irrational (if with rational, then would be rational).
π, e and the rest
The most famous irrational numbers aren't roots at all. , the ratio of a circle's circumference to its diameter, was proved irrational by Johann Lambert in 1761; , the base of natural logarithms (chapter 10), by Euler in 1737. Both are more than irrational: they're transcendental, meaning no equation with whole-number coefficients, , has them as a solution (Hermite proved it for in 1873, Lindemann for in 1882), while solves . Lindemann's result settled a problem from ancient Greece: it's impossible to construct, with compass and straightedge, a square with the same area as a given circle.
Decimals that neither end nor repeat
Chapter 7 showed that every fraction's decimal ends or repeats, and every ending or repeating decimal is a fraction. So an irrational number's decimal must go on for ever without repeating: and never settle into a cycle. It's easy to write such decimals deliberately: , with one more 0 each time, never repeats, so it's irrational. Seeing the first million digits of a decimal can't tell you whether it's rational, though; only a proof can.
The real numbers
The line with no gaps
Fractions are dense: between any two there's another (chapter 6). Yet they have gaps: is a point of the line that no fraction reaches, and so are , and infinitely many others. The real numbers are all the points of the line, rational and irrational together, and the defining property they have and the fractions lack is completeness: no gaps.
One way to say it precisely uses shrinking intervals. Chapter 10's search for can be done by halving: (since ); , so ; , so ; and so on. Each interval lies inside the previous one, and their widths halve towards zero. In the fractions, these intervals close in on nothing: no fraction lies in all of them. Completeness is the statement that in the real numbers, every such nest of shrinking intervals contains exactly one number. That number is .
Every real number is the limit of fractions (its decimal cut off after 1, 2, 3, … places), so fractions can approximate any real number as closely as you like; they just can't reach the irrational ones. That's what every calculation with or actually does.
Both kinds are everywhere
Between any two different real numbers there's a rational one and an irrational one. For rationals: if , choose a power of ten with ; then some decimal with places lies strictly between them. For irrationals: take a rational between them and add a tiny irrational amount, , small enough to stay below . So the two kinds are thoroughly interleaved: no interval, however short, is all one or the other.
The number systems, together
| Aspect | Whole numbers | Integers | Rationals | Reals |
|---|---|---|---|---|
| Examples | 0, 7, 1729 | , 0, 12 | , , | , , , |
| Always possible | , | , , | , , , (not by 0) | All of those, and limits |
| What they added | Counting (ch01–05) | Opposites (ch09) | Reciprocals (ch06) | Completeness: no gaps (ch12) |
| Decimals | End | End | End or repeat | Any infinite decimal |
| How many | Countably many | Countably many | Countably many | Uncountably many |
Each system contains the one before and fills a gap in it: subtraction needed negatives, division needed fractions, and lengths like and limits needed the reals. One more gap remains ( has no real solution), which the complex numbers fill, beyond this book. The last row is the strangest, and it's the subject of the next section.
How many?
Fractions can be listed
Two infinite collections have the same size if their members can be paired off one to one with none left over, the same idea as chapter 1's counting by matching. The whole numbers and the even numbers have the same size in this sense (pair with ), even though one seems to contain the other: that's a strange but harmless feature of infinite collections. A collection that can be paired with the whole numbers, that is, written as a list with a first, second, third member and so on, is countable.
Surprisingly, the positive fractions are countable. Arrange them in a grid (numerators across, denominators down) and walk the diagonals, skipping fractions not in lowest terms; or use the elegant Calkin–Wilf order, which the lab prints: , every positive fraction exactly once. Dense as they are, the fractions can be counted.
The reals can't be listed
So there are strictly more real numbers than fractions, and since the fractions can be listed, the irrational numbers can't: in a precise sense, almost every real number is irrational. Most of them can't even be described: there are only countably many finite descriptions (strings of symbols), but uncountably many reals. Every number you will ever compute with is one of the rare describable ones.
Real numbers in a computer
A computer can't store an infinite decimal. A double-precision float (chapter 7) is a fraction with a 53-bit numerator: a finite set of about rational numbers, sprinkled along the line, dense near 0 and sparse far from it. Between 1 and 2 there are exactly of them, about 4.5 million billion, evenly spaced apart; near the spacing is 2, and near it's about . math.sqrt(2) returns the float nearest to , the fraction , whose square is 2.0000000000000004 when computed in floats. No float is , and none can be.
So floating-point arithmetic is real arithmetic approximated by rounding to the nearest member of a finite set, which is why its laws hold only approximately (chapter 10's companion). Other approaches exist: decimal and fraction types (exact for rationals, chapters 6 and 7), arbitrary-precision libraries that carry as many digits as asked, symbolic systems that keep as a symbol, and "exact real arithmetic" that computes digits on demand. One limit applies to all of them: there's no general method to decide whether two computed real numbers are equal, since that might require checking infinitely many digits. That's the deep reason every numerical comparison in code needs a tolerance.
Lab: the real line in code
Experiment 1: trapping √2 in shrinking intervals
from fractions import Fraction as F
lo, hi = F(1), F(2) # 1² < 2 < 2²
for step in range(1, 31):
mid = (lo + hi) / 2
if mid * mid < 2:
lo = mid
else:
hi = mid
if step in (1, 2, 3, 10, 20, 30):
print(f"step {step:>2}: {float(lo):.10f} < √2 < {float(hi):.10f} width 1/{(1 / (hi - lo)):.0f}")
print("is either end ever exactly √2? lo² == 2:", lo * lo == 2, "| hi² == 2:", hi * hi == 2)
step 1: 1.0000000000 < √2 < 1.5000000000 width 1/2
step 2: 1.2500000000 < √2 < 1.5000000000 width 1/4
step 3: 1.3750000000 < √2 < 1.5000000000 width 1/8
step 10: 1.4140625000 < √2 < 1.4150390625 width 1/1024
step 20: 1.4142131805 < √2 < 1.4142141342 width 1/1048576
step 30: 1.4142135615 < √2 < 1.4142135624 width 1/1073741824
is either end ever exactly √2? lo² == 2: False | hi² == 2: False
Each step halves the interval known to contain : after 30 steps it's narrower than a billionth, and is pinned to 9 decimal places. Every endpoint is an exact fraction, and none of them squares to exactly 2, as the proof guarantees; the intervals close in on a point that only the real numbers contain. Bisection gains one binary digit per step, much slower than Heron's averaging (chapter 10), but it works for any continuous equation and never fails to make progress, which is why it's a standard method.
Experiment 2: the best fractions for √2
from fractions import Fraction as F
from decimal import Decimal, getcontext
getcontext().prec = 40
r2 = Decimal(2).sqrt()
# Fractions p/q from p² - 2q² = ±1 (Pell's equation): each is the best approximation for its size
p, q = 1, 1
for _ in range(8):
p, q = p + 2 * q, p + q
err = abs(Decimal(p) / Decimal(q) - r2)
print(f"{p:>6}/{q:<6} p² - 2q² = {p * p - 2 * q * q:+d} error {err:.2e} error × q² = {float(err * q * q):.4f}")
3/2 p² - 2q² = +1 error 8.58e-2 error × q² = 0.3431
7/5 p² - 2q² = -1 error 1.42e-2 error × q² = 0.3553
17/12 p² - 2q² = +1 error 2.45e-3 error × q² = 0.3532
41/29 p² - 2q² = -1 error 4.20e-4 error × q² = 0.3536
99/70 p² - 2q² = +1 error 7.22e-5 error × q² = 0.3535
239/169 p² - 2q² = -1 error 1.24e-5 error × q² = 0.3536
577/408 p² - 2q² = +1 error 2.12e-6 error × q² = 0.3536
1393/985 p² - 2q² = -1 error 3.64e-7 error × q² = 0.3536
The fractions satisfy (Pell's equation): their squares miss 2 by exactly , the closest a fraction with denominator can get, and never by 0. Their errors shrink like : the last column settles at 0.3536, which is . These are the convergents of 's continued fraction , the same machinery as chapter 5's companion used for . Indian mathematicians knew : the Śulba Sūtras (first millennium BCE) give the diagonal of a square as times the side, which is exactly .
Experiment 3: what a computer's "real numbers" are
import math
from fractions import Fraction as F
for x in (1.0, 1000.0, 1e6, 1e16, 1e300):
print(f"next float after {x:<7g} is {math.ulp(x):.3g} away")
print("floats from 1 to 2:", 2 ** 52, "| sqrt(2) as a float is exactly", F(math.sqrt(2)))
print("its square:", math.sqrt(2) ** 2, "| exact square of that fraction minus 2:", float(F(math.sqrt(2)) ** 2 - 2))
next float after 1 is 2.22e-16 away
next float after 1000 is 1.14e-13 away
next float after 1e+06 is 1.16e-10 away
next float after 1e+16 is 2 away
next float after 1e+300 is 1.49e+284 away
floats from 1 to 2: 4503599627370496 | sqrt(2) as a float is exactly 6369051672525773/4503599627370496
its square: 2.0000000000000004 | exact square of that fraction minus 2: 2.7343234630647693e-16
The gap to the next float grows with the number's size: at 1, 2 at (chapter 10's companion), and at . There are exactly floats from 1 up to 2. math.sqrt(2) is the fraction (the denominator is ); its exact square exceeds 2 by about , and squaring it in floats gives 2.0000000000000004.
Experiment 4: listing the fractions, and escaping any list
from fractions import Fraction as F
from itertools import islice
def rationals():
"""Every positive fraction exactly once, in lowest terms (the Calkin-Wilf order)."""
q = F(1)
while True:
yield q
q = 1 / (2 * (q.numerator // q.denominator) - q + 1)
print("first 15:", " ".join(str(q) for q in islice(rationals(), 15)))
print("position of 22/7 in the list:", next(i for i, q in enumerate(rationals(), 1) if q == F(22, 7)))
# Cantor's diagonal: from any list of decimals, build one that isn't in it.
listed = ["0.1415926535", "0.7182818284", "0.4152135623", "0.5000000000", "0.3333333333",
"0.1234567890", "0.9999999999", "0.0000000001", "0.5772156649", "0.6180339887"]
new = "0." + "".join("5" if s[2 + i] != "5" else "4" for i, s in enumerate(listed))
print("diagonal digits:", "".join(s[2 + i] for i, s in enumerate(listed)), "-> new number", new)
print("differs from row k in digit k:", all(new[2 + i] != s[2 + i] for i, s in enumerate(listed)))
first 15: 1 1/2 2 1/3 3/2 2/3 3 1/4 4/3 3/5 5/2 2/5 5/3 3/4 4
position of 22/7 in the list: 519
diagonal digits: 1150369047 -> new number 0.5545555555
differs from row k in digit k: True
The Calkin–Wilf sequence lists every positive fraction exactly once, in lowest terms, using one line of arithmetic per step: is the 519th. That's what "countable" means. The second part runs Cantor's construction on a list of ten decimals (the beginnings of , and others): reading the diagonal and changing each digit gives a number that differs from every row somewhere. For a finite list that's unsurprising; the proof's point is that the same construction defeats every infinite list too.
Worked examples
Rational or irrational?
Pinning a root
Between two numbers
Exercises
Practice
- Which are rational? , , , , , , , .
- Write , and as a whole number times the square root of a number with no square factor.
- Between which two consecutive whole numbers does each lie? , , .
- Find to 2 decimal places by halving or by trying decimals.
- Is the product of two irrational numbers always irrational? Give examples.
- Write a decimal that neither ends nor repeats, different from those in the chapter.
- A float has 52 bits after its leading 1. How far apart are consecutive floats between 2 and 4? Between 1024 and 2048?
Answers
- Rational: , 0.25, , , . Irrational: , , .
- , , .
- 5 and 6; 14 and 15; 31 and 32.
- , , and , so , which is 2.65 to 2 places.
- No: , . Sometimes yes: is irrational.
- For example (one more 1 each time) or (the primes in order).
- Between 2 and 4 the spacing is ; between 1024 and 2048 it's .
Harder
- Prove that is irrational by the parity-style argument, using divisibility by 3 instead of by 2.
- Prove that is irrational. (If , what is ?)
- Show that between any two different rational numbers there is an irrational number.
- Explain why the Calkin–Wilf list contains no fraction twice, and why it still "misses" though it contains fractions as close to as you like.
- A program checks whether a computed length equals with
x == math.sqrt(2). Explain why this is unreliable, and what to do instead.
Answers
- Suppose in lowest terms: . If 3 didn't divide , then would leave remainder 1 or 2 on division by 3, and remainder 1 (since , ; chapter 5's "remainders multiply"), so . So , , , , and likewise : a common factor, contradiction.
- Then , so with positive whole numbers. But has the prime 5 and doesn't, contradicting unique factorisation. So is irrational, which is why the digit counts of chapter 10 ( for ) never fall into a simple repeating pattern.
- For rationals , the number is between them (since ) and irrational (if it were rational, , and so , would be too).
- The list is generated so that each positive fraction in lowest terms appears at exactly one position (it corresponds to the fraction's position in a binary tree in which each fraction has one parent). It contains only fractions, and isn't one, so it's never in the list, though for any tolerance some fraction in the list (such as a Pell fraction) is within it. "Close to" and "equal to" are different, which is the whole point of irrational numbers.
math.sqrt(2)is the nearest float to , andx, computed some other way (from a diagonal , or by Heron's method, or from data), may round to a neighbouring float. Exact equality between results of different computations is luck. Compare with a tolerance suited to the scale (math.isclose(x, math.sqrt(2), rel_tol=1e-12)), or, if exactness matters, keep the quantity symbolic or compare with 2 in exact arithmetic when is rational.
Challenge
- Prove that the decimal , with 1s exactly at the positions (that is, 1, 2, 6, 24, 120, …) and 0s elsewhere, is irrational. (Liouville used a number like this in 1844 to give the first example of a transcendental number.)
Discussion
If its decimal repeated with some period from some point on, then every block of consecutive digits after that point would contain the same number of 1s as every other such block. But the gaps between consecutive 1s, , grow without bound, so eventually there are blocks of digits containing no 1s at all, between 1s that are further apart than , and also blocks that do contain a 1. So the decimal can't be eventually periodic, and by chapter 7 the number isn't a fraction. (Liouville's harder argument shows it isn't even the solution of any whole-number polynomial equation: it's approximated "too well" by its own truncations, with errors far smaller than for every .)
In real life
Misconceptions and limits
The model to keep
Common misconceptions
| Misconception | What's actually true | Where it's shown |
|---|---|---|
| " is (or 1.414), just to more places than we bother with." | No fraction equals ; approximations get close but never reach it. | √2 is irrational |
| "If fractions are dense, they fill the line." | Dense means another between any two; there are still gaps at , , … | The line with no gaps |
| "π = 22/7." | is an approximation (3.1428…); is irrational. | π, e and the rest |
| "A decimal that looks random is irrational." | Only a proof decides; any finite number of digits fits a fraction. | Decimals that neither end nor repeat |
| "Irrational times irrational is irrational." | . | Which other roots are irrational |
| "All infinities are the same size." | The fractions can be listed; the reals can't. | The reals can't be listed |
| "A computer stores √2." | It stores the nearest of finitely many fractions. | Real numbers in a computer |
| "Two numbers computed to be equal are equal." | In floats, equality is luck; use tolerances, or exact types where possible. | Harder exercise 12 |
Limits and trade-offs
- "Real" is a model. No measurement ever produces an irrational number to infinitely many digits; the real numbers are the mathematical idealisation that makes calculus and geometry work, and every computation approximates them.
- Some questions about specific numbers are open. Whether is irrational, or whether 's digits are evenly distributed, is unknown.
- Exactness has costs. Symbolic and exact real arithmetic avoid rounding but are slower and can't always decide equality; floating point is fast and approximate. Choose per problem.
- This book's numbers stop here, at the line. Solving needs complex numbers, and making "limit" precise needs analysis; both lie beyond this book.
Revision sheet
Key ideas
- Rational: with whole , . Irrational: a real number that isn't rational.
- is irrational: forces both and even, against lowest terms.
- is a whole number or irrational (unique factorisation: exponents must be even).
- Rational + irrational, and non-zero rational × irrational, are irrational; irrational × irrational can be rational.
- (Lambert 1761) and (Euler 1737) are irrational; both are transcendental (Lindemann 1882, Hermite 1873).
- Decimals: rational ⇔ ends or repeats; irrational ⇔ neither.
- Reals = all points of the line = all infinite decimals (with 0.4999… = 0.5). Completeness: nested shrinking intervals close on exactly one real.
- Between any two reals: a rational and an irrational.
- : each fills a gap (opposites, reciprocals, limits).
- Countable: can be listed (whole numbers, integers, fractions). Uncountable: the reals (Cantor's diagonal).
- Floats: a finite set of fractions ; spacing grows with size; equality needs tolerance.
Glossary
- Rational number: a number that can be written as a fraction of whole numbers.
- Irrational number: a real number that can't be written as a fraction of whole numbers, such as .
- Real number: any point of the number line; any infinite decimal.
- Transcendental number: a number that isn't the solution of any polynomial equation with whole-number coefficients, such as and .
- Algebraic number: a number that is a solution of such an equation, such as .
- Dense: having another member between any two.
- Completeness: the property of the real numbers that shrinking nested intervals always contain exactly one number; no gaps.
- Bisection: finding a number by repeatedly halving an interval known to contain it.
- Pell's equation: , whose solutions give the best fractions near .
- Countable: able to be listed, paired one to one with the whole numbers.
- Uncountable: too many to list, like the real numbers.
- Cantor's diagonal argument: building a number that differs from the th listed number in its th digit, to show no list contains every real.