Math_kv Math_kv - 1 year ago 40
R Question

Handling reals in Fortran together with R

I am having trouble handling reals in Fortran, which I use together with R. The following code is written in Fortran:

Subroutine realtest(lol)
implicit none
Real lol
lol = 10.0

I compile with the command
R CMD SHLIB realtest.f
. If I run the shared object in R as:

res <- .Fortran("realtest",lol= as.numeric(1.2))

The resulting value of
is 1.2, but it should have been 10. If I do the whole thing with Integers instead, I get the correct value 10.

Answer Source

Try using double precision instead of real; the following works for me:

! realtest.f90
subroutine realtest(x)
    implicit none

    double precision, intent(inout) :: x
    x = 10.0
end subroutine realtest

From R,

res <- .Fortran("realtest", x = as.double(1.2))
# $x
# [1] 10