In R, when does a vector become "Too long"?

Asked

Viewed 708 times

6

When trying to create an infinite vector (1:Inf) I received the following message:

Error in 1:Inf : result would be Too long a vector

However, when the vector does not know in memory the message is usually different. The code below creates the three situations: a) can create the vector, b) does not fit in memory and c) "Too long".

object.size(1:1e9) # limite superior do tipo a
# 4000000040 bytes

object.size(1:1e10) # limite inferior do tipo b
# Error: cannot allocate vector of size 74.5 Gb

...

object.size(1:1e15) # limite superior do tipo b
# Error: cannot allocate vector of size 7450580.6 Gb

object.size(1:1e16) # limite inferior do tipo c
# Error in 1:1e+16 : result would be too long a vector

Question: given that the two vectors (type b and type c) would not fit in memory, such as the R defines that it falls in one case or the other?

1 answer

5


This has to do with the R source code. See that function : is defined in C here. There, you may find that this error appears in this condition:

 double r = fabs(n2 - n1);
 if(r >= R_XLEN_T_MAX)
     errorcall(call, _("result would be too long a vector"));

That constant R_XLEN_T_MAX in turn is defined here.

# define R_XLEN_T_MAX 4503599627370496

That is, if the number is greater than 4503599627370496, will make the mistake. Now see that:

> 4503599627370496 > 1e15
[1] TRUE
> 4503599627370496 > 1e16
[1] FALSE

See also this here:

> k <- 1:4503599627370496
Error: cannot allocate vector of size 33554432.0 Gb
> k <- 1:4503599627370497
Error in 1:4503599627370497 : result would be too long a vector
  • Does this number have any reason to be?

  • 1

    @Tomásbarcellos Then I no longer know... Until I tried to look, ams I did not find... He is 2 52...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.