TExcelTAD
Home › Basic Math & Pre-Algebra › What a Logarithm Is

What a Logarithm Is

A logarithm answers the question "which exponent do I need?" If 2k=322^k = 32, then kk is the logarithm of 32 in base 2, and k=5k = 5. Logarithms undo powers the way subtraction undoes addition, and they turn up in programming every time something is halved or doubled over and over.

Exponent in, exponent out

A power takes a base and an exponent and gives a result. A logarithm takes the base and the result and gives back the exponent:

25=32  ⟺  log⁡2(32)=5103=1000  ⟺  log⁡10(1000)=334=81  ⟺  log⁡3(81)=45−1=15  ⟺  log⁡5(15)=−1\begin{aligned} 2^5 = 32 &\iff \log_2(32) = 5 \\ 10^3 = 1000 &\iff \log_{10}(1000) = 3 \\ 3^4 = 81 &\iff \log_3(81) = 4 \\ 5^{-1} = \frac{1}{5} &\iff \log_5\left(\frac{1}{5}\right) = -1 \end{aligned}

Read log⁡2(32)\log_2(32) as "log base 2 of 32". The general definition:

log⁡b(x)=ymeansby=x(b>0, b≠1, x>0)\log_b(x) = y \quad \text{means} \quad b^y = x \qquad (b > 0,\ b \ne 1,\ x > 0)

So to evaluate a log, ask: "bb to what gives xx?"

Question Answer
log⁡2(8)\log_2(8) 2 to what is 8? → 3
log⁡10(100)\log_{10}(100) 10 to what is 100? → 2
log⁡7(7)\log_7(7) 7 to what is 7? → 1
log⁡9(1)\log_9(1) 9 to what is 1? → 0
log⁡2(14)\log_2\left(\frac{1}{4}\right) 2 to what is 14\frac{1}{4}? → −2
log⁡4(2)\log_4(2) 4 to what is 2? → 12\frac{1}{2} (because 4=2\sqrt{4} = 2)

Two facts appear in every table: log⁡b(b)=1\log_b(b) = 1 and log⁡b(1)=0\log_b(1) = 0, for any base bb.

Logarithms in Python

The math module has a general log(x, base) plus dedicated, more accurate versions for the common bases:

python · try it yourself
import math

print("log2(32)     =", math.log2(32))
print("log10(1000)  =", math.log10(1000))
print("log(81, 3)   =", math.log(81, 3))
print("log2(0.25)   =", math.log2(0.25))
print("log2(1000)   =", math.log2(1000))
print("check: 2 **", round(math.log2(1000), 4), "=", round(2 ** math.log2(1000), 6))
log2(32)     = 5.0
log10(1000)  = 3.0
log(81, 3)   = 4.0
log2(0.25)   = -2.0
log2(1000)   = 9.965784284662087
check: 2 ** 9.9658 = 1000.0

Most logarithms are not whole numbers. log⁡2(1000)≈9.97\log_2(1000) \approx 9.97 because 1000 lies just under 210=10242^{10} = 1024. You can always check a log by raising the base back up.

Note: You can only take the log of a positive number. byb^y is positive for every yy, so no exponent gives 0 or a negative result. math.log(0) raises ValueError: math domain error.

Logs as "how many times"

For a whole-number answer, log⁡2(n)\log_2(n) counts how many times you can halve nn before reaching 1 (or how many doublings take 1 up to nn). That is the logarithm's most useful meaning in computing:

python · try it yourself
import math

def halvings(n):
    steps = 0
    while n > 1:
        n = n / 2
        steps += 1
    return steps

for n in [8, 64, 1000, 1_000_000]:
    print(f"{n:>9,}: halvings = {halvings(n):2d}, log2 = {math.log2(n):6.2f}")
        8: halvings =  3, log2 =   3.00
       64: halvings =  6, log2 =   6.00
    1,000: halvings = 10, log2 =   9.97
1,000,000: halvings = 20, log2 =  19.93

When nn is not a power of two, the step count is log⁡2(n)\log_2(n) rounded up.

