0

When I to convert large numbers formatted as character strings to numeric R changes the last digits. This also happens when I pass it the number itself.

For example:

> options(scipen = 999)
> as.numeric("3411190080123000215")
[1] 3411190080123000320

> as.numeric(3411190080123000215)
[1] 3411190080123000320

This also happens when I use other numeric functions:

> floor(3411190080123000215)
[1] 3411190080123000320

Could this be an issue with my settings?

Thank you!!

Matt Phair
  • 11
  • 2
  • 1
    I think this just comes down to http://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal . (Try `all.equal(3411190080123000215, 3411190080123000320)` ) – user20650 Feb 24 '17 at 19:45
  • You might be able to use [`bit64`](https://cran.r-project.org/web/packages/bit64/index.html) for your work. NB: not all functions that use integers deal correctly with this package. (I'll be *really* happy when R natively supports 64-bit integers.) – r2evans Feb 24 '17 at 19:55
  • Welcome to the world of floating bit precision – Repmat Feb 24 '17 at 19:55

1 Answers1

0

The issue is that you are not actually using integers, you are using floats.

x <- as.numeric("3411190080123000215")
is.integer(x)

However, your number is too large to be stored as an integer anyway. Check out the gmp R package for arbitrarily large integers.

thc
  • 9,527
  • 1
  • 24
  • 39