Trait in arith coil-stdlib

Saturating

trait Saturating<S>
Import
use arith::{Saturating};
Source: src/arith.hy

Integer arithmetic that clamps to INT_MIN / INT_MAX instead of panicking.

Implemented for int.

Required methods#

NameDescription
saturating_adda + b clamped to the type’s range.
saturating_suba - b clamped to the type’s range.
saturating_mula * b clamped to the type’s range.
saturating_powa ** 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_MIN
  • arith::INT_MAX
  • int
  • Checked: Integer arithmetic that returns None instead 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.
↑ ↓ navigate ↵ open