Trait in arith coil-stdlib

Checked

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

Integer arithmetic that returns None instead of panicking when the result does not fit.

Implemented for int. Call the methods on a value: a.checked_add(b).

Required methods#

NameDescription
checked_adda + b, or None when it does not fit.
checked_suba - b, or None when it does not fit.
checked_mula * b, or None when it does not fit.
checked_diva / b, or None when b is zero or the quotient does not fit.
checked_rema % b, or None when b is zero.
checked_neg-a, or None when it does not fit.
checked_powa ** exp, or None when it does not fit or exp is negative.
checked_shla << n, or None when n is outside 0..=63.
checked_shra >> n, or None when n is outside 0..=63.

Examples#

use arith::{Checked, INT_MAX};
use tests::assert;
assert!((5.checked_add(3) ?? 0) == 8);
assert!(INT_MAX.checked_add(1).is_none());
assert!(7.checked_div(0).is_none());
assert!(3.checked_pow(40).is_none());

See also#

  • int
  • 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.
  • Overflowing: Integer arithmetic that returns the wrapped result together with whether it wrapped.
↑ ↓ navigate ↵ open