Logarithm Laws
Because a logarithm is an exponent, every exponent law has a logarithm twin. These laws turn multiplication into addition, division into subtraction and powers into multiplication, which is why logarithms were invented in the first place and why they are still used in code to keep numbers manageable.
The three main laws
For a base (, ) and positive , :
| Law | Rule | Example (base 10) |
|---|---|---|
| Product | ||
| Quotient | ||
| Power |
Plus the facts from the previous lesson: , , and .
Why they hold
Write and as powers of : and , so and . Then
Each log law is the matching exponent law seen from the other side.
import math
x, y = 37.0, 5.5
print(round(math.log2(x * y), 10), "==", round(math.log2(x) + math.log2(y), 10))
print(round(math.log2(x / y), 10), "==", round(math.log2(x) - math.log2(y), 10))
print(round(math.log2(x ** 3), 10), "==", round(3 * math.log2(x), 10))7.6688849843 == 7.6688849843
2.750021747 == 2.750021747
15.6283600969 == 15.6283600969
The change-of-base formula
Calculators and most languages only offer a few bases (10, 2 and ). To get any other base, divide:
For example, . This is how math.log(x, base) works internally.
import math
print(math.log10(50) / math.log10(3))
print(math.log2(50) / math.log2(3))
print(math.log(50, 3))
print("check: 3 ** log(50, 3) =", round(3 ** math.log(50, 3), 9))3.5608767950073115
3.560876795007312
3.5608767950073115
check: 3 ** log(50, 3) = 50.0
The tiny difference in the last digit comes from floating-point rounding, not from the math.
Mistakes to avoid
Using the laws
Expand a log into simpler pieces:
Combine pieces into a single log:
Solve an equation where the unknown is an exponent: . Take log of both sides, then use the power law to bring down:
In software
- Multiplying many probabilities. A language model scores a sentence by multiplying the probabilities of its tokens, often thousands of numbers smaller than 1. The product underflows to 0.0 in floating point. Using the product law, programs add log-probabilities instead, which stay reasonable. The training loss of LLMs is built on these logs.
- Solving for time. "When will a value that doubles every month reach a million?" needs an exponent, so it needs a log, as in the example above.
import math
probs = [0.01] * 400 # 400 tokens, each with probability 0.01
product = 1.0
for p in probs:
product *= p
print("plain product:", product)
log_sum = sum(math.log10(p) for p in probs)
print("sum of log10:", log_sum, " -> probability = 10 **", log_sum)plain product: 0.0
sum of log10: -800.0 -> probability = 10 ** -800.0
The true probability is , far below the smallest float (about ), but its logarithm, −800, is an ordinary number.
Exercises
Exercise 1: Expand
Expand and as far as possible.
Answer
and .
Solution
Exercise 2: Combine
Write as a single number: and .
Answer
2 and 4.
Solution
Exercise 3: Change of base
Compute using only math.log10, and check the result by raising 5 to it.
Answer
.
Solution
.
import math
k = math.log10(200) / math.log10(5)
print(round(k, 3), round(5 ** k, 6))3.292 200.0
Exercise 4: Doubling time
A user base doubles every month, starting at 1,500 users. After how many months does it first exceed 1,000,000? Solve with logs, then round up.
Answer
, so after 10 months.
Solution
The value is checked only at whole months, so round up to 10. After 9 months there are users; after 10, 1,536,000.
import math
x = math.log(1_000_000 / 1500) / math.log(2)
print(round(x, 2), 1500 * 2**9, 1500 * 2**10)9.38 768000 1536000
Test yourself
equals…
Simplify .
- 10
- 45
- 5
- 14
Why do ML programs add log-probabilities instead of multiplying probabilities?
- Addition is more accurate than multiplication in general
- Logs make probabilities larger than 1
- Products of many small numbers underflow to 0 in floating point
- Python cannot multiply floats