[R] Error of exp() in a dll from Fortran

Duncan Murdoch murdoch at stats.uwo.ca
Sat Jul 12 12:21:21 CEST 2008


On 12/07/2008 3:56 AM, Hao Ren Sr wrote:
> Hi, 
> 
> I have an error happened when I use exp() in my Fortran code. My Fortran 
> code is
>      SUBROUTINE f(n,c)
> 	
> 	    REAL, INTENT(IN) :: n(10)
> 	    REAL, INTENT(OUT) :: c(10)
> 	    INTEGER :: k
> 	
> 	    DO k=1, 10
> 	       c(k) = exp( n(k) )
> 	    END DO
>      END 
> 
> I compiled the fortran code as a dll 
> 
>  Rcmd SHLIB -o f.dll f.f -L -lacml 
> 
> Then I loaded the dll file to R, and try some number 
> 
>> dyn.load("C:/Program Files/R/R-2.7.1/bin/code/f.dll")
>> .Fortran("f",as.real(c(1:10)),A=real(10))
> [[1]]
> [1]  1  2  3  4  5  6  7  8  9 10 
> 
> $A
> [1]  17066.21  58265.20 179948.41 390001.06 576529.12      0.00      0.00
> [8]      0.00      0.00      0.00 
> 
> I tried to figur out what happened, but confused. Any suggestion?

real and as.real set double precision values in R.  In Fortran, the REAL 
type is single precision.

The most efficient thing to do is to declare n and c to be double 
precision in your Fortran code, but you could use single and as.single 
in R to have it automatically convert back and forth.  There is no 
single precision type in R, so this is inefficient.

Duncan Murdoch

> Thanks 
> 
> Hao Ren
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list