我一直在玩C99的四倍精度长双.据我所知,(平台特定的)numpy支持long double和128bit float.
我遇到了一些我无法解释的事情.
鉴于:
>>> import numpy as np
Run Code Online (Sandbox Code Playgroud)
计算需要超过64位但小于128位的数字表示为整数:
>>> 2**64+2
18446744073709551618 # note the '8' at the end
>>> int(2**64+2)
18446744073709551618 # same obviously
Run Code Online (Sandbox Code Playgroud)
如果我在C99 128位长双倍中计算相同的数字,我得到18446744073709551618.000000
现在,如果我使用numpy long double:
>>> a=np.longdouble(2)
>>> b=np.longdouble(64)
>>> a**b+a
18446744073709551618.0 # all good...
Run Code Online (Sandbox Code Playgroud)
这些不正确的结果怎么样:
>>> np.longdouble(2**64+2)
18446744073709551616.0 # Note '6'; appears 2**64 not done in long double
>>> np.longdouble(int(2**64+2))
18446744073709551616.0 # can't force the use of a Python long
>>> n=int(2**64+2)
>>> np.longdouble(n)
18446744073709551616.0 …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用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'))
Run Code Online (Sandbox Code Playgroud)
本质上,它与此 …