Midpoint Formula Overflow Error
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The midpoint formula looks harmless, but it can overflow in software when you add two very large integers before dividing by two. This is the same class of bug that appears in binary search midpoint calculations. The math is correct on paper, but the intermediate computation may exceed the numeric type used in code.
Where the Overflow Comes From
The textbook midpoint between two one-dimensional values is:
(a + b) / 2
If a and b are both large positive integers, a + b may overflow before the division happens.
For example, with 32-bit signed integers:
- '
a = 2_000_000_000' - '
b = 2_000_000_000'
The average should still be representable, but the sum may not be. That is the bug.
Safer Midpoint Formula
The standard safe reformulation is:
a + (b - a) / 2
This avoids adding two potentially huge numbers together. Instead, it computes the distance first and adds half of that distance back to a.
Here is a simple C++ example:
This pattern is widely used because it preserves the same midpoint result for ordered integer ranges without overflowing in the same way as (a + b) / 2.
Midpoint of 2D Points Has the Same Issue
For a 2D point midpoint, the textbook formula is:
- x midpoint:
(x1 + x2) / 2 - y midpoint:
(y1 + y2) / 2
The same overflow risk applies independently to each coordinate.
A safer approach is:
This avoids the dangerous direct sum.
Integer Versus Floating-Point Midpoints
If your coordinates are floating-point values, overflow is less likely in ordinary application ranges, but precision issues can still appear with extreme magnitudes. The safe reformulation is still a good habit when values may be large.
For integer geometry, the issue is usually overflow. For floating-point geometry, the issue is more often precision loss or rounding behavior.
Why This Shows Up in Binary Search Too
The binary search bug where mid = (low + high) / 2 can overflow is exactly the same arithmetic problem. The safe version there is:
low + (high - low) / 2
That connection is useful because once you understand the midpoint overflow problem in one context, you can recognize it everywhere else.
Use Wider Types When Appropriate
Another partial mitigation is to use a wider numeric type for the intermediate calculation, such as 64-bit integers. That can postpone overflow, but it does not eliminate the underlying design issue if the domain can still exceed the wider type.
So the best practice is usually:
- choose an appropriate numeric type
- use the safer midpoint formula anyway
Common Pitfalls
- Writing
(a + b) / 2because it looks mathematically obvious. - Assuming the final midpoint being representable means the intermediate sum is safe.
- Fixing only one coordinate of a geometric midpoint and forgetting the other.
- Confusing integer overflow with floating-point rounding issues.
- Ignoring the same bug pattern when writing binary search or range code.
Summary
- Midpoint overflow happens when the intermediate sum exceeds the numeric type.
- The safer formula is
a + (b - a) / 2. - The same problem appears in geometry and binary search.
- Use the safe formula for each coordinate in point midpoint code.
- Wider types help, but better arithmetic structure is the real fix.

