NSs created using integer overflow + R on linux

I run an R script on a UNIX-based system, the script contains multiplication of large numbers, so the results when NAs overflow with integers, but when I run the same script on windows, this problem does not appear.

but I have to maintain the script all night on the desktop (Unix).

Is there any solution to this problem?

thanks

for(ol in seq(1,nrow(yi),by=25)) { for(oh in seq(1,nrow(yi),by=25)) { A=(N*(ol^2)) + ((N*(N+1)*(2*N+1))/6) -(2*ol*((N*N+1)/2)) + (2*N*ol*(N-oh+1)) + ((N-oh+1)*N^2) + (2*N*(oh-N-1)*(oh+N)) } } with : N=16569 = nrow(yi) 

but the first round is not calculated on unix.

+3
source share
3 answers

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 ?

+5
source

Given your comments, I assume that you seriously misunderstand the β€œcorrectness” of numbers in R. You say that the result you get on Windows is similar to -30598395869593930593 . Now, on both 32-bit and 64-bit, this precision is not even possible using a double, not to mention using an integer:

 > x <- -30598395869593930593 > format(x,scientific=F) [1] "-30598395869593931776" > all.equal(x,as.numeric(format(x,scientific=F))) [1] TRUE > as.integer(x) [1] NA 

You have 16 digits that you can trust, and everything else. Again, the accuracy of 16 digits is already pretty strong. Most measuring instruments do not even come close to this.

+3
source

Can you use integers for floating point numbers to use floating point math for calculations?

For instance:

 > x=as.integer(1000000) > x*x [1] NA Warning message: In x * x : NAs produced by integer overflow > x=as.numeric(1000000) > x*x [1] 1e+12 

As an aside, it is not entirely clear why the warning will appear in one environment, but not in another. At first I thought that 32-bit and 64-bit R assemblies could use 32-bit and 64-bit integers respectively, but there is no case . How are your environments configured the same in terms of displaying alerts?

+2
source

Source: https://habr.com/ru/post/1381814/


All Articles