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,163
  • 1
  • 10
  • 29

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
  • 127,288
  • 10
  • 191
  • 288
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
  • 23,412
  • 5
  • 38
  • 64