python - 混合使用 NumPy longdouble 和 SymPy 数值计算——精度如何?

标签 python numpy sympy long-double

我有一段代码,其中一部分计算是使用 NumPy 函数和 longdouble 完成的,另一部分是使用 SymPy 符号微分和数值计算完成的,然后连接在一起(到 SymPy float )。 Sympy 评估可以任意精度完成,但是什么精度才足够好,即不会“污染” longdoubles 结果?据我所知,NumPy longdouble 实际上只有 80 位长,尽管在我的系统上被称为 float128Wikipedia说大约 80 位精度:

Bounds on conversion between decimal and binary for the 80-bit format can be given as follows: if a decimal string with at most 18 significant digits is correctly rounded to an 80-bit IEEE 754 binary floating point value (as on input) then converted back to the same number of significant decimal digits (as for output), then the final string will exactly match the original; while, conversely, if an 80-bit IEEE 754 binary floating point value is correctly converted and (nearest) rounded to a decimal string with at least 21 significant decimal digits then converted back to binary format it will exactly match the original.

此外,我在交互式提示中挖掘:

>>> numpy.finfo(numpy.double).precision
15
>>> numpy.dtype(numpy.double).itemsize
8
>>> numpy.finfo(numpy.longdouble).precision
18
>>> numpy.dtype(numpy.longdouble).itemsize
16
>>> 

所以,wiki 说精度取决于数字转换的方式(1821 数字),而 Numpy 只是说它是 18数字。有趣的是,默认 double 精度等于默认 SymPy 数值评估精度(15 vs. 15)。

假设我在某一点将 longdouble 结果转换为 SymPy float(然后在 SymPy 上工作),我应该设置什么 SymPy 精度? 18 位数? 21?多一点?

我在 Linux 64 位 (Sandy Bridge)、NumPy 1.6.2、SymPy 0.7.1.rc1 上使用 Python 2.7。实际代码is here (第 130 行附近的 nsk 类)。

最佳答案

IIRC,精度实际上取决于平台。不管怎样,对于这个问题:我认为你看错了细节。

>>> print numpy.finfo(numpy.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
---------------------------------------------------------------------

eps 是满足 1.0 + eps != 1.0 的最小正数,因此如果您的答案大约为 1,则您有 18 位有效小数。由于浮点运算的性质,这会随着数字本身的值而变化,但您总是会得到 18 位有效数字(无论有多少位小数)。

关于python - 混合使用 NumPy longdouble 和 SymPy 数值计算——精度如何?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23309743/

相关文章:

Python zeromq——单个订阅者的多个发布者?

python - Tkinter:通过多处理启动进程创建不需要的新窗口

python - 如何将多维数组从一种特定排列 reshape 为另一种排列?

c++ - 将 Python 嵌入到 C++ 中调用两个函数并传递数组

python - 通过仅替换一个变量来获取两个变量中多项式的值

python - 使用 PySide 渲染 latex/mathml

python - 使matplotlib在数学模式下显示衬线字体

python - pip:证书失败,但 curl 有效

python - 如何[使用 Python] 创建 SIFT 的描述符数据库?

python - 如何使用 numpy mggrid 或 meshgrid 来完成这个简单的任务