From gilles.guillot at inapg.inra.fr  Wed Jun 15 13:41:37 2005
From: gilles.guillot at inapg.inra.fr (Gilles GUILLOT)
Date: Wed, 15 Jun 2005 13:41:37 +0200
Subject: [Rd] Calling C function from Fortran
Message-ID: <200506151341.37076.gilles.guillot@inapg.inra.fr>

Hi all,

the example in the R doc 
and the hints from Shusong Jin ,  Ingmar Visser and Reid Huntsinger 
(thanks all three) refer to the case where the function does not have 
arguments.
I'm still looking for a proper sequence of commands 
to call C functions with arguemnts from R.


Imagine I want to evaluate the gamma function.
I want to use the C function called by R.
(I guess it is the one corresponding to the source code 
 I found in the directory  R-2.1.0/src/nmath/gamma.c
of the source distribution).

The following programs do not work (it returns fancy values)


#include <R.h>
#include <Rmath.h>
void F77_SUB(mygammac)(double x, double y) { y = gammafn(x); }


   subroutine mygammaf(x,y)
   double precision x,y
   call mygammac(x,y)  
   end

called in R through 
x <- 3
y <- -999
res <- .Fortran("mygammaf",
                as.double(x),
                as.double(y))

While changing the C code into 
#include <R.h>
#include <Rmath.h>
void F77_SUB(mygammac)(double *x, double *y) { *y = gammafn(*x); }

seems to work fine.
But  R-2.1.0/src/nmath/gamma.c does not need a pointer ?

What is wrong whit he first set of lines ?

What is the correct way to call the C function in R-2.1.0/src/nmath/gamma.c ?

Thanks in advance

Gilles



[Rd] Calling C function from Fortran
Gilles GUILLOT