error solved

Solved – overflow encountered in long_scalars


In this article, you will learn how to solve overflow encountered in long_scalars error in Python.

Let’s look at a code example that produces the same error.

import numpy as np
np.seterr(all='warn')
B = np.array([144],dtype='double')
b=B[-1]
print ("Hello python:", b**b)

Output

/tmp/sessions/5e568ca1c8f93f65/main.py:9: RuntimeWarning: overflow encountered in double_scalars
  print ("Hello python:", b**b)

In order to solve overflow encountered in long_scalars error in Python you need choose appropriate dtypes so that no operation overflows as shown in the code snippet below:

import numpy as np
np.seterr(all='warn')
B = np.array([143],dtype='double')
b=B[-1]
print ("Hello python:", b**b)

Output

Hello python: 1.6332525972973913e+308

please note that the maximum value storable in an int32 is 2**31-1.


Share on social media

//