Fortran双精度转换为Python float [英] Fortran double precision converted to Python float

查看:206
本文介绍了Fortran双精度转换为Python float的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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

probdouble类型.我希望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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