python runtimewarning_Python的RuntimeWarning:长标量overfow遇到

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

yields

RuntimeWarning: overflow encountered in long_scalars

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.

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

while on 64-bit NumPy:

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

-4249290049419214848

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

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.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值