When printing a high number in R I expect to see a rounded value due to floating point magic. Indeed :
options(scipen = 999)
x <- 10000000000000000000000000
x
#> [1] 9999999999999998758486016
However I expected that this rounded number would be rounded to itself, and it appears it isn't
x == 9999999999999998758486016
#> [1] FALSE
9999999999999998758486016
#> [1] 9999999999999996611002368
I found manually the min number that rounds to the original rounded value
x == 9999999999999998799999999
#> [1] FALSE
9999999999999998799999999
#> [1] 9999999999999996611002368
x == 9999999999999998800000000
#> [1] TRUE
9999999999999998800000000
#> [1] 9999999999999998758486016
While an explanation would be appreciated, I have a practical issue. I'd like to design a faithful dput() equivalent that would work with any number.
The constraint to satisfy is : x == as.numeric(my_deparser(x))
If mydeparser() could return "9999999999999998800000000" for the above for instance I'd be happy, because
10000000000000000000000000 == as.numeric("9999999999999998800000000")
#> [1] TRUE
I've tried format(), dput(), deparse() with no luck.
How can I achieve this ?
My session info :
R version 4.1.3 (2022-03-10) Platform: aarch64-apple-darwin20 (64-bit) Running under: macOS Monterey 12.0.1