On Mon, 22 May 2000 berwin@maths.uwa.edu.au wrote:
> Dear all,
>
> I was playing around with some C-code that I dynamically linked to R
> and noticed the following inconsistency:
>
> > dat <- matrix(1,3,3)
> > is.double(dat)
> [1] TRUE
> > is.integer(dat)
> [1] FALSE
> > dat <- matrix(1:9,3,3)
> > is.double(dat)
> [1] FALSE
> > is.integer(dat)
> [1] TRUE
>
> How did I find out? I passed `dat' to a double * in the C-code and
> printed out the matrix from C. But in the .C() call I just passed
> down `dat' and forgot to coerce to double via `as.double(dat)'. Of
> course the print out from the C routine was garbage, but since I used
> dat <- matrix(1,3,3)
> first, I was confused. Note that R says that `dat' is.double! But it
> seems to pass dat to C as an integer matrix nevertheless! Using the
> second matrix it became clear to me why I saw garbage being printed
> out from the C-routine. My question is now why does the first
> `is.double(dat)' returns `TRUE'?
Because it is double! This refers to the storage mode, not the
mathematical properties of the object's content.
I can find no inconsistency. 1 is double, and 1:9 is integer (just as
in S-PLUS 3.x). Why should this not be the case? Currently the
only time in R you are likely to get an integer result is a : or seq
expression, but you are not supposed to rely on this. (Since I know
you have a copy, this is exactly the problem of the wam example
on page 203 of `S Prograaming'.)
For example, if printit.c is
void printit(double *x, int *n)
{
int i;
for(i = 0; i < *n; i++) printf("[%d] %f\n", i, x[i]);
}
> dat <- matrix(1,3,3)
> is.double(dat)
[1] TRUE> invisible(.C("printit", dat, as.integer(9)))
[0] 1.000000
[1] 1.000000
[2] 1.000000
[3] 1.000000
[4] 1.000000
[5] 1.000000
[6] 1.000000
[7] 1.000000
[8] 1.000000> dat <- matrix(1:9,3,3)
> invisible(.C("printit", dat, as.integer(9)))
[0] 0.000000
[1] 0.000000
[2] 0.000000
[3] 0.000000
[4] 0.000000
[5] 0.000000
[6] 0.000000
[7] 0.000000
[8] 0.000000
as I would expect.
I think the moral is that you cannot assume the storage mode of
expressions returned by R (or by S).
--
Brian D. Ripley, ripley@stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272860 (secr)
Oxford OX1 3TG, UK Fax: +44 1865 272595
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To:
r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._