Struct hmath::Ratio

source ·
pub struct Ratio { /* private fields */ }

Implementations§

source§

impl Ratio

source

pub fn add_rat(&self, other: &Ratio) -> Self

source

pub fn add_rat_mut(&mut self, other: &Ratio)

source

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

source

pub fn add_bi_mut(&mut self, other: &BigInt)

source

pub fn add_i32(&self, other: i32) -> Self

source

pub fn add_i32_mut(&mut self, other: i32)

source§

impl Ratio

source

pub fn div_rat(&self, other: &Ratio) -> Self

source

pub fn div_rat_mut(&mut self, other: &Ratio)

source

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

source

pub fn div_bi_mut(&mut self, other: &BigInt)

source

pub fn div_i32(&self, other: i32) -> Self

source

pub fn div_i32_mut(&mut self, other: i32)

source§

impl Ratio

source

pub fn mul_rat(&self, other: &Ratio) -> Self

source

pub fn mul_rat_mut(&mut self, other: &Ratio)

source

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

source

pub fn mul_bi_mut(&mut self, other: &BigInt)

source

pub fn mul_i32(&self, other: i32) -> Self

source

pub fn mul_i32_mut(&mut self, other: i32)

source§

impl Ratio

source

pub fn pow_i32(&self, exp: i32) -> Self

source

pub fn pow_i32_mut(&mut self, exp: i32)

source§

impl Ratio

source

pub fn sub_rat(&self, other: &Ratio) -> Self

source

pub fn sub_rat_mut(&mut self, other: &Ratio)

source

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

source

pub fn sub_bi_mut(&mut self, other: &BigInt)

source

pub fn sub_i32(&self, other: i32) -> Self

source

pub fn sub_i32_mut(&mut self, other: i32)

source§

impl Ratio

source

pub fn lt_rat(&self, other: &Ratio) -> bool

self < other

source

pub fn gt_rat(&self, other: &Ratio) -> bool

self > other

source

pub fn eq_rat(&self, other: &Ratio) -> bool

self == other

source

pub fn neq_rat(&self, other: &Ratio) -> bool

self != other

source

pub fn leq_rat(&self, other: &Ratio) -> bool

self <= other

source

pub fn geq_rat(&self, other: &Ratio) -> bool

self >= other

source

pub fn comp_rat(&self, other: &Ratio) -> Ordering

source

pub fn lt_one(&self) -> bool

self < 1

source

pub fn gt_one(&self) -> bool

self > 1

source

pub fn lt_i32(&self, other: i32) -> bool

self < other

source

pub fn gt_i32(&self, other: i32) -> bool

self > other

source

pub fn eq_i32(&self, other: i32) -> bool

self == other

source

pub fn neq_i32(&self, other: i32) -> bool

self != other

source

pub fn leq_i32(&self, other: i32) -> bool

self <= other

source

pub fn geq_i32(&self, other: i32) -> bool

self >= other

source

pub fn comp_i32(&self, other: i32) -> Ordering

source§

impl Ratio

source

pub fn from_ieee754_f32(n: f32) -> Result<Self, ConversionError>

If you don’t know what ieee754 is, you’re okay to use this function. Though the ieee 754 standard distinguishes negative 0 and positive 0, it doesn’t distinguish between them. It returns an error if n is NaN or Inf.

source

pub fn to_ieee754_f32(&self) -> Result<f32, ConversionError>

If you don’t know what ieee754 is, you’re okay to use this function. This function does not return f32::NAN or f32::INFINITY, but it returns ConversionError::NotInRange instead.

source

pub fn from_ieee754_f64(n: f64) -> Result<Self, ConversionError>

If you don’t know what ieee754 is, you’re okay to use this function. Though the ieee 754 standard distinguishes negative 0 and positive 0, it doesn’t distinguish between them. It returns an error if n is NaN or Inf.

source

pub fn to_ieee754_f64(&self) -> Result<f64, ConversionError>

If you don’t know what ieee754 is, you’re okay to use this function. This function does not return f64::NAN or f64::INFINITY, but it returns ConversionError::NotInRange instead.

source§

impl Ratio

source

pub fn from_denom_and_numer(denom: BigInt, numer: BigInt) -> Self

source

pub fn from_denom_and_numer_i32(denom: i32, numer: i32) -> Self

source

pub fn from_denom_and_numer_raw(denom: BigInt, numer: BigInt) -> Self

This function does not do any safety checks. Use this function only when you’re sure that the properties below are satisfied.

  • denom and numer are coprime
  • denom is positive
  • denom is 1 when numer is 0
source

pub fn from_bi(n: BigInt) -> Self

source

pub fn from_ubi(n: UBigInt) -> Self

source

pub fn from_i32(n: i32) -> Self

source

pub fn from_i64(n: i64) -> Self

source

pub fn from_i128(n: i128) -> Self

source

pub fn from_string(s: &str) -> Result<Self, ConversionError>

source

pub fn to_ratio_string(&self) -> String

Ratio { 4, 7 } -> “4/7”.

source

pub fn to_approx_string(&self, max_len: usize) -> String

Ratio { 4, 7 } -> “1.75”. The length of the returned string is less or equal to digits. If digits is less than 6, it’ll count that as 6.

source

pub fn to_scientific_notation(&self, digits_max_len: usize) -> String

‘9.8e5’

source

pub fn to_string(&self) -> String

self.to_approx_string(12)

source§

impl Ratio

source

pub fn neg(&self) -> Self

source

pub fn neg_mut(&mut self)

source

pub fn abs(&self) -> Self

source

pub fn abs_mut(&mut self)

source

