Skip to content

The biggest inputs

Read · 1 of 3

Python's limits

Python's whole numbers never overflow, which removes a whole family of bugs. But it has others: recursion stops at about 1,000 nested calls, and decimals are stored in binary, so 0.1 + 0.2 isn't exactly 0.3.

Always try the biggest input the problem allows, not just a comfortable small one.

Read this lesson as text

Python doesn't overflow, but it has other limits.

Python's limits

Python's whole numbers never overflow, which removes a whole family of bugs. But it has others: recursion stops at about 1,000 nested calls, and decimals are stored in binary, so 0.1 + 0.2 isn't exactly 0.3.

Always try the biggest input the problem allows, not just a comfortable small one.

Break it: Break the recursive sum

This should print 1 + 2 + … + n. It's right for every small n. Find an input that breaks it.

def total(n):
    if n == 0:
        return 0
    return n + total(n - 1)

print(total(int(input())))

Input rules: one whole number n from 0 to 1,000,000.

Comparing decimals

a = float(input())
b = float(input())
print(a + b == 0.3)

Does this correctly say whether a + b equals 0.3?

  • Yes
  • No: decimals aren't stored exactly, so 0.1 + 0.2 isn't exactly 0.3
  • No: it crashes on decimals
Show the answer

No: decimals aren't stored exactly, so 0.1 + 0.2 isn't exactly 0.3. 0.1 + 0.2 is 0.30000000000000004 in binary floating point, so == says False. Compare with a small tolerance: abs(a + b - 0.3) < 1e-9.