【发布时间】:2012-06-07 15:38:41
【问题描述】:
我正在使用 f2py 编译一个供 Python 脚本使用的数值模块。我已将代码简化为以下最小示例:
fd.f:
module fd
! Double precision real kind
integer, parameter :: dp = selected_real_kind(15)
contains
subroutine lprsmf(th)
implicit none
real(dp) th
write(*,*) 'th - fd',th
end subroutine lprsmf
end module fd
times.f:
subroutine itimes(th)
use fd
implicit none
real(dp) th
write(*,*) 'th - it',th
call lprsmf(th)
end subroutine itimes
reprun.py:
import it
th = 200
it.itimes(th)
编译运行使用的命令如下(注意我在Windows下使用cmd):
gfortran -c fd.f
f2py.py -c -m it --compiler=mingw32 fd.o itimes.f
reprun.py
输出是:
th - it 1.50520876326836550E-163
th - fd 1.50520876326836550E-163
我的第一个猜测是 th 没有正确地从 reprun.py 传递到子程序 itimes。但是,我不理解这种行为,因为代码的完整版本包括其他输入,所有这些都正确传递。从 Fortran 调用 itime 时,我无法让它做同样的事情,所以我假设它与 Python/Fortran 接口有关。谁能提供有关为什么会发生这种行为的任何见解?
编辑:将 reprun.py 中的 th = 200 替换为 th = 200.0 会产生以下输出:
th - it 1.19472349365371216E-298
th - fd 1.19472349365371216E-298
【问题讨论】:
-
我对 Python 或 f2py 一无所知,但是如果将 th = 200 替换为 th = 200.0 会发生什么?
-
@HighPerformanceMark,见编辑。它仍然是一个垃圾值,但是是一个不同的值。