pub fn reci(&self) -> Self

1 / self

source

pub fn reci_mut(&mut self)

self = 1 / self

source

pub fn truncate(&self) -> Self

source

pub fn truncate_mut(&mut self)

source

pub fn truncate_bi(&self) -> BigInt

source

pub fn frac(&self) -> Self

self - truncate(self)

source

pub fn frac_mut(&mut self)

self -= truncate(self)

source

pub fn truncate_and_frac(&self) -> (BigInt, Self)

If you need both self.truncate_bi and self.frac, use this method. It’s way cheaper.

source

pub fn floor(&self) -> Ratio

It returns the largest integer less than or equal to self.

source

pub fn floor_bi(&self) -> BigInt

It returns the largest integer less than or equal to self.

source§

impl Ratio

source

pub fn from_raw( denom: Vec<u32>, denom_neg: bool, numer: Vec<u32>, numer_neg: bool ) -> Self

This function DOES NOT check whether denom and numer are coprime. Avoid using this function except when converting the result of Ratio::into_raw to Ratio. In most cases, it’s safer to use Ratio::from_denom_and_numer.

source

pub fn into_raw(self) -> (Vec<u32>, bool, Vec<u32>, bool)

source

pub fn zero() -> Self

source

pub fn one() -> Self

source

pub fn is_neg(&self) -> bool

source

pub fn is_one(&self) -> bool

source

pub fn is_zero(&self) -> bool

source

pub fn is_integer(&self) -> bool

source

pub fn shrink(&mut self, limit: usize) -> Result<usize, usize>

It shrinks the size of self.numer and self.denom until they’re less than or equal to 2^(limit * 32). It may lose accuracy. If denom and numer are already small enough, it returns Ok(0). If it successfully shrinks, it returns Ok(n) where n is how much numbers it removed. Sometimes, the shrinked result doesn’t satisfy the limit. It returns Err(n) in those cases where n is how much numbers it removed.

Trait Implementations§

source§

impl Clone for Ratio

source§

fn clone(&self) -> Ratio

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 Debug for Ratio

source§

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

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

impl Default for Ratio

source§

fn default() -> Self

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

impl Display for Ratio

source§

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

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

impl<T: Copy> From<&T> for Ratiowhere Ratio: From<T>,

source§

fn from(n: &T) -> Self

Converts to this type from the input type.
source§

impl From<&str> for Ratio

If it fails to parse the string, it returns 0.

source§

fn from(n: &str) -> Self

Converts to this type from the input type.
source§

impl From<BigInt> for Ratio

source§

fn from(n: BigInt) -> Self

Converts to this type from the input type.
source§

impl From<Ratio> for BigInt

source§

fn from(n: Ratio) -> Self

Converts to this type from the input type.
source§

impl From<Ratio> for UBigInt

source§

fn from(n: Ratio) -> Self

Converts to this type from the input type.
source§

impl From<String> for Ratio

If it fails to parse the string, it returns 0.

source§

fn from(n: String) -> Self

Converts to this type from the input type.
source§

impl From<UBigInt> for Ratio

source§

fn from(n: UBigInt) -> Self

Converts to this type from the input type.
source§

impl From<bool> for Ratio

source§

fn from(b: bool) -> Self

Converts to this type from the input type.
source§

impl From<f32> for Ratio

It returns 0 for NaN, Ratio(f32::MAX) for f32::Inf and Ratio(f32::MIN) for f32::NegInf.

source§

fn from(n: f32) -> Self

Converts to this type from the input type.
source§

impl From<f64> for Ratio

It returns 0 for NaN, Ratio(f64::MAX) for f64::Inf and Ratio(f64::MIN) for f64::NegInf.

source§

fn from(n: f64) -> Self

Converts to this type from the input type.
source§

impl From<i128> for Ratio

source§

fn from(n: i128) -> Self

Converts to this type from the input type.
source§

impl From<i16> for Ratio

source§

fn from(n: i16) -> Self

Converts to this type from the input type.
source§

impl From<i32> for Ratio

source§

fn from(n: i32) -> Self

Converts to this type from the input type.
source§

impl From<i64> for Ratio

source§

fn from(n: i64) -> Self

Converts to this type from the input type.
source§

impl From<i8> for Ratio

source§

fn from(n: i8) -> Self

Converts to this type from the input type.
source§

impl From<isize> for Ratio

source§

fn from(n: isize) -> Self

Converts to this type from the input type.
source§

impl From<u128> for Ratio

source§

fn from(n: u128) -> Self

Converts to this type from the input type.
source§

impl From<u16> for Ratio

source§

fn from(n: u16) -> Self

Converts to this type from the input type.
source§

impl From<u32> for Ratio

source§

fn from(n: u32) -> Self

Converts to this type from the input type.
source§

impl From<u64> for Ratio

source§

fn from(n: u64) -> Self

Converts to this type from the input type.
source§

impl From<u8> for Ratio

source§

fn from(n: u8) -> Self

Converts to this type from the input type.
source§

impl From<usize> for Ratio

source§

fn from(n: usize) -> Self

Converts to this type from the input type.
source§

impl Ord for Ratio

source§

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

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

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

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl PartialEq<Ratio> for Ratio

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Ratio> for Ratio

source§

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

This method 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

This method 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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Eq for Ratio

source§

impl StructuralEq for Ratio

source§

impl StructuralPartialEq for Ratio

Auto Trait Implementations§

§

impl RefUnwindSafe for Ratio

§

impl Send for Ratio

§

impl Sync for Ratio

§

impl Unpin for Ratio

§

impl UnwindSafe for Ratio

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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.