All Python errors
OverflowError in Python: number too large for float
OverflowError means a calculation with fractional numbers (float) produced a value too large to store. To fix it, calculate with whole numbers, which can be as long as needed, or make the values smaller.
Code with the error
growth = 2.0 ** 5000
print(growth)
Error message (Python 3.12, last line)
Corrected code
growth = 2 ** 5000
print(len(str(growth)))
Output
1506
Common causes
- A float is raised to a very large power.
- A huge whole number is converted with float().
- The / operator divides a huge whole number, and the result must be a float.
- A value in a loop is multiplied again and again by mistake.
Practise in the course
Lesson: Math OperationsFrequently asked questions
Can whole numbers overflow in Python?
No. int grows as long as there is memory; the limit applies to float.
What is the largest float?
About 1.8 times 10 to the power 308.