Struct Fraction

Source
pub struct Fraction {
    pub sign: bool,
    pub num: u128,
    pub denom: u128,
}
Expand description

A fraction that can be factorized.

Fields§

§sign: bool

The sign, where false is positive and true is negative. Fractions with a numerator of 0 ignore the sign when checking equality.

§num: u128

The numerator.

§denom: u128

The denominator.

Implementations§

Source§

impl Fraction

Source

pub const ONE: Self

The multiplicative base fraction, 1/1.

Source

pub const ZERO: Self

The additive base fraction, 0/1.

Source

pub const NEG_ONE: Self

The negative multiplicative base fraction, -1/1.

Source

pub const ONE_OVER_ZERO: Self

The fraction that represents positive infinity, 1/0.

Source

pub const ZERO_OVER_ZERO: Self

The fraction that represents an undefined value, 0/0.

Source

pub const NEG_ONE_OVER_ZERO: Self

The fraction that represents negative infinity, -1/0.

Source

pub fn new(num: impl Into<Fraction>, denom: impl Into<Fraction>) -> Self

Creates a new fraciton from a numerator and denominator.

§Examples:

Basic usage:

let frac = Fraction::new(2, -5);

assert_eq!(frac.sign, true);
assert_eq!(frac.num, 2);
assert_eq!(frac.denom, 5);
Source

pub fn whole(num: impl Into<Fraction>) -> Self

Creates a new fraction from a numerator.

§Examples:

Basic usage:

let frac = Fraction::whole(36);

assert_eq!(frac.sign, false);
assert_eq!(frac.num, 36);
assert_eq!(frac.denom, 1);
Source

pub const fn recip(&self) -> Self

The multiplicative reciprocal of this fraction, created by swapping the numerator and the denominator.

Source

pub fn factorize(&self) -> impl Iterator<Item = Factor>

Breaks this fraction down into it’s prime factors. The factors are always yielded in order from lowest to highest, and no two factors with the same base will appear.

§Examples

Basic usage:

let frac = Fraction::new(-120, 130);
let factors: Vec<Factor> = frac.factorize().collect();

assert_eq!(factors, [
    Factor::NegativeOne,
    Factor::Prime(2, 2),
    Factor::Prime(3, 1),
    Factor::Prime(13, -1),
]);

Trait Implementations§

Source§

impl Clone for Fraction

Source§

fn clone(&self) -> Fraction

Returns a duplicate of the value. Read more
1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Fraction

Source§

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

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

impl Display for Fraction

Source§

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

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

impl Div for &Fraction

Source§

type Output = Fraction

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Fraction

Performs the / operation. Read more
Source§

impl Div for Fraction

Source§

type Output = Fraction

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Fraction

Performs the / operation. Read more
Source§

impl DivAssign<&Fraction> for Fraction

Source§

fn div_assign(&mut self, other: &Self)

Performs the /= operation. Read more
Source§

impl DivAssign for Fraction

Source§

fn div_assign(&mut self, other: Self)

Performs the /= operation. Read more
Source§

impl From<Factor> for Fraction

Source§

fn from(fac: Factor) -> Self

Converts to this type from the input type.
Source§

impl From<i128> for Fraction

Source§

fn from(num: i128) -> Self

Converts to this type from the input type.
Source§

impl From<i16> for Fraction

Source§

fn from(num: i16) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Fraction

Source§

fn from(num: i32) -> Self

Converts to this type from the input type.
Source§

impl From<i64> for Fraction

Source§

fn from(num: i64) -> Self

Converts to this type from the input type.
Source§

impl From<i8> for Fraction

Source§

fn from(num: i8) -> Self

Converts to this type from the input type.
Source§

impl From<u128> for Fraction

Source§

fn from(num: u128) -> Self

Converts to this type from the input type.
Source§

impl From<u16> for Fraction

Source§

fn from(num: u16) -> Self

Converts to this type from the input type.
Source§

impl From<u32> for Fraction

Source§

fn from(num: u32) -> Self

Converts to this type from the input type.
Source§

impl From<u64> for Fraction

Source§

fn from(num: u64) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for Fraction

Source§

fn from(num: u8) -> Self

Converts to this type from the input type.
Source§

impl Mul for &Fraction

Source§

type Output = Fraction

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Fraction

Performs the * operation. Read more
Source§

impl Mul for Fraction

Source§

type Output = Fraction

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Fraction

Performs the * operation. Read more
Source§

impl MulAssign<&Fraction> for Fraction

Source§

fn mul_assign(&mut self, other: &Self)

Performs the *= operation. Read more
Source§

impl MulAssign for Fraction

Source§

fn mul_assign(&mut self, other: Self)

Performs the *= operation. Read more
Source§

impl Neg for &Fraction

Source§

type Output = Fraction

The resulting type after applying the - operator.
Source§

fn neg(self) -> Fraction

Performs the unary - operation. Read more
Source§

impl Neg for Fraction

Source§

type Output = Fraction

The resulting type after applying the - operator.
Source§

fn neg(self) -> Fraction

Performs the unary - operation. Read more
Source§

impl PartialEq for Fraction

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

const 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 Product<Factor> for Fraction

Source§

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

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

impl Product for Fraction

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 Copy for Fraction

Source§

impl Eq for Fraction

Auto Trait Implementations§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V