Surprising results from INTEGER_GET_REGION with ALTREP object

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Surprising results from INTEGER_GET_REGION with ALTREP object

Ralf Stubner
Dear Listmembers,

wanting to learn more about ALTREP I wrote the following function to
extract a subsequence from an integer vector:

#include <Rinternals.h>

SEXP integer_get_region(SEXP _x, SEXP _i, SEXP _n) {
  int i = INTEGER(_i)[0];
  int n = INTEGER(_n)[0];
  SEXP result = PROTECT(Rf_allocVector(INTSXP, n));
  INTEGER_GET_REGION(_x, i, n, INTEGER(result));
  UNPROTECT(1);
  return result;
}

For "shorter" vectors, the result is as expected:

> dyn.load("altrep_int_region.so")
> .Call("integer_get_region", 1:1e9, 0L, 10L)
 [1]  1  2  3  4  5  6  7  8  9 10

But not for "longer" vectors:

> .Call("integer_get_region", 1:1e10, 0L, 10L)
 [1]          0 1072693248          0 1073741824          0 1074266112
 [7]          0 1074790400          0 1075052544


Am I doing something wrong or is this a bug? I am using

> R.version.string
[1] "R version 3.5.2 (2018-12-20)"

Thanks
Ralf

--
Ralf Stubner
Senior Software Engineer / Trainer

daqana GmbH
Dortustraße 48
14467 Potsdam

T: +49 331 23 61 93 11
F: +49 331 23 61 93 90
M: +49 162 20 91 196
Mail: [hidden email]

Sitz: Potsdam
Register: AG Potsdam HRB 27966
Ust.-IdNr.: DE300072622
Geschäftsführer: Dr.-Ing. Stefan Knirsch, Prof. Dr. Dr. Karl-Kuno Kunze


______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Surprising results from INTEGER_GET_REGION with ALTREP object

Tomas Kalibera
On 3/1/19 1:52 PM, Ralf Stubner wrote:

> Dear Listmembers,
>
> wanting to learn more about ALTREP I wrote the following function to
> extract a subsequence from an integer vector:
>
> #include <Rinternals.h>
>
> SEXP integer_get_region(SEXP _x, SEXP _i, SEXP _n) {
>    int i = INTEGER(_i)[0];
>    int n = INTEGER(_n)[0];
>    SEXP result = PROTECT(Rf_allocVector(INTSXP, n));
>    INTEGER_GET_REGION(_x, i, n, INTEGER(result));
>    UNPROTECT(1);
>    return result;
> }
>
> For "shorter" vectors, the result is as expected:
>
>> dyn.load("altrep_int_region.so")
>> .Call("integer_get_region", 1:1e9, 0L, 10L)
>   [1]  1  2  3  4  5  6  7  8  9 10
>
> But not for "longer" vectors:
>
>> .Call("integer_get_region", 1:1e10, 0L, 10L)
>   [1]          0 1072693248          0 1073741824          0 1074266112
>   [7]          0 1074790400          0 1075052544
>
>
> Am I doing something wrong or is this a bug? I am using

The problem is that 1:1e10 is a vector of doubles, not integers. See ?colon

"
For numeric arguments, a numeric vector.  This will be of type
‘integer’ if ‘from’ is integer-valued and the result is
representable in the R integer type, otherwise of type ‘"double"’
(aka ‘mode’ ‘"numeric"’).
"

Best
Tomas

>
>> R.version.string
> [1] "R version 3.5.2 (2018-12-20)"
>
> Thanks
> Ralf
>
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel



        [[alternative HTML version deleted]]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel