TExcelTAD
Home › Calculus Essentials › Exponential and Logarithmic Functions

Exponential and Logarithmic Functions

Exponential functions describe quantities that grow or shrink by the same factor in every time step: compound interest, population growth, radioactive decay, cooling coffee. Logarithms are their inverses and answer "how many steps does it take?". Both are central to calculus and to machine learning.

Exponential functions

An exponential function has the variable in the exponent:

f(x)=a⋅bxb>0, b≠1, a≠0f(x) = a \cdot b^x \qquad b > 0,\ b \ne 1,\ a \ne 0
Base Behaviour Example
b>1b > 1 growth: increases faster and faster 2x2^x doubles every step
0<b<10 < b < 1 decay: shrinks toward 0 0.5x0.5^x halves every step

For any base: the domain is all reals, bx>0b^x > 0 always (the graph never touches the xx-axis, which is a horizontal asymptote), and b0=1b^0 = 1, so the graph passes through (0,1)(0, 1).

Exponential growth eventually beats every polynomial, however large its degree:

python · try it yourself
for x in [5, 10, 20, 30, 40]:
    print(f"x = {x:2}:  x^5 = {x**5:>12,}   2^x = {2**x:>16,}")
x =  5:  x^5 =        3,125   2^x =               32
x = 10:  x^5 =      100,000   2^x =            1,024
x = 20:  x^5 =    3,200,000   2^x =        1,048,576
x = 30:  x^5 =   24,300,000   2^x =    1,073,741,824
x = 40:  x^5 =  102,400,000   2^x = 1,099,511,627,776

x5x^5 leads at first, but 2x2^x overtakes it shortly before x=23x = 23 and then leaves it far behind. That is why an algorithm with running time O(2n)O(2^n) is hopeless for large nn even when O(n5)O(n^5) is still workable.

The number e

One base is special in calculus: e≈2.718281828e \approx 2.718281828. It appears when growth is compounded continuously:

e=lim⁡n→∞(1+1n)ne = \lim_{n \to \infty} \left(1 + \frac{1}{n}\right)^n
python · try it yourself
import math

for n in [1, 10, 100, 10_000, 1_000_000]:
    print(f"n = {n:>9,}: (1 + 1/n)^n = {(1 + 1/n)**n:.8f}")
print(f"math.e            = {math.e:.8f}")
n =         1: (1 + 1/n)^n = 2.00000000
n =        10: (1 + 1/n)^n = 2.59374246
n =       100: (1 + 1/n)^n = 2.70481383
n =    10,000: (1 + 1/n)^n = 2.71814593
n = 1,000,000: (1 + 1/n)^n = 2.71828047
math.e            = 2.71828183

What makes exe^x special is that its slope at every point equals its own value: the derivative of exe^x is exe^x. You will prove this in Derivatives of Exponentials and Logs. Any exponential can be rewritten with base ee, since bx=exln⁡bb^x = e^{x \ln b}.

Logarithms

The logarithm base bb is the inverse of bxb^x:

y=log⁡b(x)  ⟺  by=x(x>0)y = \log_b(x) \iff b^y = x \qquad (x > 0)

log⁡28=3\log_2 8 = 3 because 23=82^3 = 8. The natural logarithm ln⁡x\ln x uses base ee; log⁡10\log_{10} is the common log. In Python, math.log(x) is ln⁡\ln, and math.log2 and math.log10 give the other two.

Law Formula
product log⁡(xy)=log⁡x+log⁡y\log(xy) = \log x + \log y
quotient log⁡(x/y)=log⁡x−log⁡y\log(x/y) = \log x - \log y
power log⁡(xp)=plog⁡x\log(x^p) = p \log x
change of base log⁡bx=ln⁡x/ln⁡b\log_b x = \ln x / \ln b
inverse ln⁡(ex)=x\ln(e^x) = x, eln⁡x=xe^{\ln x} = x

