Search

Search papers and lessons

Logarithm

Level 1 · 9 min read

A logarithm answers the question 'what exponent do I raise this base to?', which turns multiplication into addition and makes numbers that span many orders of magnitude easy to compare.

On this page

  • Colour = topic mix
  • Discrete math
  • Sign in to see your progress

TL;DR

A logarithm answers one question: how many times do I multiply a base number by itself to get this value? If 23=82^3 = 8, then the logarithm of 8 in base 2 is 3. Logarithms turn multiplication into addition, so they make huge and tiny numbers manageable.

Logs also compress growth. A quantity that doubles every year gets only one unit bigger on a log scale each year, so a log tells you how long something has been growing, not how big it has become.

Where you'll see it: in the entropy of a message measured in bits, in the cross-entropy loss that trains classifiers, and in the log-likelihood that statisticians maximise to fit a model.

Intuition

The exponent question

Multiplying a number by 2 over and over (that is, taking powers of 2) gives 2, 4, 8, 16, 32, 64. Each step adds one to the count of doublings. The logarithm reverses this: given 32, it asks how many 2s you multiply together. The answer is 5, because 25=322^5 = 32.

Read log⁡232=5\log_2 32 = 5 as "two to the power of what gives 32?" The number 2 is the base, 32 is the value, and 5 is the answer.

Why it exists

Some problems make you multiply long chains of numbers. Compound interest, population growth, sound loudness and earthquake strength all do this. Before calculators, astronomers and navigators spent hours multiplying numbers with many digits. The trick that saved them was to look up each number's logarithm in a table, add the logs, and look up the answer in reverse. Adding is much easier than multiplying, so the logarithm turned a hard operation into an easy one.

The same idea still helps in modern work. A value of one billion and a value of one thousandth differ by a factor of a trillion. On a log scale they sit nine units apart, which is easy to read on a plot and easy to reason about.

246810−3−2−1012xlog(x)
  • natural log, ln x
  • base-10 log
Both curves cross zero at x = 1, because any base raised to the power 0 is 1. The natural log equals 1 at x = e, about 2.718.

The math

Definition

Definition: Logarithm

For a base b>0b \gt 0 with b≠1b \neq 1, and a positive number xx, the logarithm log⁡bx\log_b x is the exponent yy that satisfies by=xb^y = x (here byb^y is the exponential function with base bb):

log⁡bx=y⟺by=x.\log_b x = y \quad \Longleftrightarrow \quad b^y = x.

Here bb is the base, xx is the value you want to take the log of, and yy is the answer. The base must be positive and not equal to 1, because 1y1^y is always 1 and could never produce other values.

The natural logarithm uses the base e≈2.71828e \approx 2.71828, Euler's number. We write ln⁡x\ln x for log⁡ex\log_e x. In a lot of mathematics and machine learning, "log" means the natural log. In engineering and information theory, base 2 and base 10 appear often.

Key identities

For positive xx and yy, and any valid base bb:

log⁡b(xy)=log⁡bx+log⁡bylog⁡b ⁣(xy)=log⁡bx−log⁡bylog⁡b ⁣(xk)=klog⁡bxlog⁡b1=0log⁡bb=1\begin{aligned} \log_b(xy) &= \log_b x + \log_b y \\ \log_b\!\left(\frac{x}{y}\right) &= \log_b x - \log_b y \\ \log_b\!\left(x^k\right) &= k \log_b x \\ \log_b 1 &= 0 \\ \log_b b &= 1 \end{aligned}

The first identity is the reason logarithms were invented. Multiplying xx and yy becomes adding their logs.

Logs and powers undo each other: blog⁡bx=xb^{\log_b x} = x and log⁡b(by)=y\log_b(b^y) = y. This is what makes the lookup-and-reverse trick work.

Changing the base

If you only have natural logs on your calculator, you can get a log in any base with

log⁡bx=ln⁡xln⁡b.\log_b x = \frac{\ln x}{\ln b}.

For example, log⁡210=ln⁡10ln⁡2≈2.3025850.693147≈3.3219\log_2 10 = \dfrac{\ln 10}{\ln 2} \approx \dfrac{2.302585}{0.693147} \approx 3.3219. Because the ratio always divides by the same constant ln⁡b\ln b, all logs are the same curve stretched by a fixed factor. This is why changing the base never changes which numbers are big and which are small.

Worked examples

Example
Reading a log from a power

Find log⁡264\log_2 64.

  1. We need the exponent yy with 2y=642^y = 64.
  2. Count the doublings: 2,4,8,16,32,642, 4, 8, 16, 32, 64. That is six steps, so 26=642^6 = 64.
  3. Therefore log⁡264=6\log_2 64 = 6.

Interpretation: starting from 1 and doubling six times gives 64.

Example
Using the product rule on a big number

Estimate log⁡10(50 000)\log_{10}(50\,000), using the known value log⁡105≈0.699\log_{10} 5 \approx 0.699.

  1. Write 50 000=5×10450\,000 = 5 \times 10^4.
  2. Apply the product rule: log⁡10(5×104)=log⁡105+log⁡10(104)\log_{10}(5 \times 10^4) = \log_{10} 5 + \log_{10}(10^4).
  3. The second term is 44, since 104=10 00010^4 = 10\,000.
  4. Adding, 0.699+4=4.6990.699 + 4 = 4.699.

Interpretation: 50 000 has 5 digits, and log⁡10\log_{10} of any 5-digit number lies between 4 and 5. The whole part is the digit count minus one, and the fractional part says where the number falls within that range.

Example
Doubling time under 7 percent growth

