Struct BigNumBase

Source
pub struct BigNumBase<T>
where T: Base,
{ pub sig: u64, pub exp: u64, pub base: T, }
Expand description

This is the main struct for bignumbe-rs.

It takes a generic argument for the base, e.g. BigNumBase<Binary>. It is recommended to either create a custom type alias or use one of the predefined ones (BigNumBin, BigNumOct, BigNumDec, BigNumHex). You should be able to use them pretty much exactly like other numbers in most contexts. For convenience I define From and all math operations for u64, but keep in mind that the From implementation, like new, involves recalculating the base ranges.

use bignumbe_rs::{BigNumBase, Binary};

type BigNum = BigNumBase<Binary>;

let bn1 = BigNum::from(1);
let bn2 = BigNum::from(u64::MAX);

// Since this operation's result doesn't fit in `u64` it wraps over to the minimum
// significand and increments the `exp`
assert_eq!(bn1 + bn2, BigNum::new(1 << 63, 1));

assert_eq!(bn1 / bn2, BigNum::from(0));
assert_eq!(bn1 * bn2, bn2);
assert_eq!(bn2 * bn2, BigNum::new(u64::MAX - 1, 64));

Fields§

§sig: u64§exp: u64§base: T

Implementations§

Source§

impl<T> BigNumBase<T>
where T: Base,

Source

pub fn new(sig: u64, exp: u64) -> Self

Creates a new BigNumBase instance that represents the value sig * T::NUMBER^exp. E.g. BigNumBin::new(12341234, 12341) represents 12341234 * 2^12341. This method will perform normalization if necessary, to ensure the significand is in the valid range (if the number is non-compact). As such when creating a BigNum from scratch you should always use this unless you absolutely need a raw constructor

Source

pub fn new_raw(sig: u64, exp: u64) -> Self

Creates a BigNumBase directly from values, panicking if not possible. This is mostly for testing but may be more performant on inputs that are guaranteed valid

Source

pub fn fuzzy_eq(self, other: Self, margin: u64) -> bool

Allows fuzzy comparison between two values. Since operations can result in loss of precision this allows you to compare values that may have drifted. Since each operation can result in an error of 1, an upper bound is the sum of the number of operations performed on each operand. E.g. for n: BigNumDec, to ensure that (n * 1000) / 500 = (n / 500) * 1000, you might use a margin of 4

Trait Implementations§

Source§

impl<T> Add<BigNumBase<T>> for u64
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: BigNumBase<T>) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> Add<u64> for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: u64) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> Add for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T> AddAssign<u64> for BigNumBase<T>
where T: Base,

Source§

fn add_assign(&mut self, rhs: u64)

Performs the += operation. Read more
Source§

impl<T> AddAssign for BigNumBase<T>
where T: Base,

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<T> Clone for BigNumBase<T>
where T: Base + Clone,

Source§

fn clone(&self) -> BigNumBase<T>

Returns a copy 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<T> Debug for BigNumBase<T>
where T: Base + Debug,

Source§

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

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

impl Display for BigNumBase<Decimal>

Source§

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

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

impl<T> Div<BigNumBase<T>> for u64
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: BigNumBase<T>) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> Div<u64> for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u64) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> Div for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self::Output

Performs the / operation. Read more
Source§

impl<T> DivAssign<u64> for BigNumBase<T>
where T: Base,

Source§

fn div_assign(&mut self, rhs: u64)

Performs the /= operation. Read more
Source§

impl<T> From<u64> for BigNumBase<T>
where T: Base,

Source§

fn from(value: u64) -> Self

Converts to this type from the input type.
Source§

impl<T> Mul<BigNumBase<T>> for u64
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: BigNumBase<T>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T> Mul<f64> for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Self::Output

Performs the * operation. Read more
Source§

impl<T> Mul<u64> for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u64) -> Self::Output

Performs the * operation. Read more
Source§

impl<T> Mul for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T> MulAssign<f64> for BigNumBase<T>
where T: Base,

Source§

fn mul_assign(&mut self, rhs: f64)

Performs the *= operation. Read more
Source§

impl<T> MulAssign<u64> for BigNumBase<T>
where T: Base,

Source§

fn mul_assign(&mut self, rhs: u64)

Performs the *= operation. Read more
Source§

impl<T> MulAssign for BigNumBase<T>
where T: Base,

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl<T> Ord for BigNumBase<T>
where T: Base,

Source§

fn cmp(&self, other: &Self) -> 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<T> PartialEq for BigNumBase<T>
where T: Base,

Source§

fn eq(&self, other: &Self) -> 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<T> PartialOrd for BigNumBase<T>
where T: Base,

Source§

fn partial_cmp(&self, other: &Self) -> 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<T> Pred for BigNumBase<T>
where T: Base,

Source§

fn pred(self) -> Self

Source§

impl<T> Product for BigNumBase<T>
where T: Base,

Source§

fn product<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by multiplying the items.
Source§

impl<T> Shl<u64> for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: u64) -> Self::Output

Performs the << operation. Read more
Source§

impl<T> Shr<u64> for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: u64) -> Self::Output

Performs the >> operation. Read more
Source§

impl<T> Sub<BigNumBase<T>> for u64
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: BigNumBase<T>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> Sub<u64> for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: u64) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> Sub for BigNumBase<T>
where T: Base,

Source§

type Output = BigNumBase<T>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl<T> SubAssign<u64> for BigNumBase<T>
where T: Base,

Source§

fn sub_assign(&mut self, rhs: u64)

Performs the -= operation. Read more
Source§

impl<T> SubAssign for BigNumBase<T>
where T: Base,

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<T> Succ for BigNumBase<T>
where T: Base,

Source§

fn succ(self) -> Self

Source§

impl<T> Sum for BigNumBase<T>
where T: Base,

Source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<T> Copy for BigNumBase<T>
where T: Base + Copy,

Source§

impl<T> Eq for BigNumBase<T>
where T: Base,

Auto Trait Implementations§

§

impl<T> Freeze for BigNumBase<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for BigNumBase<T>
where T: RefUnwindSafe,

§

impl<T> Send for BigNumBase<T>
where T: Send,

§

impl<T> Sync for BigNumBase<T>
where T: Sync,

§

impl<T> Unpin for BigNumBase<T>
where T: Unpin,

§

impl<T> UnwindSafe for BigNumBase<T>
where T: UnwindSafe,

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, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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.