pub struct FixedPoint<P, const I: isize, const F: isize> { /* private fields */ }Expand description
A fixed-point number implementation using an underlying primitive integer type.
§Type Parameters
P- The primitive integer type (signed or unsigned) used to store the fixed-point valueI- The number of integer bits (can be negative for sub-integer representations)F- The number of fractional bits (can be negative for super-integer representations)
§Examples
Basic usage with different bit configurations:
// 8-bit unsigned with 4 integer and 4 fractional bits
let fp = FixedPoint::<u8, 4, 4>::from_f64(2.25);
assert_eq!(fp.to_f64(), 2.25);
// 16-bit signed with 8 integer and 4 fractional bits
let fp = FixedPoint::<i16, 8, 4>::from_f64(-1.5);
assert_eq!(fp.to_bits(), -24);The total width is calculated as I + F:
assert_eq!(FixedPoint::<u8, 8, 0>::width(), 8);
assert_eq!(FixedPoint::<i8, 7, -3>::width(), 4);Implementations§
Source§impl<P, const I: isize, const F: isize> FixedPoint<P, I, F>where
P: RegInt,
impl<P, const I: isize, const F: isize> FixedPoint<P, I, F>where
P: RegInt,
Sourcepub fn from_bits(bits: P) -> Self
pub fn from_bits(bits: P) -> Self
Creates a fixed-point number from its raw bit representation.
§Panics
- (At compile time) If the primitive type P is not wide enough for the specified I + F bit width
- (At compile time) If I + F is not positive
- If the provided bits would overflow the fixed-point representation
§Examples
let fp = FixedPoint::<u8, 4, 4>::from_bits(16); // represents 1.0
assert_eq!(fp.to_f64(), 1.0);The following should not compile:
FixedPoint::<u8, 5, 4>::from_bits(0); // u8 not large enoughFixedPoint::<i8, -5, 4>::from_bits(0); // invalid negative widthSourcepub const fn to_bits(self) -> P
pub const fn to_bits(self) -> P
Returns the raw bit representation of the fixed-point number.
§Examples
let fp = FixedPoint::<u16, 8, 2>::from_f64(2.25);
assert_eq!(fp.to_bits(), 9);Sourcepub const fn fracwidth() -> isize
pub const fn fracwidth() -> isize
Returns the number of fractional bits.
§Examples
assert_eq!(FixedPoint::<u8, 10, -4>::fracwidth(), -4);Sourcepub const fn width() -> usize
pub const fn width() -> usize
Returns the total bit width (I + F) of the fixed-point representation.
§Examples
assert_eq!(FixedPoint::<u8, 8, 0>::width(), 8);
assert_eq!(FixedPoint::<i8, 7, -3>::width(), 4);Sourcepub fn is_signed() -> bool
pub fn is_signed() -> bool
Returns true if the fixedpoint representation (underlying primitive type) is signed.
§Examples
assert_eq!(FixedPoint::<u16, 8, 2>::is_signed(), false);
assert_eq!(FixedPoint::<i16, 8, 2>::is_signed(), true);Sourcepub fn zero() -> Self
pub fn zero() -> Self
Returns a fixed-point representation of zero.
§Examples
let zero = FixedPoint::<u8, 4, 4>::zero();
assert_eq!(zero.to_f64(), 0.0);Sourcepub fn max_value() -> Self
pub fn max_value() -> Self
Returns the maximum representable value for this fixed-point type.
§Examples
assert_eq!(FixedPoint::<u8, 2, 6>::max_value().to_f32(), 3.984375);
assert_eq!(FixedPoint::<i8, 3, 4>::max_value().to_f32(), 3.9375);Sourcepub fn min_value() -> Self
pub fn min_value() -> Self
Returns the minimum representable value for this fixed-point type.
§Examples
assert_eq!(FixedPoint::<u8, 2, 6>::min_value().to_f32(), 0.0);
assert_eq!(FixedPoint::<i8, 3, 4>::min_value().to_f32(), -4.0);Sourcepub fn resolution() -> Self
pub fn resolution() -> Self
Returns the smallest representable positive value (the resolution).
§Examples
let res = FixedPoint::<u8, 4, 4>::resolution();
assert_eq!(res.to_f64(), 0.0625); // 2^(-4)Sourcepub fn quantize<T>(value: T) -> Twhere
T: Float + 'static,
P: AsPrimitive<T>,
pub fn quantize<T>(value: T) -> Twhere
T: Float + 'static,
P: AsPrimitive<T>,
Quantizes a floating-point value to the resolution of this fixed-point type and returns it as a floating-point value.
This is equivalent to converting to fixed-point and back to floating-point.
§Examples
// 2.3 gets quantized to the nearest representable value
let quantized = FixedPoint::<u8, 4, 4>::quantize(2.3);
assert_eq!(quantized, 2.3125);Sourcepub fn from_f32(value: f32) -> Selfwhere
P: AsPrimitive<f32>,
pub fn from_f32(value: f32) -> Selfwhere
P: AsPrimitive<f32>,
Creates a fixed-point number from a 32-bit floating-point value.
Values are rounded to the nearest representable fixed-point value. Ties are rounded away from 0. Out-of-range values are saturated to the min/max representable values.
§Panics
Panics if the input is NaN.
§Examples
let fp = FixedPoint::<u8, 4, 4>::from_f32(1.5);
assert_eq!(fp.to_bits(), 24);
// saturation behavior
let min_fp = FixedPoint::<i8, 4, 4>::from_f64(-100.0);
assert_eq!(min_fp, FixedPoint::<i8, 4, 4>::min_value());Sourcepub fn from_f64(value: f64) -> Selfwhere
P: AsPrimitive<f64>,
pub fn from_f64(value: f64) -> Selfwhere
P: AsPrimitive<f64>,
Creates a fixed-point number from a 64-bit floating-point value.
Values are rounded to the nearest representable fixed-point value. Ties are rounded away from 0. Out-of-range values are saturated to the min/max representable values.
§Panics
Panics if the input is NaN.
§Examples
let fp = FixedPoint::<u16, 8, 2>::from_f64(2.25);
assert_eq!(fp.to_bits(), 9);
// Saturation behavior
let max_fp = FixedPoint::<u8, 4, 4>::from_f64(100.0);
assert_eq!(max_fp, FixedPoint::<u8, 4, 4>::max_value());Sourcepub fn to_f32(self) -> f32where
P: AsPrimitive<f32>,
pub fn to_f32(self) -> f32where
P: AsPrimitive<f32>,
Converts the fixed-point number to a 32-bit floating-point value.
§Examples
assert_eq!(FixedPoint::<u16, 8, 2>::from_bits(8).to_f32(), 2.0);
assert_eq!(FixedPoint::<u16, 8, 2>::from_bits(9).to_f32(), 2.25);
assert_eq!(FixedPoint::<i16, 8, 4>::from_bits(-24).to_f32(), -1.5);
assert_eq!(FixedPoint::<u8, 4, 4>::from_bits(1).to_f32(), 0.0625);
assert_eq!(FixedPoint::<i8, 4, 4>::from_bits(-1).to_f32(), -0.0625);
assert_eq!(FixedPoint::<u8, 4, 4>::from_bits(0).to_f32(), 0.0);Sourcepub fn to_f64(self) -> f64where
P: AsPrimitive<f64>,
pub fn to_f64(self) -> f64where
P: AsPrimitive<f64>,
Converts the fixed-point number to a 64-bit floating-point value.
§Examples
assert_eq!(FixedPoint::<u16, 8, 2>::from_bits(8).to_f64(), 2.0);
assert_eq!(FixedPoint::<u16, 8, 2>::from_bits(9).to_f64(), 2.25);
assert_eq!(FixedPoint::<i16, 8, 4>::from_bits(-24).to_f64(), -1.5);
assert_eq!(FixedPoint::<u8, 4, 4>::from_bits(1).to_f64(), 0.0625);
assert_eq!(FixedPoint::<i8, 4, 4>::from_bits(-1).to_f64(), -0.0625);
assert_eq!(FixedPoint::<u8, 4, 4>::from_bits(0).to_f64(), 0.0);Trait Implementations§
Source§impl<P: Clone, const I: isize, const F: isize> Clone for FixedPoint<P, I, F>
impl<P: Clone, const I: isize, const F: isize> Clone for FixedPoint<P, I, F>
Source§fn clone(&self) -> FixedPoint<P, I, F>
fn clone(&self) -> FixedPoint<P, I, F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T, P, const I: isize, const F: isize> From<T> for FixedPoint<P, I, F>
Automatic conversion from floating-point types to fixed-point.
impl<T, P, const I: isize, const F: isize> From<T> for FixedPoint<P, I, F>
Automatic conversion from floating-point types to fixed-point.
This provides convenient syntax for creating fixed-point numbers from floats. Note that this is a lossy conversion that will never fail (unless NaN). Saturation and rounding are applied.
§Panics
Panics if the value is NaN.
§Examples
let fp: FixedPoint<u8, 4, 4> = 2.5.into();
assert_eq!(fp.to_f64(), 2.5);