TL;DR
A logarithm answers one question: how many times do I multiply a base number by itself to get this value? If , 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 .
Read 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.
- natural log, ln x
- base-10 log
The math
Definition
For a base with , and a positive number , the logarithm is the exponent that satisfies (here is the exponential function with base ):
Here is the base, is the value you want to take the log of, and is the answer. The base must be positive and not equal to 1, because is always 1 and could never produce other values.
The natural logarithm uses the base , Euler's number. We write for . 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 and , and any valid base :
The first identity is the reason logarithms were invented. Multiplying and becomes adding their logs.
Logs and powers undo each other: and . 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
For example, . Because the ratio always divides by the same constant , 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
Find .
- We need the exponent with .
- Count the doublings: . That is six steps, so .
- Therefore .
Interpretation: starting from 1 and doubling six times gives 64.
Estimate , using the known value .
- Write .
- Apply the product rule: .
- The second term is , since .
- Adding, .
Interpretation: 50 000 has 5 digits, and 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.
A savings account grows by a factor of 1.07 each year. How many years until the balance doubles?
- We need with .
- Take natural logs of both sides: .
- Solve: .
- Check: and , 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.
A message reports an event that had a 1 in 1000 chance. Measured in bits, how surprising is it?
- Surprise in bits (the idea behind Entropy) is of the inverse probability, here .
- .
- A fair coin flip is 1 bit, since . 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.
| Situation | Problem | Better choice |
|---|---|---|
| Value is exactly 0 | is undefined, and tends to | Add a small constant, or model the zero case separately |
| Value is negative | No real logarithm | Work with absolute values, or with a different transform |
| Very small | loses precision in floating point | Use log1p(x) in numerical libraries |
| Many small probabilities multiplied | Products underflow to zero in floating point | Sum the logs instead (for sums of probabilities, the log-sum-exp trick) |
| Plotting values from 1 to a billion | Linear axes hide everything below the top | Log axis |
Common mistakes:
- Log of a sum does not split. is not . Only products split.
- Squares and logs. , but is a different number.
- Mixing bases. Formulas in bits (base 2) and nats (base e) differ by a constant factor of . 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 came later, from the study of area under the curve . Leonhard Euler in the 18th century connected logarithms with exponentials and complex numbers, and made 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 , and how do you know?Show answer
, so . Check by multiplying: , , , which is four factors of 3 in total. The answer is the exponent, not the base or the result.
QuestionCompute and . What general rule does each one illustrate?Show answer
, because the natural log undoes the exponential with base . , because . The rule is , which holds for any base and for negative exponents too.
QuestionWhy does the product rule 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 and , add them to get , 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 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.