除了除零之外,double_scalars中遇到溢出的原因是什么?

时间:2017-04-14 05:37:55

标签: python numpy

我读到了关于双标量但只是部分理解的内容。根据我的理解,这是Numpy可以计算的范围。这就是为什么这里的大多数问题集中在除以零(这是一个错误,因为答案将超出范围(无穷大))。

但我不确定我的理解是否正确。另外,我看不到double_scalars中遇到RuntimeWarning:overflow的其他原因。什么可能导致双标量中遇到溢出?

3 个答案:

答案 0 :(得分:3)

溢出错误意味着操作产生的值超出为相应数据类型定义的范围。对于numpy double,该范围为public boolean isCyclic(Node first) { if(first == null) { return false; } Node fast = first; Node slow = first; while(fast.getNext() != null && fast.getNext().getNext() != null) { slow = slow.getNext(); fast = fast.getNext().getNext(); if(slow == fast) { return true; } } return false; } 。此外,为了进行良好的讨论,请read this SO post

示例:

(-1.79769313486e+308, 1.79769313486e+308)

输出:

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

答案 1 :(得分:2)

RuntimeWarning的另一个非常流行的原因:溢出遇到的是浮点错误。有关详细信息,请查看here

此外,这里有一些浮点异常的定义。

浮点异常在IEEE 754标准1中定义:

除以零:从有限数得到的无穷结果。 溢出:结果太大而无法表达。 下溢:结果如此接近于零,导致某些精度丢失。 操作无效:结果不是可表达的数字,通常表示产生了NaN。

我希望这有帮助,祝你好运。

答案 2 :(得分:2)

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)

因此,对于双精度,任何超出范围divide, exp, sqrt, ...的numpy函数(例如~[-1.797e+308, 1.797e+308])都会引发溢出警告。

例如:

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