Sub topic 5
Common logarithms, natural logarithms and base 2
This chapter assumes you can already write a logarithm in exponential form, log_b(x) = y meaning b^y = x. It also assumes you know the special values log_b(1) = 0 and log_b(b) = 1, and you can run basic Python.
In principle a logarithm can use any positive base other than 1. In practice three bases cover almost all uses: 10, e and 2. Each has its own notation, its own typical applications, and its own function in Python and on calculators.
Why three bases dominate
The base of a logarithm sets the "unit" in which you count exponents. Base 10 counts powers of ten, base 2 counts doublings, and base e counts continuous growth. The three bases are summarized here.
| Base | Name | Common notation | Python function | Typical use |
|---|---|---|---|---|
| 10 | Common logarithm | `log10(x)`, `lg(x)`, sometimes `log(x)` | `math.log10(x)` | Scientific notation, decibels, pH |
| e ≈ 2.71828 | Natural logarithm | `ln(x)`, sometimes `log(x)` | `math.log(x)` | Calculus, continuous growth and decay |
| 2 | Binary logarithm | `log2(x)`, `lb(x)` | `math.log2(x)` | Bits, algorithm complexity |
Common logarithms: base 10
The common logarithm is log_10(x). Because our number system is decimal, it is easy to evaluate for powers of ten.
log10(1000) = 3, because10^3 = 1000.log10(0.01) = -2, because10^(-2) = 0.01.log10(1) = 0andlog10(10) = 1, the special values from the previous chapter.
For numbers that are not exact powers of ten, the value falls between two integers. Since 100 < 350 < 1000, the value of log10(350) lies between 2 and 3. A calculator gives about 2.544.
This gives a useful reading rule. For a positive number written in scientific notation as m × 10^k with 1 ≤ m < 10, the common logarithm is k + log10(m). The integer part, k, tells you the order of magnitude. The fractional part, log10(m), lies in the interval from 0 up to (but not including) 1.
import math
for x in [0.01, 1, 7, 350, 1000, 4.2e6]:
print(x, round(math.log10(x), 4))
# 0.01 -2.0
# 1 0.0
# 7 0.8451
# 350 2.5441
# 1000 3.0
# 4200000.0 6.6232For 4.2e6, the integer part 6 is the exponent in the scientific notation and 0.6232 is log10(4.2).
Another useful fact is that the number of digits of a positive integer n equals floor(log10(n)) + 1. For example, 350 has floor(2.544) + 1 = 3 digits.
Natural logarithms: base e
The number e ≈ 2.718281828 is an irrational constant that arises in continuous growth. The natural logarithm is ln(x) = log_e(x). It is "natural" because it is the logarithm that makes calculus formulas simplest. For instance, the derivative of ln(x) is 1/x, with no extra constant. You do not need calculus for this chapter, but it explains why ln appears everywhere in science and statistics.
The same pattern holds as for other bases:
ln(e) = 1, becausee^1 = e.ln(1) = 0, becausee^0 = 1.ln(e^3) = 3. More generallyln(e^x) = xande^(ln x) = xforx > 0.
The last pair of identities is the inverse relationship between e^x and ln(x). It is the reason ln is the standard tool for undoing an exponential with base e.
import math
print(math.e) # 2.718281828459045
print(math.log(math.e)) # 1.0
print(math.log(1)) # 0.0
print(math.exp(math.log(5))) # 5.000000000000001 or 5.0, depending on rounding
print(math.log(math.exp(3))) # 3.0math.exp(x) computes e^x. Floating-point arithmetic may leave a tiny error in the last digit, so compare with math.isclose instead of ==.
Where natural logs appear
Growth that compounds continuously follows A = A0 · e^(rt), where A0 is the starting amount, r is the rate and t is time. To find how long growth takes, you take ln of both sides. Half-life, doubling time and many probability formulas work the same way.
Binary logarithms: base 2
The binary logarithm is log_2(x). It asks "how many times must I double 1 to reach x?" It is also the number of times you can halve x before reaching 1.
| x | 1 | 2 | 4 | 8 | 16 | 1024 |
|---|---|---|---|---|---|---|
| log2(x) | 0 | 1 | 2 | 3 | 4 | 10 |
In computing, base 2 appears for three reasons.
- Bits. A bit has two states. Distinguishing
nequally likely options takesceil(log2(n))bits. Storing 1000 distinct values needsceil(log2(1000)) = 10bits, since2^10 = 1024 ≥ 1000. - Binary search. Each comparison halves the remaining range, so searching a sorted list of
nitems needs at most aboutlog2(n)steps. - Tree depth. A balanced binary tree with
nleaves has depth aboutlog2(n).
import math
n = 1_000_000
print(math.log2(n)) # 19.931568569324174
print(math.ceil(math.log2(n))) # 20A sorted list of one million items can be searched in at most 20 comparisons.
Comparing the three on one value
Take x = 1000. The three logarithms are different numbers because they count different units.
import math
x = 1000
print(math.log10(x)) # 3.0 (powers of ten)
print(math.log(x)) # 6.907755278982137 (powers of e)
print(math.log2(x)) # 9.965784284662087 (doublings)The smaller the base, the larger the logarithm. Base 2 grows more slowly per unit of exponent than base 10, so you need more exponent units to reach the same value. Here log2(1000) is almost 10 because 2^10 = 1024 is just above 1000.
Converting between bases
All three logarithms of the same number are proportional. Using the change-of-base formula covered in a later chapter, log_b(x) = ln(x) / ln(b), so:
log10(x) = ln(x) / ln(10) ≈ 0.4343 · ln(x)log2(x) = ln(x) / ln(2) ≈ 1.4427 · ln(x)ln(x) = ln(10) · log10(x) ≈ 2.3026 · log10(x)
You can use this to switch between the three at a glance. The check below confirms it for x = 1000.
import math
x = 1000
print(math.isclose(math.log10(x), math.log(x) / math.log(10))) # True
print(math.isclose(math.log2(x), math.log(x) / math.log(2))) # TrueEdge cases and practical points
- Domain. All three functions accept only positive inputs. In Python,
math.log10(0)andmath.log10(-5)raiseValueError: math domain error. - Precision for powers.
math.log2is exact for powers of 2 andmath.log10is exact for most powers of 10 in practice.math.log(8) / math.log(2)may give3.0000000000000004or2.9999999999999996depending on rounding, so prefermath.log2when the base is 2. - Integers and big numbers.
math.logandmath.log2accept Python integers too large for a float. For example,math.log2(2**2000)returns2000.0without overflow, whilefloat(2**2000)fails. - Values near 1. For very small
x,math.log(1 + x)loses precision. Usemath.log1p(x)instead.
import math
print(math.log2(2**2000)) # 2000.0
print(math.log1p(1e-10)) # 9.9999999995e-11Summary table: choosing a base
| Situation | Best base | Reason |
|---|---|---|
| Reading an order of magnitude | 10 | The integer part is the power of ten |
| Calculus, continuous growth, statistics | e | Simplest derivative, matches `e^x` |
| Counting bits, halving steps, tree depth | 2 | Each unit is a doubling |
Exercise
Without a calculator, evaluate log10(0.001), log2(64) and ln(e^5). Then say how many bits are needed to give a distinct label to each of 300 items.
Show answer
log10(0.001) = -3 because 10^(-3) = 0.001. log2(64) = 6 because 2^6 = 64. ln(e^5) = 5 because ln and e^x are inverses. For 300 items you need ceil(log2(300)) bits. Since 2^8 = 256 < 300 ≤ 512 = 2^9, log2(300) is between 8 and 9, so 9 bits are needed.
Exercise
A positive integer n has log10(n) ≈ 7.31. How many digits does n have, and in what range is n? Also, which Python call would you use to compute log base 2 of n most accurately?
Show answer
The number of digits is floor(7.31) + 1 = 8. Since 10^7 ≤ n < 10^8, n is between 10,000,000 and 99,999,999. More precisely n ≈ 10^7.31 ≈ 2.04 × 10^7. Use math.log2(n), which is more accurate than math.log(n) / math.log(2).