Exponential and Logarithmic Functions
An exponential function multiplies by the same factor every step; a logarithm asks how many steps it took. They are inverse functions of each other, and between them they describe compound interest, population growth, radioactive decay, algorithm running times and the number of bits in a number.
Exponential functions
The variable is in the exponent, which is what separates from a power function like .
| Property | (growth) | (decay) |
|---|---|---|
| Domain | ||
| Range | ||
| y-intercept | ||
| Behaviour | increasing, as | decreasing, as |
| Asymptote | on the left | on the right |
Every exponential with eventually outgrows every polynomial: is smaller than for a long while, but overtakes it near and is never caught again.
The natural base . It is the value of as grows without bound, the result of compounding interest "continuously". is the one exponential whose slope at every point equals its value, which makes it the default base in calculus and science (exponential and log functions in calculus). Any exponential can be rewritten with base : .
import math
for n in (1, 10, 100, 10_000, 1_000_000):
print(f"(1 + 1/{n})^{n} = {(1 + 1 / n) ** n:.8f}")
print("e =", round(math.e, 8))
x = next(x for x in range(2, 200) if 2**x > x**10 and all(2**k > k**10 for k in range(x, 200)))
print("2^x > x^10 from x =", x)(1 + 1/1)^1 = 2.00000000
(1 + 1/10)^10 = 2.59374246
(1 + 1/100)^100 = 2.70481383
(1 + 1/10000)^10000 = 2.71814593
(1 + 1/1000000)^1000000 = 2.71828047
e = 2.71828183
2^x > x^10 from x = 59
Growth and decay models
Doubling time and half-life depend only on the rate, not on the starting amount: doubles when , i.e. after . The "rule of 70" () comes from this.
Logarithms: the inverse
is one-to-one, so it has an inverse, the logarithm base :
The graph of is the graph of reflected in the line (Composition and Inverse Functions). So domain and range swap: domain , range , x-intercept , vertical asymptote . Logarithms grow very slowly: of a billion is only about 30.
Laws of logarithms (from the exponent laws):
Solving equations with these laws is covered in Exponential Equations and Logarithmic Equations.
import math
import numpy as np
import matplotlib.pyplot as plt
print("log2(32) =", math.log2(32), " log10(1000) =", math.log10(1000), " ln(e^2) =", math.log(math.e**2))
print("change of base log_3(81):", round(math.log(81) / math.log(3), 12), "=", math.log(81, 3))
print("product law:", round(math.log(6 * 7), 12), round(math.log(6) + math.log(7), 12))
x = np.linspace(-3, 3, 300)
xp = np.linspace(0.02, 8, 300)
plt.figure(figsize=(5, 5))
plt.plot(x, 2.0**x, label="2ˣ")
plt.plot(xp, np.log2(xp), label="log₂ x")
plt.plot(x, np.exp(x), label="eˣ", alpha=0.6)
plt.plot(xp, np.log(xp), label="ln x", alpha=0.6)
plt.plot([-3, 8], [-3, 8], "k:", lw=0.8, label="y = x")
plt.xlim(-3, 8); plt.ylim(-3, 8); plt.gca().set_aspect("equal")
plt.legend(); plt.grid(alpha=0.3); plt.title("Exponentials and their inverse logs")log2(32) = 5.0 log10(1000) = 3.0 ln(e^2) = 2.0
change of base log_3(81): 4.0 = 4.0
product law: 3.737669618283 3.737669618283
In software
- Logarithms count digits and steps. A number needs bits; binary search over items takes about steps; a balanced tree with nodes has height about . That is why algorithms feel instant: doubling the input adds one step.
- Exponential backoff. A client that retries a failed request waits seconds after the -th failure (often capped, with random jitter), so a struggling server is not flooded.
- Decay. Exponential moving averages, learning-rate decay in ML, and "time decay" in ranking algorithms all multiply by a fixed factor per step.
- Log scales. Decibels ( of a power ratio), the Richter scale, and log-scale axes for data spanning many orders of magnitude. In ML, losses are computed with logs of probabilities because multiplying many small probabilities underflows to 0, while adding their logs does not.
import math
for n in (1, 255, 256, 1_000_000, 2**40):
print(f"{n:>14}: bits = {n.bit_length():>2}, floor(log2)+1 = {math.floor(math.log2(n)) + 1:>2}")
def backoff(k, base=0.5, cap=30.0):
return min(cap, base * 2**k)
print("retry delays (s):", [backoff(k) for k in range(8)])
probs = [0.01] * 400
p = 1.0
for q in probs:
p *= q
print("product of 400 probabilities:", p, " sum of logs:", round(sum(math.log(q) for q in probs), 4)) 1: bits = 1, floor(log2)+1 = 1
255: bits = 8, floor(log2)+1 = 8
256: bits = 9, floor(log2)+1 = 9
1000000: bits = 20, floor(log2)+1 = 20
1099511627776: bits = 41, floor(log2)+1 = 41
retry delays (s): [0.5, 1.0, 2.0, 4.0, 8.0, 16.0, 30.0, 30.0]
product of 400 probabilities: 0.0 sum of logs: -1842.0681
Note: For very large integers,
math.log2works on a float approximation and can be off by one near powers of two.int.bit_length()is exact.
Exercises
Exercise 1: Evaluate
Find , , and without a calculator.
Answer
6, , , .
Solution
Exercise 2: Half-life
A cache entry's relevance score halves every 6 hours. Starting at 80, what is it after 1 day, and when does it first drop below 1?
Answer
5 after 24 hours; it drops below 1 after about 37.9 hours.
Solution
import math
print(80 * 0.5 ** (24 / 6), round(6 * math.log2(80), 2))5.0 37.93
Exercise 3: Combine with log laws
Write as a single logarithm, and expand .
Answer
; .
Solution
Exercise 4: Doubling time
Users grow 4% per week. Find the exact doubling time, compare with the rule of 70, and find how many weeks until the count first exceeds 10 times today's.
Answer
weeks (rule of 70: 17.5). Ten-fold after 59 whole weeks ().
Solution
import math
print(round(math.log(2) / math.log(1.04), 2), 70 / 4, round(math.log(10) / math.log(1.04), 2))17.67 17.5 58.71
Exercise 5: Levels of a tree
A tournament bracket (a complete binary tree) must hold players, with rounded up to a power of 2. Write rounds(n) returning the number of rounds, , using integer operations only, and print it for .
Answer
1, 3, 4, 5, 10.
Solution
For , = (n - 1).bit_length().
def rounds(n):
return (n - 1).bit_length()
print([rounds(n) for n in (2, 5, 16, 17, 1000)])[1, 3, 4, 5, 10]
Test yourself
What is the domain of ?
- All real numbers
Which law is correct?
Binary search on 1,000,000 sorted items needs at most about how many comparisons?
- 20
- 1,000
- 500,000
- 1,000,000