Struct Num

Source
pub struct Num(/* private fields */);
Expand description

An unlimited precision number type with some improvements and customizations over BigRational.

Implementations§

Source§

impl Num

Source

pub fn new<T, U>(numer: T, denom: U) -> Self
where BigInt: From<T> + From<U>,

Construct a rational number from its two components.

Source§

impl Num

Source

pub fn round(&self) -> Self

Round the given Num to the nearest integer.

Rounding happens based on the Round-Half-To-Even scheme (also known as the “bankers rounding” algorithm), which rounds to the closest integer as expected but if the fractional part is exactly 1/2 (i.e., equidistant from two integers) it rounds to the even one of the two.

Source

pub fn round_with(&self, precision: usize) -> Self

Round the given Num with the given precision.

Rounding happens based on the Round-Half-To-Even scheme similar to round.

Source

pub fn trunc(&self) -> Self

Round the given Num towards zero.

Source

pub fn fract(&self) -> Self

Return the fractional part of the given Num, with division rounded towards zero.

Source

pub fn to_integer(&self) -> BigInt

Convert the given Num to an integer, rounding towards zero.

Source

pub fn to_i64(&self) -> Option<i64>

Convert the given Num into a u64.

The value will be converted into an integer, with rounding towards zero. None is returned if the resulting integer does not fit into 64 bit.

Source

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

Convert the given Num into a u64.

The value will be converted into an integer, with rounding towards zero. None is returned if the resulting integer does not fit into 64 bit.

Source

pub fn to_f64(&self) -> Option<f64>

Convert the given Num into a f64.

None is returned if the numerator or the denominator cannot be converted into an f64.

Source

pub fn is_zero(&self) -> bool

Check if the given Num is zero.

Source

pub fn is_positive(&self) -> bool

Check if the given Num is positive.

Source

pub fn is_negative(&self) -> bool

Check if the given Num is negative.

Source

pub fn display(&self) -> CustomDisplay<'_>

Retrieve a display adapter that can be used for some more elaborate formatting needs.

Trait Implementations§

Source§

impl Add<&Num> for &Num

Source§

type Output = Num

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<&Num> for Num

Source§

type Output = Num

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add<Num> for &Num

Source§

type Output = Num

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T> Add<T> for &Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<T> Add<T> for Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl Add for Num

Source§

type Output = Num

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl AddAssign<&Num> for Num

Source§

fn add_assign(&mut self, rhs: &Num)

Performs the += operation. Read more
Source§

impl<T> AddAssign<T> for Num
where BigInt: From<T>,

Source§

fn add_assign(&mut self, rhs: T)

Performs the += operation. Read more
Source§

impl AddAssign for Num

Source§

fn add_assign(&mut self, rhs: Num)

Performs the += operation. Read more
Source§

impl Clone for Num

Source§

fn clone(&self) -> Num

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 Num

Source§

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

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

impl Default for Num

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Num

Source§

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

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

impl Div<&Num> for &Num

Source§

type Output = Num

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div<&Num> for Num

Source§

type Output = Num

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div<Num> for &Num

Source§

type Output = Num

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T> Div<T> for &Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<T> Div<T> for Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div for Num

Source§

type Output = Num

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl DivAssign<&Num> for Num

Source§

fn div_assign(&mut self, rhs: &Num)

Performs the /= operation. Read more
Source§

impl<T> DivAssign<T> for Num
where BigInt: From<T>,

Source§

fn div_assign(&mut self, rhs: T)

Performs the /= operation. Read more
Source§

impl DivAssign for Num

Source§

fn div_assign(&mut self, rhs: Num)

Performs the /= operation. Read more
Source§

impl From<BigInt> for Num

Source§

fn from(val: BigInt) -> Self

Converts to this type from the input type.
Source§

impl From<Num> for (BigInt, BigInt)

Source§

fn from(other: Num) -> Self

Converts to this type from the input type.
Source§

impl From<Num32> for Num

Source§

fn from(other: Num32) -> Num

Converts to this type from the input type.
Source§

impl From<Num64> for Num

Source§

fn from(other: Num64) -> Num

Converts to this type from the input type.
Source§

impl From<i128> for Num

Source§

