Struct fix::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 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 map_bits<ToBits, F>(self, f: F) -> Fix<ToBits, Base, Exp>
where F: Fn(Bits) -> ToBits,

Converts the underlying bits to another type. This operation can lose precision (e.g. u128 -> u8).

§Examples
use fix::aliases::si::Milli;
let one = Milli::new(16899u128);
let mapped = one.map_bits(|b| b as u64);
assert_eq!(mapped, Milli::new(16899u64));

Trait Implementations§

source§

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

§

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>,

§

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>,

§

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 copy 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>,

§

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>,

§

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, 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>,

§

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>,

§

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, Exp> Neg for Fix<Bits, Base, Exp>
where Bits: Neg<Output = Bits>,

§

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 + PartialOrd,

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method 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

This method 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

This method 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

This method 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

This method 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>,

§

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>,

§

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> Sub for Fix<Bits, Base, Exp>
where Bits: Sub<Output = Bits>,

§

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, 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 Base: RefUnwindSafe, Bits: RefUnwindSafe, Exp: RefUnwindSafe,

§

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

§

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

§

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

§

impl<Bits, Base, Exp> UnwindSafe for Fix<Bits, Base, Exp>
where Base: UnwindSafe, Bits: 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> 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

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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>,