While working with any mathematical operation in Python, getting the “Overflow: math range error” is widespread. Whenever you encounter any such error message, it simply means that you have exceeded the data type limit.
The limit is present for each data type in Python. Whatever calculations you perform, your value should always be within this limit if you do not want to encounter the “Overflow: math range error.
In this article, we will cover what you can do if you face the “overflow: math range” error.
Also read: Pip command not found: 4 Fixes
Example of “OverflowError: Math range error”
Let’s see an example to understand the “OverflowError: Math range error” more clearly.
#The method exp() calculates E raised to the power of x, where the value of 'E' is approximately 2.7182, and 'x' is provided by the user as an input.
import math
print("Output when x = 709 is: ")
result=math.exp(709)
print(result)
print("\nOutput when x = 709.78 is: ")
result=math.exp(709.78)
print(result)
print("\nOutput when x = 709.79 is: ")
result=math.exp(709.79)
print(result)
The exp() method of math library gives an error if the value of x exceeds 709.78 approximately. The output of the above code is shown below.
As you can see in the output, as soon as the value exceeded 709.78, the “OverflowError: math range error” was thrown. This happened because the value generated by math.exp(709.79) was more than the limit of the data type of variable result — double.
Since we have understood the OverflowError: Math range error clearly, let’s see how we can solve it.
How to solve the “OverflowError: Math range error”
You can fix the “OverflowError: math range error” by using the solutions mentioned below.
Using the try-except block
The exception handling of python can be used to fix the “OverflowError: math range error”. Put your exp() function inside the try block and while catching the error, put a print statement where you display the message to the user. See the code written below for better understanding.
import math
print("The value of 'x' is 711.")
x=711
try:
result=math.exp(x)
print(result)
except OverflowError:
print("\nThe value of 'x' should not exceed 709.78.")
print("inf")
print("\nHave a good day!\n")
The output of the above code is shown below.
Using the if-else block
You can check the value of ‘x’ provided by the user before passing it into the exp() function. If the value exceeds 709.78, you can print ‘inf’ where inf means infinite output.
import math
print("The value of 'x' is 711.")
x=711
if (x<709):
result=math.exp(x)
print(result)
else:
print("\nThe value of 'x' should not exceed 709.78.")
print("inf")
print("\nHave a good day!\n")
The screenshot attached below shows the output of the above code.
Using the exp() method of numpy library
You can use the exp() function of the numpy library to fix the Overflow error. If the value of x exceeds 709.78, the numpy gives a warning and shows infinity as output, due to which the program doesn’t halt.
import math
import numpy as np
print("\nOutput when x = 709.78 is: ")
result=math.exp(709.78)
print(result)
print("\nOutput when x = 709.79 is: ")
result=np.exp(709.79)
print(result)
print("\nHave a good day!\n")
The output of the code written above is given below.
Also read: Is Python case sensitive when dealing with identifiers?