pub enum Int {
Small(i64),
Big(Arc<BigInt>),
}Expand description
An integer: inline when it fits in an i64, otherwise a shared big integer.
Constructors keep the representation canonical (Big only outside i64 range);
equality, ordering and hashing are numeric even for a hand-built non-canonical Big.
Variants§
Implementations§
Source§impl Int
impl Int
Sourcepub fn from_big(b: BigInt) -> Self
pub fn from_big(b: BigInt) -> Self
Converts a big integer, keeping the representation canonical.
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Whether the value is negative.
Sourcepub fn from_literal(digits: &str, mode: IntMode) -> Result<Self>
pub fn from_literal(digits: &str, mode: IntMode) -> Result<Self>
Parses a decimal numeral as written in a spec.
In IntMode::Tlc a numeral above 2^31 - 1 is EC.TLC_INTEGER_TOO_BIG, even
when it is the operand of unary minus (-2147483648 is rejected, as in TLC).
§Errors
ErrorKind::IntegerTooBig in TLC mode; ErrorKind::Argument if digits is
not a non-empty string of ASCII digits.
Sourcepub fn from_literal_value(
value: BigInt,
text: &str,
mode: IntMode,
) -> Result<Self>
pub fn from_literal_value( value: BigInt, text: &str, mode: IntMode, ) -> Result<Self>
Accepts a numeral already converted to a big integer (for \b, \o, \h
numerals). text is the numeral as SANY prints it, used in the error message.
§Errors
ErrorKind::IntegerTooBig in TLC mode when the value exceeds 2^31 - 1.
Sourcepub fn neg(&self, mode: IntMode) -> Result<Self>
pub fn neg(&self, mode: IntMode) -> Result<Self>
Unary minus, -a (Integers.Neg).
§Errors
ErrorKind::Overflow in TLC mode for -(-2^31)
(Overflow when computing --2147483648).
Sourcepub fn div(&self, other: &Self, mode: IntMode) -> Result<Self>
pub fn div(&self, other: &Self, mode: IntMode) -> Result<Self>
Floored division, a \div b (Integers.Divide).
§Errors
ErrorKind::DivisionByZero when b = 0; ErrorKind::Overflow in TLC mode
for -2^31 \div -1.
Sourcepub fn div_naturals(&self, other: &Self, mode: IntMode) -> Result<Self>
pub fn div_naturals(&self, other: &Self, mode: IntMode) -> Result<Self>
a \div b as Naturals.Divide computes it, for specifications that load
Naturals but not Integers.
In TLC mode this ports tlc2.module.Naturals.Divide (tlaplus 94d0c50):
Java’s truncating int division, decremented when the quotient is
negative and inexact. A quotient that truncates to 0 is therefore
never decremented, so -1 \div 2 and 1 \div -2 are 0, not the floored
-1; and -2147483648 \div -1 wraps to -2147483648 instead of
overflowing. In big mode it is floored division, like Int::div.
use dive_value::{Int, IntMode};
let q = |a: i64, b: i64| Int::from(a).div_naturals(&Int::from(b), IntMode::Tlc);
assert_eq!(q(-1, 2)?, Int::from(0_i64));
assert_eq!(q(-7, 2)?, Int::from(-4_i64));
assert_eq!(q(-2_147_483_648, -1)?, Int::from(-2_147_483_648_i64));
assert_eq!(Int::from(-1_i64).div(&Int::from(2_i64), IntMode::Tlc)?, Int::from(-1_i64));§Errors
ErrorKind::DivisionByZero when b = 0.
Sourcepub fn rem(&self, other: &Self, mode: IntMode) -> Result<Self>
pub fn rem(&self, other: &Self, mode: IntMode) -> Result<Self>
a % b, defined for b > 0, with a result in 0 .. b-1 (Integers.Mod).
§Errors
ErrorKind::Argument when b <= 0.
Sourcepub fn pow(&self, other: &Self, mode: IntMode) -> Result<Self>
pub fn pow(&self, other: &Self, mode: IntMode) -> Result<Self>
a ^ b for b >= 0 (Integers.Expt).
§Errors
ErrorKind::Argument when b < 0; ErrorKind::NullPowerNull for 0^0;
ErrorKind::Overflow in TLC mode (Overflow when computing a^b);
ErrorKind::DiveLimit in big mode when the result would exceed 2^26 bits.
Trait Implementations§
impl Eq for Int
Source§impl Ord for Int
Numeric order. A Big that fits in an i64 (possible only by building the
variant directly) still compares by value.
impl Ord for Int
Numeric order. A Big that fits in an i64 (possible only by building the
variant directly) still compares by value.