Module coil-stdlib

arith

Plain +, -, * and ** on int panic when the result does not fit. Import one of these traits to say what should happen instead: Checked gives an Option, Wrapping wraps modulo 2^64, Saturating clamps, and Overflowing returns the wrapped value with a flag.

use arith::{Checked, Wrapping};
use tests::{assert};

fn main() {
    assert!(9223372036854775807.checked_add(1).is_none());
    assert!(31.wrapping_pow(20) != 0);
}

Part of coil-stdlib. spool projects get it automatically; with plain coil, pass --root ~/.coil/stdlib/src.

Import what you use (use path::* is not allowed):

use arith::{Checked, Wrapping};

Traits#

NameDescription
CheckedInteger arithmetic that returns None instead of panicking when the result does not fit.
WrappingInteger arithmetic that wraps around modulo 2^64 instead of panicking.
SaturatingInteger arithmetic that clamps to INT_MIN / INT_MAX instead of panicking.
OverflowingInteger arithmetic that returns the wrapped result together with whether it wrapped.

Constants#

NameDescription
INT_MAXThe largest int, 9223372036854775807.
INT_MINThe smallest int, -9223372036854775808.
↑ ↓ navigate ↵ open