Fortran double precision converted to Python float

余生颓废 提交于 2020-01-16 05:04:44

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!