RuntimeWarning: overflow encountered in exp [Solved]

avatar
Borislav Hadzhiev

Last updated: Jul 2, 2023
3 min

banner

# Table of Contents

  1. RuntimeWarning: overflow encountered in exp [Solved]
  2. Resolving the warning when working with arrays
  3. Suppressing the NumPy warnings to resolve the issue

# RuntimeWarning: overflow encountered in exp [Solved]

The NumPy "RuntimeWarning: overflow encountered in exp" occurs when you try to pass a larger number than is supported to the numpy.exp() method.

To solve the error, convert the number or array of numbers to np.float128 before calling exp().

Here is an example of when the warning is shown.

main.py
import numpy as np # โ›”๏ธ RuntimeWarning: overflow encountered in exp print(np.exp(800))

runtime warning overflow encountered in exp

The numpy.exp method calculates the exponential of a number or all elements in an array.

However, the number we passed to the method is too large which caused the warning.

The largest representable NumPy float is 1.7976931348623157e+308 and its logarithm is ~709.782, so np.exp(800) returns inf.

One way to resolve the issue is to convert the number to np.float128.

main.py
import numpy as np num = 800 num = np.float128(num) # ๐Ÿ‘‡๏ธ 2.7263745721125665673e+347 print(np.exp(num))

convert number to float128

# Resolving the warning when working with arrays

You will also get the warning when one or more of the elements in an array are too large to be handled by the np.exp() method.

main.py
import numpy as np arr = np.array([1, 3, 800]) # โ›”๏ธ RuntimeWarning: overflow encountered in exp print(np.exp(arr))

One way to solve the error is to set the dtype keyword argument to np.float128 to set the type of the numbers in the array to float128.

main.py
import numpy as np arr = np.array([1, 3, 800], dtype=np.float128) # [2.71828183e+000 2.00855369e+001 2.72637457e+347] print(np.exp(arr))

using dtype to cast array to np float 128

We set the dtype (data type) keyword argument to np.float128 to cast the numbers in the array to values of type float128.

You can also use the ndarray.astype() method to achieve the same result.

main.py
import numpy as np arr = np.array([1, 3, 800]) arr = arr.astype(np.float128) # [2.71828183e+000 2.00855369e+001 2.72637457e+347] print(np.exp(arr))

using numpy astype to cast array elements to np float 128

The numpy.ndarray.astype method creates a copy of the array, cast to the specified type.

# Suppressing the NumPy warnings to resolve the issue

Alternatively, you can suppress the NumPy warning by using the warnings built-in module.

main.py
import warnings import numpy as np warnings.filterwarnings('ignore') arr = np.array([1, 3, 800]) # ๐Ÿ‘‡๏ธ [ 2.71828183 20.08553692 inf] print(np.exp(arr))

suppressing the numpy warnings

The warnings.filterwarnings method inserts an entry into the list of warning filters.

When the action is set to ignore, warnings are never printed.

However, if you use this approach you disable all warnings (not just ones from NumPy).

An alternative approach is to only disable RuntimeWarnings.

main.py
import warnings import numpy as np warnings.filterwarnings('ignore', category=RuntimeWarning) arr = np.array([1, 3, 800]) # ๐Ÿ‘‡๏ธ [ 2.71828183 20.08553692 inf] print(np.exp(arr))

only disable runtime warnings

We set the category keyword argument to RuntimeWarning to only disable RuntimeWarnings.

You can achieve the same result by only using the NumPy module.

main.py
import numpy as np np.warnings.filterwarnings( 'ignore', category=RuntimeWarning ) arr = np.array([1, 3, 800]) print(np.exp(arr))

only disable runtime warnings using numpy

We used numpy.warnings instead of the built-in warnings module.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev