Part I · Maths · Chapter 3
Exponentials and logarithms
Repeated multiplication gives exponentials; asking "how many times?" gives logarithms. Money grows by the first, and every AI loss function is written with the second.
Language models multiply hundreds of probabilities together, and the result is too small for a computer to store. Logarithms turn that multiplication into addition, which is why the training loss of every model in this book has a "log" in it. Compound interest and the "e" inside softmax all come from the same idea.
Repeated multiplication
Put 1,000 in an account paying 7% a year. Each year the balance is multiplied by 1.07 (chapter 2):
- After 1 year: 1,000 × 1.07 = 1,070.
- After 2 years: 1,070 × 1.07 = 1,144.90.
- After 3 years: 1,144.90 × 1.07 = 1,225.04.
After n years you have multiplied by 1.07 a total of n times, which is a power:
A function where the variable sits in the power is an exponential. Compare it with simple growth, where you earn 7% of the original 1,000 each year and nothing on the interest: after 10 years that is 1,000 × (1 + 0.07 × 10) = 1,700. The gap widens every year.
Power rules you will use
- am × an = am+n. Worked: 23 × 22 = 8 × 4 = 32 = 25.
- (am)n = amn. Worked: (22)3 = 43 = 64 = 26.
- a0 = 1 and a−n = 1 / an. Worked: 2−3 = 1/8 = 0.125.
- A fractional power is a root: a1/2 = √a, and a1/20 is the twentieth root.
Decay
Multiplying by a number below 1 shrinks things exponentially. Halve something three times and you have 0.53 = 0.125 of it. Halve it 100 times and you have 0.5100 ≈ 0.000…0008, with 30 zeros after the decimal point. Keep that number in mind for the next section.
The number e
Suppose a bank pays 100% a year but adds the interest in smaller and smaller pieces. Paid once: 1 becomes 2. Paid monthly, 1/12 each time: (1 + 1/12)12 = 2.613. Paid daily: (1 + 1/365)365 = 2.7146. The more often you compound, the closer you get to a fixed number:
You will meet e mostly through the function exp(x) = ex. It has two properties that make it the favourite of AI: it is always positive, whatever x is (exp(−5) = 0.0067, exp(0) = 1, exp(3) = 20.1), and its slope at every point equals its own value (chapter 5). Softmax (chapter 9) uses exp to turn any list of scores into positive numbers.
Logarithms: counting the multiplications
A logarithm answers the question "what power do I need?"
- log10 1,000 = 3, because 103 = 1,000.
- log2 8 = 3, because 23 = 8.
- log2 (1/8) = −3, because 2−3 = 1/8.
So log undoes exp, and exp undoes log: ln(e3) = 3 and eln 5 = 5.
The rules that make logs useful
Check the first with base 10: log 100 + log 1,000 = 2 + 3 = 5, and log(100 × 1,000) = log 100,000 = 5. Logs turn multiplication into addition. Before calculators, engineers multiplied large numbers with printed log tables for exactly this reason.
The second rule solves "how long until it doubles?". We need 1.07n = 2. Take ln of both sides: n × ln 1.07 = ln 2, so n = 0.6931 / 0.0677 = 10.24 years. The "rule of 72" is a shortcut for the same thing: 72 / 7 = 10.3.
At 10% a year, how many years until money doubles? Use ln 2 = 0.6931 and ln 1.1 = 0.0953.
Answer
n = 0.6931 / 0.0953 = 7.27 years. The rule of 72 says 72 / 10 = 7.2.
Logs of numbers below 1 are negative
Probabilities sit between 0 and 1, so their logs are zero or negative: ln 1 = 0, ln 0.9 = −0.105, ln 0.5 = −0.693, ln 0.1 = −2.303, ln 0.001 = −6.91. The closer the probability is to zero, the larger the negative number. Flip the sign and you get a "surprise" score: an event you gave probability 0.9 surprises you by 0.105; one you gave 0.001 surprises you by 6.91. That surprise is exactly the loss used to train language models (chapter 9).
In AI: adding logs instead of multiplying probabilities
A language model gives a probability to each next token. The probability of a whole sentence is the product of the step probabilities. Three steps with probabilities 0.9, 0.8 and 0.7 give 0.9 × 0.8 × 0.7 = 0.504. A thousand steps of about 0.5 each would give 0.51000 ≈ 10−301. The 32-bit floats that models compute in round anything below about 10−45 to exactly zero, and even 64-bit floats give up below about 10−308, which a few more steps would reach.
Take logs instead: ln 0.9 + ln 0.8 + ln 0.7 = −0.105 − 0.223 − 0.357 = −0.685, and exp(−0.685) = 0.504, the same answer. A thousand steps simply add up to about −693, which is easy to store. That is why models report "log probabilities" (often shortened to logprobs) and why training minimises a sum of negative logs.
Growth numbers in practice
Banks usually add interest monthly, not yearly. Divide the yearly rate by 12 and multiply that many times: growth = (1 + rate / 12) ** months. For 10,000 at 7% over 10 years that is 1.005833120 = 2.00966, so the lump sum grows to 20,096.61. Yearly compounding would give 10,000 × 1.0710 = 19,671.51, a little less, because the interest starts earning sooner.
Saving a fixed amount every month adds a geometric series, and the sum has a closed form: contribution * (growth - 1) / monthly_rate. Saving 200 a month for those 10 years gives 200 × (2.00966 − 1) / 0.005833 = 34,616.96. At a 0% rate the formula divides by zero, so a careful calculator needs a separate branch for that case.
Money code should use a decimal type, not binary floats. Floats cannot hold 0.1 exactly: adding 0.1 ten times in Python gives 0.9999999999999999, and rounding errors like that show up in cents. Python's Decimal rounds the way a person expects.
Two more habits. To express a future amount in today's money, divide by (1 + inflation) ** years. And to turn a start and end value into one average yearly return, take a root: (end / start) ** (1 / years) - 1. If 10,000 grows to 26,533 in 20 years, the average return is 2.65331/20 − 1 = 5.0% a year.
Self-check
What is log10 0.01, and why?
−2, because 10−2 = 1/100 = 0.01.
Why do models work with log probabilities rather than probabilities?
A product of many probabilities becomes too small for floating point and rounds to zero. The log of a product is the sum of the logs, which stays a manageable negative number and is cheaper to compute.
10,000 grows at 5% a year while prices rise 3% a year. Roughly what is it worth in today's money after 20 years?
10,000 × 1.0520 / 1.0320 = 10,000 × 2.6533 / 1.8061 = 14,691.
Why does steady compound growth look like a straight line on a log scale?
Because log(start × (1 + r)n) = log(start) + n × log(1 + r). That is a straight-line function of n with slope log(1 + r).
Further reading
- What's so special about Euler's number e? | Chapter 5, Essence of calculus video beginner
Shows why e appears when a quantity grows in proportion to itself, linking exponentials to their own derivative. - Logarithm Fundamentals | Ep. 6 Lockdown live math video beginner
A gentle live lesson treating logs as the inverse of exponentials, with the rules derived rather than memorised. - Logarithms (Khan Academy, Algebra 2) video beginner
Practice unit covering log rules, the constant e and natural log, and solving exponential equations. - An Intuitive Guide To Exponential Functions & e article beginner
Builds e from continuous compound growth so the number feels natural instead of arbitrary.
Free, checked on 2026-10-21. All resources for this chapter.