pub struct Fraction {
pub numerator: i128,
pub denominator: i128,
}Fields§
§numerator: i128§denominator: i128Implementations§
Trait Implementations§
Source§impl Mul<Fraction> for Fuel
Fuel * Fraction, Fuel / Fraction – always round up on loss of precision
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§impl MulAssign<&Fraction> for FuelResult
impl MulAssign<&Fraction> for FuelResult
Source§fn mul_assign(&mut self, rhs: &Fraction)
fn mul_assign(&mut self, rhs: &Fraction)
*= operation. Read moreSource§impl MulAssign<Fraction> for FuelResult
impl MulAssign<Fraction> for FuelResult
Source§fn mul_assign(&mut self, rhs: Fraction)
fn mul_assign(&mut self, rhs: Fraction)
*= operation. Read moreSource§impl PartialOrd for Fraction
impl PartialOrd for Fraction
impl Copy for Fraction
impl Eq for Fraction
Auto Trait Implementations§
impl Freeze for Fraction
impl RefUnwindSafe for Fraction
impl Send for Fraction
impl Sync for Fraction
impl Unpin for Fraction
impl UnwindSafe for Fraction
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.