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_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Check whether the fraction is equal to 0
§Example
use taco_threshold_automaton::expressions::fraction::Fraction;
// 0/5 is simplified to 0/1 upon creation
let f = Fraction::new(0, 5, false);
assert_eq!(f.is_zero(), true);
let f = Fraction::new(1, 2, false);
assert_eq!(f.is_zero(), false);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);Sourcepub fn numerator(&self) -> u32
pub fn numerator(&self) -> u32
Get the numerator of the fraction
§Example
use taco_threshold_automaton::expressions::fraction::Fraction;
let f = Fraction::new(42, 1, false);
assert_eq!(f.numerator(), 42);Sourcepub fn get_ceil(&self) -> Option<u32>
pub fn get_ceil(&self) -> Option<u32>
Get the next closest integer while always rounding up
Returns None for negative fractions
§Example
use taco_threshold_automaton::expressions::fraction::Fraction;
// ceil(5/2) = 3
let f = Fraction::new(5, 2, false);
assert_eq!(f.get_ceil(), Some(3));
// ceil(6/2) = 3
let f = Fraction::new(6, 2, false);
assert_eq!(f.get_ceil(), Some(3));
// negative fractions cannot be rounded to a non-negative integer
let f = Fraction::new(5, 2, true);
assert_eq!(f.get_ceil(), None);Sourcepub fn get_floor(&self) -> Option<u32>
pub fn get_floor(&self) -> Option<u32>
Get the next closest integer while always rounding down
Returns None for negative fractions
§Example
use taco_threshold_automaton::expressions::fraction::Fraction;
// floor(5/2) = 2
let f = Fraction::new(5, 2, false);
assert_eq!(f.get_floor(), Some(2));
// floor(6/2) = 3
let f = Fraction::new(6, 2, false);
assert_eq!(f.get_floor(), Some(3));
// negative fractions cannot be rounded to a non-negative integer
let f = Fraction::new(5, 2, true);
assert_eq!(f.get_floor(), None);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)
+= operation. Read moreimpl Copy for Fraction
Source§impl DivAssign for Fraction
impl DivAssign for Fraction
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
/= operation. Read moreimpl Eq for Fraction
Source§impl MulAssign for Fraction
impl MulAssign for Fraction
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
*= operation. Read more