TExcelTAD
Home › Basic Math & Pre-Algebra › Common and Natural Logs

Common and Natural Logs

Two logarithm bases get special names. The common log uses base 10 and fits our decimal number system. The natural log uses the strange-looking base e≈2.71828e \approx 2.71828, which turns out to be the natural base for growth, interest and calculus. Programmers meet both, plus base 2, and must watch which one a function means.

Three bases, three names

Base Name Math notation Python
10 common log log⁡(x)\log(x) or log⁡10(x)\log_{10}(x) math.log10(x)
e≈2.71828e \approx 2.71828 natural log ln⁡(x)\ln(x) math.log(x)
2 binary log log⁡2(x)\log_2(x) or lg⁡(x)\operatorname{lg}(x) math.log2(x)

Note: In Python, NumPy, JavaScript (Math.log), C and Excel's LN, plain log means the natural log. On a school calculator log usually means base 10. When a formula says "log" without a base, check which one it means.

The common log counts digits

log⁡10\log_{10} tells you how many powers of ten fit into a number. For a positive whole number, the integer part of its common log is one less than its digit count:

python · try it yourself
import math

for n in [7, 42, 999, 1000, 123_456, 10**12]:
    lg = math.log10(n)
    print(f"{n:>14,}  log10 = {lg:7.4f}  digits = {math.floor(lg) + 1}")
             7  log10 =  0.8451  digits = 1
            42  log10 =  1.6232  digits = 2
           999  log10 =  2.9996  digits = 3
         1,000  log10 =  3.0000  digits = 4
       123,456  log10 =  5.0915  digits = 6
1,000,000,000,000  log10 = 12.0000  digits = 13

Tip: For very large integers, len(str(n)) is the safe way to count digits in code: math.log10 works with floats and can be off by one right at powers of ten for huge values.

Where ee comes from

Imagine 1 unit of money growing at 100% interest per year. Paid once a year you end with 2. Split it into nn smaller payments and you get (1+1/n)n(1 + 1/n)^n. More frequent compounding helps, but less and less, and the result creeps toward a limit:

python · try it yourself
import math

for n in [1, 2, 12, 365, 10_000, 1_000_000]:
    print(f"n = {n:>9,}:  (1 + 1/n)^n = {(1 + 1 / n) ** n:.6f}")
print("math.e              =", round(math.e, 6))
n =         1:  (1 + 1/n)^n = 2.000000
n =         2:  (1 + 1/n)^n = 2.250000
n =        12:  (1 + 1/n)^n = 2.613035
n =       365:  (1 + 1/n)^n = 2.714567
n =    10,000:  (1 + 1/n)^n = 2.718146
n = 1,000,000:  (1 + 1/n)^n = 2.718280
math.e              = 2.718282

That limit is e=2.718281828…e = 2.718281828\ldots, an irrational number like π\pi. Continuous compounding at rate rr for time tt multiplies money by erte^{rt}, which links ee to compound interest. In calculus, exe^x is the one function that is its own slope, which is why it is "natural".

ln⁡\ln undoes exe^x

The natural log is the exponent you need on ee:

ln⁡(x)=ymeansey=xln⁡(e)=1,ln⁡(1)=0,ln⁡(ex)=x,eln⁡x=x\begin{aligned} &\ln(x) = y \quad \text{means} \quad e^y = x \\ &\ln(e) = 1, \quad \ln(1) = 0, \quad \ln(e^x) = x, \quad e^{\ln x} = x \end{aligned}

All log laws hold for ln⁡\ln too. The change-of-base formula connects the three bases: log⁡2(x)=ln⁡(x)ln⁡(2)\log_2(x) = \frac{\ln(x)}{\ln(2)} and log⁡10(x)=ln⁡(x)ln⁡(10)\log_{10}(x) = \frac{\ln(x)}{\ln(10)}.

A common use is solving for time in growth problems. How long does money take to double at 5% continuous growth? Solve e0.05t=2e^{0.05t} = 2:

0.05 t=ln⁡(2)t=ln⁡(2)0.05≈0.69310.05≈13.86 years\begin{aligned} 0.05\,t &= \ln(2) \\ t &= \frac{\ln(2)}{0.05} \approx \frac{0.6931}{0.05} \approx 13.86 \text{ years} \end{aligned}

This gives the rule of 70: doubling time≈70/(rate in percent)\text{doubling time} \approx 70 / (\text{rate in percent}), since ln⁡(2)≈0.693\ln(2) \approx 0.693.

python · try it yourself
import math

