【发布时间】:2014-08-25 07:33:31
【问题描述】:
我正在尝试在我的 Python 代码中使用 np.longdouble dtype,并正在尝试使用 NumPy 来处理我从使用 Cython 编译的 C 模块中获得的长双精度数。
假设我这样做:
import numpy as np
print np.finfo(np.longdouble)
Machine parameters for float128
---------------------------------------------------------------------
precision= 18 resolution= 1e-18
machep= -63 eps= 1.08420217249e-19
negep = -64 epsneg= 5.42101086243e-20
minexp=-16382 tiny= 3.36210314311e-4932
maxexp= 16384 max= 1.18973149536e+4932
nexp = 15 min= -max
---------------------------------------------------------------------
a = np.longdouble(1e+346)
a
Out[4]: inf
b = np.longdouble(1e+347)
b
Out[6]: inf
c = a/b
/usr/lib/python2.7/site-packages/spyderlib/widgets/externalshell/start_ipython_kernel.py:1:
RuntimeWarning: invalid value encountered in longdouble_scalars
# -*- coding: utf-8 -*-
c
Out[8]: nan
a.dtype, b.dtype, c.dtype
Out[9]: (dtype('float128'), dtype('float128'), dtype('float128'))
本质上,它与this 问题中的相同问题有关,我知道Python 首先将1e+346 转换为浮点数,其表示为inf。但是,有人可以提出解决方法吗?有没有办法创建不首先转换为浮点数的 NumPy longdoubles?
我有一个可以输出长双精度的 C 模块,我想在 dtype np.longdouble 的 numpy 数组中使用它。
即使解决方案涉及重新编译 Python/NumPy,我也愿意尝试。
【问题讨论】:
-
可能仍然不受支持,请参见例如numpy-discussion.10968.n7.nabble.com/…
标签: python numpy cython long-double