Python的RuntimeWarning:长标量overfow遇到 [英] Python RuntimeWarning: overfow encountered in long scalars

查看:11446
本文介绍了Python的RuntimeWarning:长标量overfow遇到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来编程,并在我最新的的Python 2.7 的项目,我遇到了以下内容:
RuntimeWarning:在long_scalars overfow遭遇
可能有人请详细说明这是什么意思,我可以做些什么来解决这个问题的。
在code贯穿但我不知道这是否是一个好主意,只是忽略警告。

I am new to programming and in my latest Python 2.7 project I encountered the following: "RuntimeWarning: overfow encountered in long_scalars" Could someone please elaborate what this means and what I could do to fix that. The code runs through but I'm not sure if it is a good idea to just ignore the warning.

这期间追加过程发生,如:

It happens during an append process like:

SomeList.append(VeryLongFormula)

有关的评论感谢
最好
ŧ

Thanks for the comments Best T

推荐答案

下面是它发出同样的警告的例子:

Here's an example which issues the same warning:

import numpy as np
np.seterr(all='warn')
A = np.array([10])
a=A[-1]
a**a

收益

RuntimeWarning: overflow encountered in long_scalars

在上面的例子中发生,因为 A 是DTYPE INT32 和maximim值的可存储 INT32 2 ** 31-1。由于 10 ** 10 -10 2 ** 32-1 ,求幂结果在一个数字,比它可以存储在一个更大的 INT32

In the example above it happens because a is of dtype int32, and the maximim value storable in an int32 is 2**31-1. Since 10**10 > 2**32-1, the exponentiation results in a number that is bigger than that which can be stored in an int32.

请注意,你不能依靠 np.seterr(全部='警告')来捕获所有的溢出
误差numpy的。例如,在32位numpy的

Note that you can not rely on np.seterr(all='warn') to catch all overflow errors in numpy. For example, on 32-bit NumPy

>>> np.multiply.reduce(np.arange(21)+1)
-1195114496

,而在64位numpy的

while on 64-bit NumPy:

>>> np.multiply.reduce(np.arange(21)+1)
-4249290049419214848

都失败没有任何警告,尽管它也是由于溢出​​错误。正确的答案是,21!等于

Both fail without any warning, although it is also due to an overflow error. The correct answer is that 21! equals

In [47]: import math

In [48]: math.factorial(21)
Out[50]: 51090942171709440000L

据numpy的开发者,罗伯特·克恩

不像真正的浮点错误(如硬件FPU设置一个
  旗
  每当它是溢出一个原子操作),需要
  实施整数溢出检测自己。我们这样做的
  该
      标量,但不是数组,因为这将是落实太慢
  对于
      在阵列的每个原子操作。

Unlike true floating point errors (where the hardware FPU sets a flag whenever it does an atomic operation that overflows), we need to implement the integer overflow detection ourselves. We do it on the scalars, but not arrays because it would be too slow to implement for every atomic operation on arrays.

所以责任在于你选择合适的 dtypes 所以没有操作溢出。

So the burden is on you to choose appropriate dtypes so that no operation overflows.

这篇关于Python的RuntimeWarning:长标量overfow遇到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