pub struct Frac { /* private fields */ }
Expand description
Representation of fractional value.
Implementations§
Source§impl 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§
Source§impl AddAssign<&Frac> for Frac
impl AddAssign<&Frac> for Frac
Source§fn add_assign(&mut self, rhs: &Self)
fn add_assign(&mut self, rhs: &Self)
Performs the
+=
operation. Read moreSource§impl AddAssign for Frac
impl AddAssign for Frac
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+=
operation. Read moreSource§impl DivAssign<&Frac> for Frac
impl DivAssign<&Frac> for Frac
Source§fn div_assign(&mut self, rhs: &Self)
fn div_assign(&mut self, rhs: &Self)
Performs the
/=
operation. Read moreSource§impl DivAssign for Frac
impl DivAssign for Frac
Source§fn div_assign(&mut self, rhs: Self)
fn div_assign(&mut self, rhs: Self)
Performs the
/=
operation. Read moreSource§impl MulAssign<&Frac> for Frac
impl MulAssign<&Frac> for Frac
Source§fn mul_assign(&mut self, rhs: &Self)
fn mul_assign(&mut self, rhs: &Self)
Performs the
*=
operation. Read moreSource§impl MulAssign for Frac
impl MulAssign for Frac
Source§fn mul_assign(&mut self, rhs: Self)
fn mul_assign(&mut self, rhs: Self)
Performs the
*=
operation. Read moreSource§impl Ord for Frac
impl Ord for Frac
Source§impl PartialOrd for Frac
impl PartialOrd for Frac
Source§fn 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)
);
Source§impl 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
Source§impl SubAssign<&Frac> for Frac
impl SubAssign<&Frac> for Frac
Source§fn sub_assign(&mut self, rhs: &Self)
fn sub_assign(&mut self, rhs: &Self)
Performs the
-=
operation. Read moreSource§impl SubAssign for Frac
impl SubAssign for Frac
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-=
operation. Read moreimpl Copy for Frac
impl Eq for Frac
impl StructuralPartialEq for Frac
Auto Trait Implementations§
impl Freeze for Frac
impl RefUnwindSafe for Frac
impl Send for Frac
impl Sync for Frac
impl Unpin for Frac
impl UnwindSafe for Frac
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