const_frac::frac

Struct Frac

Source
pub struct Frac { /* private fields */ }
Expand description

Representation of fractional value.

Implementations§

Source§

impl Frac

Source

pub const fn is_positive(&self) -> bool

Source

pub const fn is_negative(&self) -> bool

Source

pub const fn numer(&self) -> u128

Source

pub const fn denom(&self) -> u128

Source

pub const fn exp2(&self) -> i16

Source

pub const fn exp5(&self) -> i16

Source

pub const fn exp_pi(&self) -> i16

Source

pub const fn new( neg: bool, numer: u128, denom: u128, exp2: i16, exp5: i16, exp_pi: i16, ) -> Self

for internal use.

Source

pub const fn from_int(i: i128) -> Self

Source

pub const fn from_exp10(exp10: i16) -> Self

Source

pub const fn from_exp_pi(exp_pi: i16) -> Self

Source

pub const fn from_ratio(numer: i128, denom: u128, exp10: i16) -> Self

Source

pub const fn from_f64(d: f64) -> Self

Source

pub const fn regularize(self) -> Self

Source

pub const fn recip(self) -> Self

§panic

When ‘numer’ is zero, this method goes into panic.

Source

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));
Source

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),
);
Source

pub const fn mul(self, rhs: Self) -> Self

Source

pub const fn div(self, rhs: Self) -> Self

§panic

When ‘rhs.numer’ is zero, this method goes into panic.

Source

pub const fn neg(self) -> Self

Source

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.

Source

pub const fn to_f64(self) -> f64

Source

pub const fn to_ratio(self) -> Result<(i128, NonZeroI128), Error>

Source

pub const fn to_rational128(self) -> Ratio<i128>

Trait Implementations§

Source§

impl Add<&Frac> for Frac

Source§

type Output = Frac

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &Self) -> Self

Performs the + operation. Read more
Source§

impl Add for Frac

Source§

type Output = Frac

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign<&Frac> for Frac

Source§

fn add_assign(&mut self, rhs: &Self)

Performs the += operation. Read more
Source§

impl AddAssign for Frac

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Frac

Source§

fn clone(&self) -> Frac

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Frac

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Frac

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Div<&Frac> for Frac

Source§

type Output = Frac

The resulting type after applying the / operator.
Source§

fn div(self, rhs: &Self) -> Self

Performs the / operation. Read more
Source§

impl Div for Frac

Source§

type Output = Frac

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Self) -> Self

Performs the / operation. Read more
Source§

impl DivAssign<&Frac> for Frac

Source§

fn div_assign(&mut self, rhs: &Self)

Performs the /= operation. Read more
Source§

impl DivAssign for Frac

Source§

fn div_assign(&mut self, rhs: Self)

Performs the /= operation. Read more
Source§

impl From<f64> for Frac

Source§

fn from(d: f64) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Frac

Source§

type Err = StringStreamError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Frac

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> Into<Ratio<T>> for Frac
where T: Clone + Debug + From<i128> + Integer,

Source§

fn into(self) -> Ratio<T>

Converts this type into the (usually inferred) input type.
Source§

impl Into<f64> for Frac

Source§

fn into(self) -> f64

Converts this type into the (usually inferred) input type.
Source§

impl Mul<&Frac> for Frac

Source§

type Output = Frac

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: &Self) -> Self

Performs the * operation. Read more
Source§

impl Mul for Frac

Source§

type Output = Frac

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self

Performs the * operation. Read more
Source§

impl MulAssign<&Frac> for Frac

Source§

fn mul_assign(&mut self, rhs: &Self)

Performs the *= operation. Read more
Source§

impl MulAssign for Frac

Source§

fn mul_assign(&mut self, rhs: Self)

Performs the *= operation. Read more
Source§

impl Neg for Frac

Source§

type Output = Frac

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Ord for Frac

Source§

fn cmp(&self, other: &Frac) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Frac

Source§

fn eq(&self, other: &Frac) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Frac

Source§

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 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Real for Frac

Source§

fn from_int(i: isize) -> Self

Source§

fn from_f64(d: f64) -> Self

Source§

fn from_frac(frac: Self) -> Self

Source§

fn add_ref(self, rhs: &Self) -> Self

Source§

fn sub_ref(self, rhs: &Self) -> Self

Source§

fn mul_ref(self, rhs: &Self) -> Self

Source§

fn div_ref(self, rhs: &Self) -> Self

Source§

fn poweri(self, n: i16) -> Self

Source§

impl Sub<&Frac> for Frac

Source§

type Output = Frac

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &Self) -> Self

Performs the - operation. Read more
Source§

impl Sub for Frac

Source§

type Output = Frac

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl SubAssign<&Frac> for Frac

Source§

fn sub_assign(&mut self, rhs: &Self)

Performs the -= operation. Read more
Source§

impl SubAssign for Frac

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl Copy for Frac

Source§

impl Eq for Frac

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.