In software

  • Binary search checks the middle of a sorted list and throws half away each step. A million sorted items need at most about log⁡2(1,000,000)≈20\log_2(1{,}000{,}000) \approx 20 comparisons, a billion about 30. See Binary Search.
  • Bits needed: to store nn different values you need ⌈log⁡2(n)⌉\lceil \log_2(n) \rceil bits. 256 values → 8 bits; 1000 values → 10 bits.
  • Tree height: a balanced binary tree with nn nodes is about log⁡2(n)\log_2(n) levels deep, which is why balanced search trees stay fast. Discrete Mathematics analyses such algorithms in depth.
  • Number of digits: a positive whole number nn has ⌊log⁡10(n)⌋+1\lfloor \log_{10}(n) \rfloor + 1 decimal digits. 12,345→⌊4.09⌋+1=512{,}345 \to \lfloor 4.09 \rfloor + 1 = 5.

Exercises

Exercise 1: Evaluate by hand

Find log⁡2(64)\log_2(64), log⁡10(10,000)\log_{10}(10{,}000), log⁡5(125)\log_5(125), log⁡3(1)\log_3(1) and log⁡2(18)\log_2\left(\frac{1}{8}\right).

Answer

6, 4, 3, 0, −3.

Solution
log⁡2(64):26=64→6log⁡10(10000):104=10000→4log⁡5(125):53=125→3log⁡3(1):30=1→0log⁡2(18):2−3=18→−3\begin{aligned} &\log_2(64){:} && 2^6 = 64 && \to 6 \\ &\log_{10}(10000){:} && 10^4 = 10000 && \to 4 \\ &\log_5(125){:} && 5^3 = 125 && \to 3 \\ &\log_3(1){:} && 3^0 = 1 && \to 0 \\ &\log_2\left(\tfrac{1}{8}\right){:} && 2^{-3} = \tfrac{1}{8} && \to -3 \end{aligned}

Exercise 2: Rewrite

Rewrite as a logarithm: 62=366^2 = 36 and 10−2=0.0110^{-2} = 0.01. Rewrite as a power: log⁡4(64)=3\log_4(64) = 3.

Answer

log⁡6(36)=2\log_6(36) = 2, log⁡10(0.01)=−2\log_{10}(0.01) = -2, and 43=644^3 = 64.

Solution

In by=xb^y = x the base stays the base, the exponent yy becomes the log's value and xx goes inside: by=x  ⟺  log⁡b(x)=yb^y = x \iff \log_b(x) = y.

Exercise 3: Between which whole numbers?

Without a calculator, between which two whole numbers is log⁡2(300)\log_2(300)? Check with Python.

Answer

Between 8 and 9 (it is about 8.23).

Solution

28=256<300<512=292^8 = 256 < 300 < 512 = 2^9, so 8<log⁡2(300)<98 < \log_2(300) < 9.

python · try it yourself
import math
print(round(math.log2(300), 2))
8.23

Exercise 4: Binary search budget

A sorted list holds 50,000 user IDs. What is the largest number of halving steps binary search needs? Write a loop that counts it.

Answer

16 steps (log⁡2(50,000)≈15.6\log_2(50{,}000) \approx 15.6, rounded up).

Solution

215=32,768<50,000≤65,536=2162^{15} = 32{,}768 < 50{,}000 \le 65{,}536 = 2^{16}, so 16 halvings bring 50,000 down to 1.

python · try it yourself
n, steps = 50_000, 0
while n > 1:
    n = (n + 1) // 2      # the larger half, worst case
    steps += 1
print(steps)
16

Test yourself

What is log⁡3(9)\log_3(9)?

  1. 3
  2. 2
  3. 27
  4. 12\frac{1}{2}

log⁡b(x)=y\log_b(x) = y means…

  1. xy=bx^y = b
  2. yb=xy^b = x
  3. by=xb^y = x
  4. b⋅y=xb \cdot y = x

About how many steps does binary search need on 1,000,000 sorted items?

  1. 20
  2. 1,000
  3. 500,000
  4. 6