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>
impl<const DENOM: i64> Rational<DENOM>
Sourcepub fn to_storage(self) -> i64
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.
Sourcepub fn from_storage(storage: i64) -> Self
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.
Sourcepub fn aprox_float_fast(f: f64) -> Option<Self>
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.
Sourcepub fn to_f64(self) -> f64
pub fn to_f64(self) -> f64
This will loose precision, try to only convert at the start and end of your calculation.
pub fn clamp(self, low: Self, high: Self) -> Self
Sourcepub const fn max() -> Self
pub const fn max() -> Self
The maximum representable number.
Note that unlike floats rationals do not have pos/neg inf.
Sourcepub const fn min() -> Self
pub const fn min() -> Self
The minimum representable number.
Note that unlike floats rationals do not have pos/neg inf.
pub fn checked_add(self, other: Self) -> Option<Self>
pub fn checked_mul(self, other: Self) -> Option<Self>
pub fn checked_sub(self, other: Self) -> Option<Self>
pub fn checked_div(self, other: Self) -> Option<Self>
pub fn wrapping_add(self, other: Self) -> Self
pub fn wrapping_mul(self, other: i64) -> Self
pub fn wrapping_sub(self, other: Self) -> Self
pub fn wrapping_div(self, other: i64) -> Self
Sourcepub fn saturating_add(self, other: Self) -> Self
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));pub fn saturating_mul(self, other: i64) -> Self
pub fn saturating_sub(self, other: Self) -> Self
Trait Implementations§
Source§impl<'de, const DENOM: i64> Deserialize<'de> for Rational<DENOM>
impl<'de, const DENOM: i64> Deserialize<'de> for Rational<DENOM>
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Source§impl<const DENOM: i64> Ord for Rational<DENOM>
impl<const DENOM: i64> Ord for Rational<DENOM>
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<const DENOM: i64> PartialOrd for Rational<DENOM>
impl<const DENOM: i64> PartialOrd for Rational<DENOM>
impl<const DENOM: i64> Copy for Rational<DENOM>
impl<const DENOM: i64> Eq for Rational<DENOM>
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.