2

I would like to find out more about the precision computation in R would differenciate between E.g.

-1128347132904321674821 < -1128347132904321674822
-1128347132904321674821 > -1128347132904321674822
-1128347132904321674821 == -1128347132904321674822

However the first two answers are FALSEand the third is TRUE

I just want to know how to include all the number in the points

Pork Chop
  • 28,528
  • 5
  • 63
  • 77

2 Answers2

4

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
mrip
  • 14,913
  • 4
  • 40
  • 58
  • Unless you postpend an L (e.g. 1L), R treats it as a floating-point number. class(1L) vs class(1). Appending the L makes it an integer (not, as in c, a long) – Josiah Yoder Aug 03 '21 at 21:14
3

If you need lots of digits' accuracy, use either gmp or Rmpfr (or both :-) ).

But make sure that's what your need is, as opposed to general floating-point calculation accuracy limits.

Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73
  • gmp does what I need, cheers! Im not intending going beyond this number so I don't think I would need Rmpfr. Than you for your help! – Pork Chop Oct 11 '13 at 18:24