Trait in arith coil-stdlib

Overflowing

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

Integer arithmetic that returns the wrapped result together with whether it wrapped.

Implemented for int.

Required methods#

NameDescription
overflowing_addThe wrapped a + b, and whether it wrapped.
overflowing_subThe wrapped a - b, and whether it wrapped.
overflowing_mulThe wrapped a * b, and whether it wrapped.
overflowing_negThe wrapped -a, and whether it wrapped.
overflowing_powThe wrapped a ** exp, and whether it wrapped.
overflowing_shla << (n & 63), and whether n was outside 0..=63.
overflowing_shra >> (n & 63), and whether n was outside 0..=63.

Examples#

use arith::{Overflowing, INT_MAX, INT_MIN};
use tests::assert;
let (v, wrapped) = INT_MAX.overflowing_add(1);
assert!(v == INT_MIN && wrapped);
let (w, again) = 2.overflowing_mul(3);
assert!(w == 6 && !again);

See also#

  • 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.
  • Saturating: Integer arithmetic that clamps to INT_MIN / INT_MAX instead of panicking.
↑ ↓ navigate ↵ open