Skip to content

Why does R round natural logarithms as it does?

An answer to this question on Stack Overflow.

Question

I was perplexed by the difference in R:

log(0.0001)/0.0001
-92103.4

and, e.g., Google calculator rendering:

 ln(0.0001)/0.0001
 -92103.4037198

why is the rounding so different?

Answer

Increasing the displayed precision with, e.g.:

options(digits=20)

fixes the "problem"

> log(0.0001)/0.0001
[1] -92103.4
> options(digits=20)
> log(0.0001)/0.0001
[1] -92103.403719761816319

Note that the internal precision is always high and can be viewed with the .Machine variable:

> .Machine
#Many other details here
$double.digits
[1] 53

the foregoing indicates that the machine has a 53-bit mantissa, which indicates that double-precision floating-point is being used for the calculations, which is standard.