Python 3.x np.array2string误报值?

Python 3.x np.array2string误报值?,python-3.x,numpy,jupyter,precision,Python 3.x,Numpy,Jupyter,Precision,我使用numpy和numpy.array2string来探索小变化和数值稳定性的影响。我对以下练习感到困惑: <Get vector data through prior code> print(data.shape) print(data.dtype) variance = np.var(data) print(variance.dtype) print(variance) print(np.array2string(variance, suppress_small=True,fo

我使用numpy和numpy.array2string来探索小变化和数值稳定性的影响。我对以下练习感到困惑:

<Get vector data through prior code>
print(data.shape)
print(data.dtype)
variance = np.var(data)
print(variance.dtype)
print(variance)
print(np.array2string(variance, suppress_small=True,formatter={'float': '{: 8.55f}'.format}))
这让我大吃一惊:array2string不仅仅是用(明显荒谬的)位数重新格式化值,它似乎在改变值本身,远远高于舍入误差应该起作用的点。(它似乎不是格式化程序,因为我可以删除它,但仍然可以看到更改的值。)

float32可以轻松地直接表示这两个整数值中的任何一个。
我在这里不明白什么?请重申我对客观现实的信念


更新:No float32不能准确表示37166410.0,我需要一副阅读眼镜。

“float32可以轻松地直接表示这两个整数值中的任何一个。”不,它不能。37166410附近可精确表示的值之间的间距为4。这可能会给你一个正在发生的事情的提示。顺便说一下,如果你打印
variance+1
:),你可能会更加怀疑自己对客观现实的信心@WarrenWeckesser:当
repr(np.float32(37166411))
返回
37166412.0
时,它可以帮助用户看到37166412.0可以表示为float32,但37166411不是。但是
repr(np.float32(37166410))
返回
37166410.0
,尽管37166410似乎不能准确地表示为float32。这是一个bug吗?@WarrenWeckesser:另外,
repr(np.nextafter(37166405,np.inf,dtype='float32')
返回
'37166410.0'
,这似乎意味着37166410.0可以表示为float32,但从数字上看,它的行为似乎与37166408.0一样。报告不应该返回37166408吗?“这是一个错误吗?”我不知道——我一直在想同样的事情。数组中显示值的跳转,例如
np.array(范围(3716639037166430),dtype=np.float32)
让我感到惊讶。Ping@MarkDickinson!
torch.Size([1, 64])  # shape of the data
torch.float32        # dtype of the data
float32              # dtype of the variance result
37166410.0
37166408.0000000000000000000000000000000000000000000000000000000