pub struct Fix<Bits, Base, Exp> {
pub bits: Bits,
/* private fields */
}Expand description
Fixed-point number representing Bits × Base Exp.
Bitsis an integer primitive type, or any type which can be created from a type-level integer and exponentiated.Baseis anUnsignedtype-level integer.Expis a signed type-levelInteger.
§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: BitsThe underlying integer.
Implementations§
Source§impl<Bits, Base, Exp> Fix<Bits, Base, Exp>
impl<Bits, Base, Exp> Fix<Bits, Base, Exp>
Sourcepub fn new(bits: Bits) -> Self
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 000Sourcepub const fn constant(bits: Bits) -> Self
pub const fn constant(bits: Bits) -> Self
Like Self::new, but creates numbers in the constant context.
Sourcepub fn convert<ToExp>(self) -> Fix<Bits, Base, ToExp>
pub fn convert<ToExp>(self) -> Fix<Bits, Base, ToExp>
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());Sourcepub fn widen<ToBits>(self) -> Fix<ToBits, Base, Exp>where
ToBits: From<Bits>,
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));Sourcepub fn narrow<ToBits>(self) -> Option<Fix<ToBits, Base, Exp>>where
ToBits: TryFrom<Bits>,
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)));Trait Implementations§
Source§impl<Bits, Base, Exp> AddAssign for Fix<Bits, Base, Exp>where
Bits: AddAssign,
impl<Bits, Base, Exp> AddAssign for Fix<Bits, Base, Exp>where
Bits: AddAssign,
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+= operation. Read moreSource§impl<Bits, Base, Exp> CheckedAdd for Fix<Bits, Base, Exp>where
Bits: CheckedAdd,
impl<Bits, Base, Exp> CheckedAdd for Fix<Bits, Base, Exp>where
Bits: CheckedAdd,
Source§fn checked_add(&self, v: &Self) -> Option<Self>
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>,
impl<Bits, Base, LExp, RExp> CheckedDivFix<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>where
Bits: CheckedDiv,
LExp: Sub<RExp>,
Source§impl<Bits, Base, LExp, RExp> CheckedMulFix<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>where
Bits: CheckedMul,
LExp: Add<RExp>,
impl<Bits, Base, LExp, RExp> CheckedMulFix<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>where
Bits: CheckedMul,
LExp: Add<RExp>,
Source§impl<Bits, Base, Exp> CheckedSub for Fix<Bits, Base, Exp>where
Bits: CheckedSub,
impl<Bits, Base, Exp> CheckedSub for Fix<Bits, Base, Exp>where
Bits: CheckedSub,
Source§fn checked_sub(&self, v: &Self) -> Option<Self>
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> DivAssign<Bits> for Fix<Bits, Base, Exp>where
Bits: DivAssign,
impl<Bits, Base, Exp> DivAssign<Bits> for Fix<Bits, Base, Exp>where
Bits: DivAssign,
Source§fn div_assign(&mut self, rhs: Bits)
fn div_assign(&mut self, rhs: Bits)
Performs the
/= operation. Read moreSource§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue128
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue128
Source§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue16
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue16
Source§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue32
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue32
Source§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue64
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue64
Source§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue8
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for IFixValue8
Source§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue128
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue128
Source§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue16
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue16
Source§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue32
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue32
Source§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue64
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue64
Source§impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue8
impl<Bits, Exp> From<Fix<Bits, UInt<UInt<UInt<UInt<UTerm, B1>, B0>, B1>, B0>, Exp>> for UFixValue8
Source§impl<Bits, Base, Exp> MulAssign<Bits> for Fix<Bits, Base, Exp>where
Bits: MulAssign,
impl<Bits, Base, Exp> MulAssign<Bits> for Fix<Bits, Base, Exp>where
Bits: MulAssign,
Source§fn mul_assign(&mut self, rhs: Bits)
fn mul_assign(&mut self, rhs: Bits)
Performs the
*= operation. Read moreSource§impl<Bits, Base, LExp, RExp> MulDiv<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>where
Bits: MulDiv,
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>
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>
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 moreSource§fn mul_div_floor(
self,
num: Fix<Bits, Base, RExp>,
denom: Fix<Bits, Base, RExp>,
) -> Option<Self::Output>
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 moreSource§fn mul_div_round(
self,
num: Fix<Bits, Base, RExp>,
denom: Fix<Bits, Base, RExp>,
) -> Option<Self::Output>
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 moreSource§impl<Bits, Base, Exp> Ord for Fix<Bits, Base, Exp>where
Bits: Ord,
impl<Bits, Base, Exp> Ord for Fix<Bits, Base, Exp>where
Bits: Ord,
Source§impl<Bits, Base, Exp> PartialOrd for Fix<Bits, Base, Exp>where
Bits: PartialOrd,
impl<Bits, Base, Exp> PartialOrd for Fix<Bits, Base, Exp>where
Bits: PartialOrd,
Source§impl<Bits, Base, Exp> RemAssign<Bits> for Fix<Bits, Base, Exp>where
Bits: RemAssign,
impl<Bits, Base, Exp> RemAssign<Bits> for Fix<Bits, Base, Exp>where
Bits: RemAssign,
Source§fn rem_assign(&mut self, rhs: Bits)
fn rem_assign(&mut self, rhs: Bits)
Performs the
%= operation. Read moreSource§impl<Bits, Base, LExp, RExp> RemAssign<Fix<Bits, Base, RExp>> for Fix<Bits, Base, LExp>where
Bits: RemAssign,
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>)
fn rem_assign(&mut self, rhs: Fix<Bits, Base, RExp>)
Performs the
%= operation. Read moreSource§impl<Bits, Base, Exp> SaturatingAdd for Fix<Bits, Base, Exp>where
Bits: SaturatingAdd,
impl<Bits, Base, Exp> SaturatingAdd for Fix<Bits, Base, Exp>where
Bits: SaturatingAdd,
Source§fn saturating_add(&self, v: &Self) -> Self
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,
impl<Bits, Base, Exp> SaturatingSub for Fix<Bits, Base, Exp>where
Bits: SaturatingSub,
Source§fn saturating_sub(&self, v: &Self) -> Self
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> SubAssign for Fix<Bits, Base, Exp>where
Bits: SubAssign,
impl<Bits, Base, Exp> SubAssign for Fix<Bits, Base, Exp>where
Bits: SubAssign,
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-= operation. Read moreimpl<Bits, Base, Exp> Copy for Fix<Bits, Base, Exp>where
Bits: Copy,
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>
impl<Bits, Base, Exp> Send for Fix<Bits, Base, Exp>
impl<Bits, Base, Exp> Sync for Fix<Bits, Base, Exp>
impl<Bits, Base, Exp> Unpin for Fix<Bits, Base, Exp>
impl<Bits, Base, Exp> UnwindSafe for Fix<Bits, Base, Exp>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more