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

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Sat, 12 Jul 2008 06:21:21 -0400

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.



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. Received on Sat 12 Jul 2008 - 10:26:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 12 Jul 2008 - 12:32:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive