[Rd] NUMERIC_POINTER question

Simon Urbanek simon.urbanek at r-project.org
Tue Sep 13 20:44:13 CEST 2005


Eryk,

On Sep 13, 2005, at 2:26 PM, nwew wrote:

> printf("%f\n",NUMERIC_POINTER(mat)[1]);
> [...]
> However it prints
> 0.0000
> if xx at data are integers ( xx at data<-matrix(1:12,3,4) ).
>
> Can anyone explain it to me why?
> I thought that NUMERIC_POINTER makes it clear that i expect  
> datatype numeric.
> (Why otherwise the distinction with INTEGER_POINTER)

You answered your own question - NUMERIC_POINTER expects that the  
SEXP you pass to it is numeric=double. When you use it, it's your  
responsibility to make sure that the SEXP is numeric and not integer  
or anything else. Probably you may want to use AS_NUMERIC to ensure  
that. [btw: NUMERIC_POINTER() is a compatibility macro for REAL() and  
AS_NUMERIC(x) for coerceVector(x,REALSXP)].

Also you should be aware that C uses 0-based indices so  
NUMERIC_POINTER(mat)[1] accesses the 2nd element of the vector.

Cheers,
Simon



More information about the R-devel mailing list