问题描述
我在generation.f90
SUBROUTINE generation(t, prob)
IMPLICIT NONE
INTEGER, INTENT(IN) :: t
REAL(8), INTENT(OUT) :: prob
INTEGER :: nT2, c
Do some stuff with t, nT2 and c
prob = nT2/(DBLE(1.0)*c)
END SUBROUTINE generation
我想在Python中使用它,所以我用f2py
I want to use it in Python, so I wrap it with f2py
f2py -c -m generation generation.f90 --fcompiler=gnu95
然后在ipython
我知道
from generation import *
a = generation(10000); type(a)
,我得到float
.我检查了生成的C文件testmodule.c
and I get float
. I checked the C file testmodule.c
generated with
f2py generation.f90 -m test
和prob
是double
类型.我希望type(a)
是numpy.float64
.我该怎么办?我对Fortran和f2py还是陌生的.
and prob
is type double
. I would like type(a)
to be numpy.float64
. What should I do? I am fairly new to Fortran and f2py.
推荐答案
默认情况下,python中的浮点数是双精度的.较难的问题是获取单精度浮点数,这需要numpy或其他数学软件包.但就您而言,您不应该有任何担忧.
Floats in python are double precision by default. The harder problem is to get a single-precision float, which requires numpy or some other mathematics package. But in your case, you shouldn't have any concerns.
这篇关于Fortran双精度转换为Python float的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!