options(digits = 18)
x <- 0.127272727272727287
str(x)
# num 0.127
x
#[1] 0.127272727272727287
as.character(x)
#[1] "0.127272727272727"
as.numeric(as.character(x))
[1] 0.12727272727272701
Where does the 01
come from? What's going on here?
options(digits = 18)
x <- 0.127272727272727287
str(x)
# num 0.127
x
#[1] 0.127272727272727287
as.character(x)
#[1] "0.127272727272727"
as.numeric(as.character(x))
[1] 0.12727272727272701
Where does the 01
come from? What's going on here?
This is hinted at in the help page ?options
when you look at the section on digits. You can set the number of digits to any number up to 22, but that does not mean that R will accurately represent that many digits. R uses
IEEE double-precision. Wikipedia tells us that this representation has
Sign bit: 1 bit
Exponent: 11 bits
Significand precision: 53 bits (52 explicitly stored)
So R is storing numbers to log(2^53, 10) = 15.95459
decimal digits accuracy. Anything you get beyond that is luck.