print("ln(e)  =", math.log(math.e))
print("ln(2)  =", round(math.log(2), 4))
print("exp(ln 5) =", round(math.exp(math.log(5)), 10))
print("log2(80) via ln:", round(math.log(80) / math.log(2), 4), " direct:", round(math.log2(80), 4))

for rate in [2, 5, 7, 10]:
    exact = math.log(2) / (rate / 100)
    print(f"{rate:2d}%: doubling {exact:5.2f} years, rule of 70 says {70 / rate:5.2f}")
ln(e)  = 1.0
ln(2)  = 0.6931
exp(ln 5) = 5.0
log2(80) via ln: 6.3219  direct: 6.3219
 2%: doubling 34.66 years, rule of 70 says 35.00
 5%: doubling 13.86 years, rule of 70 says 14.00
 7%: doubling  9.90 years, rule of 70 says 10.00
10%: doubling  6.93 years, rule of 70 says  7.00

In software

  • Machine learning uses ln⁡\ln everywhere: cross-entropy loss, log-likelihood, the softmax function (built on exe^x) and the sigmoid 11+e−x\frac{1}{1 + e^{-x}}. See Logistic Regression.
  • Information: base-2 logs measure information in bits; base-ee logs measure it in "nats". Same idea, different unit.
  • Exponential decay in games and UI (smooth camera follow, fading sounds) uses e−kte^{-kt} so that the result is the same at any frame rate (see damping).
  • API naming: NumPy has np.log, np.log10, np.log2 and np.log1p (ln⁡(1+x)\ln(1 + x), accurate for tiny xx).

Exercises

Exercise 1: Exact values

Without a calculator: log⁡10(0.001)\log_{10}(0.001), ln⁡(e4)\ln(e^4), ln⁡(1)\ln(1), eln⁡9e^{\ln 9}.

Answer

−3, 4, 0, 9.

Solution
log⁡10(0.001)=log⁡10(10−3)=−3ln⁡(e4)=4ln⁡ undoes exln⁡(1)=0e0=1eln⁡9=9ex undoes ln⁡\begin{aligned} \log_{10}(0.001) &= \log_{10}(10^{-3}) = -3 \\ \ln(e^4) &= 4 && \ln \text{ undoes } e^x \\ \ln(1) &= 0 && e^0 = 1 \\ e^{\ln 9} &= 9 && e^x \text{ undoes } \ln \end{aligned}

Exercise 2: Digit count

Using log⁡10\log_{10}, how many digits does 21002^{100} have? Check with len(str(...)).

Answer

31 digits.

Solution

log⁡10(2100)=100⋅log⁡10(2)≈100⋅0.30103=30.103\log_{10}(2^{100}) = 100 \cdot \log_{10}(2) \approx 100 \cdot 0.30103 = 30.103. The integer part is 30, so there are 31 digits.

python · try it yourself
import math
print(math.floor(100 * math.log10(2)) + 1, len(str(2 ** 100)))
31 31

Exercise 3: Doubling time

A cloud bill grows 3% per month (continuously). Estimate the doubling time with the rule of 70, then compute it exactly with ln⁡\ln.

Answer

Rule of 70: about 23.3 months. Exact: ln⁡(2)/0.03≈23.1\ln(2) / 0.03 \approx 23.1 months.

Solution
rule of 70:70/3≈23.3exact:e0.03 t=2 ⇒ t=ln⁡(2)0.03≈0.693150.03≈23.1\begin{aligned} \text{rule of 70:} &\quad 70 / 3 \approx 23.3 \\ \text{exact:} &\quad e^{0.03\,t} = 2 \ \Rightarrow\ t = \frac{\ln(2)}{0.03} \approx \frac{0.69315}{0.03} \approx 23.1 \end{aligned}

Exercise 4: Which log?

A colleague writes math.log(1000) expecting 3. What do they get, and what should they write?

Answer

About 6.9078 (the natural log). They should write math.log10(1000) or math.log(1000, 10).

Solution

In Python math.log with one argument is ln⁡\ln: ln⁡(1000)=3⋅ln⁡(10)≈3⋅2.302585≈6.9078\ln(1000) = 3 \cdot \ln(10) \approx 3 \cdot 2.302585 \approx 6.9078.

python · try it yourself
import math
print(round(math.log(1000), 4), math.log10(1000))
6.9078 3.0

Test yourself

In Python, what does math.log(x) with one argument compute?

  1. log base 10
  2. log base 2
  3. natural log (base ee)
  4. it raises an error

What is ln⁡(e7)\ln(e^7)?

  1. 7
  2. e7e^7
  3. 7e7e
  4. 1

Using the rule of 70, about how long does something growing 10% a year take to double?

  1. 10 years
  2. 7 years
  3. 70 years
  4. 0.7 years