np.float64 和 python float (64) 的精度差异

Difference in precision in np.float64 and python float (64)

为什么(或看起来)python 浮点数比 numpy.float64 更精确。这里我举个例子:

import numpy as np

ann = 72.595895691
detections = np.array([0, 71.91000009999999], dtype=np.float64)
group_det = [1]
dist1 = [ann - detections[det] for det in group_det]
dist = np.zeros(len(group_det), dtype=np.float64)
for idx, det in enumerate(group_det):
    dist[idx] = ann - detections[det]
print(f'dist1: {dist1}')
print(f'dist {dist}')

这段代码的输出是:

dist1: [0.6858955910000049]
dist [0.68589559]

我的问题是为什么 dist1 更精确?

注意:代码似乎是以一种非常复杂的方式做事,但我不得不削减一小部分具有相同行为的代码。

正如@hpaulj在评论中提到的,不是计算精度。它正在打印。将此行添加到您的代码中,以查看在 numpy 中打印时您想要的精度:

np.set_printoptions(precision=16)

你的代码在上一行之后的输出:

dist1: [0.6858955910000049]
dist [0.6858955910000049]