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 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, 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§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,
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
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 moresource§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> 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