Log in R

> #Lets learn about log in R
> ?log

  • log computes logarithms, by default natural logarithms (log base e)
  • log10 computes common (i.e., base 10) logarithms
  • log2 computes binary (i.e., base 2) logarithms.

> #The general form log(x, base) computes logarithms with base base.
> log(100)
[1] 4.60517
> log(x=100)
[1] 4.60517
> log(100,base=10)
[1] 2
> log(100,2)
[1] 6.643856
> log(x=100, base=2)
[1] 6.643856
> log(x=100, base=10)
[1] 2
> #log1p(x) computes log(1+x) accurately also for #|x| << 1 (and less accurately when x is approxima#tely -1).
> #exp computes the exponential function.
> exp(10)
[1] 22026.47
> #expm1(x) computes exp(x) – 1 accurately also for #|x| << 1.
> log(exp(3))
[1] 3
> log(exp(9))
[1] 9
> log10(1e9)
[1] 9
> x <- 10^-(1+2*1:9)
> cbind(x, log(1+x), log1p(x), exp(x)-1, expm1(x))
x
[1,] 1e-03 9.995003e-04 9.995003e-04 1.000500e-03
[2,] 1e-05 9.999950e-06 9.999950e-06 1.000005e-05
[3,] 1e-07 1.000000e-07 1.000000e-07 1.000000e-07
[4,] 1e-09 1.000000e-09 1.000000e-09 1.000000e-09
[5,] 1e-11 1.000000e-11 1.000000e-11 1.000000e-11
[6,] 1e-13 9.992007e-14 1.000000e-13 9.992007e-14
[7,] 1e-15 1.110223e-15 1.000000e-15 1.110223e-15
[8,] 1e-17 0.000000e+00 1.000000e-17 0.000000e+00
[9,] 1e-19 0.000000e+00 1.000000e-19 0.000000e+00

[1,] 1.000500e-03
[2,] 1.000005e-05
[3,] 1.000000e-07
[4,] 1.000000e-09
[5,] 1.000000e-11
[6,] 1.000000e-13
[7,] 1.000000e-15
[8,] 1.000000e-17
[9,] 1.000000e-19
> x1=cbind(x, log(1+x), log1p(x), exp(x)-1, expm1(x))
> x1
x
[1,] 1e-03 9.995003e-04 9.995003e-04 1.000500e-03
[2,] 1e-05 9.999950e-06 9.999950e-06 1.000005e-05
[3,] 1e-07 1.000000e-07 1.000000e-07 1.000000e-07
[4,] 1e-09 1.000000e-09 1.000000e-09 1.000000e-09
[5,] 1e-11 1.000000e-11 1.000000e-11 1.000000e-11
[6,] 1e-13 9.992007e-14 1.000000e-13 9.992007e-14
[7,] 1e-15 1.110223e-15 1.000000e-15 1.110223e-15
[8,] 1e-17 0.000000e+00 1.000000e-17 0.000000e+00
[9,] 1e-19 0.000000e+00 1.000000e-19 0.000000e+00

[1,] 1.000500e-03
[2,] 1.000005e-05
[3,] 1.000000e-07
[4,] 1.000000e-09
[5,] 1.000000e-11
[6,] 1.000000e-13
[7,] 1.000000e-15
[8,] 1.000000e-17
[9,] 1.000000e-19
> log(x1)
x
[1,] -6.907755 -6.908255 -6.908255 -6.907255
[2,] -11.512925 -11.512930 -11.512930 -11.512920
[3,] -16.118096 -16.118096 -16.118096 -16.118096
[4,] -20.723266 -20.723266 -20.723266 -20.723266
[5,] -25.328436 -25.328436 -25.328436 -25.328436
[6,] -29.933606 -29.934406 -29.933606 -29.934406
[7,] -34.538776 -34.434215 -34.538776 -34.434215
[8,] -39.143947 -Inf -39.143947 -Inf
[9,] -43.749117 -Inf -43.749117 -Inf

[1,] -6.907255
[2,] -11.512920
[3,] -16.118096
[4,] -20.723266
[5,] -25.328436
[6,] -29.933606
[7,] -34.538776
[8,] -39.143947
[9,] -43.749117
>

Leave a Comment

Your email address will not be published. Required fields are marked *