Saturation Arithmetic
Part 1: Wikipedia
Saturation Arithmetic: all arithmetic operations are limited to a fixed range min and max value.
If result is > or < these values then it is clamped to that value. e.g.:
- (not 103)
- , rather than 3
Modern Use
In computing it is useful for getting as close numerically to the true answer as possible, rather than the wrap around that occurs with modular arithmetic.
- also enhances algorithms in many problems.
Part 2: Rust Forum Post
Integers have finite ranges (0 to ) depending on the bits n they use:
- u8: 0 - - 1 (255) There are three options to handle an arithmetic operation that results in a value that does not fit this finite range: 1) wrap around 2) saturate at the for addition and 0 for subtraction 3) check the value and result in a panic (rust specific)
Part 3: Rust std::num Documentation
Although values for u32 should never overflow, sometimes in certain debug situations a panic may result. Thus saturation can be used.
Example:
use std::num::Saturating;
let max = Saturating(u32::MAX);
let one = Saturating(1u32);
assert_eq!(u32::MAX, (max_one).0);
#concepts (Private) #computerscience (Private) #arithmetic (Private) #algorithms (Private) #notes (Private) #research (Private) #explanation (Private) #reference (Private) #saturation (Private) #saturation_arithmetic (Private) #mathematics (Private) #rust (Private) #rustlang (Private)