In mathematical calculation, if the value reaches the allowed data type limit in the python, the exception “OverflowError: math range error” is thrown away. We’ll see the specifics of this “OverflowError” error in this article.
In python, the maximum limit is set for each data type. The value should be within the data type limit when performing any mathematical operations. If the value is greater then the data type would not be able to handle the value. In this case, python creates an error stating that the value exceeds the permissible limit.
The developer should be taking the proper action on the interest in this situation. We’ll see how to handle that situation in this post. We discus all possible solutions to this error.
Traceback (most recent call last):
File "D:\pythonexample.py", line 2, in <module>
answer=math.exp(1000)
OverflowError: math range error
>>>
Root Cause
Python makes use of the operands when running the mathematical operations. The operands are the variable of any one of the python data types. The variable can store the declared data types to their maximum limit.
If the program tries to store the value more than the maximum limit permitted for the data type, then python may trigger an error stating that the allowed limit is overflowing.
How to reproduce this issue
This problem can be replicated in the python math operation called exp. This task is to find the exponential power solution for the value. The data type limit allowed is 709.78271. If the program simulates to generate a value greater than the permissible limit, the python program will show the error.
pythonexample.py
import math
answer=math.exp(1000)
print(answer)
Output
Traceback (most recent call last):
File "D:\pythonexample.py", line 2, in <module>
answer=math.exp(1000)
OverflowError: math range error
>>>
Solution 1
As discussed earlier, the value should not exceed permissible data type maximum limit. Find the exponential value with less will solve the problem. A if condition is applied to verify the input value before the exponential operation is completed. If the input value is higher, the caller will be shown the correct error message.
The code below illustrates how the exponential function can be used without throwing an error into the program.
pythonexample.py
import math
val = 1000
if val<50:
answer=math.exp(val)
print(answer)
else:
print("Input value is greater than allowed limit")
Output
Input value is greater than allowed limit
>>>
Solution 2
If the input value is not reliable, the other way this error can be treated is by using the form of try-except. Add the corresponding code to the try block. If the error happens then catch the error and take the alternative decision to proceed.
This way, this overflow exception will be handled in the code. The code below shows how to handle the overflow error in the python program using try and except.
pythonexample.py
import math
try:
answer=math.exp(1000)
except OverflowError:
answer = float('inf')
print(answer)
Output
inf
>>>