Algebra
Logarithms
The inverse of exponentiation — turn multiplication into addition
A logarithm answers "what exponent do I need?" — log_b(x) is the power you raise b to to get x. They turn multiplication into addition (log(ab) = log a + log b), making computations possible before calculators. Today they're the spine of decibels, pH, the Richter scale, computational complexity, information theory, and any quantity that spans many orders of magnitude.
- Definitionlog_b(x) = y ⟺ b^y = x
- Common bases10 (common log), e ≈ 2.718 (natural log), 2 (binary log)
- Product rulelog(ab) = log(a) + log(b)
- Quotient rulelog(a/b) = log(a) − log(b)
- Power rulelog(a^n) = n · log(a)
- Change of baselog_b(x) = log_c(x) / log_c(b)
Interactive visualization
Press play, or step through manually. The visualization is yours to drive — try it before reading on.
Watch the 60-second explainer
A condensed visual walkthrough — narrated, captioned, under a minute.
The definition
The logarithm of x with base b is the exponent y such that:
log_b(x) = y ⟺ b^y = x
Logarithm and exponentiation are inverses. log_b(b^y) = y and b^(log_b(x)) = x. Whatever exponentiation does, log undoes; whatever log does, exponentiation undoes.
Examples:
- log_2(8) = 3 because 2³ = 8.
- log_10(1000) = 3 because 10³ = 1000.
- log_10(0.01) = −2 because 10⁻² = 0.01.
- log_e(e²) = 2 — natural log of an exponential of e returns the exponent.
- log_b(1) = 0 always, because b⁰ = 1 for any b ≠ 0.
- log_b(b) = 1 always.
The three common bases
| Base | Notation | Used in | Why this base |
|---|---|---|---|
| 10 (common log) | log(x), log_10(x) | Engineering, decibels, Richter scale, pH | Matches our base-10 number system; orders of magnitude readable |
| e ≈ 2.71828 (natural log) | ln(x) | Mathematics, physics, growth/decay models | Derivative of e^x is itself; no conversion factor in calculus |
| 2 (binary log) | lg(x), log_2(x) | Computer science, information theory | Bit counts, halving algorithms, binary trees |
The logarithm in different bases differ by a multiplicative constant — change of base — but they're all valid. Convert via:
log_b(x) = log_c(x) / log_c(b)
The three rules — and why they matter
| Rule | Formula | Why it works |
|---|---|---|
| Product | log(ab) = log(a) + log(b) | Exponents add when same-base products multiply |
| Quotient | log(a/b) = log(a) − log(b) | Exponents subtract when same-base products divide |
| Power | log(a^n) = n · log(a) | Repeated multiplication = scaled exponent |
The product rule is the historical motivation for inventing logs. Multiplication is hard by hand; addition is easy. Logs let you turn multiplication problems into addition problems via tables — the slide rule's superpower.
Worked examples
Example 1 — solving an exponential equation
Solve 2^x = 100.
2^x = 100
log(2^x) = log(100)
x · log(2) = log(100)
x = log(100) / log(2) ≈ 2 / 0.301 ≈ 6.64
Verify — 2^6.64 ≈ 100. Logs are how you "bring exponents down" to solve for them.
Example 2 — compound interest doubling time
Money grows at 7% per year. How long to double?
2 = 1.07^t
log(2) = t · log(1.07)
t = log(2) / log(1.07) ≈ 0.301 / 0.0294 ≈ 10.24 years
The "rule of 72" is a shortcut — divide 72 by the percent rate. 72 / 7 ≈ 10.3, matching our exact answer. Rule of 72 works because ln(2) ≈ 0.693 ≈ 0.72.
Example 3 — pH
pH = −log_10([H⁺]) where [H⁺] is hydrogen ion concentration in moles per liter. Pure water has [H⁺] = 10⁻⁷, so pH = 7. Lemon juice has [H⁺] = 10⁻², so pH = 2. Each pH unit is 10× concentration. Logs are why a pH change of 1 means a 10× difference, of 5 means 100,000×.
JavaScript: log functions
// Built-in functions
Math.log(x) // natural log, base e
Math.log10(x) // common log, base 10
Math.log2(x) // binary log, base 2
// Custom base via change of base
function logBase(x, base) {
return Math.log(x) / Math.log(base);
}
logBase(1000, 10); // 3
logBase(8, 2); // 3
logBase(100, 7); // 2.366...
// Inverse — exponentiation
Math.exp(x) // e^x
Math.pow(b, y) // b^y
// Verify they're inverses
Math.log(Math.exp(5)); // 5
Math.exp(Math.log(5)); // 5
// Solve a^x = b for x
function solveExp(a, b) {
return Math.log(b) / Math.log(a);
}
solveExp(2, 100); // 6.643...
// Doubling time at rate r (as decimal, e.g., 0.07 for 7%)
function doublingTime(r) {
return Math.log(2) / Math.log(1 + r);
}
doublingTime(0.07); // 10.24 years
Log scales — when and why
| Scale | Where used | 1 unit means |
|---|---|---|
| Decibel (dB) | Audio, signal processing | 10× power ratio = 10 dB; 2× power ≈ 3 dB |
| Richter scale | Earthquake magnitude | 1 unit = 10× ground motion = ~32× energy |
| pH | Chemistry — acidity | 1 unit = 10× hydrogen ion concentration |
| Magnitude (astronomy) | Star brightness | 5 magnitudes = 100× brightness ratio |
| Octave (music) | Pitch | 1 octave = 2× frequency |
| Big-O complexity | Algorithm analysis | log_2 dominates halving algorithms |
Log scales are appropriate when the underlying quantity varies multiplicatively, not additively. Pitch, brightness, sound intensity — human perception of these is logarithmic, not linear (Weber-Fechner law). So measuring them in log units matches our intuition.
Logarithms in information theory
Shannon's information content of an event with probability p is −log(p) bits (with log base 2). The expected information from a probability distribution is −Σ pᵢ · log(pᵢ) — the famous entropy.
This is why log_2 is the right base for information — a bit is binary, so each step in the log is a yes/no question. log_2(2) = 1 bit; log_2(8) = 3 bits (specifying one of 8 equally-likely outcomes takes 3 yes/no questions). Compression algorithms, channel capacity, cryptographic strength — all measured in bits via log_2.
Where logs show up everywhere
- Algorithm complexity. log n for binary search, balanced trees. n log n for merge sort, quicksort. Sorted-data algorithms have a log somewhere.
- Continuous compounding. e^(rt) for growth at rate r over time t. Inverting — solving for t — uses logs.
- Decay processes. Radioactive half-life. Drug elimination from body. RC circuit voltage decay. All exponential, all log-based half-life calculations.
- Statistics — likelihood. Maximum likelihood estimation usually maximizes log-likelihood (sum of logs) instead of likelihood (product) for numerical stability and easier algebra.
- Number theory. Prime number theorem — the number of primes less than n is approximately n / ln(n). Logarithms and primes are deeply intertwined.
- Cryptography. RSA security relies on integer factorization being hard; discrete log over elliptic curves is the basis of ECDSA. The "discrete logarithm problem" is named after exactly this.
Common mistakes
- Confusing log of a sum with sum of logs. log(a + b) ≠ log(a) + log(b). The product rule applies to multiplication, not addition. log(2 + 3) = log(5) ≈ 1.61; log(2) + log(3) = log(6) ≈ 1.79. Different.
- Computing log of negative or zero. Real log is defined only for positive numbers. log(0) = −∞ (undefined as a number); log(negative) doesn't exist in real arithmetic.
- Wrong base assumption. "log" without subscript is base 10 in engineering, base e in math, base 2 in computer science. Always confirm context. Math.log in JavaScript is natural (base e); Math.log10 and Math.log2 are explicit.
- Forgetting that log is monotonic but slow-growing. log(x) increases with x but very slowly — log(100) ≈ 4.6, log(1,000,000) ≈ 13.8. Useful for problems where you want to compress vast ranges; misleading if you forget how compressed it is.
- Misapplying the change-of-base formula. log_a(b) ≠ a / b. The correct formula is log_a(b) = log(b) / log(a) — division of two logs, not the arguments.
- Numerical issues with log near zero. log(very small number) is very negative — overflow or precision loss.
log1p(x)computes log(1 + x) accurately even for tiny x; preferred tolog(1 + x)when x is small.
Frequently asked questions
What does log(x) mean exactly?
log_b(x) is the exponent y such that b^y = x. So log_2(8) = 3 because 2³ = 8. log_10(100) = 2 because 10² = 100. The logarithm answers the question "to what power must I raise b to get x?" When the base is unspecified, "log" usually means natural log (base e) in math; base 10 in engineering; base 2 in computer science. Always check context.
Why does log(ab) = log(a) + log(b)?
Because exponents add when bases multiply. b^x · b^y = b^(x+y). Take log of both sides — log(b^x · b^y) = x + y = log(b^x) + log(b^y). With ab = b^x · b^y, that becomes log(ab) = log(a) + log(b). This is the property that lets you turn multiplication into addition — the original motivation for invention of logarithms (Napier, 1614) for ship navigation calculations.
What's the natural log and why is it "natural"?
ln(x) = log_e(x), where e ≈ 2.718. It's natural because the derivative of e^x is itself — the only base for which exponential growth equals its rate. ln(x) shows up in calculus everywhere — d/dx(ln x) = 1/x, ∫1/x dx = ln|x|, derivatives and integrals of exponential functions. Other bases require a conversion factor; e doesn't.
Why use log scales in plotting?
When data spans many orders of magnitude (1 to 10⁹), a linear plot crushes the small values to invisibility. A log scale spaces orders of magnitude evenly — so 1, 10, 100, 1000 are equally spaced. Used in finance, biology (pH, growth rates), audio (decibels), seismology (Richter), and physics (frequency spectra). Equal slopes on a log-log plot mean a power-law relationship.
Why log base 2 in computer science?
Because computer algorithms binary-divide. Binary search halves the search range each step — number of steps is log_2(n). Trees with branching factor 2 have log_2(n) depth. Bit counts — n bits represent 2ⁿ values, so log_2(values) gives bits needed. log_2 dominates CS analysis just as log_10 dominates engineering.
How were logarithms used before calculators?
To multiply, look up log(a) and log(b) in a table; add them; look up the inverse in the same table to get a × b. Tables of logs (slide rules, log tables) made multiplication of large numbers feasible by hand. Astronomers, navigators, engineers from 1614 to ~1970 spent enormous time consulting log tables. The slide rule — a mechanical log table — was the engineer's calculator until electronic ones arrived.
What does log of a negative number give?
Not a real number. Real-valued log is defined for positive arguments only — there's no real exponent y such that b^y is negative for positive b. Complex logarithms exist — log(−1) = iπ via Euler's formula — but they're multi-valued and have branch cuts. For real-valued mathematics, treat log(negative) as undefined.