run 时:
>>> import numpy as np
>>> np.exp(1000)
<stdin>:1: RuntimeWarning: overflow encountered in exp
显示溢出警告. 但是,为什么下面没有给出下溢警告呢?
>>> np.exp(-100000)
0.0
run 时:
>>> import numpy as np
>>> np.exp(1000)
<stdin>:1: RuntimeWarning: overflow encountered in exp
显示溢出警告. 但是,为什么下面没有给出下溢警告呢?
>>> np.exp(-100000)
0.0
默认情况下,下溢错误被忽略.
当前设置可以按如下方式判断:
print(np.geterr())
{'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}
要像溢出一样对下溢发出警告,可以使用np.seterr:
np.seterr(under="warn")
np.exp(-100000) # RuntimeWarning: underflow encountered in exp
或者,你可以像这样使用np.errstate:
import numpy as np
with np.errstate(under="warn"):
np.exp(-100000) # RuntimeWarning: underflow encountered in exp