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)
Call
What 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
Parameter
Description
x
Required positional real number (int or float).
Return Value
Return Value
Description
float
e ** 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}")
Symmetric midpoint. At z = 0.0, math.exp(-0.0) equals 1.0, giving an exact probability of 0.5000.
Symmetric tails. Probabilities at -2.0 and 2.0 sum cleanly to 1.0.
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")
Output
1.0
0.0
inf
exp(710) exceeds maximum float
How It Works
Zero exponent. Any nonzero finite number to power zero is one, so math.exp(0) returns 1.0.
Negative and positive infinity. Negative infinity decays to 0.0 while positive infinity returns inf directly.
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.