[R] behaviour of .Fortran in terms of "double precision" and "real" datatype

Duncan Murdoch murdoch at stats.uwo.ca
Wed Apr 30 02:45:35 CEST 2008


On 29/04/2008 6:52 PM, Sebastian Eiser wrote:
> Hello R-world!
> 
> This is my first post to the R list, so I feel that I need to say thanks to
> the community (I'm using R now for 5 months)!
> 
> I'd greatly appreciate if somebody could explain some odd behavior of
> ".Fortran" to me. Let's assume a primitive Fortran subroutine
> 
> barfoo.f
> ----------------------------
>       subroutine bar(y, v)
>       double precision y
>       real v
> 
>       y = 1.455523
>       v = 3.2
> 
>       end
> ----------------------------
> 
> after $ R CMD SHLIB barfoo.f (uses gfortran & gcc), I switch to R-2.6.2
> R> dynload("barfoo.so")
> R> .Fortran("bar", y=as.double(3), v=as.real(2.1))
> $y
> [1] 1.455523
> 
> $v
> [1] 2.099999
> --------------
> the double value of y changes according to the programme, but the v value is
> not being updated (besides an error is made).

as.real() is the same as as.double().  You want as.single(), or to 
convert the Fortran code to use double.  R doesn't use single precision 
internally; it will convert everything back and forth from double just 
for the call.

Duncan Murdoch

> 
> The reason why I"m interested is because I want to run an elaborate Fortran
> programme within a MCMC in R. All arguments for the programme are 'real'.
> Could somebody explain to me what's going on? Also, feel free to try that on
> your machine.
> 
> thanks very much in advance,
> Sebastian
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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