[Rd] Reading 64-bit integers
William Dunlap
wdunlap at tibco.com
Tue Mar 29 21:33:01 CEST 2011
> -----Original Message-----
> From: r-devel-bounces at r-project.org
> [mailto:r-devel-bounces at r-project.org] On Behalf Of Jon Clayden
> Sent: Tuesday, March 29, 2011 5:46 AM
> To: r-devel at r-project.org
> Subject: [Rd] Reading 64-bit integers
>
> Dear all,
>
> I see from some previous threads that support for 64-bit integers in R
> may be an aim for future versions, but in the meantime I'm wondering
> whether it is possible to read in integers of greater than 32 bits at
> all. Judging from ?readBin, it should be possible to read 8-byte
> integers to some degree, but it is clearly limited in practice by R's
> internally 32-bit integer type:
>
> > x <- as.raw(c(0,0,0,0,1,0,0,0))
> > (readBin(x,"integer",n=1,size=8,signed=F,endian="big"))
> [1] 16777216
> > x <- as.raw(c(0,0,0,1,0,0,0,0))
> > (readBin(x,"integer",n=1,size=8,signed=F,endian="big"))
> [1] 0
>
> For values that fit into 32 bits it works fine, but for larger values
> it fails. (I'm a bit surprised by the zero - should the value not be
> NA if it is out of range?) The value can be represented as a double,
> though:
>
> > 4294967296
> [1] 4294967296
>
> I wouldn't expect readBin() to return a double if an integer was
> requested, but is there any way to get the correct value out of it?
In S+'s readBin() you can use the argument output="double" to
read 8-byte integers from a file and to put the closest equivalent
value into a double precision vector. It is also useful when reading
unsigned 4-byte integers that may be above 2^31. The help file says
output
Like what but used when the type of data in the file is
different than the type of S-PLUS vector used to store the
data. what refers to the data in the file and output refers to
the output of readBin. output=double() is useful for reading
unsigned 4 byte integers (so integers bigger than 2^31 are not
read as negative numbers) or even for 8 byte integers on
32-bit
versions of Splus. In the latter case you may lose some
precision, but reading it as 4 byte integers will omit the 4
high order bytes.
> I
> suppose one could read the bytes into a raw vector and then
> reconstruct the number manually from that, but is there a more elegant
> or built-in solution that I'm not aware of?
>
> This is R 2.12.1 on Mac OS X.6.7 - .Machine$sizeof.long is 8.
>
> Many thanks,
> Jon
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
More information about the R-devel
mailing list