A formula that cannot go negative returning negative numbers sounds like a broken theorem. The theorem is fine, and the computer is doing exactly what it was built to do: approximate. The gap between an exact mathematical object and its finite-precision implementation is where the surprise lives, and learning to read that gap is a transferable skill.
The trending query The KL Divergence Is Never Negative, Except in PyTorch reached the Hacker News front page in late November 2026, which is why it earns a closer read here. The post underneath the spike is a numerical-computing lesson disguised as a library quirk. The durable content is how to reason when exact mathematics meets approximate machines.
What KL divergence measures
KL divergence, short for Kullback-Leibler divergence, compares two probability distributions over the same outcomes. In words: for each outcome, take the probability under the first distribution, divide by the probability under the second, take the logarithm, and average that quantity weighted by the first distribution's probabilities. When the two distributions match everywhere, every term is the logarithm of 1, and the result is exactly zero.
In exact arithmetic the quantity can never be negative. The weighted average is an expectation of a logarithm, and a classical result called Jensen's inequality pins such expectations at zero or above. That is a theorem about real numbers computed with infinite precision, and its proof leaves no loophole.
Where negative numbers sneak in
Computers store probabilities as floating-point numbers, finite approximations with a fixed number of digits, and the smallest nonzero double-precision value sits near 10 to the power of minus 308. Probabilities below that underflow to zero, and logarithms of surviving tiny values behave badly: a ratio of two near-zero quantities can land just below 1, whose logarithm is a hair under zero.
Reordering makes it worse. Floating-point addition is approximate and order-dependent, so summing many tiny terms in a different sequence changes the final digits. Feed a distribution to itself, where the true answer is exactly zero, and these roundings can conspire to return something like minus 0.0000001, a value the theorem forbids and the arithmetic permits.
Drop in your PDF - it doesn't leave your device.
Every miss becomes a red node on your map.
Free plan: 21 nodes, no card, no countdown.
Reading numerical libraries carefully
Numerical libraries make trade-offs for speed, and an implementation may rely on approximations that hold only under assumptions the caller is expected to know: inputs kept away from zero, arguments passed in log space, distributions normalized first. When an impossible value appears, the artifact usually points at one of those assumptions, and checking the documented requirements settles it quickly.
The habit generalizes past this one formula. Validate numerical outputs against mathematical expectations, and treat an impossible value as a bug report naming the part of the computation to inspect. A probability above 1 or a determinant of the wrong sign carries the same message: the exact object and its floating-point shadow have parted company somewhere specific.
| Symptom | Likely cause | First response |
|---|---|---|
| Tiny negative KL | Rounding while the true value sits near zero | Check the true value's size and the sum's precision |
| NaN in the loss | Logarithm of an underflowed zero | Work in log space, keep inputs off zero |
| Value far from theory | Approximation used outside its assumptions | Re-read the documented input requirements |
Key takeaways
- KL divergence compares two probability distributions and is mathematically zero or positive, with equality exactly when they match.
- Floating-point storage is finite: near-zero probabilities, underflow near 10 to the minus 308, and order-dependent sums all introduce rounding.
- A computed negative KL usually means the true value is extremely close to zero and rounding tipped the total under.
- Numerical libraries trade speed for assumptions, so callers need to know each routine's documented input conditions.
- Validate outputs against mathematical expectations, and read an impossible value as a bug report about precision.
Quick answers
What is KL divergence?
A measure of how one probability distribution differs from another, computed as a weighted average of logarithmic ratios.
Why is it never negative?
Jensen's inequality forces the expectation of the log ratio to zero or above in exact arithmetic.
Why can a library output a negative value?
Rounding in near-zero terms and the reordering of sums can tip a result whose true value sits essentially at zero.
Is the mathematics wrong?
No. The theorem stands, and the computed value is an approximation carrying its own rounding error.
What should I do about a negative KL?
Treat it as a bug report: check the true value's size, the input assumptions, and the precision of the sum.
One thing to do tonight
Compute KL divergence between a distribution and itself in your favourite tool, with exact fractions first and then floats, and compare the two answers. Seeing the gap on your own screen makes every warning above concrete.