A logarithm answers one question: what power do I raise this base to? It turns multiplication into addition and squashes enormous ranges into readable ones — which is why it appears in every loss function you will meet.
Controls
base b2.00
evaluate at x8.0
Product → sum demo
y = log_b(x)
the inverse of an exponential
Live Calculation
log₂(x)
0
ln(x)
0
log₁₀(x)
0
log_b(x)
0
Logarithms
The inverse of raising to a power — and the reason loss functions are full of logs.
Quick Context
A logarithm asks: what exponent turns the base into this number?
Logs and exponentials are the same relationship read in opposite directions. Tick show b^x and you will see the two curves mirror each other across the diagonal.
The Property That Matters Most
A logarithm converts multiplication into addition. Change the two numbers in the product demo and watch both sides stay equal. This is not a curiosity — it is why logs are everywhere in machine learning.
Probabilities multiply. Multiply a thousand numbers below 1 and you get something so small the computer rounds it to zero — numerical underflow. Take logs and that product becomes a sum of manageable negative numbers. Every implementation of maximum likelihood, cross-entropy loss and Naive Bayes relies on this exact substitution.
The Three Bases You Will Meet
Base 2 — information theory. log₂ counts bits: how many yes/no questions to pin something down. Entropy is measured this way.
Base e ≈ 2.71828 — the natural log, written ln. It appears everywhere in calculus because its derivative is simply 1/x. This is the default in ML loss functions.
Base 10 — human-readable scales: decibels, pH, the Richter scale.
The base only rescales the curve — every log is a constant multiple of every other. Watch the three readouts stay in fixed proportion as you slide x.
Log Scales Tame Huge Ranges
Notice how brutally the curve flattens. Going from 1 to 10 climbs as much as going from 10 to 100, or 100 to 1000 — each multiplication by 10 adds a constant amount. Tick log-scale the x axis and those equal jumps become evenly spaced.
That compression is why loss curves are so often plotted on a log axis: it makes an improvement from 0.001 to 0.0001 as visible as one from 1.0 to 0.1.
The Domain Restriction
Logs are only defined for x > 0. As x approaches 0 the curve dives to negative infinity; at 0 and below there is no answer at all, because no power of a positive base ever produces zero or a negative number.
This causes real bugs. Cross-entropy computes −log(p), so a model that confidently predicts probability 0 for the correct answer produces infinite loss and NaNs everywhere. Every framework quietly clamps p away from zero for exactly this reason. Slide x below about 0.1 and watch the value plunge.
The inverse of exponentiation
A logarithm answers one question: what power do I need?
log₂(8) = 3 because 2³ = 8
The base (2 here) is the number being raised; the logarithm is the exponent. Three bases account for nearly everything you will meet:
Base
Written
Used for
2
log₂
Information, bits, algorithm complexity
e ≈ 2.718
ln
Calculus, statistics, machine learning
10
log₁₀
Decibels, pH, orders of magnitude
The natural log is the default in machine learning because its derivative is the tidiest possible: d/dx ln(x) = 1/x. Every log in a loss function is a natural log unless it says otherwise.
The property that does all the work
log(a × b) = log(a) + log(b)
Logarithms turn multiplication into addition. That one identity is why they were invented, and why they are still everywhere in machine learning.
The related rules:
log(a / b) = log(a) − log(b)
log(aⁿ) = n × log(a)
log(1) = 0, for any base
log(0) is undefined and tends to −∞
The practical consequence appears immediately in probability. A model computing the joint probability of 1,000 independent events multiplies 1,000 numbers below 1 — and the result underflows to exactly zero in floating point, destroying the calculation.
0.1 ** 400 # 0.0 -- underflow: the information is gone
import math
sum(math.log(0.1) for _ in range(400)) # -921.03 -- fine
Taking logs converts that product into a sum of 1,000 manageable numbers. This is why every implementation works in log-probabilities: Naive Bayes, hidden Markov models, language models, and the log-likelihood that underlies most of statistics.
Compressing scale
Logarithms squash large ranges into small ones. Data spanning 1 to 1,000,000 spans 0 to 6 after a log₁₀.
That makes them the standard fix for skewed features. Income, city population, website visits and word frequencies all have long right tails, where a few enormous values dominate every distance calculation and every squared error. A log transform pulls the tail in and often turns a curved relationship into a straight one, which is exactly what a linear model wants.
Two practical notes. log(0) is undefined, so use log1p(x) (which computes log(1 + x)) when zeros are present. And remember to invert the transform before reporting numbers — a mean of logged values back-transforms to a geometric mean, not the ordinary one.
Log scales on charts do the same job visually: exponential growth becomes a straight line, and small values stop being invisible next to large ones.
What logs do to numbers
Logs turn multiplication into addition and squash range. Both properties are the reason they show up throughout machine learning.
example_01.pyNumPy
import numpy as np
print("log turns multiplication into addition:")
a, b = 400.0, 25.0
print(" log(a*b) = %.6f" % np.log(a * b))
print(" log(a)+log(b) = %.6f" % (np.log(a) + np.log(b)))
print()
print("and powers into multiplication:")
print(" log(a**7) = %.6f" % np.log(a ** 7))
print(" 7*log(a) = %.6f" % (7 * np.log(a)))
print()
print("it squashes a huge range into a small one:")
for x in (1, 10, 1_000, 1_000_000, 10 ** 12):
print(" %14d -> log10 %5.1f ln %7.3f" % (x, np.log10(x), np.log(x)))
print()
print("which is why likelihoods are summed as logs, not multiplied:")
print("%8s %26s %16s" % ("n", "0.6 multiplied n times", "logs summed"))
for n in (100, 800, 2000, 5000):
probs = np.full(n, 0.6)
print("%8d %26s %16.2f" % (n, probs.prod(), np.log(probs).sum()))
print()
print("past about n=1500 the product jams against the smallest number a float")
print("can hold and stops moving, so every further term is invisible to it.")
print("the sum of logs keeps counting, linearly, forever.")
print()
print("changing base is just a constant factor:")
x = 64.0
print(" log2(64) = %.4f" % np.log2(x))
print(" ln(64)/ln(2) = %.4f" % (np.log(x) / np.log(2)))
Output
Guided tour
Set base 2 and x = 8. The answer is exactly 3 — three doublings from 1 reaches 8.
Keep the base and try x = 16, then 32. Each doubling of x adds exactly 1 to the log.
Slide x toward 0. The curve dives without limit — the underflow trap in one picture.
Change p and q in the product demo and confirm log(p×q) = log(p) + log(q) every time.
Raise the base toward 10. The curve flattens: a larger base needs fewer powers to reach the same number.
In one line
A log is an exponent in disguise. It converts products into sums, compresses vast ranges into readable ones, and is undefined at zero — three facts that between them explain log-likelihood, cross-entropy loss, entropy in bits, and most NaN bugs in training code.
Logs inside loss functions
Cross-entropy, the standard classification loss, is built from logarithms:
loss = −[ y log(p) + (1 − y) log(1 − p) ]
Read what it does for a true label of 1. If the model predicts 0.9, the loss is −log(0.9) = 0.105 — small. If it predicts 0.1, the loss is −log(0.1) = 2.303 — over twenty times larger. And as the prediction approaches 0, the loss grows without bound.
That unbounded growth is the design. A model that is confidently wrong is punished far more than one that is uncertainly wrong, which is precisely the behaviour you want from a probability estimator. Squared error, by contrast, caps the penalty at 1 and gives confidently wrong predictions a gentle nudge.
The other reason logs appear here is calculus: log turns the product of many probabilities into a sum, and derivatives of sums are trivial. Maximising a likelihood becomes maximising a log-likelihood, and the optimum is in the same place because log is monotonic.
Logs in complexity
Algorithm analysis uses log₂, and the intuition is halving. Binary search halves the search space each step, so a list of 1,000,000 takes about 20 comparisons, because 2²⁰ ≈ 1,000,000.
n
log₂(n)
8
3
1,000
~10
1,000,000
~20
1,000,000,000
~30
That table is worth internalising, because it explains why O(log n) algorithms feel instantaneous at any realistic scale, and why O(n log n) sorting is close enough to linear that nobody worries about it.
Questions people ask
What is the difference between log and ln?ln is base e. Plain log means base 10 in mathematics and base e in most programming languages — check the library rather than assuming.
Why is log(0) undefined? No power of any base gives zero; the values tend to −infinity. In code, add a small epsilon or use log1p.
Can I take the log of a negative number? Not with real numbers. Shift the data first if you need to.
When should I log-transform a feature? When it is strongly right-skewed, spans several orders of magnitude, or has a multiplicative relationship with the target.
Does the base matter? For the mathematics, no — changing base multiplies by a constant. For interpretation it does: bits are base 2, nats are base e.
Why do models use log-probabilities? To avoid underflow and to turn products into sums, which are numerically stable and easy to differentiate.
Recap in one screen
A logarithm is the exponent: log₂(8) = 3 because 2³ = 8.
Logs compress skewed data, straighten multiplicative relationships and make charts readable.
Cross-entropy uses log so that confident mistakes are punished without limit.
log₂(n) is "how many halvings", which is why binary search on a million items takes 20 steps.
Recall check
0 of 3
Say the answer out loud before you reveal it — recalling it is what makes it stick, and rereading it is not.
Without scrolling back — what is the one-line takeaway from this module?
A log is an exponent in disguise. It converts products into sums, compresses vast ranges into readable ones, and is undefined at zero — three facts that between them explain log-likelihood, cross-entropy loss, entropy in bits, and most NaN bugs in training code.
What does this module say about “The Property That Matters Most”?
A logarithm converts multiplication into addition . Change the two numbers in the product demo and watch both sides stay equal. This is not a curiosity — it is why logs are everywhere in machine learning.
What does this module say about “The Three Bases You Will Meet”?
The base only rescales the curve — every log is a constant multiple of every other. Watch the three readouts stay in fixed proportion as you slide x.
Cheat sheet
Logarithms
A logarithm answers one question: what power do I raise this base to? It turns multiplication into addition and squashes enormous ranges into readable ones — which is why it appears in every loss function you will meet.
Ashish Jangra builds and maintains VizLearn. Every module here is written and the visualisation behind it hand-built, so the numbers in a readout come from the same code that draws the picture. Corrections are genuinely welcome and get priority over everything else — if a page states something wrong, or an animation misrepresents what the algorithm does, get in touch.