Module 5 · Intermediate · Exponents and logarithms

The number e explained

The number e (2.718...): where it comes from with compound interest, continuous growth, why eˣ is special, and where e appears in ML.

By theprogrammers team · · 6 min read

You've met π: about 3.14, the number that connects a circle's width to its distance around. Mathematics has another famous constant that's just as important for data science, and far less well known: e, about 2.718.

You'll see e everywhere in machine learning formulas: in the sigmoid of logistic regression, in softmax, in the normal distribution's bell curve, in log loss, and in the natural logarithm, ln. Most beginners just accept it as "some number". This lesson shows where it actually comes from, with nothing more than compound interest and a calculator, and why it's the natural choice for describing growth.

What you need first: exponential growth and decay.

Key terms

  • e: a constant, approximately 2.71828, called Euler's number.
  • Compounding: adding interest to the balance so that the interest itself earns interest.
  • Continuous compounding: compounding infinitely often, every instant.
  • Exponential function: eˣ, also written exp(x).
  • Natural logarithm: ln(x), the logarithm with base e.

Where e comes from: a generous bank

Imagine a bank offering 100% interest a year. Put in ₹1, and after a year you have ₹2.

Now ask: what if they paid the interest twice a year, 50% each time? After six months, ₹1.50. Then 50% of that: ₹2.25. Better.

Monthly, 100%/12 each month: 1 × (1 + 1/12)¹² ≈ ₹2.61.

Daily: (1 + 1/365)³⁶⁵ ≈ ₹2.71.

More often keeps paying more. Does it grow for ever? Let's check:

import math

for n in [1, 2, 12, 365, 1_000_000]:
    print(n, round((1 + 1 / n) ** n, 6))
print("e =", math.e)
1 2.0
2 2.25
12 2.613035
365 2.714567
1000000 2.71828
e = 2.718281828459045

The amount levels off. However often the bank compounds, every second or every instant, ₹1 can never grow beyond ₹2.71828… in a year. That limit is e.

import numpy as np
import matplotlib.pyplot as plt

n = np.arange(1, 61)
fig, ax = plt.subplots()
ax.plot(n, (1 + 1 / n) ** n, "o", ms=4, label="(1 + 1/n)ⁿ")
ax.axhline(math.e, color="tab:red", ls="--", label="e ≈ 2.71828")
ax.set_xlabel("Number of times interest is paid per year, n")
ax.set_ylabel("Value of ₹1 after one year")
ax.set_title("Compounding more often approaches e")
ax.legend()
plt.show()
Dots for (1 + 1/n)ⁿ against n from 1 to 60: the first is at 2, the second at 2.25, then the dots rise quickly and flatten, creeping up towards a dashed red horizontal line at e ≈ 2.71828 without crossing it.

Like π, e has decimals that go on for ever without repeating: 2.718281828459045…

Continuous growth: Pe^(rt)

That idea generalises. Growth that happens continuously, every instant rather than in yearly jumps, follows:

A = P × e^(rt)

  • P: the starting amount.
  • r: the continuous growth rate.
  • t: the time.

₹10,000 at 5% compounded continuously for 10 years: 10,000 × e^0.5 ≈ ₹16,487. Yearly compounding gives 10,000 × 1.05¹⁰ ≈ ₹16,289: a little less.

Many natural processes change continuously, not in yearly steps: bacteria multiplying, a population growing, a hot drink cooling, a radioactive sample decaying. That's why e appears throughout science.

What makes eˣ special

Here's the property that makes e the "natural" base. Look at the curve y = eˣ. At any point, its slope (how steep it is) is equal to its height.

  • At x = 0, the height is e⁰ = 1, and the slope is 1.
  • At x = 1, the height is e ≈ 2.718, and the slope is also 2.718.
  • At x = 2, both are e² ≈ 7.389.

You can check it by measuring the slope over a tiny step, using rise over run from slope and intercept:

h = 0.000001
for x in [0, 1, 2]:
    slope = (math.exp(x + h) - math.exp(x)) / h
    print(x, round(math.exp(x), 4), round(slope, 4))
0 1.0 1.0
1 2.7183 2.7183
2 7.3891 7.3891

eˣ is its own slope. In words: the bigger it is, the faster it grows, in exact proportion. That's the definition of natural, unconstrained growth. For any other base, like 2ˣ or 10ˣ, the slope is the height multiplied by an awkward constant. With e, the constant is exactly 1, which makes the maths of growth, and of the Calculus course, much cleaner.

e in Python

MathsPython (one number)NumPy (whole arrays)
emath.enp.e
eˣmath.exp(x)np.exp(x)
ln(x)math.log(x)np.log(x)
print(math.exp(1), math.exp(0), math.exp(-1))
print(np.exp([0, 1, 2]).round(3))
print(math.log(math.e), math.log(1))
2.718281828459045 1.0 0.36787944117144233
[1.    2.718 7.389]
1.0 0.0
  • e⁻¹ = 1/e ≈ 0.368, as the negative-power rule from exponents and powers says.
  • math.log with one input is the natural logarithm, base e. It undoes exp: ln(e) = 1. Logarithms get their own lesson next.

Where e appears in machine learning

FormulaWhereWhat e does
1/(1 + e⁻ˣ)the sigmoid, in logistic regressionsqueezes any score into 0 to 1
eˣⁱ/Σeˣʲsoftmax, in multi-class modelsturns scores into probabilities that add to 1
e^(−x²/2)the normal distributioncreates the bell-curve shape
−ln(p)log loss (cross-entropy)punishes confident wrong answers
e^(−rt)decay schedulessmooth continuous decay

e appears in these formulas because eˣ is always positive, grows smoothly, and has that neat slope property, which makes the calculus behind training simple. Whenever you see exp or log in ML code, you're looking at e.

Practice

  1. Is e a whole number, a fraction, or neither? Roughly what is it?
Show the answer

Neither: like π, it's an irrational number whose decimals never end or repeat. It's about 2.718.

  1. Calculate (1 + 1/10)¹⁰ and (1 + 1/100)¹⁰⁰. Which is closer to e?
Show the answer

About 2.594 and 2.705. The second is closer: larger n gets closer to e.

  1. ₹5,000 grows continuously at 8% a year. What's it worth after 5 years?
Show the answer

5,000 × e^(0.08 × 5) = 5,000 × e^0.4 ≈ 5,000 × 1.492 = ₹7,459.

  1. What are e⁰ and ln(1)?
Show the answer

Both are special values: e⁰ = 1, and ln(1) = 0.

  1. Which Python function should you use for eˣ on a NumPy array: math.exp or np.exp?
Show the answer

np.exp. math.exp only works on single numbers.

Key takeaways

  • e ≈ 2.71828 is the limit of (1 + 1/n)ⁿ as compounding becomes continuous.
  • Continuous growth follows A = Pe^(rt).
  • eˣ is its own slope, which makes it the natural base for growth.
  • In Python: math.e, math.exp, math.log (natural log), or NumPy's np.exp and np.log.
  • e is inside the sigmoid, softmax, the normal distribution and log loss.

Frequently asked questions

What is the number e?

A mathematical constant, approximately 2.71828, called Euler's number. It's the limit of (1 + 1/n)ⁿ as n grows, the result of compounding 100% growth continuously for one period.

Why is e used in machine learning?

Because eˣ is always positive, grows smoothly, and is its own slope, which keeps the calculus of training simple. It appears in the sigmoid, softmax, the normal distribution and log loss.

What is the difference between log and ln?

ln is the natural logarithm, with base e. "log" can mean base 10 or base e depending on context. In Python, math.log and np.log are natural logarithms; math.log10 is base 10.

What to learn next

  1. Undo exponentials in logarithms explained.
  2. Revisit exponential growth and decay.