Logs turn multiplication into addition. That is why the graph of ln⁡x\ln x grows so slowly: going from 1 to 1,000,000 only raises ln⁡\ln from 0 to about 13.8.

Solving with logs

How long until an investment growing 6% a year doubles?

1.06t=2  ⇒  tln⁡1.06=ln⁡2  ⇒  t=ln⁡2ln⁡1.06≈11.9 years1.06^t = 2 \;\Rightarrow\; t \ln 1.06 = \ln 2 \;\Rightarrow\; t = \frac{\ln 2}{\ln 1.06} \approx 11.9 \text{ years}

Growth and decay models

N(t)=N0ektk>0 growth, k<0 decayN(t) = N_0 e^{kt} \qquad k > 0 \text{ growth},\ k < 0 \text{ decay}

With a half-life TT, the decay constant is k=−ln⁡2/Tk = -\ln 2 / T:

python · try it yourself
import numpy as np

N0, half_life = 800.0, 5.0            # grams, years
k = -np.log(2) / half_life
for t in [0, 5, 10, 15, 20]:
    print(f"t = {t:2} years: {N0 * np.exp(k * t):6.1f} g")

t_10g = np.log(10 / N0) / k
print(f"down to 10 g after {t_10g:.2f} years")
t =  0 years:  800.0 g
t =  5 years:  400.0 g
t = 10 years:  200.0 g
t = 15 years:  100.0 g
t = 20 years:   50.0 g
down to 10 g after 31.61 years

In software: softmax and log-loss

A classifier produces raw scores (logits) z1,…,znz_1, \ldots, z_n. Softmax turns them into probabilities with the exponential, which makes every value positive and exaggerates differences:

softmax⁡(z)i=ezi∑jezj\operatorname{softmax}(z)_i = \frac{e^{z_i}}{\sum_j e^{z_j}}

Training then minimises the log-loss (cross-entropy) −ln⁡(p)-\ln(p), where pp is the probability the model gave to the correct class. A confident correct answer (pp near 1) costs almost nothing; a confident wrong one costs a lot.

python · try it yourself
import numpy as np

def softmax(z):
    z = np.asarray(z, dtype=float)
    e = np.exp(z - z.max())          # subtract the max: same result, no overflow
    return e / e.sum()

logits = [2.0, 1.0, 0.1]
p = softmax(logits)
print("probabilities:", np.round(p, 4), " sum:", round(p.sum(), 6))
print("loss if class 0 is correct:", round(-np.log(p[0]), 4))
print("loss if class 2 is correct:", round(-np.log(p[2]), 4))
print("huge logits still fine:", np.round(softmax([1000, 999, 998]), 4))
probabilities: [0.659  0.2424 0.0986]  sum: 1.0
loss if class 0 is correct: 0.417
loss if class 2 is correct: 2.317
huge logits still fine: [0.6652 0.2447 0.09  ]

Subtracting the largest logit works because ez−c/∑ezj−c=ez/∑ezje^{z - c} / \sum e^{z_j - c} = e^z / \sum e^{z_j}: the factor e−ce^{-c} cancels. Without it, np.exp(1000) overflows to infinity. LLMs apply softmax over vocabularies of 100,000+ tokens at every step (Sampling).

Logs show up elsewhere in computing too: log⁡2n\log_2 n is the number of halvings in binary search, the bits needed to store nn values, and the height of a balanced tree.

Exercises

Exercise 1: Evaluate logs

Without a calculator, find log⁡232\log_2 32, log⁡100.001\log_{10} 0.001, ln⁡(e5)\ln(e^5) and log⁡31\log_3 1.

Answer

5, −3, 5, 0.

