Struct fix_rat::Rational[][src]

#[repr(transparent)]pub struct Rational<const DENOM: i64> { /* fields omitted */ }

A ratonal number with a fixed denominator, therefore size_of<Rational>() == size_of<i64>().

Plus operations have more intuitive valid ranges

If you want to represent numbers in range -x to x choose DENOM as i64::MAX / x.

Rational then subdivides the whole range into i64::MAX equally-sized parts. Smaller operations are lost, going outside the range overflows.

DENOM needs to be positive or you will enter the bizarro universe.

I would strongly recommend to choose DENOM as 1 << x for x in 0..63.

The regular operations (+,-, *, /) behave just like on a regular integer, panic on overflow in debug mode, wrapping in release mode. Use the wrapping_op, checked_op or saturating_op methods to explicitly chose a behaviour.

Implementations

impl<const DENOM: i64> Rational<DENOM>[src]

pub fn to_storage(self) -> i64[src]

Returns the underlying integer type, for example for storing in an atomic number.

This should compile to a no-op.

pub fn from_storage(storage: i64) -> Self[src]

Builds from the underlying integer type, for example after retrieving from an atomic number.

This should compile to a no-op.

Use from_int if you have an integer that you want to convert to a rational.

pub fn from_int(i: i64) -> Self[src]

Converts an integer to a Rational.

pub fn aprox_float_fast(f: f64) -> Option<Self>[src]

Since rational numbers can not represent inf, nan and other fuckery, this returns None if the input is wrong.

This will loose precision, try to only convert at the start and end of your calculation.

pub fn to_f64(self) -> f64[src]

This will loose precision, try to only convert at the start and end of your calculation.

pub fn to_i64(self) -> i64[src]

this will integer-round, potentially loosing a lot of precision.

pub fn clamp(self, low: Self, high: Self) -> Self[src]

pub const fn max() -> Self[src]

The maximum representable number.

Note that unlike floats rationals do not have pos/neg inf.

pub const fn min() -> Self[src]

The minimum representable number.

Note that unlike floats rationals do not have pos/neg inf.

pub fn checked_add(self, other: Self) -> Option<Self>[src]

pub fn checked_mul(self, other: Self) -> Option<Self>[src]

pub fn checked_sub(self, other: Self) -> Option<Self>[src]

pub fn checked_div(self, other: Self) -> Option<Self>[src]

pub fn wrapping_add(self, other: Self) -> Self[src]

pub fn wrapping_mul(self, other: Self) -> Self[src]

pub fn wrapping_sub(self, other: Self) -> Self[src]

pub fn wrapping_div(self, other: Self) -> Self[src]

pub fn saturating_add(self, other: Self) -> Self[src]

Don't use this in parallel code if other parallel code is also subtracting, otherwise you loose determinism.

use fix_rat::Rational;
let max = Rational::<{1024}>::max();
let one = Rational::<{1024}>::from_int(1);
assert_ne!(max.saturating_add(one)-max, (max-max).saturating_add(one));

pub fn saturating_mul(self, other: Self) -> Self[src]

pub fn saturating_sub(self, other: Self) -> Self[src]

Trait Implementations

impl<const DENOM: i64> Add<Rational<DENOM>> for Rational<DENOM>[src]

type Output = Self

The resulting type after applying the + operator.

impl<const DENOM: i64> Clone for Rational<DENOM>[src]

impl<const DENOM: i64> Copy for Rational<DENOM>[src]

impl<const DENOM: i64> Debug for Rational<DENOM>[src]

impl<const DENOM: i64> Default for Rational<DENOM>[src]

impl<'de, const DENOM: i64> Deserialize<'de> for Rational<DENOM>[src]

impl<const DENOM: i64> Div<i64> for Rational<DENOM>[src]

type Output = Self

The resulting type after applying the / operator.

impl<const DENOM: i64> Eq for Rational<DENOM>[src]

impl<const DENOM: i64> From<f64> for Rational<DENOM>[src]

impl<const DENOM: i64> From<i64> for Rational<DENOM>[src]

impl<const DENOM: i64> Mul<i64> for Rational<DENOM>[src]

type Output = Self

The resulting type after applying the * operator.

impl<const DENOM: i64> Ord for Rational<DENOM>[src]

impl<const DENOM: i64> PartialEq<Rational<DENOM>> for Rational<DENOM>[src]

impl<const DENOM: i64> PartialOrd<Rational<DENOM>> for Rational<DENOM>[src]

impl<const DENOM: i64> Serialize for Rational<DENOM>[src]

impl<const DENOM: i64> StructuralEq for Rational<DENOM>[src]

impl<const DENOM: i64> StructuralPartialEq for Rational<DENOM>[src]

impl<const DENOM: i64> Sub<Rational<DENOM>> for Rational<DENOM>[src]

type Output = Self

The resulting type after applying the - operator.

impl<const DENOM: i64> Sum<Rational<DENOM>> for Rational<DENOM>[src]

Auto Trait Implementations

impl<const DENOM: i64> RefUnwindSafe for Rational<DENOM>[src]

impl<const DENOM: i64> Send for Rational<DENOM>[src]

impl<const DENOM: i64> Sync for Rational<DENOM>[src]

impl<const DENOM: i64> Unpin for Rational<DENOM>[src]

impl<const DENOM: i64> UnwindSafe for Rational<DENOM>[src]

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.