问题
I have the following subroutine in 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
I want to use it in Python, so I wrap it with f2py
f2py -c -m generation generation.f90 --fcompiler=gnu95
Then in ipython
I do
from generation import *
a = generation(10000); type(a)
and I get float
. I checked the C file testmodule.c
generated with
f2py generation.f90 -m test
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.
回答1:
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.
回答2:
As the other answer mentions, Python's float
is already a double-precision number.
However, if you want to convert it, you can use
import numpy as np
a = np.float64(a)
See the numpy documentation for details on conversions.
来源:https://stackoverflow.com/questions/23075305/fortran-double-precision-converted-to-python-float