pub struct BigInt<const BASE: usize>(/* private fields */);Expand description
BigInt: represents an arbitrary-size integer in base BASE.
BASE may be anywhere from 2-256.
If you would like to be able to represent a larger base than 256, then increase Digit
and DoubleDigit as needed, as high as u64 + u128.
Implementations§
source§impl<const BASE: usize> BigInt<BASE>
impl<const BASE: usize> BigInt<BASE>
sourcepub unsafe fn from_raw_parts(digits: Vec<Digit>) -> Self
pub unsafe fn from_raw_parts(digits: Vec<Digit>) -> Self
Create a new BigInt directly from a Vec of individual digits.
Ensure the resulting int is properly normalized, and that no digits are greater than or equal to the base, to preserve soundness.
To construct a negative BigInt from raw parts, simply apply the negation
operator (-) afterwards.
use big_int::*;
assert_eq!(
unsafe { -BigInt::<10>::from_raw_parts(vec![1, 5]) },
(-15).into()
);sourcepub fn display(&self, alphabet: &str) -> Result<String, BigIntError>
pub fn display(&self, alphabet: &str) -> Result<String, BigIntError>
Convert a BigInt to a printable string using the provided alphabet alphabet.
Display uses this method with the default alphabet STANDARD_ALPHABET.
use big_int::*;
assert_eq!(
BigInt::<10>::from(6012).display(STANDARD_ALPHABET).unwrap(),
"6012".to_string()
);sourcepub fn normalized(self) -> Self
pub fn normalized(self) -> Self
Return a normalized version of the BigInt. Remove trailing zeros, and disable the parity flag
if the resulting number is zero.
use big_int::*;
let n = unsafe { BigInt::<10>::from_raw_parts(vec![0, 0, 8, 3]) };
assert_eq!(n.normalized(), 83.into());sourcepub fn normalize(&mut self)
pub fn normalize(&mut self)
Normalize a BigInt in place. Remove trailing zeros, and disable the parity flag
if the resulting number is zero.
use big_int::*;
let mut n = unsafe { BigInt::<10>::from_raw_parts(vec![0, 0, 8, 3]) };
n.normalize();
assert_eq!(n, 83.into());sourcepub fn parse(value: &str, alphabet: &str) -> Result<Self, ParseError>
pub fn parse(value: &str, alphabet: &str) -> Result<Self, ParseError>
Parse a BigInt from a value: &str, referencing the provided alphabet
to determine what characters represent which digits. FromStr uses this method
with the default alphabet STANDARD_ALPHABET.
use big_int::*;
assert_eq!(BigInt::parse("125", STANDARD_ALPHABET), Ok(BigInt::<10>::from(125)));sourcepub fn div_rem(self, other: Self) -> Result<(Self, Self), BigIntError>
pub fn div_rem(self, other: Self) -> Result<(Self, Self), BigIntError>
Divide one BigInt by another, returning the quotient & remainder as a pair,
or an error if dividing by zero. This algorithm has a different time complexity
than BigInt::div_rem_lowmem which makes it faster for most use cases, but also uses more memory.
b - base
d - number of digits in quotient
Time complexity: O(d * log(b))
Memory complexity: O(d^2)
use big_int::*;
let a: BigInt<10> = 999_999_999.into();
let b = 56_789.into();
assert_eq!(a.div_rem(b), Ok((17_609.into(), 2_498.into())));sourcepub fn div_rem_lowmem(self, other: Self) -> Result<(Self, Self), BigIntError>
pub fn div_rem_lowmem(self, other: Self) -> Result<(Self, Self), BigIntError>
Divide one BigInt by another, returning the quotient & remainder as a pair,
or an error if dividing by zero.
b - base
d - number of digits in quotient
Time complexity: O(d * b)
Memory complexity: O(d)
use big_int::*;
let a: BigInt<10> = 999_999_999.into();
let b = 56_789.into();
assert_eq!(a.div_rem_lowmem(b), Ok((17_609.into(), 2_498.into())));sourcepub fn convert<const TO: usize>(self) -> BigInt<TO>
pub fn convert<const TO: usize>(self) -> BigInt<TO>
Convert a BigInt from its own base to another target base.
use big_int::*;
assert_eq!(
BigInt::<10>::from(99825).convert(),
BigInt::<16>::from(99825)
);sourcepub fn convert_lowmem<const TO: usize>(self) -> BigInt<TO>
pub fn convert_lowmem<const TO: usize>(self) -> BigInt<TO>
Convert a BigInt from its own base to another target base using BigInt::div_rem_lowmem.
Has lower memory usage, but greater time complexity.
use big_int::*;
assert_eq!(
BigInt::<10>::from(99825).convert_lowmem(),
BigInt::<16>::from(99825)
);Trait Implementations§
source§impl<const BASE: usize> AddAssign for BigInt<BASE>
impl<const BASE: usize> AddAssign for BigInt<BASE>
source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
+= operation. Read moresource§impl<const BASE: usize> DivAssign for BigInt<BASE>
impl<const BASE: usize> DivAssign for BigInt<BASE>
source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moresource§impl<const BASE: usize> MulAssign for BigInt<BASE>
impl<const BASE: usize> MulAssign for BigInt<BASE>
source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read moresource§impl<const BASE: usize> Ord for BigInt<BASE>
impl<const BASE: usize> Ord for BigInt<BASE>
source§impl<const BASE: usize> PartialEq for BigInt<BASE>
impl<const BASE: usize> PartialEq for BigInt<BASE>
source§impl<const BASE: usize> PartialOrd for BigInt<BASE>
impl<const BASE: usize> PartialOrd for BigInt<BASE>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moresource§impl<const BASE: usize> ShlAssign for BigInt<BASE>
impl<const BASE: usize> ShlAssign for BigInt<BASE>
source§fn shl_assign(&mut self, rhs: Self)
fn shl_assign(&mut self, rhs: Self)
Shifts a BigInt left by multiples of its BASE (not by 2).
source§impl<const BASE: usize> ShrAssign for BigInt<BASE>
impl<const BASE: usize> ShrAssign for BigInt<BASE>
source§fn shr_assign(&mut self, rhs: Self)
fn shr_assign(&mut self, rhs: Self)
Shifts a BigInt right by multiples of its BASE (not by 2).
source§impl<const BASE: usize> SubAssign for BigInt<BASE>
impl<const BASE: usize> SubAssign for BigInt<BASE>
source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
-= operation. Read more