Fix

Struct Fix 

Source
pub struct Fix<Bits, Base, Exp> {
    pub bits: Bits,
    /* private fields */
}
Expand description

Fixed-point number representing Bits × Base Exp.

  • Bits is an integer primitive type, or any type which can be created from a type-level integer and exponentiated.
  • Base is an Unsigned type-level integer.
  • Exp is a signed type-level Integer.

§Summary of operations

Lower case variables represent values of Bits. Upper case B and E represent type-level integers Base and Exp, respectively.

  • −(x BE) = (−x) BE
  • (x BE) + (y BE) = (x + y) BE
  • (x BE) − (y BE) = (x − y) BE
  • (x BEx) × (y BEy) = (x × y) BEx + Ey
  • (x BEx) ÷ (y BEy) = (x ÷ y) BEx − Ey
  • (x BEx) % (y BEy) = (x % y) BEx
  • (x BE) × y = (x × y) BE
  • (x BE) ÷ y = (x ÷ y) BE
  • (x BE) % y = (x % y) BE

Fields§

§bits: Bits

The underlying integer.

Implementations§

Source§

impl<Bits, Base, Exp> Fix<Bits, Base, Exp>

Source

pub fn new(bits: Bits) -> Self

Creates a number.

§Examples
use fix::aliases::si::{Kilo, Milli};
Milli::new(25); // 0.025
Kilo::new(25); // 25 000
Source

pub const fn constant(bits: Bits) -> Self

Like Self::new, but creates numbers in the constant context.

Source

pub fn convert<ToExp>(self) -> Fix<Bits, Base, ToExp>
where Bits: FromUnsigned + Pow + Mul<Output = Bits> + Div<Output = Bits>, Base: Unsigned, Exp: Sub<ToExp>, Diff<Exp, ToExp>: Abs + IsLess<Z0>, AbsVal<Diff<Exp, ToExp>>: Integer,

Converts to another Exp.

§Examples
use fix::aliases::si::{Kilo, Milli};
let kilo = Kilo::new(5);
let milli = Milli::new(5_000_000);
assert_eq!(kilo, milli.convert());
assert_eq!(milli, kilo.convert());
Source

pub fn widen<ToBits>(self) -> Fix<ToBits, Base, Exp>
where ToBits: From<Bits>,

Converts the underlying bits to a wider type.

§Examples
use fix::aliases::si::Milli;
let one = Milli::new(16899u64);
let mapped = one.widen::<u128>();
assert_eq!(mapped, Milli::new(16899u128));
Source

pub fn narrow<ToBits>(self) -> Option<Fix<ToBits, Base, Exp>>
where ToBits: TryFrom<Bits>,

Attempts to converts underlying bits to a narrower type. Returns None if conversion fails.

§Examples
use fix::aliases::si::Milli;
let one = Milli::new(16899u128);
let mapped = one.narrow::<u64>();
assert_eq!(mapped, Some(Milli::new(16899u64)));
Source§

impl<Bits, Base, Exp> Fix<Bits, Base, Exp>
where Self: CheckedSub, Bits: Copy,

Source

pub fn abs_diff(&self, v: &Self) -> Fix<Bits, Base, Exp>

Trait Implementations§

Source§

impl<Bits, Base, Exp> Add for Fix<Bits, Base, Exp>
where Bits: Add<Output = Bits>,

Source§

type Output = Fix<Bits, Base, Exp>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

impl<Bits, Base, Exp> AddAssign for Fix<Bits, Base, Exp>
where Bits: AddAssign,

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<Bits, Base, Exp> CheckedAdd for Fix<Bits, Base, Exp>
where Bits: CheckedAdd,

Source§

fn checked_add(&self, v: &Self) -> Option<Self>

Adds two numbers, checking for overflow. If overflow happens, None is returned.
Source§

impl<Bits, Base, LExp, RExp> CheckedDivFix<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>
where Bits: CheckedDiv, LExp: Sub<RExp>,

Source§

type Output = Fix<Bits, Base, <LExp as Sub<RExp>>::Output>

Source§

fn checked_div(&self, v: &Fix<Bits, Base, RExp>) -> Option<Self::Output>

Source§

impl<Bits, Base, LExp, RExp> CheckedMulFix<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>
where Bits: CheckedMul, LExp: Add<RExp>,

Source§

type Output = Fix<Bits, Base, <LExp as Add<RExp>>::Output>

Source§

fn checked_mul(&self, v: &Fix<Bits, Base, RExp>) -> Option<Self::Output>

Source§

impl<Bits, Base, Exp> CheckedSub for Fix<Bits, Base, Exp>
where Bits: CheckedSub,

Source§

fn checked_sub(&self, v: &Self) -> Option<Self>

Subtracts two numbers, checking for underflow. If underflow happens, None is returned.
Source§

impl<Bits, Base, Exp> Clone for Fix<Bits, Base, Exp>
where Bits: Clone,

Source§

fn clone(&self) -> Self

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<Bits, Base, Exp> Debug for Fix<Bits, Base, Exp>
where Bits: Debug, Base: Unsigned, Exp: Integer,

