pub struct Rational { /* private fields */ }Expand description
A type representing a signed projectively-extended rational number
§Examples
use extended_rational::Rational;
let a = Rational::new(3, 17);
let b = Rational::new(-4, 7);
assert_eq!(a+b, Rational::new(-47, 119));Use the ratio! macro for more convenient use.
let a = ratio!(3, 17);
let b = ratio!(-4, 7);Or for easy conversions from primitive types.
let a = ratio!(-77.332);
let b = ratio!(2u8);Implementations§
Source§impl Rational
impl Rational
Sourcepub fn new(numerator: i64, denominator: i64) -> Rational
pub fn new(numerator: i64, denominator: i64) -> Rational
Create a new signed rational with the given numerator and denominator.
Sourcepub fn new_raw(unsigned: URational, negative: bool) -> Rational
pub fn new_raw(unsigned: URational, negative: bool) -> Rational
Create a new signed rational with the given unsigned rational and sign.
Sourcepub fn numerator_mut(&mut self) -> &mut u64
pub fn numerator_mut(&mut self) -> &mut u64
Returns the underlying numerator of this rational mutably.
Sourcepub fn denominator(self) -> u64
pub fn denominator(self) -> u64
Returns the underlying denominator of this rational.
Sourcepub fn denominator_mut(&mut self) -> &mut u64
pub fn denominator_mut(&mut self) -> &mut u64
Returns the underlying denominator of this rational mutably.
Sourcepub fn unsigned_mut(&mut self) -> &mut URational
pub fn unsigned_mut(&mut self) -> &mut URational
Returns the underlying unsigned rational of this rational mutably.
Sourcepub fn try_unsigned(self) -> URational
pub fn try_unsigned(self) -> URational
Returns the underlying unsigned rational of this rational, panicking if sign is negative.
Does not panic in optimized builds.
Sourcepub fn min_pos_value() -> Rational
pub fn min_pos_value() -> Rational
Returns the smallest positive value a signed rational can store.
Sourcepub fn max_neg_value() -> Rational
pub fn max_neg_value() -> Rational
Returns the largest negative value a signed rational can store.
Sourcepub fn negative_one() -> Rational
pub fn negative_one() -> Rational
Returns a signed rational representing -1.
Sourcepub fn is_infinity(self) -> bool
pub fn is_infinity(self) -> bool
Returns true if this rational is ∞ and false otherwise.
Sourcepub fn is_signed(self) -> bool
pub fn is_signed(self) -> bool
Returns true if this rational is a signed number (not NaN, 0, or ∞) and false otherwise.
Sourcepub fn is_negative(self) -> bool
pub fn is_negative(self) -> bool
Returns true if this rational is a negative number and false otherwise.
Sourcepub fn reciprocal(self) -> Rational
pub fn reciprocal(self) -> Rational
Returns the reciprocal of this rational.
Sourcepub fn negative_reciprocal(self) -> Rational
pub fn negative_reciprocal(self) -> Rational
Returns the negative reciprocal of this rational.
Sourcepub fn complexity(self) -> u64
pub fn complexity(self) -> u64
Returns the complexity of this rational (max of absolute values of numerator and denominator).
Sourcepub fn is_positive(self) -> bool
pub fn is_positive(self) -> bool
Returns true if this rational is a positive number and false otherwise.
Sourcepub fn floor(self) -> Rational
pub fn floor(self) -> Rational
Returns this rational with no fractional component by rounding towards zero.
Sourcepub fn ceil(self) -> Rational
pub fn ceil(self) -> Rational
Returns this rational with no fractional component by rounding away from zero.
Sourcepub fn add_exact(self, other: Rational) -> Option<Rational>
pub fn add_exact(self, other: Rational) -> Option<Rational>
Computes self + other, returning None if rounding occurred.
Sourcepub fn sub_exact(self, other: Rational) -> Option<Rational>
pub fn sub_exact(self, other: Rational) -> Option<Rational>
Computes self - other, returning None if rounding occurred.
Sourcepub fn mul_exact(self, other: Rational) -> Option<Rational>
pub fn mul_exact(self, other: Rational) -> Option<Rational>
Computes self * other, returning None if rounding occurred.
Trait Implementations§
Source§impl AddAssign for Rational
impl AddAssign for Rational
Source§fn add_assign(&mut self, other: Rational)
fn add_assign(&mut self, other: Rational)
+= operation. Read moreSource§impl DivAssign for Rational
impl DivAssign for Rational
Source§fn div_assign(&mut self, other: Rational)
fn div_assign(&mut self, other: Rational)
/= operation. Read moreSource§impl MulAssign for Rational
impl MulAssign for Rational
Source§fn mul_assign(&mut self, other: Rational)
fn mul_assign(&mut self, other: Rational)
*= operation. Read moreSource§impl PartialOrd for Rational
impl PartialOrd for Rational
Source§impl RemAssign for Rational
impl RemAssign for Rational
Source§fn rem_assign(&mut self, other: Rational)
fn rem_assign(&mut self, other: Rational)
%= operation. Read moreSource§impl SubAssign for Rational
impl SubAssign for Rational
Source§fn sub_assign(&mut self, other: Rational)
fn sub_assign(&mut self, other: Rational)
-= operation. Read more