我正在尝试np.longdouble
在我的 Python 代码中使用 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'))
从本质上讲,它与这个问题中的相同问题有关,我知道 Python 首先将 转换1e+346
为浮点数,其表示为inf
. 但是,有人可以提出解决方法吗?有没有办法创建不首先转换为浮点数的 NumPy longdoubles?
我有一个可以输出长双精度的 C 模块,我想在 dtype 的 numpy 数组中使用它np.longdouble
。
即使解决方案涉及重新编译 Python/NumPy,我也愿意尝试。