FixedPoint

Struct FixedPoint 

Source
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 value
  • I - 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,

Source

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 enough
FixedPoint::<i8, -5, 4>::from_bits(0); // invalid negative width
Source

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);
Source

pub const fn intwidth() -> isize

Returns the number of integer bits.

§Examples
assert_eq!(FixedPoint::<u8, 10, -4>::intwidth(), 10);
Source

pub const fn fracwidth() -> isize

Returns the number of fractional bits.

§Examples
assert_eq!(FixedPoint::<u8, 10, -4>::fracwidth(), -4);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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)
Source

pub fn quantize<T>(value: T) -> T
where 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);
Source

pub fn from_f32(value: f32) -> Self
where 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());
Source

pub fn from_f64(value: f64) -> Self
where 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());
Source

pub fn to_f32(self) -> f32
where 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);
Source

pub fn to_f64(self) -> f64
where 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>

Source§

fn clone(&self) -> FixedPoint<P, I, F>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<P, const I: isize, const F: isize> Debug for FixedPoint<P, I, F>
where P: RegInt + AsPrimitive<f64>,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T, P, const I: isize, const F: isize> From<T> for FixedPoint<P, I, F>
where T: Float + 'static, P: RegInt + AsPrimitive<T>,

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);
Source§

fn from(value: T) -> Self

Converts to this type from the input type.
Source§

impl<P: PartialEq, const I: isize, const F: isize> PartialEq for FixedPoint<P, I, F>

Source§

fn eq(&self, other: &FixedPoint<P, I, F>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<P: Copy, const I: isize, const F: isize> Copy for FixedPoint<P, I, F>

Source§

impl<P: Eq, const I: isize, const F: isize> Eq for FixedPoint<P, I, F>

Source§

impl<P, const I: isize, const F: isize> StructuralPartialEq for FixedPoint<P, I, F>

Auto Trait Implementations§

§

impl<P, const I: isize, const F: isize> Freeze for FixedPoint<P, I, F>
where P: Freeze,

§

impl<P, const I: isize, const F: isize> RefUnwindSafe for FixedPoint<P, I, F>
where P: RefUnwindSafe,

§

impl<P, const I: isize, const F: isize> Send for FixedPoint<P, I, F>
where P: Send,

§

impl<P, const I: isize, const F: isize> Sync for FixedPoint<P, I, F>
where P: Sync,

§

impl<P, const I: isize, const F: isize> Unpin for FixedPoint<P, I, F>
where P: Unpin,

§

impl<P, const I: isize, const F: isize> UnwindSafe for FixedPoint<P, I, F>
where P: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.