pub struct Fraction {
negated: bool,
numerator: u32,
denominator: u32,
}Expand description
Type to representing a fraction
Fields§
§negated: boolTrue if the fraction is smaller than 0
numerator: u32Numerator
denominator: u32Denominator
Implementations§
Source§impl Fraction
impl Fraction
Sourcepub fn is_negative(&self) -> bool
pub fn is_negative(&self) -> bool
Returns true if the fraction is smaller than 0
§Example
use taco_threshold_automaton::expressions::fraction::Fraction;
// 1/2
let f = Fraction::new(1, 2, false);
assert_eq!(f.is_negative(), false);
// -1/2
let f = Fraction::new(1, 2, true);
assert_eq!(f.is_negative(), true);Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Check whether the fraction represents an integer
§Example
use taco_threshold_automaton::expressions::fraction::Fraction;
// 1/2 cannot be represented as an integer
let f = Fraction::new(1, 2, false);
assert_eq!(f.is_integer(), false);
// 2/2 -> 1
let f = Fraction::new(2, 2, false);
assert_eq!(f.is_integer(), true);Sourcefn is_simplified(&self) -> bool
fn is_simplified(&self) -> bool
Check if the fraction is simplified
Sourcepub fn new(numerator: u32, denominator: u32, negated: bool) -> Self
pub fn new(numerator: u32, denominator: u32, negated: bool) -> Self
Create a new fraction
Create a new fraction with the given numerator and denominator. Upon creation, the fraction is simplified to the maximal possible extent.
§Example
use taco_threshold_automaton::expressions::fraction::Fraction;
let f = Fraction::new(42, 2, false);
assert_eq!(i64::try_from(f).unwrap(), 21);
assert_eq!(f.is_negative(), false);
assert_eq!(f.numerator(), 21);
assert_eq!(f.denominator(), 1);Sourcefn canonicalize(self) -> Self
fn canonicalize(self) -> Self
Simplify the fraction to the maximal possible extent and canonicalize its representation
Sourcepub fn inverse(&self) -> Self
pub fn inverse(&self) -> Self
Compute the inverse of the fraction
Panics if the numerator of the fraction is 0
§Example
use taco_threshold_automaton::expressions::fraction::Fraction;
let f1 = Fraction::new(13, 2, false).inverse();
let f2 = Fraction::new(2, 13, false);
assert_eq!(f1, f2);Sourcepub fn denominator(&self) -> u32
pub fn denominator(&self) -> u32
Get the denominator of the fraction
§Example
use taco_threshold_automaton::expressions::fraction::Fraction;
let f = Fraction::new(13, 2, false);
assert_eq!(f.denominator(), 2);Trait Implementations§
Source§impl AddAssign for Fraction
impl AddAssign for Fraction
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+= operation. Read moreSource§impl DivAssign for Fraction
impl DivAssign for Fraction
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Performs the
/= operation. Read moreSource§impl MulAssign for Fraction
impl MulAssign for Fraction
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the
*= operation. Read moreSource§impl Ord for Fraction
impl Ord for Fraction
Source§impl PartialOrd for Fraction
impl PartialOrd for Fraction
Source§impl SubAssign for Fraction
impl SubAssign for Fraction
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-= operation. Read moreimpl Copy for Fraction
impl Eq for Fraction
impl StructuralPartialEq 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