Solution
25=32  ⇒  log⁡232=510−3=0.001  ⇒  log⁡100.001=−3ln⁡ and e undo each other  ⇒  ln⁡(e5)=530=1  ⇒  log⁡31=0\begin{aligned} 2^5 = 32 &\;\Rightarrow\; \log_2 32 = 5 \\ 10^{-3} = 0.001 &\;\Rightarrow\; \log_{10} 0.001 = -3 \\ \ln \text{ and } e \text{ undo each other} &\;\Rightarrow\; \ln(e^5) = 5 \\ 3^0 = 1 &\;\Rightarrow\; \log_3 1 = 0 \end{aligned}

Exercise 2: Simplify with log laws

Write ln⁡(x3y/z2)\ln(x^3 \sqrt{y} / z^2) as a sum and difference of simple logs.

Answer

3ln⁡x+12ln⁡y−2ln⁡z3\ln x + \frac{1}{2}\ln y - 2\ln z

Solution
ln⁡(x3y/z2)=ln⁡(x3)+ln⁡(y1/2)−ln⁡(z2)=3ln⁡x+12ln⁡y−2ln⁡z\begin{aligned} \ln(x^3 \sqrt{y} / z^2) &= \ln(x^3) + \ln(y^{1/2}) - \ln(z^2) \\ &= 3\ln x + \tfrac{1}{2}\ln y - 2\ln z \end{aligned}
python · try it yourself
import sympy as sp

x, y, z = sp.symbols('x y z', positive=True)
print(sp.expand_log(sp.log(x**3 * sp.sqrt(y) / z**2)))
3*log(x) + log(y)/2 - 2*log(z)

Exercise 3: Solve an exponential equation

Solve 5e0.2t=405e^{0.2t} = 40.

Answer

t=5ln⁡8≈10.397t = 5\ln 8 \approx 10.397

Solution
e0.2t=8  ⇒  0.2t=ln⁡8  ⇒  t=ln⁡80.2=5ln⁡8e^{0.2t} = 8 \;\Rightarrow\; 0.2t = \ln 8 \;\Rightarrow\; t = \frac{\ln 8}{0.2} = 5\ln 8
python · try it yourself
import math

t = 5 * math.log(8)
print(round(t, 3), round(5 * math.exp(0.2 * t), 6))
10.397 40.0

Exercise 4: Binary search steps

Binary search halves the range each step. How many steps does it need, at most, for a sorted list of 1,000,000 items? Use log⁡2\log_2.

Answer

20 steps.

Solution

After kk halvings a range of nn items has n/2kn / 2^k left. We need 2k≥n2^k \ge n, so k=⌈log⁡2n⌉k = \lceil \log_2 n \rceil.

python · try it yourself
import math

n = 1_000_000
print(math.log2(n), math.ceil(math.log2(n)))
19.931568569324174 20

Exercise 5: Softmax by hand

Compute softmax of the logits [0,ln⁡3][0, \ln 3] exactly.

Answer

[14,34]\left[\frac{1}{4}, \frac{3}{4}\right]

Solution
e0=1,eln⁡3=3,sum=4softmax⁡=[14,34]\begin{aligned} &e^0 = 1, \quad e^{\ln 3} = 3, \quad \text{sum} = 4 \\ &\operatorname{softmax} = \left[\tfrac{1}{4}, \tfrac{3}{4}\right] \end{aligned}

The difference of ln⁡3\ln 3 between the logits means the second class is 3 times as likely as the first.

Test yourself

Which expression equals log⁡210\log_2 10?

  1. ln⁡2/ln⁡10\ln 2 / \ln 10
  2. ln⁡10/ln⁡2\ln 10 / \ln 2
  3. ln⁡10−ln⁡2\ln 10 - \ln 2
  4. 10/210 / 2

Why does a stable softmax implementation subtract max⁡(z)\max(z) from every logit first?

  1. To make the probabilities sum to 1
  2. To avoid overflow in exp; the result is mathematically identical
  3. To make all probabilities equal
  4. Because exp only accepts negative numbers

A substance has a half-life of 3 hours. What fraction is left after 12 hours?

  1. 1/41/4
  2. 1/81/8
  3. 1/121/12
  4. 1/161/16