log computes logarithms, by default natural logarithms,
log10 computes common (i.e., base 10) logarithms, and
log2 computes binary (i.e., base 2) logarithms.
The general form log(x, base) computes logarithms with base
base.
log1p(x) computes \(\log(1+x)\) accurately also for
\(|x| \ll 1\).
exp computes the exponential function.
expm1(x) computes \(\exp(x) - 1\) accurately also for
\(|x| \ll 1\).
Arguments
- x
a numeric or complex vector.
- base
a positive or complex number: the base with respect to which logarithms are computed. Defaults to \(e\)=
exp(1).
Value
A vector of the same length as x containing the transformed
values. log(0) gives -Inf, and log(x) for
negative values of x is NaN. exp(-Inf) is 0.
For complex inputs to the log functions, the value is a complex number with imaginary part in the range \([-\pi, \pi]\): which end of the range is used might be platform-specific.
Details
All except logb are generic functions: methods can be defined
for them individually or via the Math
group generic.
log10 and log2 are only convenience wrappers, but logs
to bases 10 and 2 (whether computed via log or the wrappers)
will be computed more efficiently and accurately where supported by the OS.
Methods can be set for them individually (and otherwise methods for
log will be used).
logb is a wrapper for log for compatibility with S. If
(S3 or S4) methods are set for log they will be dispatched.
Do not set S4 methods on logb itself.
All except log are primitive functions.
Examples
log(exp(3))
#> [1] 3
log10(1e7) # = 7
#> [1] 7
x <- 10^-(1+2*1:9)
cbind(deparse.level=2, # to get nice column names
x, log(1+x), log1p(x), exp(x)-1, expm1(x))
#> x log(1 + x) log1p(x) exp(x) - 1 expm1(x)
#> [1,] 1e-03 9.995003e-04 9.995003e-04 1.000500e-03 1.000500e-03
#> [2,] 1e-05 9.999950e-06 9.999950e-06 1.000005e-05 1.000005e-05
#> [3,] 1e-07 1.000000e-07 1.000000e-07 1.000000e-07 1.000000e-07
#> [4,] 1e-09 1.000000e-09 1.000000e-09 1.000000e-09 1.000000e-09
#> [5,] 1e-11 1.000000e-11 1.000000e-11 1.000000e-11 1.000000e-11
#> [6,] 1e-13 9.992007e-14 1.000000e-13 9.992007e-14 1.000000e-13
#> [7,] 1e-15 1.110223e-15 1.000000e-15 1.110223e-15 1.000000e-15
#> [8,] 1e-17 0.000000e+00 1.000000e-17 0.000000e+00 1.000000e-17
#> [9,] 1e-19 0.000000e+00 1.000000e-19 0.000000e+00 1.000000e-19