Rounding Modes
When a result cannot be represented exactly, a rounding mode decides which representable value to pick.
Why rounding is needed
Most arithmetic results do not land exactly on a representable number, so the hardware must choose a neighbor. IEEE-754 defines several rounding modes and requires results to be correctly rounded as if computed to infinite precision first.
Round to nearest, ties to even
The default mode picks the closest representable value; when a result is exactly halfway, it rounds to the neighbor whose last bit is 0. This banker's rounding removes the upward bias that always rounding halves up would introduce.
Directed rounding
Three directed modes exist: toward +∞ (ceiling), toward −∞ (floor), and toward zero (truncation). These are essential for interval arithmetic, where you deliberately round bounds outward to guarantee an enclosure.
Accumulated bias
The choice of mode matters over long computations. Consistently rounding one direction accumulates drift, while round-to-even keeps the expected error near zero across many operations.
Decimal rounding
The same principles apply when displaying decimal results. Ties-to-even is common in finance and statistics precisely because it does not systematically inflate sums.
| Value | Nearest-even | Toward zero | Ceiling |
|---|---|---|---|
| 2.5 | 2 | 2 | 3 |
| 3.5 | 4 | 3 | 4 |
| -2.5 | -2 | -2 | -2 |
print(round(2.5)) # 2 (ties to even)
print(round(3.5)) # 4
import math
print(math.floor(2.7), math.ceil(2.1)) # 2 3