Saturating Arithmetic at a Fixed Boundary
Saturating arithmetic clamps a result to the representable range. It gives a different result from wraparound when a mathematical result exceeds that range.
- Source
- Anbloq
- Language
- English
- Created
- Updated
- Chapters
- 4
- Reading time
- 1 min
In this article4 chapters
Compare two values
Adding five to 253 in an unsigned eight-bit saturated field returns 255. Under modulo 256 wraparound it gives two. The unsigned range determines the bounds in both cases.
Decide whether lost precision matters
Clamping audio sample levels can preserve an output bound but loses original magnitude beyond it. Use a wider intermediate value when later processing needs that magnitude. Fixed-width and large integer types guide the storage choice.[1]
Compare results on opposite sides of the maximum
For an unsigned range zero through fifteen, adding four to thirteen gives seventeen mathematically. Saturation clips to fifteen. Wraparound modulo sixteen gives one. The two rules agree for additions whose exact result remains in range. See wraparound counters.
Exact result Saturating Wrapping
14 14 14
15 15 15
17 15 1
Choose the rule that matches the quantity
Saturating brightness can avoid turning extra brightness into a low value. A sequence counter may need wraparound instead. Repeated saturating operations with mixed signs can depend on grouping. Test both the lower limit when subtraction is allowed and the upper limit.