pub struct Q<T, A, const F: i8> { /* private fields */ }Expand description
Fixed point integer
Generics:
T: Base integerA: Accumulator for intermediate resultsF: Number of fractional bits right of the decimal point
F negative is supported analogously.
Q32<31>is(-1..1).step_by(2^-31)Q<i16, _, 20>is(-1/32..1/32).step_by(2^-20)Q<u8, _, 4>is(0..16).step_by(1/16)Q<u8, _, -2>is(0..1024).step_by(4)
assert_eq!(Q8::<4>::from_int(3), Q8::from_bits(3 << 4));
assert_eq!(7 * Q8::<4>::from_f32(1.5), 10);
assert_eq!(Q8::<4>::from_f32(1.5).apply(7), 10);
assert_eq!(7 / Q8::<4>::from_f32(1.5), 4);Implementations§
Source§impl<T, A, const F: i8> Q<T, A, F>
impl<T, A, const F: i8> Q<T, A, F>
Source§impl<T: Shift, A, const F: i8> Q<T, A, F>
impl<T: Shift, A, const F: i8> Q<T, A, F>
Sourcepub fn scale<const F1: i8>(self) -> Q<T, A, F1>
pub fn scale<const F1: i8>(self) -> Q<T, A, F1>
Convert to a different number of fractional bits (truncating)
Use this liberally for Add/Sub/Rem with Q’s of different F.
assert_eq!(Q8::<4>::from_bits(32).scale::<0>(), Q8::from_bits(2));Trait Implementations§
Source§impl<T: AddAssign<T>, A, const F: i8> AddAssign for Q<T, A, F>
impl<T: AddAssign<T>, A, const F: i8> AddAssign for Q<T, A, F>
Source§fn add_assign(&mut self, rhs: Self)
fn add_assign(&mut self, rhs: Self)
Performs the
+= operation. Read moreSource§impl<T, A, const F: i8> AsPrimitive<f32> for Q<T, A, F>where
T: AsPrimitive<f32>,
A: 'static,
impl<T, A, const F: i8> AsPrimitive<f32> for Q<T, A, F>where
T: AsPrimitive<f32>,
A: 'static,
Source§impl<T, A, const F: i8> AsPrimitive<f64> for Q<T, A, F>where
T: AsPrimitive<f64>,
A: 'static,
impl<T, A, const F: i8> AsPrimitive<f64> for Q<T, A, F>where
T: AsPrimitive<f64>,
A: 'static,
Source§impl<T: BitAndAssign<T>, A, const F: i8> BitAndAssign for Q<T, A, F>
impl<T: BitAndAssign<T>, A, const F: i8> BitAndAssign for Q<T, A, F>
Source§fn bitand_assign(&mut self, rhs: Self)
fn bitand_assign(&mut self, rhs: Self)
Performs the
&= operation. Read moreSource§impl<T: BitOrAssign<T>, A, const F: i8> BitOrAssign for Q<T, A, F>
impl<T: BitOrAssign<T>, A, const F: i8> BitOrAssign for Q<T, A, F>
Source§fn bitor_assign(&mut self, rhs: Self)
fn bitor_assign(&mut self, rhs: Self)
Performs the
|= operation. Read moreSource§impl<T: BitXorAssign<T>, A, const F: i8> BitXorAssign for Q<T, A, F>
impl<T: BitXorAssign<T>, A, const F: i8> BitXorAssign for Q<T, A, F>
Source§fn bitxor_assign(&mut self, rhs: Self)
fn bitxor_assign(&mut self, rhs: Self)
Performs the
^= operation. Read moreimpl<T: Copy, A, const F: i8> Copy for Q<T, A, F>
Source§impl<T, A, const F: i8> Debug for Q<T, A, F>where
T: Debug,
Binary, octal, and hexadecimal formatting always include the fixed-point radix point.
Even whole-valued cases keep a trailing . to distinguish them from raw integer formatting.
impl<T, A, const F: i8> Debug for Q<T, A, F>where
T: Debug,
Binary, octal, and hexadecimal formatting always include the fixed-point radix point.
Even whole-valued cases keep a trailing . to distinguish them from raw integer formatting.
assert_eq!(format!("{:?}", Q8::<4>::from_bits(0x14)), "20");
assert_eq!(format!("{:#b}", Q8::<3>::from_bits(0b01101001)), "0b1101.001");
assert_eq!(format!("{:x}", Q8::<-2>::from_bits(3)), "c.");
assert_eq!(format!("{:x}", Q8::<4>::from_bits(-0x14)), "-1.4");Source§impl<'de, T, A, const F: i8> Deserialize<'de> for Q<T, A, F>where
T: Deserialize<'de>,
impl<'de, T, A, const F: i8> Deserialize<'de> for Q<T, A, F>where
T: Deserialize<'de>,
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Source§impl<T, A, const F: i8> Div for Q<T, A, F>where
Self: DivAssign,
Q/Q -> Q
impl<T, A, const F: i8> Div for Q<T, A, F>where
Self: DivAssign,
Q/Q -> Q
assert_eq!(Q8::<4>::from_int(3) / Q8::from_int(2), Q8::from_f32(1.5));Source§impl<T: Copy + Shift + Accu<A> + Div<T, Output = T>, A: Shift + Div<A, Output = A>, const F: i8, const F1: i8> DivAssign<Q<T, A, F1>> for Q<T, A, F>
Q /= Q’
impl<T: Copy + Shift + Accu<A> + Div<T, Output = T>, A: Shift + Div<A, Output = A>, const F: i8, const F1: i8> DivAssign<Q<T, A, F1>> for Q<T, A, F>
Q /= Q’
let mut q = Q8::<4>::from_f32(0.75);
q /= Q8::<3>::from_int(3);
assert_eq!(q, Q8::from_f32(0.25));Source§fn div_assign(&mut self, rhs: Q<T, A, F1>)
fn div_assign(&mut self, rhs: Q<T, A, F1>)
Performs the
/= operation. Read moreSource§impl<T: DivAssign<T>, A, const F: i8> DivAssign<T> for Q<T, A, F>
impl<T: DivAssign<T>, A, const F: i8> DivAssign<T> for Q<T, A, F>
Source§fn div_assign(&mut self, rhs: T)
fn div_assign(&mut self, rhs: T)
Performs the
/= operation. Read moreimpl<T: Eq, A, const F: i8> Eq for Q<T, A, F>where
Self: PartialEq,
Source§impl<T, A, const F: i8> Format for Q<T, A, F>where
T: AsFloat,
Available on crate feature defmt only.
impl<T, A, const F: i8> Format for Q<T, A, F>where
T: AsFloat,
Available on crate feature
defmt only.Source§impl<T: Accu<A> + Shift, A, const F: i8> From<(T, i8)> for Q<T, A, F>
Lossy conversion from a dynamically scaled integer
impl<T: Accu<A> + Shift, A, const F: i8> From<(T, i8)> for Q<T, A, F>
Lossy conversion from a dynamically scaled integer
assert_eq!(Q8::<8>::from((1, 3)).into_bits(), 1 << 5);Source§impl<T, A, const F: i8> From<Q<T, A, F>> for (T, i8)
Lossless conversion into a dynamically scaled integer
impl<T, A, const F: i8> From<Q<T, A, F>> for (T, i8)
Lossless conversion into a dynamically scaled integer
let q: (i8, i8) = Q8::<8>::from_bits(9).into();
assert_eq!(q, (9, 8));Source§impl<T, A, const F: i8> FromPrimitive for Q<T, A, F>where
T: 'static + Copy + FromPrimitive + Shift,
A: 'static,
f32: AsPrimitive<Q<T, A, F>>,
f64: AsPrimitive<Q<T, A, F>>,
Self: Copy + 'static,
impl<T, A, const F: i8> FromPrimitive for Q<T, A, F>where
T: 'static + Copy + FromPrimitive + Shift,
A: 'static,
f32: AsPrimitive<Q<T, A, F>>,
f64: AsPrimitive<Q<T, A, F>>,
Self: Copy + 'static,
Source§fn from_i64(n: i64) -> Option<Self>
fn from_i64(n: i64) -> Option<Self>
Converts an
i64 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i128(n: i128) -> Option<Self>
fn from_i128(n: i128) -> Option<Self>
Converts an
i128 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned. Read moreSource§fn from_u64(n: u64) -> Option<Self>
fn from_u64(n: u64) -> Option<Self>
Converts an
u64 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u128(n: u128) -> Option<Self>
fn from_u128(n: u128) -> Option<Self>
Converts an
u128 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned. Read moreSource§fn from_f32(n: f32) -> Option<Self>
fn from_f32(n: f32) -> Option<Self>
Converts a
f32 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_f64(n: f64) -> Option<Self>
fn from_f64(n: f64) -> Option<Self>
Converts a
f64 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned. Read moreSource§fn from_isize(n: isize) -> Option<Self>
fn from_isize(n: isize) -> Option<Self>
Converts an
isize to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i8(n: i8) -> Option<Self>
fn from_i8(n: i8) -> Option<Self>
Converts an
i8 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i16(n: i16) -> Option<Self>
fn from_i16(n: i16) -> Option<Self>
Converts an
i16 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_i32(n: i32) -> Option<Self>
fn from_i32(n: i32) -> Option<Self>
Converts an
i32 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_usize(n: usize) -> Option<Self>
fn from_usize(n: usize) -> Option<Self>
Converts a
usize to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§fn from_u8(n: u8) -> Option<Self>
fn from_u8(n: u8) -> Option<Self>
Converts an
u8 to return an optional value of this type. If the
value cannot be represented by this type, then None is returned.Source§impl<T, A, const F: i8> Mul for Q<T, A, F>where
Self: MulAssign,
Q*Q -> Q
impl<T, A, const F: i8> Mul for Q<T, A, F>where
Self: MulAssign,
Q*Q -> Q
assert_eq!(
Q8::<4>::from_f32(0.75) * Q8::from_int(3),
Q8::from_f32(2.25)
);Source§impl<T: Accu<A>, A: Mul<Output = A>, const F: i8> Mul<T> for Q<T, A, F>
Wide multiplication to accumulator
impl<T: Accu<A>, A: Mul<Output = A>, const F: i8> Mul<T> for Q<T, A, F>
Wide multiplication to accumulator
assert_eq!(Q8::<3>::from_bits(4) * 2, Q::from_bits(8));
assert_eq!(Q8::<3>::from_bits(4) / 2, Q8::from_bits(2));Source§impl<T: Copy + Accu<A>, A: Shift + Mul<A, Output = A>, const F: i8, const F1: i8> MulAssign<Q<T, A, F1>> for Q<T, A, F>
Q *= Q’
impl<T: Copy + Accu<A>, A: Shift + Mul<A, Output = A>, const F: i8, const F1: i8> MulAssign<Q<T, A, F1>> for Q<T, A, F>
Q *= Q’
let mut q = Q8::<4>::from_f32(0.25);
q *= Q8::<3>::from_int(3);
assert_eq!(q, Q8::from_f32(0.75));Source§fn mul_assign(&mut self, rhs: Q<T, A, F1>)
fn mul_assign(&mut self, rhs: Q<T, A, F1>)
Performs the
*= operation. Read moreSource§impl<T: MulAssign<T>, A, const F: i8> MulAssign<T> for Q<T, A, F>
impl<T: MulAssign<T>, A, const F: i8> MulAssign<T> for Q<T, A, F>
Source§fn mul_assign(&mut self, rhs: T)
fn mul_assign(&mut self, rhs: T)
Performs the
*= operation. Read moreSource§impl<T, A, const F: i8> Num for Q<T, A, F>
impl<T, A, const F: i8> Num for Q<T, A, F>
type FromStrRadixErr = <T as Num>::FromStrRadixErr
Source§fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
fn from_str_radix(str: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr>
Convert from a string and radix (typically
2..=36). Read moreSource§impl<T: Ord, A, const F: i8> Ord for Q<T, A, F>where
Self: PartialOrd,
impl<T: Ord, A, const F: i8> Ord for Q<T, A, F>where
Self: PartialOrd,
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl<T: PartialOrd, A, const F: i8> PartialOrd for Q<T, A, F>
impl<T: PartialOrd, A, const F: i8> PartialOrd for Q<T, A, F>
impl<T, A, const F: i8> Pod for Q<T, A, F>
Source§impl<T: RemAssign<T>, A, const F: i8> RemAssign for Q<T, A, F>
impl<T: RemAssign<T>, A, const F: i8> RemAssign for Q<T, A, F>
Source§fn rem_assign(&mut self, rhs: Self)
fn rem_assign(&mut self, rhs: Self)
Performs the
%= operation. Read moreSource§impl<T: ShlAssign<U>, U, A, const F: i8> ShlAssign<U> for Q<T, A, F>
impl<T: ShlAssign<U>, U, A, const F: i8> ShlAssign<U> for Q<T, A, F>
Source§fn shl_assign(&mut self, rhs: U)
fn shl_assign(&mut self, rhs: U)
Performs the
<<= operation. Read moreSource§impl<T: ShrAssign<U>, U, A, const F: i8> ShrAssign<U> for Q<T, A, F>
impl<T: ShrAssign<U>, U, A, const F: i8> ShrAssign<U> for Q<T, A, F>
Source§fn shr_assign(&mut self, rhs: U)
fn shr_assign(&mut self, rhs: U)
Performs the
>>= operation. Read moreSource§impl<A, const F: i8> Signed for Q<i8, A, F>
impl<A, const F: i8> Signed for Q<i8, A, F>
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
Source§impl<A, const F: i8> Signed for Q<Wrapping<i8>, A, F>
impl<A, const F: i8> Signed for Q<Wrapping<i8>, A, F>
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
Source§impl<A, const F: i8> Signed for Q<i16, A, F>
impl<A, const F: i8> Signed for Q<i16, A, F>
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
Source§impl<A, const F: i8> Signed for Q<Wrapping<i16>, A, F>
impl<A, const F: i8> Signed for Q<Wrapping<i16>, A, F>
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
Source§impl<A, const F: i8> Signed for Q<i32, A, F>
impl<A, const F: i8> Signed for Q<i32, A, F>
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
Source§impl<A, const F: i8> Signed for Q<Wrapping<i32>, A, F>
impl<A, const F: i8> Signed for Q<Wrapping<i32>, A, F>
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
Source§impl<A, const F: i8> Signed for Q<i64, A, F>
impl<A, const F: i8> Signed for Q<i64, A, F>
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
Source§impl<A, const F: i8> Signed for Q<Wrapping<i64>, A, F>
impl<A, const F: i8> Signed for Q<Wrapping<i64>, A, F>
Source§fn is_positive(&self) -> bool
fn is_positive(&self) -> bool
Returns true if the number is positive and false if the number is zero or negative.
Source§fn is_negative(&self) -> bool
fn is_negative(&self) -> bool
Returns true if the number is negative and false if the number is zero or positive.
Source§impl<T: SubAssign<T>, A, const F: i8> SubAssign for Q<T, A, F>
impl<T: SubAssign<T>, A, const F: i8> SubAssign for Q<T, A, F>
Source§fn sub_assign(&mut self, rhs: Self)
fn sub_assign(&mut self, rhs: Self)
Performs the
-= operation. Read moreSource§impl<T: Shift + ToPrimitive + AsFloat, A, const F: i8> ToPrimitive for Q<T, A, F>
impl<T: Shift + ToPrimitive + AsFloat, A, const F: i8> ToPrimitive for Q<T, A, F>
Source§fn to_i64(&self) -> Option<i64>
fn to_i64(&self) -> Option<i64>
Converts the value of
self to an i64. If the value cannot be
represented by an i64, then None is returned.Source§fn to_i128(&self) -> Option<i128>
fn to_i128(&self) -> Option<i128>
Converts the value of
self to an i128. If the value cannot be
represented by an i128 (i64 under the default implementation), then
None is returned. Read moreSource§fn to_u64(&self) -> Option<u64>
fn to_u64(&self) -> Option<u64>
Converts the value of
self to a u64. If the value cannot be
represented by a u64, then None is returned.Source§fn to_u128(&self) -> Option<u128>
fn to_u128(&self) -> Option<u128>
Converts the value of
self to a u128. If the value cannot be
represented by a u128 (u64 under the default implementation), then
None is returned. Read moreSource§fn to_f32(&self) -> Option<f32>
fn to_f32(&self) -> Option<f32>
Converts the value of
self to an f32. Overflows may map to positive
or negative inifinity, otherwise None is returned if the value cannot
be represented by an f32.Source§fn to_f64(&self) -> Option<f64>
fn to_f64(&self) -> Option<f64>
Converts the value of
self to an f64. Overflows may map to positive
or negative inifinity, otherwise None is returned if the value cannot
be represented by an f64. Read moreSource§fn to_isize(&self) -> Option<isize>
fn to_isize(&self) -> Option<isize>
Converts the value of
self to an isize. If the value cannot be
represented by an isize, then None is returned.Source§fn to_i8(&self) -> Option<i8>
fn to_i8(&self) -> Option<i8>
Converts the value of
self to an i8. If the value cannot be
represented by an i8, then None is returned.Source§fn to_i16(&self) -> Option<i16>
fn to_i16(&self) -> Option<i16>
Converts the value of
self to an i16. If the value cannot be
represented by an i16, then None is returned.Source§fn to_i32(&self) -> Option<i32>
fn to_i32(&self) -> Option<i32>
Converts the value of
self to an i32. If the value cannot be
represented by an i32, then None is returned.Source§fn to_usize(&self) -> Option<usize>
fn to_usize(&self) -> Option<usize>
Converts the value of
self to a usize. If the value cannot be
represented by a usize, then None is returned.Source§fn to_u8(&self) -> Option<u8>
fn to_u8(&self) -> Option<u8>
Converts the value of
self to a u8. If the value cannot be
represented by a u8, then None is returned.Source§impl<T, A, const F: i8> TransparentWrapper<T> for Q<T, A, F>
impl<T, A, const F: i8> TransparentWrapper<T> for Q<T, A, F>
Source§fn wrap_ref(s: &Inner) -> &Self
fn wrap_ref(s: &Inner) -> &Self
Convert a reference to the inner type into a reference to the wrapper
type.
Source§fn wrap_mut(s: &mut Inner) -> &mut Self
fn wrap_mut(s: &mut Inner) -> &mut Self
Convert a mutable reference to the inner type into a mutable reference to
the wrapper type.
Source§fn wrap_slice(s: &[Inner]) -> &[Self]
fn wrap_slice(s: &[Inner]) -> &[Self]
Convert a slice to the inner type into a slice to the wrapper type.
Source§fn wrap_slice_mut(s: &mut [Inner]) -> &mut [Self]
fn wrap_slice_mut(s: &mut [Inner]) -> &mut [Self]
Convert a mutable slice to the inner type into a mutable slice to the
wrapper type.
Source§fn peel_ref(s: &Self) -> &Inner
fn peel_ref(s: &Self) -> &Inner
Convert a reference to the wrapper type into a reference to the inner
type.
Source§fn peel_mut(s: &mut Self) -> &mut Inner
fn peel_mut(s: &mut Self) -> &mut Inner
Convert a mutable reference to the wrapper type into a mutable reference
to the inner type.
Source§fn peel_slice(s: &[Self]) -> &[Inner]
fn peel_slice(s: &[Self]) -> &[Inner]
Convert a slice to the wrapped type into a slice to the inner type.
Auto Trait Implementations§
impl<T, A, const F: i8> Freeze for Q<T, A, F>where
T: Freeze,
impl<T, A, const F: i8> RefUnwindSafe for Q<T, A, F>where
T: RefUnwindSafe,
A: RefUnwindSafe,
impl<T, A, const F: i8> Send for Q<T, A, F>
impl<T, A, const F: i8> Sync for Q<T, A, F>
impl<T, A, const F: i8> Unpin for Q<T, A, F>
impl<T, A, const F: i8> UnsafeUnpin for Q<T, A, F>where
T: UnsafeUnpin,
impl<T, A, const F: i8> UnwindSafe for Q<T, A, F>where
T: UnwindSafe,
A: UnwindSafe,
Blanket Implementations§
impl<T> AnyBitPattern for Twhere
T: Pod,
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
Source§impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
impl<T> CheckedBitPattern for Twhere
T: AnyBitPattern,
Source§type Bits = T
type Bits = T
Self must have the same layout as the specified Bits except for
the possible invalid bit patterns being checked during
is_valid_bit_pattern.Source§fn is_valid_bit_pattern(_bits: &T) -> bool
fn is_valid_bit_pattern(_bits: &T) -> bool
If this function returns true, then it must be valid to reinterpret
bits
as &Self.