Python math.exp() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
math module

Definition

math.exp(x) returns Euler's constant e raised to the power x (e ** x), computed more accurately than math.e ** x.

Inputs larger than roughly 709.78 exceed the maximum finite float and raise OverflowError. When x is very close to 0 and you immediately subtract 1, use math.expm1() instead.

math.exp() is a function in Python's standard math module: import it with import math and call math.exp(...), or import the name directly with from math import exp.

Syntax

Import math and pass the real exponent x.

python
import math

y = math.exp(x)
CallWhat it does
math.exp(0)Returns 1.0.
math.exp(1)Returns 2.718281828459045 (math.e).
math.exp(float("-inf"))Returns 0.0.
math.exp(1000)Raises OverflowError: result exceeds float range.

Use math.exp2() (Python 3.11+) for base 2, and math.log() for the inverse natural logarithm.

Function signatures
python
math.exp(x, /)
# returns: float e ** x

Parameters

ParameterDescription
xRequired positional real number (int or float).

Return Value

Return ValueDescription
floate ** x. Returns inf for inf, 0.0 for -inf, and nan for nan.

Python Examples

Example 1 — Model Continuous Bacterial Colony Growth

Apply the continuous exponential formula N0 * math.exp(rate * hours) across elapsed time steps.

python
import math

initial = 100.0
rate = 0.25
for hours in [0, 4, 8, 12]:
    count = initial * math.exp(rate * hours)
    print(f"hour {hours:>2}: {count:.2f}")

How It Works

  1. Zero elapsed time. At hour zero, math.exp(0.0) equals 1.0, leaving the initial count of 100.00.
  2. Unit exponent at hour four. Multiplying 0.25 * 4 yields 1.0, so the population scales by math.e to 271.83.
  3. Multi-hour growth. Successive intervals scale smoothly without intermediate discrete step rounding.

Example 2 — Compute Logistic Sigmoid Probabilities

Map raw model scores to probabilities between 0 and 1 with 1 / (1 + math.exp(-z)).

python
import math


def sigmoid(z):
    return 1.0 / (1.0 + math.exp(-z))


for score in [-2.0, 0.0, 2.0, 5.0]:
    print(f"z={score:>4} -> p={sigmoid(score):.4f}")

How It Works

  1. Symmetric midpoint. At z = 0.0, math.exp(-0.0) equals 1.0, giving an exact probability of 0.5000.
  2. Symmetric tails. Probabilities at -2.0 and 2.0 sum cleanly to 1.0.
  3. Positive saturation. Larger positive scores push the output close to 1.0 while keeping values strictly below one.

Example 3 — Check Overflow Boundaries and Special Floats

Observe how finite overflow raises OverflowError while infinite inputs return IEEE values.

python
import math

print(math.exp(0))
print(math.exp(float("-inf")))
print(math.exp(float("inf")))

try:
    math.exp(710)
except OverflowError:
    print("exp(710) exceeds maximum float")

How It Works

  1. Zero exponent. Any nonzero finite number to power zero is one, so math.exp(0) returns 1.0.
  2. Negative and positive infinity. Negative infinity decays to 0.0 while positive infinity returns inf directly.
  3. Finite overflow. Passing finite 710 exceeds the largest IEEE 754 float (~1.8e308) and triggers OverflowError.

Frequently Asked Questions

Yes. math.exp(x) avoids intermediate rounding errors in the constant math.e and handles boundary cases cleanly.
For IEEE 754 64-bit floats, any finite input above roughly 709.7827 raises OverflowError.
Python follows standard floating-point rules where an infinite input propagates inf, whereas a finite input whose result overflows raises OverflowError.
Use expm1(x) whenever x is close to zero, because math.exp(x) is close to 1.0 and subtracting 1 discards precision.
Call math.log(y), which returns the natural logarithm and recovers x for positive y.

Next: math.exp2()

Return 2 raised to the power x as a float (Python 3.11+).

exp2() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

16 people found this page helpful