Skip to content

Strange number produced by dnorm

5 messages · Martin Becker, Brian Ripley, Peter Dalgaard +1 more

#
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:
#
On Tue, 3 Apr 2007, Tong Wang wrote:

            
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.
#
Tong Wang wrote:
Wrong printf format. "%d" is for integers.
4 days later
#
Having a hard time picking  up C :(      Thanks a lot for all your time .

tong

----- Original Message -----
From: Peter Dalgaard <p.dalgaard at biostat.ku.dk>
Date: Wednesday, April 4, 2007 12:44 am
Subject: Re: [Rd] Strange number produced by dnorm
To: Tong Wang <wangtong at usc.edu>
Cc: R-devel <r-devel at r-project.org>