Python double_scalars 中遇到的溢出除了被零除还有哪些原因?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43405777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are the causes of overflow encountered in double_scalars besides division by zero?
提问by Jan
I read about double scalar but just partly understand. From my understanding, it is the range that is available to calculate by Numpy. That's why most questions here focusing on the division by zero (which is an error because the answer will be out of range (infinity)).
我读过双标量,但只是部分理解。根据我的理解,这是Numpy可以计算的范围。这就是为什么这里的大多数问题都集中在除以零上(这是一个错误,因为答案将超出范围(无穷大))。
But I am so unsure that my understanding is correct. Also, I can't see other causes about RuntimeWarning:overflow
encountered in double_scalars. What can cause overflow encountered in double scalars?
但我不确定我的理解是否正确。另外,我看不到有关RuntimeWarning:overflow
在 double_scalars 中遇到的其他原因。什么会导致双标量溢出?
回答by devautor
Overflow error implies that an operation yields a value out of the range defined for the corresponding data type. For numpy double, that range is (-1.79769313486e+308, 1.79769313486e+308)
. Also, for a good discussion, please read this SO post.
溢出错误意味着操作产生的值超出为相应数据类型定义的范围。对于 numpy double,该范围是(-1.79769313486e+308, 1.79769313486e+308)
. 另外,为了进行良好的讨论,请阅读这篇 SO post。
Example:
例子:
import numpy as np
np.seterr(all='warn')
print "Range of numpy double:", np.finfo(np.double).min, np.finfo(np.double).max
A = np.array([143],dtype='double')
a=A[-1]
print "At the border:", a**a
B = np.array([144],dtype='double')
b=B[-1]
print "Blowing out of range:", b**b
Output:
输出:
Range of numpy double: -1.79769313486e+308 1.79769313486e+308
At the border: 1.6332525973e+308
Blowing out of range: inf
D:\anaconda\lib\site-packages\ipykernel\__main__.py:9: RuntimeWarning: overflow encountered in double_scalars
回答by MonkeyWithMachine
Another very popular cause of a RuntimeWarning:Overflow encounter is the floating point error. For more information you can look here
RuntimeWarning:Overflow 遇到的另一个非常流行的原因是浮点错误。有关更多信息,您可以查看这里
Also, here's some definitions of floating-point exceptions.
另外,这里有一些浮点异常的定义。
The floating-point exceptions are defined in the IEEE 754 standard 1:
浮点异常在 IEEE 754 标准1中定义:
Division by zero: infinite result obtained from finite numbers. Overflow: result too large to be expressed. Underflow: result so close to zero that some precision was lost. Invalid operation: result is not an expressible number, typically indicates that a NaN was produced.
除以零:从有限数获得无限结果。溢出:结果太大无法表达。下溢:结果非常接近于零以致于失去了一些精度。无效操作:结果不是可表达的数字,通常表示产生了 NaN。
I hope this helps, Good Luck.
我希望这会有所帮助,祝你好运。
回答by romeric
NumPy obeys the IEEE floating point restrictions. The smallest to largest representable numbers in floating point precisions can be queried with numpy.finfo
NumPy 遵守 IEEE 浮点数限制。可以查询浮点精度中最小到最大的可表示数 numpy.finfo
In [35]: np.finfo(dtype=np.float64)
Out[35]: finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64)
In [36]: np.finfo(dtype=np.float32)
Out[36]: finfo(resolution=1e-06, min=-3.4028235e+38, max=3.4028235e+38, dtype=float32)
So for double precision, any numpy functions (such as divide, exp, sqrt, ...
) overflowing the range ~[-1.797e+308, 1.797e+308]
will raise an overflow warning.
因此,对于双精度,任何divide, exp, sqrt, ...
溢出范围的numpy 函数(例如)~[-1.797e+308, 1.797e+308]
都会引发溢出警告。
For example:
例如:
In [37]: np.ones(1)/1e-308 # fine
Out[37]: array([ 1.00000000e+308])
In [38]: np.ones(1)/1e-309 # overflow
/usr/bin/ipython:1: RuntimeWarning: overflow encountered in divide
Out[38]: array([ inf])
In [39]: np.exp(1000.) # overflow
/usr/bin/ipython:1: RuntimeWarning: overflow encountered in exp
Out[39]: inf