As the other answers pointed out, there is something a little irreproducible / strange about your results so far. However, if you really have to do exact calculations for large integers, you probably need an interface between R and some other system.
Some of your options are:
gmp package (see this page and scroll down to R- interface with
bc calculator in googlecode - there is a page of high-precision arithmetic on the R wiki, which compares interfaces with Yacas, bc and MPFR / GMP
there is a limited interface for the PARI / GP package in the elliptical package, but it is probably much less than this would be useful than the previous three options
On most Unix or Cygwin systems, bc must be installed. GMP and Yacas are easy to install on modern Linux systems ...
Here is an extended example with a function that can choose between numerical, integer, or bigz computation.
f1 <- function(ol=1L,oh=1L,N=16569L,type=c("num","int","bigz")) { type <- match.arg(type) ## convert all values to appropriate type if (type=="int") { ol <- as.integer(ol) oh <- as.integer(oh) N <- as.integer(N) one <- 1L two <- 2L six <- 6L cc <- as.integer } else if (type=="bigz") { one <- as.bigz(1) two <- as.bigz(2) six <- as.bigz(6) N <- as.bigz(N) ol <- as.bigz(ol) oh <- as.bigz(oh) cc <- as.bigz } else { one <- 1 two <- 2 six <- 6 N <- as.numeric(N) oh <- as.numeric(oh) ol <- as.numeric(ol) cc <- as.numeric } ## if using bigz mode, the ratio needs to be converted back to bigz; ## defining cc() as above seemed to be the most transparent way to do it N*ol^two + cc(N*(N+one)*(two*N+one)/six) - ol*(N*N+one) + two*N*ol*(N-oh+one) + (N-oh+one)*N^two + two*N*(oh-N-one)*(oh+N) }
I removed a lot of unnecessary parentheses, which actually complicated what happens. Indeed, for the (1,1) case, the final result is no more than .Machine$integer.max , but some of the intermediate steps ... (for the (1,1) case, this actually reduces to $$ - 1/6 * ( N + 2) * (4 * N ^ 2-5 * N + 3) $$ ...)
f1() ## -3.032615e+12 f1() > .Machine$integer.max ## FALSE N <- 16569L N*(N+1)*(2*N+1) > .Machine$integer.max ## TRUE N*(N+1L)*(2L*N+1L) ## integer overflow (NA) f1(type="int") ## integer overflow f1(type="bigz") ## "-3032615078557" print(f1(),digits=20) ## -3032615078557: no actual loss of precision in this case
PS: you have the term (N*N+1) in your equation. Should it be N*(N+1) , or did you really mean N^2+1 ?
source share