Struct const_frac::frac::Frac
source · [−]pub struct Frac { /* private fields */ }
Expand description
Representation of fractional value.
Implementations
sourceimpl Frac
impl Frac
pub const fn is_positive(&self) -> bool
pub const fn is_negative(&self) -> bool
pub const fn numer(&self) -> u128
pub const fn denom(&self) -> u128
pub const fn exp2(&self) -> i16
pub const fn exp5(&self) -> i16
pub const fn exp_pi(&self) -> i16
sourcepub const fn new(
neg: bool,
numer: u128,
denom: u128,
exp2: i16,
exp5: i16,
exp_pi: i16
) -> Self
pub const fn new(
neg: bool,
numer: u128,
denom: u128,
exp2: i16,
exp5: i16,
exp_pi: i16
) -> Self
for internal use.
pub const fn from_int(i: i128) -> Self
pub const fn from_exp10(exp10: i16) -> Self
pub const fn from_exp_pi(exp_pi: i16) -> Self
pub const fn from_ratio(numer: i128, denom: u128, exp10: i16) -> Self
pub const fn from_f64(d: f64) -> Self
pub const fn regularize(self) -> Self
sourcepub const fn add(self, rhs: Self) -> Self
pub const fn add(self, rhs: Self) -> Self
let a = Frac::from_int(3_14) * Frac::from_exp10(-2);
let b = Frac::from_int(86) * Frac::from_exp10(-2);
assert_eq!(a + b, Frac::from_int(4));
sourcepub const fn sub(self, rhs: Self) -> Self
pub const fn sub(self, rhs: Self) -> Self
let left = Frac::from_int(348_051856) * Frac::from_exp10(-6);
let right = Frac::from_int(334_133017) * Frac::from_exp10(-6);
let diff = left - right;
assert_eq!(
diff,
Frac::from_int(13_918839) * Frac::from_exp10(-6),
);
pub const fn mul(self, rhs: Self) -> Self
sourcepub const fn div(self, rhs: Self) -> Self
pub const fn div(self, rhs: Self) -> Self
panic
When ‘rhs.numer’ is zero, this method goes into panic.
pub const fn neg(self) -> Self
sourcepub const fn powi(self, exp: i16) -> Self
pub const fn powi(self, exp: i16) -> Self
let pi = Frac::from_int(314) * Frac::from_exp10(-2);
let pi2 = pi.powi(2);
assert_eq!(pi2.to_f64(), 3.14f64.powi(2));
panic
When ‘self.numer == 0 && exp < 0’, this method goes into panic.
pub const fn to_f64(self) -> f64
pub const fn to_ratio(self) -> Result<(i128, NonZeroI128), Error>
pub const fn to_rational128(self) -> Ratio<i128>
Trait Implementations
sourceimpl AddAssign<&'_ Frac> for Frac
impl AddAssign<&'_ Frac> for Frac
sourcefn add_assign(&mut self, rhs: &Self)
fn add_assign(&mut self, rhs: &Self)
Performs the +=
operation. Read more
sourceimpl AddAssign<Frac> for Frac
impl AddAssign<Frac> for Frac
sourcefn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the +=
operation. Read more
sourceimpl DivAssign<&'_ Frac> for Frac
impl DivAssign<&'_ Frac> for Frac
sourcefn div_assign(&mut self, rhs: &Self)
fn div_assign(&mut self, rhs: &Self)
Performs the /=
operation. Read more
sourceimpl DivAssign<Frac> for Frac
impl DivAssign<Frac> for Frac
sourcefn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Performs the /=
operation. Read more
sourceimpl MulAssign<&'_ Frac> for Frac
impl MulAssign<&'_ Frac> for Frac
sourcefn mul_assign(&mut self, rhs: &Self)
fn mul_assign(&mut self, rhs: &Self)
Performs the *=
operation. Read more
sourceimpl MulAssign<Frac> for Frac
impl MulAssign<Frac> for Frac
sourcefn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the *=
operation. Read more
sourceimpl Ord for Frac
impl Ord for Frac
sourceimpl PartialOrd<Frac> for Frac
impl PartialOrd<Frac> for Frac
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
assert!(Frac::from_int(1) > Frac::from_int(0));
assert!(-Frac::from_exp10(-10) < Frac::from_exp10(-10));
assert_eq!(
Frac::from_int(1).partial_cmp(&(
Frac::from_int(2) * Frac::from_int(5) / Frac::from_int(10)
)),
Some(Ordering::Equal)
);
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self
and other
) and is used by the <
operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self
and other
) and is used by the <=
operator. Read more
sourceimpl Real for Frac
impl Real for Frac
fn from_int(i: isize) -> Self
fn from_f64(d: f64) -> Self
fn from_frac(frac: Self) -> Self
fn add_ref(self, rhs: &Self) -> Self
fn sub_ref(self, rhs: &Self) -> Self
fn mul_ref(self, rhs: &Self) -> Self
fn div_ref(self, rhs: &Self) -> Self
fn poweri(self, n: i16) -> Self
sourceimpl SubAssign<&'_ Frac> for Frac
impl SubAssign<&'_ Frac> for Frac
sourcefn sub_assign(&mut self, rhs: &Self)
fn sub_assign(&mut self, rhs: &Self)
Performs the -=
operation. Read more
sourceimpl SubAssign<Frac> for Frac
impl SubAssign<Frac> for Frac
sourcefn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the -=
operation. Read more
impl Copy for Frac
impl Eq for Frac
impl StructuralEq for Frac
impl StructuralPartialEq for Frac
Auto Trait Implementations
impl RefUnwindSafe for Frac
impl Send for Frac
impl Sync for Frac
impl Unpin for Frac
impl UnwindSafe for Frac
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more