Source§

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

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

impl<Bits, Base, Exp> Default for Fix<Bits, Base, Exp>
where Bits: Default,

Source§

fn default() -> Self

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

impl<Bits, Base, Exp> Div<Bits> for Fix<Bits, Base, Exp>
where Bits: Div<Output = Bits>,

Source§

type Output = Fix<Bits, Base, Exp>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Bits) -> Self

Performs the / operation. Read more
Source§

impl<Bits, Base, LExp, RExp> Div<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>
where Bits: Div<Output = Bits>, LExp: Sub<RExp>,

Source§

type Output = Fix<Bits, Base, <LExp as Sub<RExp>>::Output>

The resulting type after applying the / operator.
Source§

fn div(self, rhs: Fix<Bits, Base, RExp>) -> Self::Output

Performs the / operation. Read more
Source§

impl<Bits, Base, Exp> DivAssign<Bits> for Fix<Bits, Base, Exp>
where Bits: DivAssign,

Source§

fn div_assign(&mut self, rhs: Bits)

Performs the /= operation. Read more
Source§

impl<Bits, Exp> FixExt for Fix<Bits, U10, NInt<Exp>>
where Exp: Unsigned + NonZero + IsLess<U20>, Bits: From<u64>,

Source§

fn one() -> Fix<Bits, U10, NInt<Exp>>

This precision’s equivalent of 1.
Source§

fn zero() -> Self

Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue128
where Bits: Into<i128>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue16
where Bits: Into<i16>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue32
where Bits: Into<i32>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue64
where Bits: Into<i64>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue8
where Bits: Into<i8>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue128
where Bits: Into<u128>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue16
where Bits: Into<u16>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue32
where Bits: Into<u32>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue64
where Bits: Into<u64>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue8
where Bits: Into<u8>, Exp: Integer,

Source§

fn from(fix: Fix<Bits, U10, Exp>) -> Self

Converts to this type from the input type.
Source§

impl<Bits, Base, Exp> Hash for Fix<Bits, Base, Exp>
where Bits: Hash,

Source§

fn hash<H>(&self, state: &mut H)
where H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<Bits, Base, Exp> Mul<Bits> for Fix<Bits, Base, Exp>
where Bits: Mul<Output = Bits>,

Source§

type Output = Fix<Bits, Base, Exp>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Bits) -> Self

Performs the * operation. Read more
Source§

impl<Bits, Base, LExp, RExp> Mul<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>
where Bits: Mul<Output = Bits>, LExp: Add<RExp>,

Source§

type Output = Fix<Bits, Base, <LExp as Add<RExp>>::Output>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Fix<Bits, Base, RExp>) -> Self::Output

Performs the * operation. Read more
Source§

impl<Bits, Base, Exp> MulAssign<Bits> for Fix<Bits, Base, Exp>
where Bits: MulAssign,

Source§

fn mul_assign(&mut self, rhs: Bits)

Performs the *= operation. Read more
Source§

impl<Bits, Base, LExp, RExp> MulDiv<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>
where Bits: MulDiv,

Source§

type Output = Fix<<Bits as MulDiv>::Output, Base, LExp>

Output type for the methods of this trait.
Source§

fn mul_div_ceil( self, num: Fix<Bits, Base, RExp>, denom: Fix<Bits, Base, RExp>, ) -> Option<Self::Output>

Calculates ceil(val * num / denom), i.e. the the smallest integer greater than or equal to the result of the division. Read more
Source§

fn mul_div_floor( self, num: Fix<Bits, Base, RExp>, denom: Fix<Bits, Base, RExp>, ) -> Option<Self::Output>

Calculates floor(val * num / denom), i.e. the largest integer less than or equal to the result of the division. Read more
Source§

fn mul_div_round( self, num: Fix<Bits, Base, RExp>, denom: Fix<Bits, Base, RExp>, ) -> Option<Self::Output>

Calculates round(val * num / denom), i.e. the closest integer to the result of the division. If both surrounding integers are the same distance (x.5), the one with the bigger absolute value is returned (round away from 0.0). Read more
Source§

impl<Bits, Base, Exp> Neg for Fix<Bits, Base, Exp>
where Bits: Neg<Output = Bits>,

Source§

type Output = Fix<Bits, Base, Exp>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl<Bits, Base, Exp> Ord for Fix<Bits, Base, Exp>
where Bits: Ord,

Source§

fn cmp(&self, rhs: &Self) -> 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<Bits, Base, Exp> PartialEq for Fix<Bits, Base, Exp>
where Bits: PartialEq,

Source§

fn eq(&self, rhs: &Self) -> 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<Bits, Base, Exp> PartialOrd for Fix<Bits, Base, Exp>
where Bits: PartialOrd,

Source§

fn partial_cmp(&self, rhs: &Self) -> 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<Bits, Base, Exp> Rem<Bits> for Fix<Bits, Base, Exp>
where Bits: Rem<Output = Bits>,

Source§

type Output = Fix<Bits, Base, Exp>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Bits) -> Self

