numpy的默认精度是64位的浮点数,能不能把精度调高,并应用在包中的函数里,比如说解方程?我只找到了调低的方法。
竟然是14年的问题,到现在都没一个人回答还一直出现在首页上。
numpy最高的精度可以用numpy.longdouble表示,复数用numpy.clongdouble,但是这个具体能精确到多少是和你的电脑硬件相关的,例如我的电脑只能表示到numpy.float64,见下面示例:In [1]:import numpy as np
# 查看 longdouble 的机器限制,可以看到在我的电脑上表示到 float64
In [2]:np.finfo(np.longdouble)
Out[2]: finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)
# 结果和 longdouble 是一样的
In [3]:np.finfo(np.float64)
Out[3]: finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)
In [4]:np.finfo(np.float32)
Out[4]: finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)
# 精度
In [5]:1+np.finfo(np.longdouble).eps
Out[5]: 1.0000000000000002
In [6]:1+np.finfo(np.longdouble).eps == 1.0
Out[6]: False
但是需要注意的是numpy官方并不支持这种做法,引用官方说明:Be warned that even if np.longdouble offers more precision than python float, it is easy to lose that extra precision, since python often forces values to pass through float.
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/wenda/wd19287.html
相关文章 Recommend