pub struct BigInt { /* private fields */ }Expand description
An exact, arbitrary-precision integer.
Implementations§
Source§impl BigInt
impl BigInt
Sourcepub fn clone_payload_bytes(&self) -> usize
pub fn clone_payload_bytes(&self) -> usize
Heap payload bytes duplicated when this integer is cloned.
Inline fields and the vector header belong to the containing value. This borrowed query neither copies limbs nor formats the integer.
Sourcepub fn to_i64(&self) -> Option<i64>
pub fn to_i64(&self) -> Option<i64>
Narrow back to an i64 iff the value fits — the basis of the “downsize when
it provably fits” path the runtime uses to stay on the fast i64 repr.
Sourcepub fn wrapping_u64(&self) -> u64
pub fn wrapping_u64(&self) -> u64
The low 64 bits, defined for every integer.
Sourcepub fn shr_bits(&self, bits: u64) -> Option<BigInt>
pub fn shr_bits(&self, bits: u64) -> Option<BigInt>
⌊self / 2^bits⌋ for a non-negative value; None for a negative one.
Sourcepub fn bitand_natural(&self, other: &BigInt) -> Option<BigInt>
pub fn bitand_natural(&self, other: &BigInt) -> Option<BigInt>
Bitwise AND of two non-negative values; None if either is negative.
Sourcepub fn bitor_natural(&self, other: &BigInt) -> Option<BigInt>
pub fn bitor_natural(&self, other: &BigInt) -> Option<BigInt>
Bitwise OR of two non-negative values; None if either is negative.
Sourcepub fn bitxor_natural(&self, other: &BigInt) -> Option<BigInt>
pub fn bitxor_natural(&self, other: &BigInt) -> Option<BigInt>
Bitwise XOR of two non-negative values; None if either is negative.
pub fn is_zero(&self) -> bool
Sourcepub fn is_odd(&self) -> bool
pub fn is_odd(&self) -> bool
Parity (the low bit of the magnitude — sign does not change it). Zero is even.
Sourcepub fn to_f64(&self) -> f64
pub fn to_f64(&self) -> f64
The binary64 nearest to this integer, ties to even, ±∞ once the rounding reaches 2^1024: the
one conversion every tier uses for an Int flowing into a Real (a widened argument, n + 0.5).
One rounding, never one per limb: the top 64 significant bits are taken exactly, every bit
below them is folded into bit 0 as a sticky bit (eleven places under the 53-bit rounding
position, so it can break a tie but never create one), and that u64 is converted with the
hardware’s round-to-nearest-even. Scaling back by a power of two is exact until it overflows,
which is exactly where the exact value rounds to infinity too.
pub fn is_negative(&self) -> bool
pub fn is_positive(&self) -> bool
Sourcepub fn mul_assign_i64(&mut self, rhs: i64)
pub fn mul_assign_i64(&mut self, rhs: i64)
In-place multiply by a machine-integer scalar, REUSING the heap magnitude buffer
(it grows by at most one limb, never reallocates from scratch). The exact-value
counterpart of self.mul(&BigInt::from_i64(rhs)) — same result, but the growing
accumulator’s Vec is mutated rather than reallocated each call, which is the hot
loop Set p to p * i (factorial). Canonical form is preserved: multiplying a
normalized magnitude by a nonzero scalar never introduces a trailing zero limb (a
zeroed low half always carries out), and a Heap value stays ≥ 2 limbs.
Sourcepub fn div_rem(&self, other: &BigInt) -> Option<(BigInt, BigInt)>
pub fn div_rem(&self, other: &BigInt) -> Option<(BigInt, BigInt)>
Truncated division toward zero: returns (quotient, remainder) with
self = q*other + r and the remainder carrying the dividend’s sign — exactly
matching Rust/i64 / and %, so the wide type is a drop-in for the narrow.
None when other is zero.
Sourcepub fn parse_decimal(s: &str) -> Option<BigInt>
pub fn parse_decimal(s: &str) -> Option<BigInt>
Parse a base-10 integer (optional leading +/-). None on any non-digit.
Sourcepub fn pow(&self, exp: u32) -> BigInt
pub fn pow(&self, exp: u32) -> BigInt
self raised to a non-negative integer power, by exponentiation-by-squaring
(0^0 == 1). Exact and unbounded — 2.pow(63) is the value i64 cannot hold.
Sourcepub fn to_le_bytes(&self) -> (bool, Vec<u8>)
pub fn to_le_bytes(&self) -> (bool, Vec<u8>)
Sign + little-endian magnitude bytes — a compact, exact serialization (the
inverse of BigInt::from_le_bytes). The wire ships these instead of a
decimal string, so there is no base conversion and no precision question.
Sourcepub fn from_le_bytes(negative: bool, bytes: &[u8]) -> BigInt
pub fn from_le_bytes(negative: bool, bytes: &[u8]) -> BigInt
Reconstruct from a sign flag and little-endian magnitude bytes (length need not be a multiple of 8; trailing zero limbs are normalized away).
Trait Implementations§
Source§impl ExactCodec for BigInt
impl ExactCodec for BigInt
Source§fn write(&self, out: &mut Vec<u8>)
fn write(&self, out: &mut Vec<u8>)
A sign byte (0 nonnegative, 1 negative), the magnitude’s length, its little-endian bytes.
fn read(buf: &[u8], pos: &mut usize) -> Option<BigInt>
fn to_bytes(&self) -> Vec<u8> ⓘ
Source§fn from_bytes(buf: &[u8]) -> Option<Self>
fn from_bytes(buf: &[u8]) -> Option<Self>
buf canonically encodes, with nothing left over.