The math.exp(x) function computes Euler's number e (≈ 2.71828) raised to the power of x ($e^x$). It is the core mathematical function for modeling exponential population growth, radioactive decay, continuous compounding interest, and neural network activation functions.
Logarithmic Inverse: math.exp() is the exact inverse of the natural logarithm math.log() (math.exp(math.log(x)) == x).
The math.exp() function is part of the standard math module:
import math
result = math.exp(x)
| Parameter | Type | Required? | Description |
|---|---|---|---|
x |
float or int |
Yes | The exponent to which Euler's number e is raised. Supports positive, negative, and zero values. |
Return Value: Returns a floating-point number (float) representing $e^x$.
Evaluating $e^x$ with zero, positive, and negative values:
import math
print("e^0: ", math.exp(0)) # Output: 1.0 (any non-zero number to power 0 is 1)
print("e^1: ", math.exp(1)) # Output: 2.718281828459045 (Euler's number e)
print("e^5: ", math.exp(5)) # Output: 148.4131591025766
print("e^-2:", math.exp(-2)) # Output: 0.1353352832366127 (1 / e^2)
e^0: 1.0
e^1: 2.718281828459045
e^5: 148.4131591025766
e^-2: 0.1353352832366127
While you can mathematically compute $e^x$ using Python's exponentiation operator (math.e ** x) or the built-in pow(math.e, x), math.exp(x) is superior:
math.e ** x first rounds math.e to a 53-bit floating-point constant and then raises it to the power, compounding rounding errors. math.exp(x) uses direct hardware-optimized polynomial approximations from the C standard library.math.exp(x) is significantly faster because it evaluates base $e$ directly in compiled machine instructions rather than generic multi-base exponentiation.Because exponential growth accelerates rapidly, values of $x > 709.78$ exceed the maximum number representable by 64-bit IEEE 754 floating-point numbers (≈ $1.79 imes 10^{308}$), raising an OverflowError:
import math
try:
val = math.exp(710)
except OverflowError as err:
print("Overflow error caught:", err)
# Output: Overflow error caught: math range error
Conversely, for large negative numbers ($x < -745$), $e^x$ approaches zero so closely that Python cleanly underflows to 0.0 without raising an exception:
print(math.exp(-800)) # Output: 0.0
When $x$ is extremely close to zero (e.g. $10^{-15}$), calculating $e^x - 1$ using math.exp(x) - 1 suffers from catastrophic cancellation, resulting in zero or losing trailing digits. Use math.expm1() for full numerical precision:
import math
tiny = 1e-15
print("Using exp() - 1: ", math.exp(tiny) - 1) # 1.1102230246251565e-15 (imprecise)
print("Using expm1(): ", math.expm1(tiny)) # 1.0000000000000005e-15 (accurate!)
In machine learning and neural networks, the logistic sigmoid activation function squashes any real-valued number into a probability between $0$ and $1$:
$$\sigma(x) = rac{1}{1 + e^{-x}}$$
Let's build a numerically stable sigmoid function with math.exp() and verify it with unit assertions:
import math
def sigmoid(x):
# Numerical stability guard against overflow
if x < -700:
return 0.0
elif x > 700:
return 1.0
return 1.0 / (1.0 + math.exp(-x))
# Unit Assertions
assert sigmoid(0) == 0.5
assert round(sigmoid(2), 4) == 0.8808
assert round(sigmoid(-2), 4) == 0.1192
assert sigmoid(-800) == 0.0
assert sigmoid(800) == 1.0
print("Sigmoid(0): ", sigmoid(0))
print("Sigmoid(2): ", round(sigmoid(2), 4))
print("Sigmoid(-2):", round(sigmoid(-2), 4))
print("All sigmoid activation assertions passed successfully!")
Open in Google Colab View on GitHub
Run all math.exp() examples interactively, test continuous growth formulas, prevent OverflowError, and verify the sigmoid activation function with unit assertions directly in Google Colab.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.