Hi All, I just started to learn compiling C codes for R usage, and got a problem when I was playing with my 'hello world' code. #include <R.h> #include <Rdefines.h> #include <Rmath.h> SEXP test( ) { double x; x=dnorm(1.0,0.0,1.0,1); printf(" x value is: %d \n",x); return(R_NilValue); } I got the result : x value is: -466460838 Could someone explain to me what was wrong here ? Thanks a lot. tong
Hi, I think the only thing that's wrong is "%d" in your printf statement, shouldn't it be "%f"? ("%d" is used for decimal (integer) numbers, "%f" for float (double) numbers) Regards, Martin Tong Wang wrote:> Hi All, > I just started to learn compiling C codes for R usage, and got a problem when I was playing with my 'hello world' code. > > #include <R.h> > #include <Rdefines.h> > #include <Rmath.h> > > SEXP test( ) { > double x; > x=dnorm(1.0,0.0,1.0,1); > printf(" x value is: %d \n",x); > return(R_NilValue); > } > > I got the result : x value is: -466460838 > > Could someone explain to me what was wrong here ? > > Thanks a lot. > > tong > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
On Tue, 3 Apr 2007, Tong Wang wrote:> Hi All,> I just started to learn compiling C codes for R usage, and got a > problem when I was playing with my 'hello world' code. > > #include <R.h> > #include <Rdefines.h> > #include <Rmath.h> > > SEXP test( ) { > double x; > x=dnorm(1.0,0.0,1.0,1); > printf(" x value is: %d \n",x); > return(R_NilValue); > } > > I got the result : x value is: -466460838 > > Could someone explain to me what was wrong here ?double value, integer format (you want %g). [Also, using printf not Rprintf.] Please do make use of the ability if your compiler to tell you this. You haven't told us your platform, but if it is still Windows XP, you want to add -Wall -pedantic to your CFLAGS. -- Brian D. Ripley, ripley at 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
Tong Wang wrote:> Hi All, > I just started to learn compiling C codes for R usage, and got a problem when I was playing with my 'hello world' code. > > #include <R.h> > #include <Rdefines.h> > #include <Rmath.h> > > SEXP test( ) { > double x; > x=dnorm(1.0,0.0,1.0,1); > printf(" x value is: %d \n",x); > return(R_NilValue); > } > > I got the result : x value is: -466460838 > > Could someone explain to me what was wrong here ? > >Wrong printf format. "%d" is for integers.