nathanesau
nathanesau

Reputation: 1721

Rstudio and R terminal give different outputs

In Rstudio (using R 3.1.1) when I run this,

length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9220 

In R 3.1.1 when I run this,

length(unique(sort(c(outer(2:100,2:100,"^")))))
# 9183

(the correct output is 9183)

I can't figure out why... help is greatly appreciated

Upvotes: 8

Views: 1304

Answers (1)

Josh O'Brien
Josh O'Brien

Reputation: 162321

As David Arenburg notes, this is a difference between 32-bit and 64-bit R versions, at least on Windows machines. Presumably, some sort of rounding error is involved. Interestingly, it is the 32-bit R gets the answer right, whereas the 64-bit R finds too many unique numbers.

First to confirm that 9183 is indeed the correct answer, I used the gmp package (a wrapper for the C multiple precision arithmetic library GMP), which provides results that are not subject to rounding errors:

library(gmp)
x <- as.bigz(2:100)
length(unique(do.call(c, sapply(x, function(X) x^X))))
[1] 9183

Here are the results from my 32-bit R:

length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9183
R.version[1:7]               _                           
# platform       i386-w64-mingw32            
# arch           i386                        
# os             mingw32                     
# system         i386, mingw32               
# status                                     
# major          3                           
# minor          1.2                         

And here are the results from my 64-bit R:

length(unique(sort(c(outer(2:100,2:100,"^")))))
# [1] 9220
R.version[1:7]
# platform       x86_64-w64-mingw32          
# arch           x86_64                      
# os             mingw32                     
# system         x86_64, mingw32             
# status                                     
# major          3                           
# minor          1.2                         

Upvotes: 3

Related Questions