TExcelTAD
Home › Basic Math & Pre-Algebra › Logarithm Laws

Logarithm Laws

Because a logarithm is an exponent, every exponent law has a logarithm twin. These laws turn multiplication into addition, division into subtraction and powers into multiplication, which is why logarithms were invented in the first place and why they are still used in code to keep numbers manageable.

The three main laws

For a base bb (b>0b > 0, b≠1b \ne 1) and positive xx, yy:

Law Rule Example (base 10)
Product log⁡b(xy)=log⁡b(x)+log⁡b(y)\log_b(xy) = \log_b(x) + \log_b(y) log⁡(100⋅1000)=2+3=5\log(100 \cdot 1000) = 2 + 3 = 5
Quotient log⁡b(x/y)=log⁡b(x)−log⁡b(y)\log_b(x/y) = \log_b(x) - \log_b(y) log⁡(106/100)=6−2=4\log(10^6 / 100) = 6 - 2 = 4
Power log⁡b(xn)=n⋅log⁡b(x)\log_b(x^n) = n \cdot \log_b(x) log⁡(10002)=2⋅3=6\log(1000^2) = 2 \cdot 3 = 6

Plus the facts from the previous lesson: log⁡b(1)=0\log_b(1) = 0, log⁡b(b)=1\log_b(b) = 1, and log⁡b(bn)=n\log_b(b^n) = n.

Why they hold

Write xx and yy as powers of bb: x=bmx = b^m and y=bny = b^n, so log⁡b(x)=m\log_b(x) = m and log⁡b(y)=n\log_b(y) = n. Then

x⋅y=bm⋅bn=bm+nso log⁡b(xy)=m+n=log⁡b(x)+log⁡b(y)x/y=bm÷bn=bm−nso log⁡b(x/y)=m−nxk=(bm)k=bmkso log⁡b(xk)=k⋅m=k⋅log⁡b(x)\begin{aligned} x \cdot y &= b^m \cdot b^n = b^{m+n} && \text{so } \log_b(xy) = m + n = \log_b(x) + \log_b(y) \\ x / y &= b^m \div b^n = b^{m-n} && \text{so } \log_b(x/y) = m - n \\ x^k &= (b^m)^k = b^{mk} && \text{so } \log_b(x^k) = k \cdot m = k \cdot \log_b(x) \end{aligned}

Each log law is the matching exponent law seen from the other side.

python · try it yourself
import math

x, y = 37.0, 5.5
print(round(math.log2(x * y), 10), "==", round(math.log2(x) + math.log2(y), 10))
print(round(math.log2(x / y), 10), "==", round(math.log2(x) - math.log2(y), 10))
print(round(math.log2(x ** 3), 10), "==", round(3 * math.log2(x), 10))
7.6688849843 == 7.6688849843
2.750021747 == 2.750021747
15.6283600969 == 15.6283600969

The change-of-base formula

Calculators and most languages only offer a few bases (10, 2 and ee). To get any other base, divide:

log⁡b(x)=log⁡(x)log⁡(b)(any common base on the right, as long as it is the same)\log_b(x) = \frac{\log(x)}{\log(b)} \qquad \text{(any common base on the right, as long as it is the same)}

For example, log⁡3(50)=log⁡10(50)log⁡10(3)≈1.698970.47712≈3.561\log_3(50) = \frac{\log_{10}(50)}{\log_{10}(3)} \approx \frac{1.69897}{0.47712} \approx 3.561. This is how math.log(x, base) works internally.

python · try it yourself
import math

print(math.log10(50) / math.log10(3))
print(math.log2(50) / math.log2(3))
print(math.log(50, 3))
print("check: 3 ** log(50, 3) =", round(3 ** math.log(50, 3), 9))
3.5608767950073115
3.560876795007312
3.5608767950073115
check: 3 ** log(50, 3) = 50.0

The tiny difference in the last digit comes from floating-point rounding, not from the math.

Mistakes to avoid

log⁡(x+y)  is NOT  log⁡(x)+log⁡(y)the product law needs a productlog⁡(x)⋅log⁡(y)  is NOT  log⁡(xy)log⁡(x)/log⁡(y)  is NOT  log⁡(x/y)(that is change of base!)\begin{aligned} &\log(x + y) \ \text{ is NOT } \ \log(x) + \log(y) && \text{the product law needs a product} \\ &\log(x) \cdot \log(y) \ \text{ is NOT } \ \log(xy) \\ &\log(x) / \log(y) \ \text{ is NOT } \ \log(x / y) && \text{(that is change of base!)} \end{aligned}

Using the laws

Expand a log into simpler pieces:

log⁡2(8x3)=log⁡2(8)+log⁡2(x3)=3+3⋅log⁡2(x)\log_2(8x^3) = \log_2(8) + \log_2(x^3) = 3 + 3 \cdot \log_2(x)

Combine pieces into a single log:

log⁡(50)+log⁡(2)=log⁡(100)=2log⁡3(54)−log⁡3(2)=log⁡3(27)=3\begin{aligned} \log(50) + \log(2) &= \log(100) = 2 \\ \log_3(54) - \log_3(2) &= \log_3(27) = 3 \end{aligned}

