Dear R-developers, Using .Call I pass a S4 class with e.g. the following class definition: setClass("mmatrix",representation( data="matrix") ) On the "C side" i do mat = GET_SLOT(vs,install("data")); and then: printf("%f\n",NUMERIC_POINTER(mat)[1]); The above print statement produces the correct output if xx<- new("mmatrix") xx at data<-matrix(1:12+0.1,3,4). (data is double) 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) cheers Eryk
On Tue, 13 Sep 2005, nwew wrote:> Dear R-developers, > > Using .Call I pass a S4 class with e.g. the following class definition: > > setClass("mmatrix",representation( > data="matrix") > ) > > On the "C side" i do > mat = GET_SLOT(vs,install("data")); > and then: > printf("%f\n",NUMERIC_POINTER(mat)[1]); > > > The above print statement produces the correct output if > xx<- new("mmatrix") > xx at data<-matrix(1:12+0.1,3,4). (data is double) > > 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)No, I think you have to make sure that the bits are in the correct mode. Could you use validation at the new() to do it? A matrix could be double, integer, logical or character at least, but on the C side you only want double. I don't think expectations come into it. Roger> > > cheers > Eryk > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >-- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: Roger.Bivand at nhh.no
You have to coerce to numeric yourself if that's what you want. Eg in the R code, as.numeric(1:12) rather than 1:12. Or check the type first in C with TYPEOF before doing NUMERIC_POINTER or INTEGER_POINTER (the difference is that the two do different casts; they both point a fixed offset into the R data structure and I'm not sure if there's a guarantee that this offset needs to be the same, so perhaps the two don't even point to the same location...). Reid Huntsinger -----Original Message----- From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org] On Behalf Of nwew Sent: Tuesday, September 13, 2005 2:26 PM To: r-devel Subject: [Rd] NUMERIC_POINTER question Dear R-developers, Using .Call I pass a S4 class with e.g. the following class definition: setClass("mmatrix",representation( data="matrix") ) On the "C side" i do mat = GET_SLOT(vs,install("data")); and then: printf("%f\n",NUMERIC_POINTER(mat)[1]); The above print statement produces the correct output if xx<- new("mmatrix") xx at data<-matrix(1:12+0.1,3,4). (data is double) 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) cheers Eryk ______________________________________________ R-devel at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-devel
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
Apparently Analagous Threads
- AS_NUMERIC and as.numeric - Could someone explain?
- Error when callin g C-Code
- .Call setAttrib(ans,R_DimSymbol,dim); Crashes.
- Changing arguments inside .Call. Wise to encourage "const" on all arguments?
- recursion problem using do.call(rbind, list(..,<S4>,..))