5

How many digits can R represent accurately? Based on the answers here and here and the documentation I would expect R to do much better than in my mini example below (where the accuracy breaks down after 16 digits):

log(.Machine$double.xmax, 10)
# 308.2547

for (i in 1:30) {
   a <- rep(1, i)  # make a vector of 1s
   b <- paste(a, collapse = '')  # turn into string
   d <- as.double(b)  # turn into double
   e <- format(d, scientific = FALSE)  # turn into string
   print(e)
}

# "1"
# "11"
# "111"
# "1111"
# "11111"
# "111111"
# "1111111"
# "11111111"
# "111111111"
# "1111111111"
# "11111111111"
# "111111111111"
# "1111111111111"
# "11111111111111"
# "111111111111111"
# "1111111111111111"
# "11111111111111112"
# "111111111111111104"
# "1111111111111111168"
# "11111111111111110656"
# "111111111111111114752"
# "1111111111111111081984"
# "11111111111111111344128"
# "111111111111111109246976"
# "1111111111111111092469760"
# "11111111111111110924697600"
# "111111111111111104952008704"
# "1111111111111111049520087040"
# "11111111111111111869590405120"
# "111111111111111105501764517888"

Note, I'm interested in integers but use explicitly doubles for max accuracy.

hypothesis
  • 679
  • 5
  • 5
  • 2
    I'd recommend you read section G https://link.springer.com/content/pdf/bbm%3A978-1-4939-2122-5%2F1.pdf – Technophobe01 Sep 06 '18 at 17:49
  • 6
    A floating point only has 16 significant digits. See section "more thoughts" of https://stackoverflow.com/a/51694011/4891738 – Zheyuan Li Sep 06 '18 at 17:53
  • https://stackoverflow.com/a/588014/5414452 and https://en.wikipedia.org/wiki/Double-precision_floating-point_format – jogo Sep 06 '18 at 18:05
  • Have a look at packages like https://cran.r-project.org/package=Rmpfr or https://cran.r-project.org/package=gmp if you need higher precision. – Ralf Stubner Sep 06 '18 at 21:00

0 Answers0