Skip to main content

Fraction

Struct Fraction 

Source
pub struct Fraction<I> { /* private fields */ }
Expand description

A fraction, usable in calculations.

A fraction is serializable as:

  • A fraction string ("1/2").
  • An integer (20), which represents an integer (denominator == 1).
  • A floating point number (1.5), which is converted to a fraction out of 4096.
  • A percentage string ("60%").
  • A two-length array ([2,5]).

Implementations§

Source§

impl<I> Fraction<I>
where I: FractionInteger,

Source

pub fn new(n: I, d: I) -> Self

Creates a new fraction.

Source

pub fn percentage(n: I) -> Self

Creates a new percentage as a fraction.

Source

pub fn from_f64(value: f64) -> Self

Creates a new fraction from an f64.

Floating point precision is preserved by creating a fraction with a denominator of 4096.

Source

pub fn numerator(&self) -> I

The numerator of the fraction.

Source

pub fn denominator(&self) -> I

The denominator of the fraction.

A flat percentage is always out of 100.

Source

pub fn is_whole(&self) -> bool

Is the fraction whole (i.e., an integer)?

Source

pub fn simplify(&self) -> Self

Simplifies the fraction.

Source

pub fn floor(&self) -> I

Returns the floored integer representation of the fraction.

The integer will be truncated, as if performing integer division.

Source

pub fn ceil(&self) -> I

Returns the ceiled integer representation of the fraction.

Source

pub fn round(&self) -> I
where I: PrimInt,

Returns the rounded integer representation of the fraction.

Source

pub fn convert<T>(self) -> Fraction<T>
where T: FractionInteger + From<I>,

Converts the Fraction<I> to a Fraction<T>, given that T: From<I>.

Source

pub fn try_convert<T>(self) -> Result<Fraction<T>, T::Error>
where T: FractionInteger + TryFrom<I>,

Attempts converting the Fraction<I> to a Fraction<T>, given that T: TryFrom<I>.

Source

pub fn inverse(&self) -> Self

Returns the inverse of this fraction.

Source§

impl<I> Fraction<I>

Source

pub fn sqrt(&self) -> Self

Source

pub fn nth_root(&self, n: u32) -> Self

Source

pub fn pow<J>( self, rhs: Fraction<J>, ) -> Result<Self, <J as TryInto<u32>>::Error>
where I: Pow<u32, Output = I> + TryInto<J> + Zero, J: FractionInteger + TryInto<u32> + Signed,

Trait Implementations§

Source§

impl<I> Add<I> for Fraction<I>
where I: FractionInteger,

Source§

type Output = Fraction<I>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<I> Add for Fraction<I>
where I: FractionInteger,

Source§

type Output = Fraction<I>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<I: Clone> Clone for Fraction<I>

Source§

fn clone(&self) -> Fraction<I>

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<I: Debug> Debug for Fraction<I>

Source§

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

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

impl<I> Default for Fraction<I>
where I: FractionInteger,

Source§

fn default() -> Self

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

impl<'de> Deserialize<'de> for Fraction<u16>

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<I> Display for Fraction<I>

Source§

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

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

impl<I> Div<I> for Fraction<I>
where I: FractionInteger,

Source§

type Output = Fraction<I>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<I> Div for Fraction<I>
where I: FractionInteger,

Source§

type Output = Fraction<I>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<I, J> From<I> for Fraction<J>

Source§

fn from(value: I) -> Self

Converts to this type from the input type.
Source§

impl<I> FromStr for Fraction<I>

Source§

type Err = Error

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<I> Mul<I> for Fraction<I>
where I: FractionInteger,

Source§

type Output = Fraction<I>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<I> Mul for Fraction<I>
where I: FractionInteger,

Source§

type Output = Fraction<I>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<I> Ord for Fraction<I>
where I: FractionInteger,

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<I> PartialEq<I> for Fraction<I>
where I: FractionInteger,

Source§

fn eq(&self, other: &I) -> 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<I> PartialEq for Fraction<I>
where I: FractionInteger,

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<I> PartialOrd<I> for Fraction<I>
where I: FractionInteger,

Source§

fn partial_cmp(&self, other: &I) -> 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<I> PartialOrd for Fraction<I>
where I: FractionInteger,

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<I> SaturatingAdd for Fraction<I>

Source§

fn saturating_add(&self, v: &Self) -> Self

Saturating addition. Computes self + other, saturating at the relevant high or low boundary of the type.
Source§

impl<I> SaturatingMul for Fraction<I>

Source§

fn saturating_mul(&self, v: &Self) -> Self

Saturating multiplication. Computes self * other, saturating at the relevant high or low boundary of the type.
Source§

impl<I> SaturatingSub for Fraction<I>

Source§

fn saturating_sub(&self, v: &Self) -> Self

Saturating subtraction. Computes self - other, saturating at the relevant high or low boundary of the type.
Source§

impl<I> Serialize for Fraction<I>

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<I> Sub<I> for Fraction<I>
where I: FractionInteger,

Source§

type Output = Fraction<I>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<I> Sub for Fraction<I>
where I: FractionInteger,

Source§

type Output = Fraction<I>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<I> WrappingAdd for Fraction<I>

Source§

fn wrapping_add(&self, v: &Self) -> Self

Wrapping (modular) addition. Computes self + other, wrapping around at the boundary of the type.
Source§

impl<I> WrappingMul for Fraction<I>

Source§

fn wrapping_mul(&self, v: &Self) -> Self

Wrapping (modular) multiplication. Computes self * other, wrapping around at the boundary of the type.
Source§

impl<I> WrappingSub for Fraction<I>

Source§

fn wrapping_sub(&self, v: &Self) -> Self

Wrapping (modular) subtraction. Computes self - other, wrapping around at the boundary of the type.
Source§

impl<I: Copy> Copy for Fraction<I>

Source§

impl<I> Eq for Fraction<I>
where I: FractionInteger,

Auto Trait Implementations§

§

impl<I> Freeze for Fraction<I>
where I: Freeze,

§

impl<I> RefUnwindSafe for Fraction<I>
where I: RefUnwindSafe,

§

impl<I> Send for Fraction<I>
where I: Send,

§

impl<I> Sync for Fraction<I>
where I: Sync,

§

impl<I> Unpin for Fraction<I>
where I: Unpin,

§

impl<I> UnsafeUnpin for Fraction<I>
where I: UnsafeUnpin,

§

impl<I> UnwindSafe for Fraction<I>
where I: 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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,