Sub topic 13
Rounding modes and guard, round, sticky bits
An exact result often has more bits than the fraction field can hold. This chapter covers how IEEE 754 chooses the stored value, and how hardware makes that choice using three extra bits. It assumes you can decode a single or double precision number, including the hidden leading 1, and that you can write binary fractions such as 0.011b.
Why rounding happens
A normalized number has the form 1.f × 2^e. Single precision keeps 23 bits of f, so its significand has 24 bits of precision. Adding, multiplying or converting two such numbers can produce a longer significand. Examples are aligning exponents before an addition and the 48-bit product of two 24-bit significands. The hardware must replace the exact result with a representable neighbour.
The spacing between neighbours at exponent e is one ulp (unit in the last place). For a fraction field of p−1 bits, 1 ulp = 2^(e−(p−1)). Every rounding rule below picks one of the two neighbours that surround the exact value.
The four IEEE 754 rounding modes
| Mode | Rule | Max error |
|---|---|---|
| Round to nearest, ties to even (default) | Pick the closer neighbour. On an exact tie, pick the one whose last fraction bit is 0. | 0.5 ulp |
| Round toward zero (truncate) | Drop the extra bits. | under 1 ulp |
| Round toward +∞ | Pick the larger neighbour. | under 1 ulp |
| Round toward −∞ | Pick the smaller neighbour. | under 1 ulp |
Take a 3-bit fraction and the exact value ±1.010|101 × 2^e, where the bar separates kept bits from discarded bits. The discarded part is nonzero and more than half an ulp.
| Mode | Positive value | Negative value |
|---|---|---|
| Nearest, ties to even | 1.011 | −1.011 |
| Toward zero | 1.010 | −1.010 |
| Toward +∞ | 1.011 | −1.010 |
| Toward −∞ | 1.010 | −1.011 |
The directed modes depend on the sign. Rounding toward −∞ moves a negative number's magnitude up. Directed modes also leave exact values unchanged, so they only act when the discarded bits are not all zero.
Why ties go to even
Round half up always breaks ties in one direction, so errors in long sums drift the same way. Ties-to-even rounds up about half the time and down about half the time, so the bias cancels. A tie means the discarded bits are exactly 1000...0.
Take a 3-bit fraction:
- 1.001|1 lies halfway between 1.001 and 1.010. The result is 1.010, which is even.
- 1.010|1 lies halfway between 1.010 and 1.011. The result is 1.010, which is even.
Guard, round and sticky bits
Hardware cannot keep an unbounded number of discarded bits, so it keeps three:
- Guard (G): the first discarded bit, worth half an ulp.
- Round (R): the second discarded bit.
- Sticky (S): the OR of every bit beyond R. It is 1 if any lower bit was nonzero.
G tells you whether the discarded part is at least half an ulp. R and S tell you whether it is exactly half or more than half. With G = 1, the pair (R, S) = (0, 0) is a tie. Any other value of (R, S) is above half. With G = 0 the discarded part is below half, so you round down.
Round to nearest, ties to even, rounds up exactly when G = 1 and (R = 1, or S = 1, or LSB = 1). LSB is the last kept bit.
Why three bits and not one
Addition shows why. To add two numbers, the hardware shifts the smaller operand right to match exponents, and bits fall off the right end. Two things can then happen to the result:
- If the operands are subtracted and nearly cancel, the result must shift left to renormalize. A left shift of one position pulls G into the fraction, and R becomes the new guard. Without R, the new guard would be unknown.
- If the result carries out, it shifts right by one, and the old LSB becomes the guard.
The sticky bit stores whether anything nonzero was shifted past R. Without it, you could not tell an exact tie from a value just above a tie, and ties-to-even would give wrong answers. With G, R and S, the hardware gets the same result as computing the exact answer and then rounding it once.
Worked examples
Use a toy format with 3 fraction bits (4-bit significand), round to nearest even.
Example 1: below half. Add 13 (1.101 × 2^3) and 1.125 (1.001 × 2^0). Shift the second operand right by 3 to get 0.001001 × 2^3. The sum is:
1.101000 × 2^3
+ 0.001001 × 2^3
= 1.110001 × 2^3The kept fraction is 110. The discarded bits are 001, so G = 0, R = 0, S = 1. G is 0, so round down to 1.110 × 2^3 = 14. The exact sum is 14.125. The error is 0.125, well within half an ulp (ulp = 1 here).
Example 2: exact tie. Add 8 (1.000 × 2^3) and 0.5. The exact sum 8.5 is 1.0001 × 2^3. The kept bits are 000 and the discarded bits are 1, 0, 0, so G = 1, R = 0, S = 0. This is a tie, and LSB = 0, so the result stays at 8.
Now add 9 (1.001 × 2^3) and 0.5. The sum 9.5 is 1.0011 × 2^3. The kept bits are 001, with G = 1 and R = S = 0, so this is a tie again. LSB = 1, so round up to 1.010 × 2^3 = 10. Adding 0.5 changed 8 by nothing and 9 by 1, which is the tie rule at work.
Running it
The function below drops k low bits of an integer significand and rounds to nearest even using G, S and the LSB. It then compares the rule with real float32 conversion, which Python performs with round to nearest even.
import struct
def round_nearest_even(m, k):
"""Drop the k low bits of non-negative integer m, nearest, ties to even."""
if k == 0:
return m
kept = m >> k
guard = (m >> (k - 1)) & 1
sticky = (m & ((1 << (k - 1)) - 1)) != 0 # R and everything below
lsb = kept & 1
if guard and (sticky or lsb):
kept += 1
return kept
for m in (0b1001100, 0b1010100, 0b1010101, 0b1010011):
print(format(m, '07b'), '->', format(round_nearest_even(m, 3), 'b'))
# 1001100 -> 1010 (tie, LSB 1, up)
# 1010100 -> 1010 (tie, LSB 0, stays)
# 1010101 -> 1011 (above half, up)
# 1010011 -> 1010 (below half, down)
def f32(x):
return struct.unpack('>f', struct.pack('>f', x))[0]
print(f32(16777217.0)) # 16777216.0 (2^24 + 1 is a tie, even neighbour wins)
print(f32(16777219.0)) # 16777220.0 (tie, 16777220 has the even fraction)
print(f32(16777218.0)) # 16777218.0 (exactly representable)Above 2^24 a float32 has spacing 2, so every odd integer is a tie. This is why 16777217 is the first integer a single precision float cannot hold.
Error bounds for GATE-style questions
Let p be the significand precision, including the hidden bit: p = 24 for single and p = 53 for double.
- Round to nearest: absolute error is at most 0.5 ulp, and relative error is at most 2^−p. This quantity is the unit roundoff, which is half of machine epsilon. Single: 2^−24. Double: 2^−53.
- Directed modes and truncation: error is under 1 ulp, and relative error is under 2^−(p−1), which is machine epsilon.
- Truncation always biases the result toward zero, so errors accumulate in one direction.
Common errors
- Using round half up, where ties-to-even is correct.
- Ignoring S. A discarded part of 1000...01 is above half, but 1000...00 is a tie.
- Rounding a negative number toward −∞ by truncating its magnitude. For negatives, truncating the magnitude moves toward zero, which is the +∞ direction.
- Checking ties-to-even on the wrong bit. The relevant bit is the last kept bit, not G.
- Forgetting the renormalization after a carry-out.
Exercise
A format has a sign bit, a hidden leading 1 and 3 fraction bits. Compute 1.011 × 2^2 + 1.100 × 2^-1 exactly, then give the stored result under round to nearest even and under round toward +∞.
Show answer
The operands are 5.5 and 0.75, so the exact sum is 6.25 = 110.01b = 1.1001 × 2^2. Kept fraction bits: 100. Discarded: 1 followed by zeros, so G = 1, R = 0, S = 0, which is a tie. Neighbours are 1.100 × 2^2 = 6.0 and 1.101 × 2^2 = 6.5. Nearest even picks 1.100 (LSB 0), so the result is 6.0. Round toward +∞ rounds up because the discarded part is nonzero and the value is positive, giving 1.101 × 2^2 = 6.5.
Exercise
In single precision with round to nearest, what is the largest possible relative error of a single rounding, and which stored float does 16777219.0 become?
Show answer
The relative error is at most 2^-24, which is half of machine epsilon 2^-23. 16777219 = 2^24 + 3 lies between 16777218 and 16777220, spaced 2 apart. It is exactly halfway, so ties-to-even applies. 16777220 = 2^24 + 4 has a fraction field ending in 0 (value 2), while 16777218 has a fraction field ending in 1 (value 1), so the stored value is 16777220.