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,
impl<I> Fraction<I>where
I: FractionInteger,
Sourcepub fn percentage(n: I) -> Self
pub fn percentage(n: I) -> Self
Creates a new percentage as a fraction.
Sourcepub fn from_f64(value: f64) -> Self
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.
Sourcepub fn denominator(&self) -> I
pub fn denominator(&self) -> I
The denominator of the fraction.
A flat percentage is always out of 100.
Sourcepub fn floor(&self) -> I
pub fn floor(&self) -> I
Returns the floored integer representation of the fraction.
The integer will be truncated, as if performing integer division.
Sourcepub fn round(&self) -> Iwhere
I: PrimInt,
pub fn round(&self) -> Iwhere
I: PrimInt,
Returns the rounded integer representation of the fraction.
Sourcepub fn convert<T>(self) -> Fraction<T>where
T: FractionInteger + From<I>,
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>.
Sourcepub fn try_convert<T>(self) -> Result<Fraction<T>, T::Error>where
T: FractionInteger + TryFrom<I>,
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§impl<I> Fraction<I>where
I: FractionInteger + Roots,
impl<I> Fraction<I>where
I: FractionInteger + Roots,
Trait Implementations§
Source§impl<I> Add<I> for Fraction<I>where
I: FractionInteger,
impl<I> Add<I> for Fraction<I>where
I: FractionInteger,
Source§impl<I> Add for Fraction<I>where
I: FractionInteger,
impl<I> Add for Fraction<I>where
I: FractionInteger,
Source§impl<I> Default for Fraction<I>where
I: FractionInteger,
impl<I> Default for Fraction<I>where
I: FractionInteger,
Source§impl<'de> Deserialize<'de> for Fraction<u16>
impl<'de> Deserialize<'de> for Fraction<u16>
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
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> Div<I> for Fraction<I>where
I: FractionInteger,
impl<I> Div<I> for Fraction<I>where
I: FractionInteger,
Source§impl<I> Div for Fraction<I>where
I: FractionInteger,
impl<I> Div for Fraction<I>where
I: FractionInteger,
Source§impl<I> Mul<I> for Fraction<I>where
I: FractionInteger,
impl<I> Mul<I> for Fraction<I>where
I: FractionInteger,
Source§impl<I> Mul for Fraction<I>where
I: FractionInteger,
impl<I> Mul for Fraction<I>where
I: FractionInteger,
Source§impl<I> Ord for Fraction<I>where
I: FractionInteger,
impl<I> Ord for Fraction<I>where
I: FractionInteger,
Source§impl<I> PartialEq<I> for Fraction<I>where
I: FractionInteger,
impl<I> PartialEq<I> for Fraction<I>where
I: FractionInteger,
Source§impl<I> PartialEq for Fraction<I>where
I: FractionInteger,
impl<I> PartialEq for Fraction<I>where
I: FractionInteger,
Source§impl<I> PartialOrd<I> for Fraction<I>where
I: FractionInteger,
impl<I> PartialOrd<I> for Fraction<I>where
I: FractionInteger,
Source§impl<I> PartialOrd for Fraction<I>where
I: FractionInteger,
impl<I> PartialOrd for Fraction<I>where
I: FractionInteger,
Source§impl<I> SaturatingAdd for Fraction<I>where
I: FractionInteger + SaturatingAdd,
impl<I> SaturatingAdd for Fraction<I>where
I: FractionInteger + SaturatingAdd,
Source§fn saturating_add(&self, v: &Self) -> Self
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>where
I: FractionInteger + SaturatingMul,
impl<I> SaturatingMul for Fraction<I>where
I: FractionInteger + SaturatingMul,
Source§fn saturating_mul(&self, v: &Self) -> Self
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>where
I: FractionInteger + SaturatingSub,
impl<I> SaturatingSub for Fraction<I>where
I: FractionInteger + SaturatingSub,
Source§fn saturating_sub(&self, v: &Self) -> Self
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> Sub<I> for Fraction<I>where
I: FractionInteger,
impl<I> Sub<I> for Fraction<I>where
I: FractionInteger,
Source§impl<I> Sub for Fraction<I>where
I: FractionInteger,
impl<I> Sub for Fraction<I>where
I: FractionInteger,
Source§impl<I> WrappingAdd for Fraction<I>where
I: FractionInteger + WrappingAdd,
impl<I> WrappingAdd for Fraction<I>where
I: FractionInteger + WrappingAdd,
Source§fn wrapping_add(&self, v: &Self) -> Self
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>where
I: FractionInteger + WrappingMul,
impl<I> WrappingMul for Fraction<I>where
I: FractionInteger + WrappingMul,
Source§fn wrapping_mul(&self, v: &Self) -> Self
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>where
I: FractionInteger + WrappingSub,
impl<I> WrappingSub for Fraction<I>where
I: FractionInteger + WrappingSub,
Source§fn wrapping_sub(&self, v: &Self) -> Self
fn wrapping_sub(&self, v: &Self) -> Self
Wrapping (modular) subtraction. Computes
self - other, wrapping around at the boundary
of the type.impl<I: Copy> Copy for Fraction<I>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.