Solve an equation where the unknown is an exponent: 2x=1002^x = 100. Take log of both sides, then use the power law to bring xx down:

log⁡(2x)=log⁡(100)x⋅log⁡(2)=2x=2log⁡(2)≈20.30103≈6.644\begin{aligned} \log(2^x) &= \log(100) \\ x \cdot \log(2) &= 2 \\ x &= \frac{2}{\log(2)} \approx \frac{2}{0.30103} \approx 6.644 \end{aligned}

In software

  • Multiplying many probabilities. A language model scores a sentence by multiplying the probabilities of its tokens, often thousands of numbers smaller than 1. The product underflows to 0.0 in floating point. Using the product law, programs add log-probabilities instead, which stay reasonable. The training loss of LLMs is built on these logs.
  • Solving for time. "When will a value that doubles every month reach a million?" needs an exponent, so it needs a log, as in the 2x=1002^x = 100 example above.
python · try it yourself
import math

probs = [0.01] * 400            # 400 tokens, each with probability 0.01
product = 1.0
for p in probs:
    product *= p
print("plain product:", product)

log_sum = sum(math.log10(p) for p in probs)
print("sum of log10:", log_sum, " -> probability = 10 **", log_sum)
plain product: 0.0
sum of log10: -800.0  -> probability = 10 ** -800.0

The true probability is 10−80010^{-800}, far below the smallest float (about 10−30810^{-308}), but its logarithm, −800, is an ordinary number.

Exercises

Exercise 1: Expand

Expand log⁡2(4y)\log_2(4y) and log⁡(x2/10)\log(x^2/10) as far as possible.

Answer

2+log⁡2(y)2 + \log_2(y) and 2⋅log⁡(x)−12 \cdot \log(x) - 1.

Solution
log⁡2(4y)=log⁡2(4)+log⁡2(y)=2+log⁡2(y)log⁡(x2/10)=log⁡(x2)−log⁡(10)=2⋅log⁡(x)−1\begin{aligned} \log_2(4y) &= \log_2(4) + \log_2(y) = 2 + \log_2(y) \\ \log(x^2/10) &= \log(x^2) - \log(10) = 2 \cdot \log(x) - 1 \end{aligned}

Exercise 2: Combine

Write as a single number: log⁡6(4)+log⁡6(9)\log_6(4) + \log_6(9) and log⁡2(80)−log⁡2(5)\log_2(80) - \log_2(5).

Answer

2 and 4.

Solution
log⁡6(4)+log⁡6(9)=log⁡6(36)=2(62=36)log⁡2(80)−log⁡2(5)=log⁡2(16)=4(24=16)\begin{aligned} \log_6(4) + \log_6(9) &= \log_6(36) = 2 && (6^2 = 36) \\ \log_2(80) - \log_2(5) &= \log_2(16) = 4 && (2^4 = 16) \end{aligned}

Exercise 3: Change of base

Compute log⁡5(200)\log_5(200) using only math.log10, and check the result by raising 5 to it.

Answer

log⁡5(200)≈3.292\log_5(200) \approx 3.292.

Solution

log⁡5(200)=log⁡(200)log⁡(5)≈2.301030.69897≈3.292\log_5(200) = \frac{\log(200)}{\log(5)} \approx \frac{2.30103}{0.69897} \approx 3.292.

python · try it yourself
import math
k = math.log10(200) / math.log10(5)
print(round(k, 3), round(5 ** k, 6))
3.292 200.0

Exercise 4: Doubling time

A user base doubles every month, starting at 1,500 users. After how many months does it first exceed 1,000,000? Solve 1500⋅2x=1 000 0001500 \cdot 2^x = 1\,000\,000 with logs, then round up.

Answer

x≈9.38x \approx 9.38, so after 10 months.

Solution
2x=1 000 000/1500≈666.67x=log⁡(666.67)log⁡(2)≈2.82390.30103≈9.38\begin{aligned} 2^x &= 1\,000\,000 / 1500 \approx 666.67 \\ x &= \frac{\log(666.67)}{\log(2)} \approx \frac{2.8239}{0.30103} \approx 9.38 \end{aligned}

The value is checked only at whole months, so round up to 10. After 9 months there are 1500⋅512=768 0001500 \cdot 512 = 768\,000 users; after 10, 1,536,000.

python · try it yourself
import math
x = math.log(1_000_000 / 1500) / math.log(2)
print(round(x, 2), 1500 * 2**9, 1500 * 2**10)
9.38 768000 1536000

Test yourself

log⁡(a)+log⁡(b)\log(a) + \log(b) equals…

  1. log⁡(a+b)\log(a + b)
  2. log⁡(ab)\log(ab)
  3. log⁡(a)⋅log⁡(b)\log(a) \cdot \log(b)
  4. log⁡(a/b)\log(a/b)

Simplify log⁡3(95)\log_3(9^5).

  1. 10
  2. 45
  3. 5
  4. 14

Why do ML programs add log-probabilities instead of multiplying probabilities?

  1. Addition is more accurate than multiplication in general
  2. Logs make probabilities larger than 1
  3. Products of many small numbers underflow to 0 in floating point
  4. Python cannot multiply floats