Exponentials, undone by logarithms
Before you start
You need nothing else first. This unit starts from scratch.
The idea in one paragraph
Exponential growth multiplies by the same factor at every step: 1, 2, 4, 8, 16. It starts slowly and then outruns any growth that adds a fixed amount per step. The logarithm runs it backwards: it counts how many times you multiplied. Because logarithms turn multiplying into adding, they shrink huge ranges of numbers: in base 10, 1, 1000 and 1,000,000 become 0, 3 and 6. The special base e ≈ 2.718 appears whenever growth is continuous, and code uses it by default.
An exponential multiplies by the same factor at every step
Put one bacterium in a dish. Every hour each bacterium splits in two. After 1 hour there are 2, after 2 hours 4, then 8, 16, 32. After n hours there are 2n: 2 multiplied by itself n times. The number on top, n, is the exponent, and 2 is the base. Growth of this kind is called exponential.
Compare it with growth that adds the same amount each step. Adding 2 per hour gives 1, 3, 5, 7, 9, 11, which is ahead at first. By hour 3, doubling has passed it (8 against 7), and by hour 10 it is at 1024 against 21.
The factor need not be 2. Money earning 10% a year is multiplied by 1.1 each year. A factor below 1 shrinks the amount: multiplying by 0.5 each step halves it, which is exponential decay.
The logarithm counts the multiplications back
The logarithm answers the reverse question: how many times must you multiply the base to reach a number? Since 2 × 2 × 2 = 8, the logarithm of 8 in base 2 is 3, written log2 8 = 3. In base 10, log10 1000 = 3, because 10 × 10 × 10 = 1000.
The key property is that logarithms turn multiplying into adding:
log(a × b) = log a + log b
That is why the log scale in the figure works. Equal steps up a log scale are equal factors, so a quantity that grows by the same factor each step climbs in a straight line. The logarithm also tames huge ranges: the numbers 1, 1000 and 1,000,000 have base-10 logarithms 0, 3 and 6.
You put 100 in an account that grows 10% a year.
- After 3 years: 100 × 1.1 × 1.1 × 1.1 = 100 × 1.13 = 133.10.
- When does it double? You need 1.1n = 2. Take logarithms of both sides: n × log 1.1 = log 2.
- So n = log 2 / log 1.1. In base 10, the "log" key on most calculators, this is 0.3010 / 0.0414 ≈ 7.27 years. Any base gives the same answer, as long as you use it on both logarithms.
Your turn. A sample of 80 grams loses half its mass every hour. How much is left after 3 hours, and after how many hours is 5 grams left?
Answer
80 × 0.53 = 10 grams. 5 grams is 80 halved 4 times (80, 40, 20, 10, 5), so after 4 hours: log2(80 / 5) = log2 16 = 4.
The base e is the one code uses by default
One base has a special role: e ≈ 2.718. It is what 1 grows to in one unit of time if it grows at a
rate of 100% compounded continuously, in ever-smaller steps. The function ex is often
written exp(x), and its slope at every point equals its own value. The logarithm in base e is the
natural logarithm, written ln or simply log. In NumPy, np.exp and np.log use
base e; np.log2 and np.log10 use bases 2 and 10.
The logarithm is defined only for positive numbers: no number of multiplications turns 2 into 0 or into −5. In
code, np.log(0) gives minus infinity and a warning. Exponentials also overflow fast: in ordinary
floating-point numbers, np.exp(710) is already too large and becomes infinity. Code that works with
chances often stores their logarithms instead, to avoid both problems.
exps.py: growth, logarithms and e in NumPy
import numpy as np
# np.arange(5) is the array [0 1 2 3 4], and ** raises 1.1 to each of them
print(100 * 1.1 ** np.arange(5)) # 10% growth, years 0 to 4
print(np.log2(8), np.log10(1000)) # count the multiplications back
print(np.log(2) / np.log(1.1)) # years for 10% growth to double
print(np.exp(1), np.log(np.exp(4)))
[100. 110. 121. 133.1 146.41] 3.0 3.0 7.272540897341713 2.718281828459045 4.0
Check yourself
-
What is log2 32? What is log10 0.01?
Answer
5, because 25 = 32. And −2, because 0.01 is 1 divided by 10 twice: 10−2 = 0.01.
-
What is ln(e4)? What is eln 7?
Answer
4 and 7. The exponential and the natural logarithm undo each other.
-
A value is multiplied by 3 at every step. On a log scale, what does its graph look like?
Answer
A straight line going up. Each step adds the same amount, log 3, to its logarithm.
Go further
Optional background, for readers who want more than this page.
-
"How many of one number multiply to make another," with many small examples.
-
Where e comes from, through growth compounded in smaller and smaller steps.
-
What's so special about Euler's number e? 3Blue1Brown, Essence of calculus, chapter 5 Video Intermediate
Why ex is its own slope; uses derivatives.
Where this shows up
Units that build on this one
- The sigmoid: from score to probability Intermediate
- Softmax: probabilities over many choices Intermediate
- Euler's method: small steps along a field Intermediate
- Floating-point numbers: rounding to the nearest stored value Intermediate