[Rd] NUMERIC_POINTER question

Witold Eryk Wolski W.E.Wolski at ncl.ac.uk
Wed Sep 14 17:45:34 CEST 2005


Thanks Roger, Simon, Reid,


It's indeed trivial, if you stop to believe that S4 provides any type of 
type safety. However, having in mind all the arguments why S4, and that 
it  was designed in order to incorporate type safety on both the R and C 
side I was not expecting that when trying for the *first* time, the 
get_slot and set_slot "stuff" I will run in exactly the problems which 
S4 is supposed to solve.

Of course I knew that "matrix" is not S4 and it is therefore even not a 
proper class. But this complexity sometimes blows your mind.

Cheers
Eryk.


Simon Urbanek wrote:
> 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