Skip to main content

BigInt

Struct BigInt 

Source
pub struct BigInt { /* private fields */ }
Expand description

An exact, arbitrary-precision integer.

Implementations§

Source§

impl BigInt

Source

pub fn zero() -> BigInt

The additive identity.

Source

pub fn from_i64(x: i64) -> BigInt

Exact widening from a machine integer.

Source

pub fn from_u64(x: u64) -> BigInt

Exact widening from an unsigned machine integer.

Source

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.

Source

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.

Source

pub fn to_u64(&self) -> Option<u64>

The value as a u64 when it is non-negative and fits.

Source

pub fn wrapping_u64(&self) -> u64

The low 64 bits, defined for every integer.

Source

pub fn bit_len(&self) -> u64

Number of significant bits of the magnitude (0 for zero).

Source

pub fn gcd(&self, other: &BigInt) -> BigInt

gcd(|self|, |other|), with gcd(a, 0) = |a|.

Source

pub fn shl_bits(&self, bits: u64) -> BigInt

self · 2^bits (exact for either sign).

Source

pub fn shr_bits(&self, bits: u64) -> Option<BigInt>

⌊self / 2^bits⌋ for a non-negative value; None for a negative one.

Source

pub fn bitand_natural(&self, other: &BigInt) -> Option<BigInt>

Bitwise AND of two non-negative values; None if either is negative.

Source

pub fn bitor_natural(&self, other: &BigInt) -> Option<BigInt>

Bitwise OR of two non-negative values; None if either is negative.

Source

pub fn bitxor_natural(&self, other: &BigInt) -> Option<BigInt>

Bitwise XOR of two non-negative values; None if either is negative.

Source

pub fn is_zero(&self) -> bool

Source

pub fn is_odd(&self) -> bool

Parity (the low bit of the magnitude — sign does not change it). Zero is even.

Source

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.

Source

pub fn is_negative(&self) -> bool

Source

pub fn is_positive(&self) -> bool

Source

pub fn abs(&self) -> BigInt

The absolute value.

Source

pub fn negated(&self) -> BigInt

Additive inverse.

Source

pub fn add(&self, other: &BigInt) -> BigInt

self + other.

Source

pub fn sub(&self, other: &BigInt) -> BigInt

self - other.

Source

pub fn mul(&self, other: &BigInt) -> BigInt

self * other.

Source

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.

Source

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.

Source

pub fn parse_decimal(s: &str) -> Option<BigInt>

Parse a base-10 integer (optional leading +/-). None on any non-digit.

Source

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.

Source

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.

Source

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 Clone for BigInt

Source§

fn clone(&self) -> BigInt

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for BigInt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for BigInt

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl ExactCodec for BigInt

Source§

fn write(&self, out: &mut Vec<u8>)

A sign byte (0 nonnegative, 1 negative), the magnitude’s length, its little-endian bytes.

Source§

fn read(buf: &[u8], pos: &mut usize) -> Option<BigInt>

Source§

fn to_bytes(&self) -> Vec<u8> ⓘ

Source§

fn from_bytes(buf: &[u8]) -> Option<Self>

The value buf canonically encodes, with nothing left over.
Source§

impl From<BigInt> for Rational

Source§

fn from(x: BigInt) -> Rational

Converts to this type from the input type.
Source§

impl From<i64> for BigInt

Source§

fn from(x: i64) -> BigInt

Converts to this type from the input type.
Source§

impl Hash for BigInt

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for BigInt

Source§

fn cmp(&self, other: &BigInt) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for BigInt

Source§

fn eq(&self, other: &BigInt) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for BigInt

Source§

fn partial_cmp(&self, other: &BigInt) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for BigInt

Source§

impl StructuralPartialEq for BigInt

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.