1

I have an 18 digit int which R doesn't understand, it returns a different value from what I enter

options(digits = 22)
> as.numeric(123456789123456789)
[1] 123456789123456784

Also when using bit64 which has an integer64 class

> as.integer64(123456789123456789)
integer64
[1] 123456789123456784

Are there other packages that can interpret this number correctly?

JCWong
  • 1,113
  • 1
  • 9
  • 27

2 Answers2

5

Use as.integer64("123456789123456789"). 123456789123456789 is a double (to avoid integer overflow) and thus subject to floating point imprecision. as.integer64(123456789123456789) creates an integer64 from this double.

Community
  • 1
  • 1
Roland
  • 122,144
  • 10
  • 182
  • 276
2

Here's an answer that is similar to the one suggested by @Roland; just using another library.

library(gmp)
x <- as.bigz("123456789123456789")
#> x
#Big Integer ('bigz') :
#[1] 123456789123456789

In this case, too, the number needs to be put in quotation marks to prevent floating-point rounding errors.

RHertel
  • 22,694
  • 5
  • 36
  • 60