Great work!

XP to next level

BugEater

Overflow in Everyday Calculators

Learning Objectives

By the end of this lesson you will be able to:

  • Identify overflow risk in standard calculator operations
  • Select targeted overflow-probing inputs for addition, subtraction, and multiplication
  • Distinguish between a correctly handled overflow and a silent failure

Calculators Look Simple — They're Not

A web calculator with two number fields and an Add button looks trivially simple. But behind that simple UI is a chain of operations:

  1. Parse string → numeric type (what type? Int32? Long? Double?)
  2. Perform arithmetic (overflow possible here)
  3. Format result → string (may silently truncate)
  4. Send to backend / display to user

Any step can fail for extreme inputs. The UI never warns you.

The Three Overflow Scenarios in Addition

For a two-number adder:

Scenario A: Both numbers are large and positive

Input A: 2,000,000,000
Input B: 2,000,000,000
Expected: 4,000,000,000 (exceeds Int32 max!)
Int32 result: wraps to -294,967,296

Scenario B: Large negative numbers

Input A: -2,000,000,000
Input B: -2,000,000,000
Expected: -4,000,000,000 (below Int32 min!)
Int32 result: wraps to 294,967,296

Scenario C: Numbers near Int32 max/min

Input A: 2,147,483,647 (Int32 max)
Input B: 1
Expected: 2,147,483,648 (should error or use larger type)
Int32 result: -2,147,483,648 (wrap-around!)

Testing Strategy for Calculator Apps

  1. Test Int32 boundary: inputs that sum to exactly 2,147,483,647
  2. Test one over Int32: inputs that sum to 2,147,483,648
  3. Test Int64 boundary: inputs that sum to 9,223,372,036,854,775,807
  4. Test one over Int64: inputs that sum to 9,223,372,036,854,775,808
  5. Test very large negative sums (crossing Int32/64 minimum)

For multiplication, overflow happens much faster:

  • 100,000 × 100,000 = 10,000,000,000 → already exceeds Int32 max!

What Good Behavior Looks Like

A properly designed calculator should either:

  • Use a type that can hold the result (Long or BigDecimal instead of int)
  • Detect overflow before it happens and return a clear error

What bad behavior looks like:

  • Returns wrong (negative) number silently
  • Returns "Infinity" or "NaN"
  • Returns a 500 error with no explanation

Pro Tip: Always test both the "just under limit" and "just over limit" cases together. Seeing the correct answer for one and the wrong answer for the other immediately confirms overflow as the root cause.

Key Takeaways

  • Simple addition can overflow Int32 with numbers above ~1 billion each
  • Multiplication overflows even faster — 100K × 100K exceeds Int32
  • Test: inputs that produce sums near Int32 max, Int32 max+1, Int64 max
  • Silent wrong result is worse than an explicit error

Quiz

A calculator app shows -94,967,295 when you multiply 50,000 × 90,000. What is the most likely explanation?

Which input combination is the BEST choice for probing Int32 overflow in an addition calculator?

A tester multiplies two values in a calculator and receives a result that is exactly zero. What should the tester suspect first?

Why does a calculator that works correctly for inputs up to 10,000 potentially fail for inputs of 100,000?