Checked
trait Checked<S> Import
Source: use arith::{Checked}; 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#
| Name | Description |
|---|---|
checked_add | a + b, or None when it does not fit. |
checked_sub | a - b, or None when it does not fit. |
checked_mul | a * b, or None when it does not fit. |
checked_div | a / b, or None when b is zero or the quotient does not fit. |
checked_rem | a % b, or None when b is zero. |
checked_neg | -a, or None when it does not fit. |
checked_pow | a ** exp, or None when it does not fit or exp is negative. |
checked_shl | a << n, or None when n is outside 0..=63. |
checked_shr | a >> 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#
intWrapping: Integer arithmetic that wraps around modulo 2^64 instead of panicking.Saturating: Integer arithmetic that clamps toINT_MIN/INT_MAXinstead of panicking.Overflowing: Integer arithmetic that returns the wrapped result together with whether it wrapped.