【问题标题】:Handling reals in Fortran together with R在 Fortran 中与 R 一起处理实数
【发布时间】:2017-01-11 16:33:56
【问题描述】:

我在与 R 一起使用的 Fortran 中处理实数时遇到问题。以下代码是用 Fortran 编写的:

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

我使用命令R CMD SHLIB realtest.f 进行编译。如果我在 R 中运行共享对象:

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

lol 的结果值是 1.2,但它应该是 10。如果我用整数做整个事情,我会得到正确的值 10。

【问题讨论】:

    标签: r fortran


    【解决方案1】:

    尝试使用double precision 而不是real;以下对我有用:

    ! realtest.f90
    !
    subroutine realtest(x)
        implicit none
    
        double precision, intent(inout) :: x
        x = 10.0
    end subroutine realtest
    

    来自 R,

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

    【讨论】:

      【解决方案2】:

      您应该将 lol 声明为 real*8,因为 R 使用双精度浮点数。

      【讨论】:

      • 请不要在您的帖子中添加任何签名和感谢等。你的名字已经自动放在你的帖子下。另请注意,real*8 不是标准 Fortran。
      猜你喜欢
      • 2017-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-31
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-07-22
      相关资源
      最近更新 更多