Saturating
trait Saturating<S> Import
Source: use arith::{Saturating}; src/arith.hy Integer arithmetic that clamps to INT_MIN / INT_MAX instead of panicking.
Implemented for int.
Required methods#
| Name | Description |
|---|---|
saturating_add | a + b clamped to the type’s range. |
saturating_sub | a - b clamped to the type’s range. |
saturating_mul | a * b clamped to the type’s range. |
saturating_pow | a ** exp clamped to the type’s range. |
Examples#
use arith::{Saturating, INT_MAX, INT_MIN};
use tests::assert;
assert!(INT_MAX.saturating_add(10) == INT_MAX);
assert!(INT_MIN.saturating_sub(1) == INT_MIN);
assert!(4.saturating_mul(5) == 20);
See also#
arith::INT_MINarith::INT_MAXintChecked: Integer arithmetic that returnsNoneinstead of panicking when the result does not fit.Wrapping: Integer arithmetic that wraps around modulo 2^64 instead of panicking.Overflowing: Integer arithmetic that returns the wrapped result together with whether it wrapped.