Sometimes while working with NumPy, you might face RunTimeWarning. One example is Runtimewarning: invalid value encountered in long_scalars error which occurs when we do mathematical operation on a list of very large numbers or vary the small numbers and then we apply operations like Nan or null as an input. In other words, this error occurs when we perform a mathematical operation that is invalid. In this short article, we will discuss how we can solve the Runtimewarning: invalid value encountered in long_scalars error using various methods. Moreover, we will discuss how to understand errors in Python taking Runtimewarning: invalid value encountered in long_scalars as an example.

## Runtimewarning: invalid value encountered in long_scalars – Possible Solutions

We know that the error Runtimewarning: invalid value encountered in long_scalars occurs when we performed an invalid mathematical operation in NumPy. Sometimes, when we are doing mathematical operations and work with very large or small numbers, some modules cannot handle this and through an error.

Here are some of the **steps through which we can prevent this error**:

- Use a function that can handle the error and can deal with very large or small numbers
- Use built-in complex mathematical functions
- Make necessary changes in the mathematical operation

In this short article, we will go through the following methods to solve the problem:

- Using if statement
- Using numpy.special.logsumexp() method

Let us now jump into the possible solutions:

### Soluton-1: Using the if statement

In Python, an if statement is used for a boolean condition. Before going to see how we can use the if statement to prevent Runtimewarning: invalid value encountered in long_scalars error, let us first see when we are getting this error:

**Error:**

```
import numpy
arr1 = [1, 2, 4, 7, 8]
# this input array causes error
arr2 = []
result1 = numpy.mean(arr1)
# this line causes the error
result2 = numpy.mean(arr2)
# printing
print(result1)
print(result2)
```

This causes the error because we are trying to find the mean of the array that is empty.

**Solution:**

```
import numpy
array1 = [1, 2, 4, 7, 8]
# this input array causes error
array2 = []
# Here we check error
if array1 and array2:
print( numpy.mean(array1))
print( numpy.mean(array2))
else:
print("The Array is not valid")
```

Output:

`The Array is not valid`

As you can see, we got rid of the error by using the if statement.

### Solution-2: Using numpy.special.logsumexp() method

First, let us when we are getting the error taking an example**:**

**Error:**

```
import numpy as np
from numpy import sinh
x = 900
y = 711
# This operation raise error
sol1 = np.log(np.sum(np.exp(x)))/np.log(np.sum(np.exp(y)))
print(sol1)
```

Output:

```
nan
/tmp/ipykernel_341710/2489633295.py:8: RuntimeWarning: overflow encountered in exp
sol1 = np.log(np.sum(np.exp(x)))/np.log(np.sum(np.exp(y)))
/tmp/ipykernel_341710/2489633295.py:8: RuntimeWarning: invalid value encountered in double_scalars
sol1 = np.log(np.sum(np.exp(x)))/np.log(np.sum(np.exp(y)))
```

Now let us see, how we can solve the problem using numpy.special.logsumexp() method.

**Solution:**

```
import numpy as np
from scipy.special import logsumexp
x = 900
y = 711
# Solution of the error with the
# help of built-in function
sol1 = logsumexp(x) - logsumexp(y)
# printing the answer
print(sol1)
```

Output:

`189.0`

Notice that this time, we didn’t get the error anymore.

## Understanding the Runtimewarning: invalid value encountered in long_scalars

Now let us try to understand the error. The error has two main parts which help us to figure out and understand it. The first part of the error is about the category of the error which in this case is the RuntimeWarning. This is not an error but a warning that occurs during the run time.

The second part of the error gives more specific information about the warning and it clearly says that an invalid value was encountered in long_scalars which means we are performing some invalid operation.

## Summary

In this short article, we discussed how we can solve Runtimewarning: invalid value encountered in long_scalars using various methods. We mainly discussed two methods along with examples.

## Related Errors:

- Typeerror: â€˜floatâ€™ object is not iterable
- Importerror: DLL load failed: the specified procedure could not be found
- TypeError: â€˜builtin_function_or_methodâ€™ object is not subscriptable
- ModuleNotFoundError: No module named â€˜bs4â€™
- Typeerror: string indices must be integers
- TypeError: canâ€™t multiply sequence by non-int of type â€˜numpy.float64â€™
- TypeError: only integer scalar arrays can be converted to a scalar index
- â€˜numpy.ndarrayâ€™ object has no attribute â€˜appendâ€™
- numpy.core.multiarray failed to import
- Attributeerror: module matplotlib has no attribute subplots