rokdocs
v0.1

rok::core::num::bigint

rok::core::num::bigint: int, the arbitrary-precision integer (stdlib 2.1.2, 2.1.6, 2.1.7). #[keyword("int")] routes the type name here; an unsuffixed literal in an int slot is built by Int::from_i64 / Int::from_literal (refs-lang 1.6.3).

A value that fits i64 lives in small, anything larger is sign + magnitude (IntMag, base-2^32 limbs). Results that fit i64 always drop back to small, so == / hash stay a field compare (spec 2.1.2 keeps promoted values big; the difference is not observable). Limb buffers are never written once in an Int, so copies share them (int is Copy, 2.13.3).

add / sub / shifts / products / Knuth D / gcd are runtime kernels (rokr/src/bignum.rs). Here: Burnikel-Ziegler division above BZ limbs, Barrett above MU, divide-and-conquer radix conversion over chunk^(2^i), small remainders from a product mod B^m - 1, and cached NTT transforms for an operand multiplied many times.

Structs

Int

pub struct Int

An arbitrary-precision integer (spec 2.1.2). Write it int.

  • pub const fn new() -> Int

    Zero.

  • pub const fn from_i64(n: i64) -> Int
  • pub const fn from_i32(n: i32) -> Int
  • pub const fn from_u64(n: u64) -> Int
  • pub fn from_literal(s: str) -> Int

    An integer literal's digits (_, 0x / 0o / 0b allowed): what an unsuffixed literal too big for i64 in an int slot lowers to.

  • pub fn parse_radix(s: str, radix: u32) -> Int | none

    s in base radix (2..=36): an optional sign, then digits of either case; none otherwise. The grammar is the runtime's, the same the primitives' FromStr reads.

  • pub fn to_str_radix(self, radix: u32) -> str

    The digits in base radix (2..=36, lowercase), - first when negative.

  • pub const fn to_i32(self) -> i32 | none
  • pub const fn to_i64(self) -> i64 | none
  • pub const fn to_u64(self) -> u64 | none
  • pub fn to_f64(self) -> f64

    The nearest f64 (ties to even; ±inf past f64::MAX).

  • pub const fn is_zero(self) -> bool
  • pub const fn is_negative(self) -> bool
  • pub const fn is_positive(self) -> bool
  • pub const fn signum(self) -> Int

    -1, 0 or 1.

  • pub const fn abs(self) -> Int
  • pub const fn bit_length(self) -> u64

    Bits of |self| without leading zeros (0 for zero; Python's bit_length).

  • pub fn pow(self, exp: u32) -> Int

    self^exp (square and multiply).

  • pub fn modpow(self, exp: Int, m: Int) -> Int

    self^exp %% m: in 0..m for m > 0, m + 1..=0 for m < 0 (the sign of m, as %%). Panics if m is zero or exp negative.

  • pub fn gcd(self, other: Int) -> Int

    The greatest common divisor, never negative (gcd(0, 0) == 0). A runtime kernel (half-gcd reductions, then Lehmer's algorithm) after one division that brings the operands to about the same length.

  • pub fn lcm(self, other: Int) -> Int

    The least common multiple, never negative (0 if either is).

  • pub fn div_euclid(self, rhs: Int) -> Int

    The quotient rounded so that rem_euclid is never negative; panics on zero.

  • pub fn rem_euclid(self, rhs: Int) -> Int

    The remainder in 0..rhs.abs(); panics on zero.

  • pub fn isqrt(self) -> Int

    floor(sqrt(self)); panics on a negative number.

  • pub fn checked_isqrt(self) -> Int | none

    floor(sqrt(self)), none for a negative number. Newton steps doubling the precision (the recursion of Python's math.isqrt): each refines the root of the top 2d bits from that of the top 2e bits, e = d / 2.

  • pub fn div_rem(self, rhs: Int) -> (Int, Int)

    (self / rhs, self % rhs) in one division (truncating, like / and %); panics on zero.

Trait implementations 36
  • impl Add for Int
  • impl Sub for Int
  • impl Mul for Int
  • impl Div for Int
  • impl Rem for Int
  • impl Modulo for Int
  • impl Neg for Int
  • impl BitAnd for Int
  • impl BitOr for Int
  • impl BitXor for Int
  • impl BitNot for Int
  • impl Shl<u32> for Int
  • impl Shr<u32> for Int
  • impl PartialEq for Int
  • impl Eq for Int
  • impl PartialOrd for Int
  • impl Ord for Int
  • impl Hash for Int
  • impl Clone for Int
  • impl Copy for Int
  • impl Default for Int
  • impl Zero for Int
  • impl One for Int
  • impl Display for Int
  • impl Debug for Int
  • impl FromStr for Int
  • impl From<i8> for Int
  • impl From<i16> for Int
  • impl From<i32> for Int
  • impl From<i64> for Int
  • impl From<isize> for Int
  • impl From<u8> for Int
  • impl From<u16> for Int
  • impl From<u32> for Int
  • impl From<u64> for Int
  • impl From<usize> for Int