How to Fix the Python Error: runtimewarning: overflow encountered in exp

The NumPy library adds a wide variety of math-related functions to Python. But that additional power also comes with some potential pitfalls. There are a number of error messages which people will see for the first time when using NumPy. For example, the runtimewarning: overflow encountered in exp warning.

What Does the Error Message Mean?

The overflow in exp Python issue is a lot less serious than it might appear at first glance. To begin with, it’s a warning rather than the more serious errors that are usually encountered in Python code. It’s essentially just a message stating that NumPy’s exp function is working with a number too large for it to properly handle. You don’t necessarily even need to fix it. Python code will often run properly even if it’s producing the warning. However, it’s generally fairly easy to fix and will help ensure the overall stability, cross-platform compatibility, and efficiency of your code.

A More In-Depth Examination of the Overflow Error

NumPy’s exp function calculates an exponential derived from the entirety of the elements passed to it. This process is limited by the capacity of the variable type used for the calculation. When a raw number is passed to exp it’ll register as a float64 type. This double precision float has a range of ±2.23×10−308 to ±1.80×10308.

The exp overflow warning will typically signal one of two results. The first is a best-case scenario where NumPy compensates for the issue and automatically compensates for the overflow. If this happens you’ll see both the Python error and the current calculation from exp. But you might also see NumPy fail to compensate for the issue. In this case, you’ll receive the same overflow warning but the resulting calculation will simply be an infinite value. The two results are usually dependent on a combination of processor type and operating system.

Manually fixing this issue offers up some more options to take care of it more efficiently than NumPy would by default. On top of this, it’s also generally a good idea to keep errors to a minimum for the sake of long-term viability. Libraries often change their tolerance for questionable code over the course of successive versions. And users might upgrade environments in ways that will change how these types of errors are automatically compensated for. Ensuring there’s no warning can help increase the overall longevity and compatibility of your code.

How To Fix the Overflow Error

There’s a few ways to take care of this error, but we’ll begin by simply recreating a situation where the error message comes up.

import numpy as np

print(np.exp(710))

In this example we’re using the default functionality in the exp function on a value of 710. The result of this calculation is then printed to screen. And along with the result we also see “runtimewarning: overflow encountered in exp”. This is due to what’s happening under the hood when we pass 710 to exp. By default the value is used as a float64. The result is a number too large for the data type. This can be demonstrated by trying it with a slightly smaller number.

import numpy as np

print(np.exp(700))

This code will work cleanly without any warnings since it remains within the maximum range of float64. However, we can’t really consider just using a different number to be an adequate solution. But what we can do is change the data type used with exp. The following code uses a float128 to allow for a wider scope.

import numpy as np

x = 710
x = np.float128(x)
print(np.exp(x))

The end result should be a cleanly processed number with no errors. Or, rather, that’s the case on most platforms. The NumPY float128 isn’t supported on the Windows builds of NumPy. As a result, running this code on Windows will typically result in an AttributeError stating that NumPY has no float128 attribute.

Users on most platforms other than Windows will see exp run smoothly and print the result to the screen without any error messages or warnings. This is the simplist way to fix the overflow warning. But this will also cause some compatibility issues with Windows. Using a float128 will solve the error on OSX, Linux and most non-Windows systems. But maintaining full compatibility with all of the major operating systems will require a little extra work.

The crux of the problem comes from the fact that we’re trying to work with numbers outside of what the data type can handle. Instead of trying to force it to work with incompatible data we can simply reformat the data before passing it to exp. Consider the following example.

import numpy as np

x = 710
x = np.clip(x, -709.78, 709.78)
print(np.exp(x))

In this example we once again begin by creating a basic integer value of 710 as x. We then use it with a NumPy function called clip. Clip will essentially clip a value at its interval edges. In this case we clip x to a point where it will work as a float64 value compatible with exp.

This isn’t the most precise solution as we lose a small amount of accuracy in comparison to use of a float128. However, the difference with values this large is essentially negligible. And we also see a boost in processing speed over the use of float128. Though the most important point simply comes from the fact that avoiding float128 provides larger cross-platform compatibility.

How to Fix the Python Error: runtimewarning: overflow encountered in exp
Scroll to top