2

Why does

format(4444444444444444444,scientific=FALSE)

return "4444444444444444672"?

I thought it might be an integer precision thing, but this number is relatively small. Thanks!

I'm running R version 3.0.0 on Ubuntu Linux.

Al R.
  • 2,430
  • 4
  • 28
  • 40

2 Answers2

5

If you give R an integer greater than:

> .Machine$integer.max
[1] 2147483647

It converts it to a double. The R-FAQ has 7.31. Questions related to floating point accuracy occur commonly on SO: Controlling number of decimal digits in print output in R

(The size of vector indices was increased with the last version of R (3.0.0) and it seems possible that the max size for an integer may be widened in the future. I don't quite understand how we can keep the limit on sizes of integers and also access vector indices that are larger.)

Community
  • 1
  • 1
IRTFM
  • 258,963
  • 21
  • 364
  • 487
4

That's not a 32-bit integer.

R> as.integer(4444444444444444444)
[1] NA
Warning message:
NAs introduced by coercion 

It's a double-precision floating point number, which only have 15-16 places of precision. The above fails because the number is greater than .Machine$integer.max. The ...672 is rounding error. If you need to use large numbers, consider packages like gmp.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418