pub struct Fixed(pub i32);Expand description
A Q16.16 fixed-point number: 16 integer bits, 16 fractional bits.
Tuple Fields§
§0: i32Implementations§
Source§impl Fixed
impl Fixed
Sourcepub const fn from_int(v: i32) -> Fixed
pub const fn from_int(v: i32) -> Fixed
From an integer, saturating.
The range of Q16.16 in i32 is [-32768, 32767] integers, so v << 16
silently wraps for |v| >= 32768: from_int(32768) used to give
-32768. A shift is a total function, which is exactly why the wrap is
invisible. Saturating keeps this module’s promise that no primitive
overflows without saying so.
Sourcepub const fn to_int(self) -> i32
pub const fn to_int(self) -> i32
The integer part, truncated toward zero.
Truncation toward zero is not self.0 >> 16 for negative values: an
arithmetic shift is a floor, so -0.5 >> 16 gives -1, not 0. That
one-bit bias on every negative fractional input is the kind of error a
control law cannot see. The magnitude is truncated and the sign restored,
which is as i32 behaviour.
Sourcepub const fn saturating_add(self, other: Fixed) -> Fixed
pub const fn saturating_add(self, other: Fixed) -> Fixed
Saturating addition.
Sourcepub const fn saturating_sub(self, other: Fixed) -> Fixed
pub const fn saturating_sub(self, other: Fixed) -> Fixed
Saturating subtraction.
Sourcepub const fn saturating_mul_int(self, k: i32) -> Fixed
pub const fn saturating_mul_int(self, k: i32) -> Fixed
Multiply by an integer, saturating.
Sourcepub const fn checked_div_int(self, k: i32) -> Option<Fixed>
pub const fn checked_div_int(self, k: i32) -> Option<Fixed>
Truncating division by an integer. None when k == 0, and also when
the quotient is not representable – the single overflowing case,
i32::MIN / -1, whose true result 2^31 is one past i32::MAX.
checked_div reports both, so this returns None for either rather than
wrapping to i32::MIN.