log1p()
log1p(x) Returns:
numeric · Updated March 13, 2026 · Base Functions math log log1p numerical-stability base
The log1p() function computes log(1 + x), the natural logarithm of one plus the input. It is more accurate than log(1 + x) when x is very small.
Syntax
log1p(x)
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
x | numeric | — | A numeric vector or scalar |
Examples
Basic usage
# log1p(0) = log(1) = 0
log1p(0)
# [1] 0
# log1p(1) = log(2)
log1p(1)
# [1] 0.6931472
# For very small values, log1p is more accurate
x <- 1e-10
log(1 + x)
# [1] 0
log1p(x)
# [1] 1e-10
Numerical stability demonstration
# When x is very small, log(1 + x) loses precision
x <- 1e-15
log(1 + x)
# [1] 0 # precision lost!
log1p(x)
# [1] 1e-15 # accurate
# Relative error comparison
x <- 1e-8
(log(1 + x) - x) / x # relative error in log()
# [1] -5.000001e-08
(log1p(x) - x) / x # relative error in log1p()
# [1] 0
Common Patterns
Statistical calculations
# Log-likelihood with small probabilities
p <- 1e-10
log(p + 1) # loses precision
log1p(p) # accurate
# Geometric mean for values near 1
x <- c(1.001, 1.002, 1.003)
exp(mean(log1p(x - 1)))
# [1] 1.002
Financial calculations
# Continuous compounding for small rates
rate <- 0.0001
time <- 1
log1p(rate * time)
# [1] 0.000099995