rguides

exp()

exp(x)

The exp() function computes the exponential of a number, raising Euler’s number e (approximately 2.71828) to the power of the input.

Syntax

exp(x)

Parameters

ParameterTypeDefaultDescription
xnumeric,A numeric vector or scalar

The input x can be any real number. Positive values produce results greater than 1, negative values produce results between 0 and 1, and exp(0) always returns exactly 1. Because exp() grows faster than any polynomial, moderately large inputs like exp(100) produce enormous numbers that can overflow to Inf in double-precision arithmetic.

Examples

Basic usage

# Exponential of 1 is Euler's number
exp(1)
# [1] 2.718282

# Exponential of 0 is 1
exp(0)
# [1] 1

# Exponential of positive numbers
exp(1:5)
# [1]   1.00000   2.71828   7.38906  20.08554  54.59815

The output above confirms that exp() is vectorized: passing 1:5 computes the exponential for each integer from 1 to 5 in a single call. The values grow quickly because e ≈ 2.718 raised to successively larger powers compounds rapidly. This rapid growth is what makes the exponential function central to modeling population dynamics, compound interest, and any process with constant relative growth rates.

Working with negative numbers

# Exponential of negative numbers approaches 0
exp(-1)
# [1] 0.3678794

exp(-10)
# [1] 4.539993e-05

Negative inputs produce values between 0 and 1, approaching zero as the input becomes more negative. The result exp(-10) ≈ 4.54 × 10⁻⁵ is computed via scientific notation, which R uses automatically for very small numbers. This property is used in probability models where probabilities must stay between 0 and 1: the exponential of a negative log-odds gives a valid probability.

Combining with other functions

# exp() is the inverse of log()
exp(log(5))
# [1] 5

# Natural exponential
exp(1)
# [1] 2.718282

The example exp(log(5)) returning exactly 5 illustrates the fundamental inverse relationship between exponentiation and the natural logarithm. This cancellation is exact in theory but can suffer from tiny floating-point rounding errors when chains of operations are long or when values are extremely large or small. The pair exp() and log() together form the backbone of log-space computation throughout statistics.

Common patterns

Probability calculations

# Logistic function
log_odds <- 0.5
1 / (1 + exp(-log_odds))
# [1] 0.6224593

# Poisson probabilities
lambda <- 3
exp(-lambda) * lambda^2 / factorial(2)
# [1] 0.2240418

The logistic function 1 / (1 + exp(-x)) maps any real number to the interval (0, 1), making it the standard activation for binary classification. The Poisson probability mass function uses exp(-lambda) to enforce the constraint that probabilities across all possible counts must sum to 1, with lambda representing both the mean and variance of the distribution.

Financial calculations

# Continuous compounding
principal <- 1000
rate <- 0.05
time <- 10
principal * exp(rate * time)
# [1] 1648.721

The expression exp(rate * time) appears in every introductory finance textbook as the formula for continuously compounded interest. Unlike discrete compounding (which uses (1 + r/n)^(n*t)), continuous compounding assumes interest is added at every instant, and the limit of (1 + r/n)^n as n → ∞ is exactly e^r. This makes exp() the natural choice for modeling growth that compounds continuously: bacterial populations, radioactive decay, and option pricing in the Black-Scholes model all use the exponential function directly.

Softmax for multi-class probabilities

The softmax function converts a vector of arbitrary real numbers into a probability distribution where all values sum to 1. This is the standard output layer in neural network classifiers and multinomial logistic regression:

logits <- c(2.0, 1.0, 0.1, -0.5)

# Numerically stable softmax (subtract max before exp)
stable_softmax <- function(x) {
  shifted <- x - max(x)
  exp(shifted) / sum(exp(shifted))
}

probs <- stable_softmax(logits)
probs
# [1] 0.59432676 0.21871456 0.0889349 0.09802378

sum(probs)  # verifies probabilities sum to 1
# [1] 1

Subtracting max(x) before exponentiating prevents overflow: without this shift, exp(2.0) is harmless but exp(700) would become Inf and corrupt the entire computation. The shift does not change the resulting probabilities because exp(x_i - c) / sum(exp(x - c)) equals exp(x_i) / sum(exp(x)) algebraically, but it keeps all intermediate values within a safe numeric range.

exp() in practice

exp() is one of the most frequently called math functions in statistical computing. It appears in:

  • Softmax and sigmoid transformations in machine learning: exp(x) / sum(exp(x))
  • Probability density functions (Poisson, exponential distribution) that involve e^(-lambda)
  • Converting log-space values back to probability space after working in log-domain for numerical stability
  • Compound growth formulas: exp(r * t) for continuous compounding at rate r over time t

exp() is the inverse of log() (natural logarithm): log(exp(x)) == x for finite x. For very small x, the subtraction exp(x) - 1 loses precision, use expm1(x) instead.

exp() is a C primitive and vectorized over its input. For very large inputs, it returns Inf; for very negative inputs, it returns 0. Check for these boundary cases in numeric pipelines where out-of-range inputs are possible.

exp() returns Inf for inputs larger than about 709 (the log of the largest representable double). For numerically stable computation with many exp() calls, such as computing softmax over a large vector, subtract the maximum value first: exp(x - max(x)) scales the exponents to avoid overflow while preserving relative magnitudes. This log-sum-exp trick is standard in machine learning implementations.

When working in log-space, a common technique for avoiding numerical underflow in probability computations — exp() converts back to the original scale. The pattern log_probs <- log(probs), then operations in log-space, then exp(log_probs) to recover probabilities, is standard in Bayesian computation, hidden Markov models, and sequence alignment algorithms. The key advantage is that products of small probabilities become sums of negative log-probabilities, which are far easier to compute without underflowing to zero during computation of long products.

When chaining exp() with log(), watch for the cancellation property: exp(log(x)) returns x exactly for positive values, but floating-point rounding can introduce small errors when intermediate values are very large or very small. For values near zero, expm1(x) is more accurate than exp(x) - 1, and for differences involving log(), log1p(x) avoids the same loss of precision. These two specialized functions exist precisely because the naive composition through exp() and log() loses significant digits in those ranges.

See also