Overflowing
trait Overflowing<S> Import
Source: use arith::{Overflowing}; src/arith.hy Integer arithmetic that returns the wrapped result together with whether it wrapped.
Implemented for int.
Required methods#
| Name | Description |
|---|---|
overflowing_add | The wrapped a + b, and whether it wrapped. |
overflowing_sub | The wrapped a - b, and whether it wrapped. |
overflowing_mul | The wrapped a * b, and whether it wrapped. |
overflowing_neg | The wrapped -a, and whether it wrapped. |
overflowing_pow | The wrapped a ** exp, and whether it wrapped. |
overflowing_shl | a << (n & 63), and whether n was outside 0..=63. |
overflowing_shr | a >> (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#
intChecked: Integer arithmetic that returnsNoneinstead 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 toINT_MIN/INT_MAXinstead of panicking.