Common and Natural Logs
Two logarithm bases get special names. The common log uses base 10 and fits our decimal number system. The natural log uses the strange-looking base , which turns out to be the natural base for growth, interest and calculus. Programmers meet both, plus base 2, and must watch which one a function means.
Three bases, three names
| Base | Name | Math notation | Python |
|---|---|---|---|
| 10 | common log | or | math.log10(x) |
| natural log | math.log(x) |
||
| 2 | binary log | or | math.log2(x) |
Note: In Python, NumPy, JavaScript (
Math.log), C and Excel'sLN, plainlogmeans the natural log. On a school calculatorlogusually means base 10. When a formula says "log" without a base, check which one it means.
The common log counts digits
tells you how many powers of ten fit into a number. For a positive whole number, the integer part of its common log is one less than its digit count:
import math
for n in [7, 42, 999, 1000, 123_456, 10**12]:
lg = math.log10(n)
print(f"{n:>14,} log10 = {lg:7.4f} digits = {math.floor(lg) + 1}") 7 log10 = 0.8451 digits = 1
42 log10 = 1.6232 digits = 2
999 log10 = 2.9996 digits = 3
1,000 log10 = 3.0000 digits = 4
123,456 log10 = 5.0915 digits = 6
1,000,000,000,000 log10 = 12.0000 digits = 13
Tip: For very large integers,
len(str(n))is the safe way to count digits in code:math.log10works with floats and can be off by one right at powers of ten for huge values.
Where comes from
Imagine 1 unit of money growing at 100% interest per year. Paid once a year you end with 2. Split it into smaller payments and you get . More frequent compounding helps, but less and less, and the result creeps toward a limit:
import math
for n in [1, 2, 12, 365, 10_000, 1_000_000]:
print(f"n = {n:>9,}: (1 + 1/n)^n = {(1 + 1 / n) ** n:.6f}")
print("math.e =", round(math.e, 6))n = 1: (1 + 1/n)^n = 2.000000
n = 2: (1 + 1/n)^n = 2.250000
n = 12: (1 + 1/n)^n = 2.613035
n = 365: (1 + 1/n)^n = 2.714567
n = 10,000: (1 + 1/n)^n = 2.718146
n = 1,000,000: (1 + 1/n)^n = 2.718280
math.e = 2.718282
That limit is , an irrational number like . Continuous compounding at rate for time multiplies money by , which links to compound interest. In calculus, is the one function that is its own slope, which is why it is "natural".
undoes
The natural log is the exponent you need on :
All log laws hold for too. The change-of-base formula connects the three bases: and .
A common use is solving for time in growth problems. How long does money take to double at 5% continuous growth? Solve :
This gives the rule of 70: , since .
import math
print("ln(e) =", math.log(math.e))
print("ln(2) =", round(math.log(2), 4))
print("exp(ln 5) =", round(math.exp(math.log(5)), 10))
print("log2(80) via ln:", round(math.log(80) / math.log(2), 4), " direct:", round(math.log2(80), 4))
for rate in [2, 5, 7, 10]:
exact = math.log(2) / (rate / 100)
print(f"{rate:2d}%: doubling {exact:5.2f} years, rule of 70 says {70 / rate:5.2f}")ln(e) = 1.0
ln(2) = 0.6931
exp(ln 5) = 5.0
log2(80) via ln: 6.3219 direct: 6.3219
2%: doubling 34.66 years, rule of 70 says 35.00
5%: doubling 13.86 years, rule of 70 says 14.00
7%: doubling 9.90 years, rule of 70 says 10.00
10%: doubling 6.93 years, rule of 70 says 7.00
In software
- Machine learning uses everywhere: cross-entropy loss, log-likelihood, the softmax function (built on ) and the sigmoid . See Logistic Regression.
- Information: base-2 logs measure information in bits; base- logs measure it in "nats". Same idea, different unit.
- Exponential decay in games and UI (smooth camera follow, fading sounds) uses so that the result is the same at any frame rate (see damping).
- API naming: NumPy has
np.log,np.log10,np.log2andnp.log1p(, accurate for tiny ).
Exercises
Exercise 1: Exact values
Without a calculator: , , , .
Answer
−3, 4, 0, 9.
Solution
Exercise 2: Digit count
Using , how many digits does have? Check with len(str(...)).
Answer
31 digits.
Solution
. The integer part is 30, so there are 31 digits.
import math
print(math.floor(100 * math.log10(2)) + 1, len(str(2 ** 100)))31 31
Exercise 3: Doubling time
A cloud bill grows 3% per month (continuously). Estimate the doubling time with the rule of 70, then compute it exactly with .
Answer
Rule of 70: about 23.3 months. Exact: months.
Solution
Exercise 4: Which log?
A colleague writes math.log(1000) expecting 3. What do they get, and what should they write?
Answer
About 6.9078 (the natural log). They should write math.log10(1000) or math.log(1000, 10).
Solution
In Python math.log with one argument is : .
import math
print(round(math.log(1000), 4), math.log10(1000))6.9078 3.0
Test yourself
In Python, what does math.log(x) with one argument compute?
- log base 10
- log base 2
- natural log (base )
- it raises an error
What is ?
- 7
- 1
Using the rule of 70, about how long does something growing 10% a year take to double?
- 10 years
- 7 years
- 70 years
- 0.7 years