Trait in arith coil-stdlib

Wrapping

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

Integer arithmetic that wraps around modulo 2^64 instead of panicking.

Useful for hashes and checksums. Implemented for int.

Required methods#

NameDescription
wrapping_adda + b modulo 2^64.
wrapping_suba - b modulo 2^64.
wrapping_mula * b modulo 2^64.
wrapping_neg-a modulo 2^64 (INT_MIN stays INT_MIN).
wrapping_powa ** exp modulo 2^64.
wrapping_shla << (n & 63): the count is taken modulo 64.
wrapping_shra >> (n & 63): the count is taken modulo 64.

Examples#

use arith::{Wrapping, INT_MAX, INT_MIN};
use tests::assert;
assert!(INT_MAX.wrapping_add(1) == INT_MIN);
assert!(1.wrapping_shl(65) == 2);

See also#

  • int
  • Checked: Integer arithmetic that returns None instead of panicking when the result does not fit.
  • Saturating: Integer arithmetic that clamps to INT_MIN / INT_MAX instead of panicking.
  • Overflowing: Integer arithmetic that returns the wrapped result together with whether it wrapped.
↑ ↓ navigate ↵ open