pub struct Rational128(_, _);
Expand description

A wrapper for any rational number with a 128 bit numerator and denominator.

Implementations§

source§

impl Rational128

source

pub fn zero() -> Self

Zero.

source

pub fn one() -> Self

One

source

pub fn is_zero(&self) -> bool

If it is zero or not

source

pub fn from(n: u128, d: u128) -> Self

Build from a raw n/d.

source

pub fn from_unchecked(n: u128, d: u128) -> Self

Build from a raw n/d. This could lead to / 0 if not properly handled.

source

pub fn n(&self) -> u128

Return the numerator.

source

pub fn d(&self) -> u128

Return the denominator.

source

pub fn to_den(self, den: u128) -> Option<Self>

Convert self to a similar rational number where denominator is the given den. This only returns if the result is accurate. None is returned if the result cannot be accurately calculated.

source

pub fn lcm(&self, other: &Self) -> Option<u128>

Get the least common divisor of self and other.

This only returns if the result is accurate. None is returned if the result cannot be accurately calculated.

source

pub fn lazy_saturating_add(self, other: Self) -> Self

A saturating add that assumes self and other have the same denominator.

source

pub fn lazy_saturating_sub(self, other: Self) -> Self

A saturating subtraction that assumes self and other have the same denominator.

source

pub fn checked_add(self, other: Self) -> Result<Self, &'static str>

Addition. Simply tries to unify the denominators and add the numerators.

Overflow might happen during any of the steps. Error is returned in such cases.

source

pub fn checked_sub(self, other: Self) -> Result<Self, &'static str>

Subtraction. Simply tries to unify the denominators and subtract the numerators.

Overflow might happen during any of the steps. None is returned in such cases.

Trait Implementations§

source§

impl Bounded for Rational128

source§

fn min_value() -> Self

Returns the smallest finite number this type can represent
source§

fn max_value() -> Self

Returns the largest finite number this type can represent
source§

impl Clone for Rational128

source§

fn clone(&self) -> Rational128

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 Rational128

source§

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

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

impl Default for Rational128

source§

fn default() -> Rational128

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

impl From<Rational128> for RationalInfinite

source§

fn from(t: Rational128) -> Self

Converts to this type from the input type.
source§

impl<T: Into<u128>> From<T> for Rational128

source§

fn from(t: T) -> Self

Converts to this type from the input type.
source§

impl Ord for Rational128

source§

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

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

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

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere
    Self: Sized + PartialOrd<Self>,

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

impl PartialEq<Rational128> for Rational128

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Rational128> for Rational128

source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

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
1.0.0 · source§

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

This method 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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Copy for Rational128

source§

impl Eq for Rational128

source§

impl StructuralEq for Rational128

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere
    T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

const: unstable · 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> LowerBounded for Twhere
    T: Bounded,

source§

fn min_value() -> T

Returns the smallest finite number this type can represent
source§

impl<T> SaturatedConversion for T

source§

fn saturated_from<T>(t: T) -> Selfwhere
    Self: UniqueSaturatedFrom<T>,

Convert from a value of T into an equivalent instance of Self. Read more
source§

fn saturated_into<T>(self) -> Twhere
    Self: UniqueSaturatedInto<T>,

Consume self to return an equivalent value of T. Read more
source§

impl<T> ToOwned for Twhere
    T: Clone,

§

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 Twhere
    U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T, S> UniqueSaturatedFrom<T> for Swhere
    S: TryFrom<T> + Bounded,

source§

fn unique_saturated_from(t: T) -> S

Convert from a value of T into an equivalent instance of Self.
source§

impl<T, S> UniqueSaturatedInto<T> for Swhere
    T: Bounded,
    S: TryInto<T>,

source§

fn unique_saturated_into(self) -> T

Consume self to return an equivalent value of T.
source§

impl<T> UpperBounded for Twhere
    T: Bounded,

source§

fn max_value() -> T

Returns the largest finite number this type can represent