Learn R Programming

VGAM (version 0.9-6)

log1pexp: Logarithms with an Unit Offset and Exponential Term

Description

Computes log(1 + exp(x)) accurately.

Usage

log1pexp(x)

Arguments

x
A vector of reals (numeric). Complex numbers not allowed since log1p does not handle these.

Value

  • Returns log(1 + exp(x)).

Details

Computes log(1 + exp(x)) accurately. An adjustment is made when x is positive and large in value.

See Also

log1p, exp.

Examples

Run this code
x <-  c(10, 50, 100, 200, 400, 500, 800, 1000, 1e4, 1e5, 1e20, Inf)
log1pexp(x)
log(1 + exp(x))  # Naive; suffers from overflow
x <- -c(10, 50, 100, 200, 400, 500, 800, 1000, 1e4, 1e5, 1e20, Inf)
log1pexp(x)
log(1 + exp(x))  # Naive; suffers from inaccuracy

Run the code above in your browser using DataLab