我試圖理解以下結果:
In [1]: with np.errstate(divide='ignore', invalid='ignore'):
...: for val in 1e-324,2e-324,3e-324:
...: val_by_zero = np.array([val])/0
...: print( val_by_zero, np.nan_to_num(val_by_zero))
...:
...:
[nan] [0.]
[nan] [0.]
[inf] [1.79769313e 308]
In [2]: np.__version__
Out[2]: '1.20.3'
In [3]: np.finfo(np.float64)
Out[3]: finfo(resolution=1e-15, min=-1.7976931348623157e 308, max=1.7976931348623157e 308, dtype=float64)
我只是很好奇 numpy 庫中到底發生了什么,它決定了 float 是 inf 還是 nan。
這是在 CentOS Linux 版本 7.9.2009 上運行的 python 3.8.12。
注意:我vals=[1e-324, 2e-324, 3e-324]在檢查了一個被不需要的大數字(例如 float max )破壞的應用程式后得出了這些數字1.79769313e 308。我想這些vals是平臺和/或配置相關的。
uj5u.com熱心網友回復:
可以表示的第一個正數float64是5e-324。
from numpy import nextafter
# Starting from 0 and going towards 1, what is the next representable value?
nextafter(0, 1, dtype='float64')
# Out: 5e-324
1e-324和2e-324更接近 0,因此它們表示為 0,但3e-324更接近,5e-324因此表示為5e-324。
from numpy import array
array([1e-324, 2e-324, 3e-324], dtype='float64')
# Out: array([0.e 000, 0.e 000, 5.e-324])
在這一點上,它歸結為positive number / 0vs 0 / 0。前者是無限的,而后者是未定義的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/453060.html
