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

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

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