A savings account grows by a factor of 1.07 each year. How many years until the balance doubles?

  1. We need nn with 1.07n=21.07^n = 2.
  2. Take natural logs of both sides: nln⁡1.07=ln⁡2n \ln 1.07 = \ln 2.
  3. Solve: n=ln⁡2ln⁡1.07=0.6931470.067659≈10.24n = \dfrac{\ln 2}{\ln 1.07} = \dfrac{0.693147}{0.067659} \approx 10.24.
  4. Check: 1.0710≈1.9671.07^{10} \approx 1.967 and 1.0711≈2.1051.07^{11} \approx 2.105, so doubling happens during year 11.

Interpretation: it takes a little over ten years. The "rule of 70" gives 70 divided by 7, which is 10, and is a quick estimate of the same thing.

Example
Bits of surprise

A message reports an event that had a 1 in 1000 chance. Measured in bits, how surprising is it?

  1. Surprise in bits (the idea behind Entropy) is log⁡2\log_2 of the inverse probability, here log⁡21000\log_2 1000.
  2. log⁡21000=ln⁡1000ln⁡2=6.9077550.693147≈9.97\log_2 1000 = \dfrac{\ln 1000}{\ln 2} = \dfrac{6.907755}{0.693147} \approx 9.97.
  3. A fair coin flip is 1 bit, since log⁡22=1\log_2 2 = 1. This event is about ten times as surprising.

Interpretation: learning something rare carries many more bits than learning something common, and the log is what makes that scale additive. Two independent events give the sum of their bits.

Tradeoffs and failure modes

Logarithms are the right tool when quantities multiply, grow by a fixed ratio, or span many orders of magnitude. They are the wrong tool when a quantity can be zero or negative, or when values differ by small additive amounts.

SituationProblemBetter choice
Value is exactly 0log⁡0\log 0 is undefined, and tends to −∞-\inftyAdd a small constant, or model the zero case separately
Value is negativeNo real logarithmWork with absolute values, or with a different transform
Very small xxln⁡(1+x)\ln(1 + x) loses precision in floating pointUse log1p(x) in numerical libraries
Many small probabilities multipliedProducts underflow to zero in floating pointSum the logs instead (for sums of probabilities, the log-sum-exp trick)
Plotting values from 1 to a billionLinear axes hide everything below the topLog axis

Common mistakes:

  • Log of a sum does not split. log⁡(x+y)\log(x + y) is not log⁡x+log⁡y\log x + \log y. Only products split.
  • Squares and logs. log⁡(x2)=2log⁡x\log(x^2) = 2\log x, but (log⁡x)2(\log x)^2 is a different number.
  • Mixing bases. Formulas in bits (base 2) and nats (base e) differ by a constant factor of ln⁡2\ln 2. Check the base before comparing numbers across sources.

History

The idea of logarithms is due to John Napier, a Scottish landowner and mathematician, who published his tables in 1614 as Mirifici Logarithmorum Canonis Descriptio. Astronomers needed to multiply and divide long numbers constantly, and his method turned those jobs into additions and subtractions. Napier's original logs were not quite the ones we use today.

Henry Briggs, a professor at Gresham College in London, met Napier and suggested base 10 logarithms, which fit the decimal system and were easier to look up. Briggs published base-10 tables in the 1620s. Those tables were used for centuries by scientists, engineers and surveyors.

The natural logarithm with base ee came later, from the study of area under the curve 1/x1/x. Leonhard Euler in the 18th century connected logarithms with exponentials and complex numbers, and made ee the default base in analysis. The slide rule, a mechanical device that adds lengths proportional to logs, was used by engineers until electronic calculators took over in the 1970s.

Logarithms caught on because they solved a real bottleneck, and because the rule that turns products into sums is simple enough to memorise and powerful enough to build whole fields on.

Check yourself

QuestionWhat is log⁡381\log_3 81, and how do you know?Show answer

81=3481 = 3^4, so log⁡381=4\log_3 81 = 4. Check by multiplying: 3×3=93 \times 3 = 9, 9×3=279 \times 3 = 27, 27×3=8127 \times 3 = 81, which is four factors of 3 in total. The answer is the exponent, not the base or the result.

QuestionCompute ln⁡(e5)\ln(e^5) and log⁡10(0.001)\log_{10}(0.001). What general rule does each one illustrate?Show answer

ln⁡(e5)=5\ln(e^5) = 5, because the natural log undoes the exponential with base ee. log⁡10(0.001)=−3\log_{10}(0.001) = -3, because 10−3=0.00110^{-3} = 0.001. The rule is log⁡b(by)=y\log_b(b^y) = y, which holds for any base and for negative exponents too.

QuestionWhy does the product rule log⁡b(xy)=log⁡bx+log⁡by\log_b(xy) = \log_b x + \log_b y make it possible to multiply long numbers by looking up tables and adding?Show answer

Multiplying two large numbers is hard by hand, but adding their logarithms is easy. You look up log⁡x\log x and log⁡y\log y, add them to get log⁡(xy)\log(xy), and then search the table backwards to find the number whose log is that sum. Each step is an addition or a lookup, which is far less work than the multiplication it replaces.

QuestionA learning algorithm assigns probability 0.01 to the correct label of a training example. Using base-2 logs, how many bits of surprise does that carry, and what happens to the total if you multiply the probabilities of 100 independent examples?Show answer

Surprise is log⁡2(1/0.01)=log⁡2100≈6.64\log_2(1/0.01) = \log_2 100 \approx 6.64 bits. For 100 independent examples, the probabilities multiply, so their logs add. Summing the surprise over the examples gives the total negative log-likelihood, which is the quantity that training tries to minimise. Adding logs avoids the numerical underflow you would get by multiplying 100 small numbers directly.

Sources