[Rd] Calling C function from Fortran

From: Gilles GUILLOT <gilles.guillot_at_inapg.inra.fr>
Date: Wed 15 Jun 2005 - 11:41:37 GMT

<div class="mail">
<address class="headers">
<span id="from">
<dfn>From</dfn>: Gilles GUILLOT <gilles.guillot_at_inapg.inra.fr>
</span>
<span id="date"><dfn>Date</dfn>: Wed 15 Jun 2005 - 11:41:37 GMT</span>
</address>

<br>

Hi all,

the example in the R doc
<br>

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.
<br>

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)

<a href="http://tolstoy.newcastle.edu.au/R/devel/05/04/0693.html#1301qlink1">#include <R.h>
</a><br>
<em class="quotelev1">#include <Rmath.h>
</em><br>void F77_SUB(mygammac)(double x, double y) { y = gammafn(x); }

   subroutine mygammaf(x,y)
<br>

   double precision x,y
<br>

   call mygammac(x,y)
<br>

   end

called in R through
<br>

x <- 3
<br>

y <- -999
<br>

res <- .Fortran("mygammaf",

                as.double(x),
                as.double(y))


While changing the C code into
<br>
<a href="http://tolstoy.newcastle.edu.au/R/devel/05/04/0693.html#1301qlink2">#include <R.h>
</a><br>
<em class="quotelev1">#include <Rmath.h>
</em><br>void F77_SUB(mygammac)(double *x, double *y) { *y = gammafn(*x); }

seems to work fine.
<br>

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

<hr>
<br>

R-devel@r-project&#46;<!--nospam-->org mailing list
<br>
<a href="https://stat.ethz.ch/mailman/listinfo/r-devel">https://stat.ethz.ch/mailman/listinfo/r-devel</a>
<span id="received"><dfn>Received on</dfn> Wed Jun 15 21:44:23 2005</span>
</div>
Received on Wed Jun 15 21:44:23 2005

This archive was generated by hypermail 2.1.8 : Mon 24 Oct 2005 - 23:17:04 GMT