【问题标题】:f2py loses double precision when passing to pythonf2py 在传递给 python 时失去双精度
【发布时间】:2014-05-06 21:03:46
【问题描述】:

我似乎无法使用 f2py 创建我的模块,以便在将数字传递回 python 时保持双精度。一个最小的例子,文件 fmodules.f90:

      subroutine example(output)
              implicit none
              double precision :: output
cf2py         intent(out) :: output
              output = 1.3
      end subroutine example

然后我用 f2py 创建模块:

$ f2py -c -m fmodules fmodules.f90

并从 python 调用它:

>>> from fmodules import example
>>> example()
1.2999999523162842

据我计算,这大约是 8 位数的精度。我的印象是双精度应该给出大约 16。我已经尝试了我能想到的所有排列,包括使用 .f2py_f2cmap 文件。关于我缺少什么的任何想法?

【问题讨论】:

标签: python fortran precision double-precision f2py


【解决方案1】:

您应该在 Fortran 代码中使用双精度设置输出,

output = 1.3d0

在 Fortran 1.3 中是单精度常量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-15
    • 1970-01-01
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多