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
impl Fraction
Sourcepub const ONE_OVER_ZERO: Self
pub const ONE_OVER_ZERO: Self
The fraction that represents positive infinity, 1/0.
Sourcepub const ZERO_OVER_ZERO: Self
pub const ZERO_OVER_ZERO: Self
The fraction that represents an undefined value, 0/0.
Sourcepub const NEG_ONE_OVER_ZERO: Self
pub const NEG_ONE_OVER_ZERO: Self
The fraction that represents negative infinity, -1/0.
Sourcepub fn new(num: impl Into<Fraction>, denom: impl Into<Fraction>) -> Self
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);
Sourcepub fn whole(num: impl Into<Fraction>) -> Self
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);
Sourcepub const fn recip(&self) -> Self
pub const fn recip(&self) -> Self
The multiplicative reciprocal of this fraction, created by swapping the numerator and the denominator.
Sourcepub fn factorize(&self) -> impl Iterator<Item = Factor>
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 DivAssign<&Fraction> for Fraction
impl DivAssign<&Fraction> for Fraction
Source§fn div_assign(&mut self, other: &Self)
fn div_assign(&mut self, other: &Self)
Performs the
/=
operation. Read moreSource§impl DivAssign for Fraction
impl DivAssign for Fraction
Source§fn div_assign(&mut self, other: Self)
fn div_assign(&mut self, other: Self)
Performs the
/=
operation. Read moreSource§impl MulAssign<&Fraction> for Fraction
impl MulAssign<&Fraction> for Fraction
Source§fn mul_assign(&mut self, other: &Self)
fn mul_assign(&mut self, other: &Self)
Performs the
*=
operation. Read moreSource§impl MulAssign for Fraction
impl MulAssign for Fraction
Source§fn mul_assign(&mut self, other: Self)
fn mul_assign(&mut self, other: Self)
Performs the
*=
operation. Read moreimpl Copy for Fraction
impl Eq for Fraction
Auto Trait Implementations§
impl Freeze for Fraction
impl RefUnwindSafe for Fraction
impl Send for Fraction
impl Sync for Fraction
impl Unpin for Fraction
impl UnwindSafe for Fraction
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<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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