POSTS
PSA: Python Float Overflow
Python 2 and Python 3 provide arbitrary precision integers. This means that you don’t need to worry about overflowing or underflowing the integer datatypes, ever. 2**10000
? No problem. It makes it pretty convenient to write code. Floats, however, are not arbitrary precision:
>>> a = 2 ** 10000
>>> a * .1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: int too large to convert to float
Luckily, float overflow throws an exception to alert you to the problem. In Java or C/C++, overflow will silently roll over into the negatives. In Javascript, it just becomes infinity
. It’s important to watch out for this whenever your code deals with truly large numbers, especially when raising numbers to arbitrary powers. I’ve seen this occur in backoff methods like the following:
def backoff_time(retry_count, initial_backoff, max_backoff):
return min(max_backoff, initial_backoff*(2**retry_count))
It will work fine as along as initial_backoff
is integral, but if someone wants to have an initial_backoff < 1
, you’re at risk of overflow.
Happy coding!
Want to get emailed about new blog posts?
Do you want to hire me? I’m available for engagements from 1 week to a few months. Hire me!