Skip to Content
Course content

14: Integers and Arbitrary Precision

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Wait, do I actually have to worry about integer overflow in Python?

If you've spent any time in C++, Java, or C#, your instinct is probably to check if a variable is a 32-bit or 64-bit integer before doing a big calculation. You're probably bracing for the moment a number hits 2,147,483,647 and suddenly flips to a negative number. In Python, you can stop worrying about that.

Python integers have arbitrary precision. This is just a fancy way of saying that Python will automatically allocate more memory to an integer as it grows. As long as your computer has available RAM, Python will keep expanding the number to fit the result. I've written scripts that handle numbers with thousands of digits without ever having to change a data type.

# This would crash or overflow in many other languages
huge_number = 10**100  # 1 followed by 100 zeros
print(huge_number) 
# Python just handles it. No "Long" or "BigInt" type required.

If there's no limit, can I just calculate anything?

Technically, yes, but practically? Not quite. While you won't get a traditional "overflow," you will eventually hit a performance wall. Arithmetic with massive numbers is slower because Python can't use a single CPU register to do the math; it has to manage the number as a collection of "digits" in memory.

I've seen juniors try to calculate 10**10000000 and wonder why their laptop fans started sounding like a jet engine. The computation takes time, and the memory usage spikes. If you find yourself doing modular exponentiation (which is common in cryptography), never do (a**b) % c. That calculates the massive number first and then divides it. Instead, use the three-argument version of the built-in pow() function, which is significantly faster and more memory-efficient.

# The slow, dangerous way
result = (7**1000000) % 13 

# The professional way
result = pow(7, 1000000, 13) 

Why did my program crash when I tried to print a massive number?

This is a weird one that catches a lot of people off guard. A few years ago, Python introduced a limit on the number of digits allowed when converting between integers and strings. They did this to prevent a specific type of Denial of Service (DoS) attack where an attacker sends a massive number to a server, and the server freezes while trying to convert that number into a string to print it or log it.

By default, the limit is 4,300 digits. If you try to print() or str() a number larger than that, Python will throw a ValueError. If you're doing legitimate scientific work or cryptography and you actually need to print a 10,000-digit number, you have to explicitly tell Python to raise the limit using the sys module.

import sys

# Let's try to print a number with 5,000 digits
big_num = 10**5000

try:
    print(big_num)
except ValueError as e:
    print(f"Caught the limit: {e}")

# Increase the limit to 10,000 digits
sys.set_int_max_str_digits(10000)
print(big_num) # Now it works!



📋 Practical Task

Exercise: The Giant Factorial String Challenge

In this exercise, you'll explore the boundaries of arbitrary precision and the string conversion limit. Your goal is to calculate a factorial large enough to trigger Python's safety limits and then resolve the issue.

  • Create a function calculate_factorial(n) that returns the factorial of n using a loop or math.factorial().
  • Call this function with n = 2000. (The result is well over 5,000 digits).
  • Attempt to print the result. You should encounter a ValueError regarding sys.set_int_max_str_digits.
  • Use the sys module to increase the integer-to-string conversion limit so that the factorial of 2,000 can be printed successfully.
  • As a final touch, print the total number of digits in the resulting factorial using len(str(result)).
Rating
0 0

There are no comments for now.

to be the first to leave a comment.