From simon.urbanek at r-project.org  Tue Sep 13 20:44:13 2005
From: simon.urbanek at r-project.org (Simon Urbanek)
Date: Tue, 13 Sep 2005 14:44:13 -0400
Subject: [Rd] NUMERIC_POINTER question
In-Reply-To: <43161F2B@webmail.ncl.ac.uk>
References: <43161F2B@webmail.ncl.ac.uk>
Message-ID: <3D7CA27B-5BD2-41AD-BBF8-151D33E00486@r-project.org>

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



[Rd] NUMERIC_POINTER question
Simon Urbanek