Rational

Struct Rational 

Source
pub struct Rational<const DENOM: i64> { /* private fields */ }
Expand description

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§

Source§

impl<const DENOM: i64> Rational<DENOM>

Source

pub fn to_storage(self) -> i64

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

This should compile to a no-op.

Source

pub fn from_storage(storage: i64) -> Self

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.

Source

pub fn from_int(i: i64) -> Self

Converts an integer to a Rational.

Source

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

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.

Source

pub fn to_f64(self) -> f64

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

Source

pub fn to_i64(self) -> i64

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

Source

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

Source

pub const fn max() -> Self

The maximum representable number.

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

Source

pub const fn min() -> Self

The minimum representable number.

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

Source

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

Source

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

Source

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

Source

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

Source

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

Source

pub fn wrapping_mul(self, other: i64) -> Self

Source

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

Source

pub fn wrapping_div(self, other: i64) -> Self

Source

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

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

pub fn saturating_mul(self, other: i64) -> Self

Source

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

Trait Implementations§

Source§

impl<const DENOM: i64> Add for Rational<DENOM>

Source§

type Output = Rational<DENOM>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<const DENOM: i64> Clone for Rational<DENOM>

Source§

fn clone(&self) -> Rational<DENOM>

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<const DENOM: i64> Debug for Rational<DENOM>

Source§

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

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

impl<const DENOM: i64> Default for Rational<DENOM>

Source§

fn default() -> Rational<DENOM>

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

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

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<const DENOM: i64> Div<i64> for Rational<DENOM>

Source§

type Output = Rational<DENOM>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<const DENOM: i64> From<f64> for Rational<DENOM>

Source§

fn from(o: f64) -> Self

Converts to this type from the input type.
Source§

impl<const DENOM: i64> From<i64> for Rational<DENOM>

Source§

fn from(o: i64) -> Self

Converts to this type from the input type.
Source§

impl<const DENOMS: i64, const DENOMO: i64> Mul<Rational<DENOMO>> for Rational<DENOMS>
where [(); { _ }]:,

Source§

type Output = Rational<{ DENOMS * DENOMO }>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Rational<DENOMO>) -> Self::Output

Performs the * operation. Read more
Source§

impl<const DENOM: i64> Mul<i64> for Rational<DENOM>

Source§

type Output = Rational<DENOM>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<const DENOM: i64> Ord for Rational<DENOM>

Source§

fn cmp(&self, other: &Rational<DENOM>) -> 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<const DENOM: i64> PartialEq for Rational<DENOM>

Source§

fn eq(&self, other: &Rational<DENOM>) -> 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<const DENOM: i64> PartialOrd for Rational<DENOM>

Source§

fn partial_cmp(&self, other: &Rational<DENOM>) -> 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<const DENOM: i64> Serialize for Rational<DENOM>

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<const DENOM: i64> Sub for Rational<DENOM>

Source§

type Output = Rational<DENOM>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<const DENOM: i64> Sum for Rational<DENOM>

Source§

fn sum<I>(i: I) -> Self
where I: Iterator<Item = Self>,

Takes an iterator and generates Self from the elements by “summing up” the items.
Source§

impl<const DENOM: i64> Copy for Rational<DENOM>

Source§

impl<const DENOM: i64> Eq for Rational<DENOM>

Source§

impl<const DENOM: i64> NoUninit for Rational<DENOM>

SAFETY: this is literally just an i64, transparent representation and all, this is therefore safe. can not auto-derive cause the derive macro can not very alignment in generic types.

Source§

impl<const DENOM: i64> StructuralPartialEq for Rational<DENOM>

Auto Trait Implementations§

§

impl<const DENOM: i64> Freeze for Rational<DENOM>

§

impl<const DENOM: i64> RefUnwindSafe for Rational<DENOM>

§

impl<const DENOM: i64> Send for Rational<DENOM>

§

impl<const DENOM: i64> Sync for Rational<DENOM>

§

impl<const DENOM: i64> Unpin for Rational<DENOM>

§

impl<const DENOM: i64> UnwindSafe for Rational<DENOM>

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

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