When you type an integer larger than the maximum integer size (which you can find by typing .Machine$integer.max
), then R coerces it to a double. Moreover, there are only (slightly less than) 2^64 unique double values. 2^64 is about 1.84*10^19, while the numbers you entered are on the order of 10^21. However, all 64 bits of a double are not precision bits. One of them is a sign bit, and 11 of them are the mantissa (i.e. exponent bits). So you only get 52 bits of precision, which translates into about 15 or 16 decimal spaces. You can test this in R:
> for(i in 10:20)
cat(i,10^i == 10^i+1,"\n")
10 FALSE
11 FALSE
12 FALSE
13 FALSE
14 FALSE
15 FALSE
16 TRUE
17 TRUE
18 TRUE
19 TRUE
20 TRUE
So you see, after about 15 digits, the precision afforded by doubles is exhausted. It is possible to do higher precision arithmetic in R, but you need to load a library that provides this capability. One such library is gmp
:
> library(gmp)
> x<-as.bigz("-1128347132904321674821")
> y<-as.bigz("-1128347132904321674822")
> x<y
[1] FALSE
> x>y
[1] TRUE
> x==y
[1] FALSE
> x==y+1
[1] TRUE