Fraction

Struct Fraction 

Source
pub struct Fraction {
    pub numerator: i128,
    pub denominator: i128,
}

Fields§

§numerator: i128§denominator: i128

Implementations§

Source§

impl Fraction

Source

pub fn new(numerator: i128, denominator: i128) -> Result<Self, FuelError>

Creates a new fraction with the given numerator and denominator Panics if given a denominator of 0

Source

pub fn reduce(&self) -> Self

Returns a new Fraction that is equal to this one, but simplified

Source

pub fn to_decimal(&self) -> f64

Returns a decimal equivalent to this Fraction

Trait Implementations§

Source§

impl<'a> Add for &'a Fraction

Source§

type Output = Fraction

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Fraction

Performs the + operation. Read more
Source§

impl Clone for Fraction

Source§

fn clone(&self) -> Fraction

Returns a duplicate 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 Fraction

Source§

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

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

impl Display for Fraction

Source§

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

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

impl Div<&Fraction> for &Fuel

Source§

type Output = Result<Fuel, FuelError>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div<&Fraction> for Fuel

Source§

type Output = Result<Fuel, FuelError>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div<Fraction> for &Fuel

Source§

type Output = Result<Fuel, FuelError>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl Div<Fraction> for Fuel

Source§

type Output = Result<Fuel, FuelError>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<'a> Div for &'a Fraction

Source§

type Output = Fraction

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Fraction

Performs the / operation. Read more
Source§

impl From<&Fuel> for Fraction

Source§

fn from(fuel: &Fuel) -> Fraction

Converts to this type from the input type.
Source§

impl Mul<&Fraction> for &Fuel

Source§

type Output = Result<Fuel, FuelError>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<&Fraction> for Fuel

Source§

type Output = Result<Fuel, FuelError>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Fraction> for &Fuel

Source§

type Output = Result<Fuel, FuelError>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl Mul<Fraction> for Fuel

Fuel * Fraction, Fuel / Fraction – always round up on loss of precision

If a Fraction’s numerator would result in an overflow of the maximum allowable Holo fuel amount, produce an Err result. Non-zero results below the minimum fractional Fuel value threshold are always rounded up.

For example, .75% of 1334 == 10.005, or 11 if rounded up. A Fraction representing .75%, Fraction::new(3, 400) multiplied by 0.001334 HoloFuel: Fuel{ units: 1334 } would result in a Some(quotient) of 1334 / 400 == 3, and then 3 * 3 == 9.

In general, any HoloFuel.units precision below the Fraction.denominator will be lost, because we perform a division by the Fraction.denominator, to avoid overflow on large values. If we had instead performed the multiplication by the numerator first, we would have have computed 1334 * 3 == 4002, and 4002 / 400 == 10; also correct, but no more useful if our intent is to “round up” since the desired result is 11 (and, it would overflow on large values). However, here we could clearly detect that 4002 % 400 == 2 remainder, indicating that we must add 1 to the result to round up.

We must find the remainder of the division by the denominator 400, after multiplying it by the numerator, see if (when rounded up by just less than the denominator), how many multiples of the denominator we missed:

// 1334 % 400 == 134, // 134 * 3 == 402 // 402 + (400 - 1) == 801 // 801 / 400 == 2. // Since all of these are small numbers, overflow is not possible (unless the Fraction is huge)

Note the checked_rem is a signed remainder, not a euclidean modulo, eg. from https://internals.rust-lang.org/t/mathematical-modulo-operator/5952:

// Remainder operator (%) // 5 % 3 // 2 // 5 % -3 // 2 // -5 % 3 // -2 // -5 % -3 // -2

// Modulo operator (%%) // 5 %% 3 // 2 // 5 %% -3 // -1 // -5 %% 3 // 1 // -5 %% -3 // -2

Therefore, when multiplying by -’ve Fuel, any checked_rem will be remain -’ve

Source§

type Output = Result<Fuel, FuelError>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<'a> Mul for &'a Fraction

Source§

type Output = Fraction

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Fraction

Performs the * operation. Read more
Source§

impl MulAssign<&Fraction> for FuelResult

Source§

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

Performs the *= operation. Read more
Source§

impl MulAssign<Fraction> for FuelResult

Source§

fn mul_assign(&mut self, rhs: Fraction)

Performs the *= operation. Read more
Source§

impl PartialEq for Fraction

Source§

fn eq(&self, other: &Fraction) -> 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 Fraction

Source§

fn partial_cmp(&self, other: &Fraction) -> 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

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<'a> Sub for &'a Fraction

Source§

type Output = Fraction

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Fraction

Performs the - operation. Read more
Source§

impl Copy for Fraction

Source§

impl Eq for Fraction

Auto Trait Implementations§

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, dest: *mut u8)

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

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more