What a Logarithm Is
A logarithm answers the question "which exponent do I need?" If , then is the logarithm of 32 in base 2, and . Logarithms undo powers the way subtraction undoes addition, and they turn up in programming every time something is halved or doubled over and over.
Exponent in, exponent out
A power takes a base and an exponent and gives a result. A logarithm takes the base and the result and gives back the exponent:
Read as "log base 2 of 32". The general definition:
So to evaluate a log, ask: " to what gives ?"
| Question | Answer |
|---|---|
| 2 to what is 8? → 3 | |
| 10 to what is 100? → 2 | |
| 7 to what is 7? → 1 | |
| 9 to what is 1? → 0 | |
| 2 to what is ? → −2 | |
| 4 to what is 2? → (because ) |
Two facts appear in every table: and , for any base .
Logarithms in Python
The math module has a general log(x, base) plus dedicated, more accurate versions for the common bases:
import math
print("log2(32) =", math.log2(32))
print("log10(1000) =", math.log10(1000))
print("log(81, 3) =", math.log(81, 3))
print("log2(0.25) =", math.log2(0.25))
print("log2(1000) =", math.log2(1000))
print("check: 2 **", round(math.log2(1000), 4), "=", round(2 ** math.log2(1000), 6))log2(32) = 5.0
log10(1000) = 3.0
log(81, 3) = 4.0
log2(0.25) = -2.0
log2(1000) = 9.965784284662087
check: 2 ** 9.9658 = 1000.0
Most logarithms are not whole numbers. because 1000 lies just under . You can always check a log by raising the base back up.
Note: You can only take the log of a positive number. is positive for every , so no exponent gives 0 or a negative result.
math.log(0)raisesValueError: math domain error.
Logs as "how many times"
For a whole-number answer, counts how many times you can halve before reaching 1 (or how many doublings take 1 up to ). That is the logarithm's most useful meaning in computing:
import math
def halvings(n):
steps = 0
while n > 1:
n = n / 2
steps += 1
return steps
for n in [8, 64, 1000, 1_000_000]:
print(f"{n:>9,}: halvings = {halvings(n):2d}, log2 = {math.log2(n):6.2f}") 8: halvings = 3, log2 = 3.00
64: halvings = 6, log2 = 6.00
1,000: halvings = 10, log2 = 9.97
1,000,000: halvings = 20, log2 = 19.93
When is not a power of two, the step count is rounded up.
In software
- Binary search checks the middle of a sorted list and throws half away each step. A million sorted items need at most about comparisons, a billion about 30. See Binary Search.
- Bits needed: to store different values you need bits. 256 values → 8 bits; 1000 values → 10 bits.
- Tree height: a balanced binary tree with nodes is about levels deep, which is why balanced search trees stay fast. Discrete Mathematics analyses such algorithms in depth.
- Number of digits: a positive whole number has decimal digits. .
Exercises
Exercise 1: Evaluate by hand
Find , , , and .
Answer
6, 4, 3, 0, −3.
Solution
Exercise 2: Rewrite
Rewrite as a logarithm: and . Rewrite as a power: .
Answer
, , and .
Solution
In the base stays the base, the exponent becomes the log's value and goes inside: .
Exercise 3: Between which whole numbers?
Without a calculator, between which two whole numbers is ? Check with Python.
Answer
Between 8 and 9 (it is about 8.23).
Solution
, so .
import math
print(round(math.log2(300), 2))8.23
Exercise 4: Binary search budget
A sorted list holds 50,000 user IDs. What is the largest number of halving steps binary search needs? Write a loop that counts it.
Answer
16 steps (, rounded up).
Solution
, so 16 halvings bring 50,000 down to 1.
n, steps = 50_000, 0
while n > 1:
n = (n + 1) // 2 # the larger half, worst case
steps += 1
print(steps)16
Test yourself
What is ?
- 3
- 2
- 27
means…
About how many steps does binary search need on 1,000,000 sorted items?
- 20
- 1,000
- 500,000
- 6