fn from(val: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Num

Source§

fn from(val: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Num

Source§

fn from(val: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Num

Source§

fn from(val: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Num

Source§

fn from(val: i8) -> Self

Converts to this type from the input type.
Source§

impl From<isize> for Num

Source§

fn from(val: isize) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Num

Source§

fn from(val: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Num

Source§

fn from(val: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Num

Source§

fn from(val: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Num

Source§

fn from(val: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Num

Source§

fn from(val: u8) -> Self

Converts to this type from the input type.
Source§

impl From<usize> for Num

Source§

fn from(val: usize) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Num

Source§

type Err = ParseNumError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Num

Source§

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

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 Mul<&Num> for &Num

Source§

type Output = Num

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<&Num> for Num

Source§

type Output = Num

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Num> for &Num

Source§

type Output = Num

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<T> Mul<T> for &Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<T> Mul<T> for Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul for Num

Source§

type Output = Num

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl MulAssign<&Num> for Num

Source§

fn mul_assign(&mut self, rhs: &Num)

Performs the *= operation. Read more
Source§

impl<T> MulAssign<T> for Num
where BigInt: From<T>,

Source§

fn mul_assign(&mut self, rhs: T)

Performs the *= operation. Read more
Source§

impl MulAssign for Num

Source§

fn mul_assign(&mut self, rhs: Num)

Performs the *= operation. Read more
Source§

impl Neg for &Num

Source§

type Output = Num

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Neg for Num

Source§

type Output = Num

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Ord for Num

Source§

fn cmp(&self, other: &Num) -> 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 Num

Source§

fn eq(&self, other: &Num) -> 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 Num

Source§

fn partial_cmp(&self, other: &Num) -> 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 Rem<&Num> for &Num

Source§

type Output = Num

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: &Num) -> Self::Output

Performs the % operation. Read more
Source§

impl Rem<&Num> for Num

Source§

type Output = Num

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: &Num) -> Self::Output

Performs the % operation. Read more
Source§

impl Rem<Num> for &Num

Source§

type Output = Num

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Num) -> Self::Output

Performs the % operation. Read more
Source§

impl<T> Rem<T> for &Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: T) -> Self::Output

Performs the % operation. Read more
Source§

impl<T> Rem<T> for Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: T) -> Self::Output

Performs the % operation. Read more
Source§

impl Rem for Num

Source§

type Output = Num

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Num) -> Self::Output

Performs the % operation. Read more
Source§

impl RemAssign<&Num> for Num

Source§

fn rem_assign(&mut self, rhs: &Num)

Performs the %= operation. Read more
Source§

impl<T> RemAssign<T> for Num
where BigInt: From<T>,

Source§

fn rem_assign(&mut self, rhs: T)

Performs the %= operation. Read more
Source§

impl RemAssign for Num

Source§

fn rem_assign(&mut self, rhs: Num)

Performs the %= operation. Read more
Source§

impl Sub<&Num> for &Num

Source§

type Output = Num

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<&Num> for Num

Source§

type Output = Num

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub<Num> for &Num

Source§

type Output = Num

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<T> Sub<T> for &Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<T> Sub<T> for Num
where BigInt: From<T>,

Source§

type Output = Num

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl Sub for Num

Source§

type Output = Num

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl SubAssign<&Num> for Num

Source§

fn sub_assign(&mut self, rhs: &Num)

Performs the -= operation. Read more
Source§

impl<T> SubAssign<T> for Num
where BigInt: From<T>,

Source§

fn sub_assign(&mut self, rhs: T)

Performs the -= operation. Read more
Source§

impl SubAssign for Num

Source§

fn sub_assign(&mut self, rhs: Num)

Performs the -= operation. Read more
Source§

impl TryFrom<&Num> for Num32

Source§

type Error = ()

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

fn try_from(other: &Num) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&Num> for Num64

Source§

type Error = ()

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

fn try_from(other: &Num) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Num> for Num32

Source§

type Error = ()

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

fn try_from(other: Num) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Num> for Num64

Source§

type Error = ()

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

fn try_from(other: Num) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Eq for Num

Source§

impl StructuralPartialEq for Num

Auto Trait Implementations§

§

impl Freeze for Num

§

impl RefUnwindSafe for Num

§

impl Send for Num

§

impl Sync for Num

§

impl Unpin for Num

§

impl UnwindSafe for Num

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.
Source§

impl<T, Rhs> NumAssignOps<Rhs> for T
where T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,

Source§

impl<T, Base> RefNum<Base> for T
where T: NumOps<Base, Base> + for<'r> NumOps<&'r Base, Base>,