log1p

Accurate log(1 + x) for small x.

Category: Mathematics > Elementary Math

Syntax

Y = log1p(X)

Returns log(1 + X) for each element, without the rounding error of forming 1 + X first.

Description

If x is smaller than the machine epsilon (about 2.2e-16), then 1 + x rounds to exactly 1 and log(1 + x) returns 0, while the true answer is close to x. log1p evaluates the function without that intermediate sum and keeps full relative accuracy.

It is the inverse of expm1 and is the right choice for compound interest rates and log-likelihoods of rare events.

Input arguments

X
Real array.

Output arguments

Y
Array of the same size as X. log1p(-1) is -Inf; values below -1 give NaN for real input.

Example

Where log(1+x) fails

x = 1e-17;
log(1 + x)
log1p(x)
log1p(-1)
Output
ans = 0
ans = 1.0000e-17
ans = -Inf

See also

expm1 log log10 exp