Sub topic 15
Logarithms in practice: decibels, pH and algorithm complexity
This chapter assumes you know the definition of a logarithm, the product, quotient and power laws, and the change-of-base formula. It also assumes you can run short Python scripts. You will see three places where logarithms are used for a practical reason: decibels, pH and the running time of algorithms.
All three use the same idea. A quantity that spans many orders of magnitude is hard to compare directly. Its logarithm turns each multiplication by a fixed factor into the addition of a fixed amount.
Why scientists and engineers use logarithmic scales
Some quantities vary enormously. The faintest sound a person can hear and the loudest sound the ear can tolerate differ in power by a factor of about 10^12. A table of raw values would be full of zeros. The logarithm compresses that range and matches how people perceive it. We tend to hear equal ratios of intensity as equal steps in loudness, not equal differences.
The product law explains why this works. If a quantity is multiplied by k, its logarithm increases by log(k). Multiplying by 10 always adds 1 to log10, whatever the starting value.
Decibels
A decibel (dB) expresses a ratio between two powers. For a measured power P and a reference power P0, the level is:
L = 10 · log10(P / P0)
Three consequences follow directly from what you already know:
- A power ratio of 10 gives 10 · log10(10) = 10 dB.
- A power ratio of 1000 gives 10 · 3 = 30 dB.
- A power ratio of 2 gives 10 · log10(2), which is about 3.01 dB. This is why "3 dB" is shorthand for "double the power".
Why amplitudes use 20 instead of 10
Quantities such as voltage or sound pressure are amplitudes. Power is proportional to the square of the amplitude. Apply the power law to see what that does to the formula:
10 · log10(A² / A0²) = 10 · 2 · log10(A / A0) = 20 · log10(A / A0)
So the factor 20 comes from the power law. Doubling an amplitude therefore adds about 6.02 dB, which is twice the 3.01 dB for doubling power.
import math
def db_power(p, p0):
return 10 * math.log10(p / p0)
def db_amplitude(a, a0):
return 20 * math.log10(a / a0)
print(round(db_power(2, 1), 2)) # 3.01
print(round(db_power(1000, 1), 2)) # 30.0
print(round(db_amplitude(2, 1), 2)) # 6.02
# Consistency check: power is amplitude squared, so both formulas agree
a, a0 = 7.0, 2.0
print(math.isclose(db_amplitude(a, a0), db_power(a ** 2, a0 ** 2))) # TrueDecibels add
Suppose a signal passes through an amplifier with a gain of 20 dB and then a cable with a loss of 3 dB. The power ratios multiply, so the decibel values add. The net effect is 20 − 3 = 17 dB. Engineers add gains and losses in decibels because the product law turns multiplication into addition.
pH
The pH of a solution measures its acidity. It is defined from the hydrogen-ion concentration [H+], in moles per litre:
pH = −log10([H+])
The minus sign makes pH positive for typical concentrations, which are below 1 mol/L. For example, pure water at 25 °C has [H+] = 10^−7, so pH = −(−7) = 7.
Because the scale is logarithmic, one pH unit is a factor of 10 in concentration. A solution at pH 3 has 10^(6−3) = 1000 times the hydrogen-ion concentration of a solution at pH 6. The scale runs in the opposite direction from the concentration. Lower pH means higher [H+].
To go backwards, use the inverse relationship: [H+] = 10^(−pH).
import math
def ph(h_concentration):
return -math.log10(h_concentration)
def h_from_ph(value):
return 10 ** (-value)
print(round(ph(1e-7), 2)) # 7.0
print(round(ph(3.2e-4), 2)) # 3.49
print(h_from_ph(3.0) / h_from_ph(6.0)) # about 1000, up to roundingNotice that 3.2e-4 is 3.2 × 10^−4. Its logarithm is log10(3.2) − 4 ≈ 0.505 − 4, so the pH is about 3.49. You can split a logarithm this way because of the product law.
Algorithm complexity
Logarithms also describe how many steps an algorithm needs. Binary search is the standard example. It looks for a target in a sorted list by comparing with the middle element, then discarding half of the list.
If the list has n items, each step halves the number of candidates. The search ends when at most one candidate remains. The question "how many times can n be halved?" is the question "what exponent of 2 gives n?" The answer is log2(n). The worst case is floor(log2(n)) + 1 comparisons.
| List size n | log2(n), approximately | Worst-case comparisons |
|---|---|---|
| 1,000 | 9.97 | 10 |
| 1,000,000 | 19.93 | 20 |
| 1,000,000,000 | 29.90 | 30 |
A list a thousand times longer needs only about 10 more steps. That is the practical meaning of logarithmic growth: the cost rises slowly while the input grows fast.
import math
def binary_search(items, target):
lo, hi = 0, len(items) - 1
steps = 0
while lo <= hi:
steps += 1
mid = (lo + hi) // 2
if items[mid] == target:
return mid, steps
elif items[mid] < target:
lo = mid + 1
else:
hi = mid - 1
return -1, steps
n = 1_000_000
items = list(range(n))
bound = math.floor(math.log2(n)) + 1 # 20
_, steps = binary_search(items, n - 1)
print(bound) # 20
print(steps <= bound) # TrueWhy the base does not matter in big-O
You will see "O(log n)" without a base. The change-of-base formula explains why. For any bases a and b:
loga(n) = logb(n) / log_b(a)
The denominator log_b(a) is a constant that does not depend on n. So log2(n) and log10(n) differ only by a constant factor, about 3.32. Big-O notation ignores constant factors, so all logarithmic bases belong to the same class.
Other logarithmic scales
The same pattern appears elsewhere. Earthquake magnitude scales are logarithmic, so each whole step corresponds to a fixed multiplication in measured ground motion. Musical pitch is also logarithmic. Each octave doubles the frequency, so equal musical intervals correspond to equal frequency ratios. The check in each case is the same. If equal ratios in the raw quantity correspond to equal differences on the scale, the scale is logarithmic.
Summary of the three uses
| Context | Formula | What one unit means |
|---|---|---|
| Decibels (power) | 10 · log10(P / P0) | 10 dB is a factor of 10 in power |
| pH | −log10([H+]) | 1 pH unit is a factor of 10 in [H+] |
| Binary search | log2(n) | One extra step when n doubles |
Exercise
A speaker's output power is raised from 5 W to 40 W. By how many decibels does the level increase? Give the answer to one decimal place. (Hint: 40 / 5 = 8 = 2^3.)
Show answer
The power ratio is 40 / 5 = 8. The gain is 10 · log10(8) = 10 · 3 · log10(2) ≈ 10 · 3 · 0.301 ≈ 9.0 dB. Equivalently, three doublings of power each add about 3.01 dB, giving 9.03 dB, or 9.0 to one decimal place.
Exercise
Lemon juice has pH 2.0 and a particular tap water has pH 8.0. (a) How many times larger is the hydrogen-ion concentration of the lemon juice? (b) Using binary search, roughly how many comparisons are needed in the worst case for a sorted list of 4,096 items?
Show answer
(a) The difference is 8 − 2 = 6 pH units, and each unit is a factor of 10, so the lemon juice has 10^6 = 1,000,000 times the concentration. (b) log2(4096) = 12, because 2^12 = 4096. The worst case is floor(12) + 1 = 13 comparisons, so about 12 to 13.