All courses
Courses › How LLMs really work · foundation

Exponentials and logarithms

Use $e^x$ and $\log x$, rules like log(ab) = log a + log b, and why logs turn tiny products into sums.

▶ Watch this lesson Start the whole course

What the animation says

Underflow: the silent zero

Score a thousand-word sentence by multiplying a thousand small probabilities, each about one in a hundred. Each factor moves the decimal point two places left. After a thousand of them, you have ten to the minus two thousand. But an ordinary double can't go below about ten to the minus three hundred eight. So the product silently becomes exactly zero. No error, no warning. This is called underflow. Here's a second sentence whose factors are about one in fifty. Which sentence scores higher?

Logs count exponents

Both tie at zero. The fix: store the exponent instead of the number. A logarithm is that exponent, the power you raise the base to. Log base ten of a thousand is three. One hundredth is ten to the minus two, so its log is minus two. And the log of one is zero. Multiplying powers adds exponents. Ten squared times ten cubed is ten to the fifth. So ten to the minus two thousand is stored as plain minus two thousand. What is log base ten of one thousandth?

e and the natural log

One thousandth is ten to the minus three, so its log is minus three. The standard base is e, about two point seven one eight. Plain log means the power you raise e to. e to any power is positive, so log only accepts positive numbers. Log and e to the x undo each other. The key rule: the log of a product is the sum of the logs. Dividing subtracts, and a power multiplies. Other bases just rescale. Now tap the expression that has no log.

Multiply by adding

Let's multiply by adding. A half, times a quarter, times a tenth, is zero point zero one two five. Take the natural log of each factor. All three are negative, because each factor is below one. Add them and you get about minus four point three eight two. e to that power gives back zero point zero one two five. Now the thousand factors of one hundredth. Each log is about minus four point six, so the sum is about minus four thousand six hundred five. No underflow. One sentence has a log of minus four thousand six hundred five, another minus five thousand. Which product is bigger?

Remember this

Minus four thousand six hundred five is bigger, so that product wins. Log keeps the order. The log of x is the power you raise e to, to get x. And e to the x undoes it. Because the log of a product is the sum of the logs, many tiny factors become a sum of modest negative numbers. That sum is safe to store, add up and compare. That's why language models work in log probabilities.

Why can't a computer multiply tiny numbers?

Say you score a 1,000-word sentence by multiplying 1,000 small fractions, each about 0.01. The answer is about $10^{-2000}$. A computer's ordinary numbers can't go below about $10^{-308}$, so the result silently becomes 0. This is called **underflow**. Now every long sentence ties at 0, and you can't compare them. Language models face exactly this problem. How do we keep such numbers usable? With logarithms.

Logs count exponents

A log answers one question: *what power must I raise the base to, to get this number?* In base 10, $\log_{10} 1000 = 3$ because $10^3=1000$. Also $\log_{10} 0.01 = -2$, because a negative power means dividing: $10^{-2}=1/10^2=0.01$. Multiplying powers adds their exponents: $10^2 \times 10^3 = 10^5$. So in log-land, multiplying becomes adding. The tiny $10^{-2000}$ becomes just $-2000$, an ordinary number. Numbers below 1 get negative logs, and the log of 1 is 0 (since $10^0=1$).

$e^{x}$ and log x, precisely

The standard base is $e \approx 2.718$. $e^x$ means $e$ raised to the power $x$, and it is always positive. The natural log $\log x$ is its inverse, defined only for $x>0$: $\log(e^x)=x$ and $e^{\log x}=x$. The rules: $$\log(ab)=\log a+\log b$$ $$\log(a/b)=\log a-\log b,\quad \log(a^n)=n\log a$$ The mirror rule is $e^{a+b}=e^a e^b$. Other bases only rescale: $\log_{10}x=\log x/\log 10$.

Multiply by adding

Take $0.5\times0.25\times0.1=0.0125$. In logs: $\log 0.5\approx-0.693$, $\log 0.25\approx-1.386$, $\log 0.1\approx-2.303$. Their sum is $-4.382$. Going back, $e^{-4.382}\approx0.0125$ ✓. Now the 1,000 factors of 0.01: $\log 0.01\approx-4.605$, so the log of the product is $1000\times(-4.605)=-4605$. This is the base-10 answer $-2000$ from earlier, rescaled to base $e$: $-2000\times\log 10\approx-2000\times2.303\approx-4605$. No underflow. A bigger (less negative) log still means a bigger product, so comparisons still work.

Remember this

$\log x$ is the power you raise $e$ to in order to get $x$, and $e^x$ undoes it. Because $\log(ab)=\log a+\log b$, a product of many tiny numbers becomes a sum of modest negative numbers. That sum is safe to store, add up and compare.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.