Performs the % operation. Read more
Source§

impl<Bits, Base, Exp> Rem for Fix<Bits, Base, Exp>
where Bits: Rem<Output = Bits>,

Source§

type Output = Fix<Bits, Base, Exp>

The resulting type after applying the % operator.
Source§

fn rem(self, rhs: Self) -> Self

Performs the % operation. Read more
Source§

impl<Bits, Base, Exp> RemAssign<Bits> for Fix<Bits, Base, Exp>
where Bits: RemAssign,

Source§

fn rem_assign(&mut self, rhs: Bits)

Performs the %= operation. Read more
Source§

impl<Bits, Base, LExp, RExp> RemAssign<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>
where Bits: RemAssign,

Source§

fn rem_assign(&mut self, rhs: Fix<Bits, Base, RExp>)

Performs the %= operation. Read more
Source§

impl<Bits, Base, Exp> SaturatingAdd for Fix<Bits, Base, Exp>
where Bits: SaturatingAdd,

Source§

fn saturating_add(&self, v: &Self) -> Self

Saturating addition. Computes self + other, saturating at the relevant high or low boundary of the type.
Source§

impl<Bits, Base, Exp> SaturatingSub for Fix<Bits, Base, Exp>
where Bits: SaturatingSub,

Source§

fn saturating_sub(&self, v: &Self) -> Self

Saturating subtraction. Computes self - other, saturating at the relevant high or low boundary of the type.
Source§

impl<Bits, Base, Exp> Sub for Fix<Bits, Base, Exp>
where Bits: Sub<Output = Bits>,

Source§

type Output = Fix<Bits, Base, Exp>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

impl<Bits, Base, Exp> SubAssign for Fix<Bits, Base, Exp>
where Bits: SubAssign,

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl<Bits, Exp> TryFrom<IFixValue128> for Fix<Bits, U10, Exp>
where Bits: From<i128>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: IFixValue128) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Exp> TryFrom<IFixValue16> for Fix<Bits, U10, Exp>
where Bits: From<i16>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: IFixValue16) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Exp> TryFrom<IFixValue32> for Fix<Bits, U10, Exp>
where Bits: From<i32>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: IFixValue32) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Exp> TryFrom<IFixValue64> for Fix<Bits, U10, Exp>
where Bits: From<i64>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: IFixValue64) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Exp> TryFrom<IFixValue8> for Fix<Bits, U10, Exp>
where Bits: From<i8>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: IFixValue8) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Exp> TryFrom<UFixValue128> for Fix<Bits, U10, Exp>
where Bits: From<u128>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: UFixValue128) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Exp> TryFrom<UFixValue16> for Fix<Bits, U10, Exp>
where Bits: From<u16>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: UFixValue16) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Exp> TryFrom<UFixValue32> for Fix<Bits, U10, Exp>
where Bits: From<u32>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: UFixValue32) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Exp> TryFrom<UFixValue64> for Fix<Bits, U10, Exp>
where Bits: From<u64>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: UFixValue64) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Exp> TryFrom<UFixValue8> for Fix<Bits, U10, Exp>
where Bits: From<u8>, Exp: Integer,

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(value: UFixValue8) -> Result<Fix<Bits, U10, Exp>>

Performs the conversion.
Source§

impl<Bits, Base, Exp> Copy for Fix<Bits, Base, Exp>
where Bits: Copy,

Source§

impl<Bits, Base, Exp> Eq for Fix<Bits, Base, Exp>
where Bits: Eq,

Auto Trait Implementations§

§

impl<Bits, Base, Exp> Freeze for Fix<Bits, Base, Exp>
where Bits: Freeze,

§

impl<Bits, Base, Exp> RefUnwindSafe for Fix<Bits, Base, Exp>
where Bits: RefUnwindSafe, Base: RefUnwindSafe, Exp: RefUnwindSafe,

§

impl<Bits, Base, Exp> Send for Fix<Bits, Base, Exp>
where Bits: Send, Base: Send, Exp: Send,

§

impl<Bits, Base, Exp> Sync for Fix<Bits, Base, Exp>
where Bits: Sync, Base: Sync, Exp: Sync,

§

impl<Bits, Base, Exp> Unpin for Fix<Bits, Base, Exp>
where Bits: Unpin, Base: Unpin, Exp: Unpin,

§

impl<Bits, Base, Exp> UnwindSafe for Fix<Bits, Base, Exp>
where Bits: UnwindSafe, Base: UnwindSafe, Exp: UnwindSafe,

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> Same for T

Source§

type Output = T

Should always be Self
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, Rhs> NumAssignOps<Rhs> for T
where T: AddAssign<Rhs> + SubAssign<Rhs> + MulAssign<Rhs> + DivAssign<Rhs> + RemAssign<Rhs>,

Source§

impl<T, Rhs, Output> NumOps<Rhs, Output> for T
where T: Sub<Rhs, Output = Output> + Mul<Rhs, Output = Output> + Div<Rhs, Output = Output> + Add<Rhs, Output = Output> + Rem<Rhs, Output = Output>,