Skip to content

fpxt

pyfxp.fxpt

fxpt(x, qi, qf, signed=True, rnd=TRUNC, ovf=WRAP)

Convert a numeric value to fixed-point representation using Q-format notation.

Parameters:

Name Type Description Default
x (int, float or array - like)

The input value(s) to convert.

required
qi int

Number of integer bits (including sign bit if signed=True).

required
qf int

Number of fractional bits.

required
signed bool

Whether the fixed-point format is signed (default is True).

True
rnd int

Rounding method to apply (default is TRUNC (0)).

Supported methods:

  • TRUNC (0): Bit Truncation. Rounds towards negative infinity.
  • CEIL (1): Round toward positive infinity.
  • TO_ZERO (2): Round toward zero.
  • AWAY (3): Round away from zero.
  • HALF_UP (4): Round to nearest; ties round towards positive infinity.
  • HALF_DOWN (5): Round to nearest; ties round toward negative infinity.
  • HALF_EVEN (6): Round to nearest; ties round to even.
  • HALF_ZERO (7): Round to nearest; ties round toward zero.
  • HALF_AWAY (8): Round to nearest; ties round away from zero.
TRUNC
ovf int

Overflow handling method (default is WRAP (0)).

Supported methods:

  • WRAP (0): Wrap around on overflow.
  • SAT (1): Saturate to maximum/minimum representable value.
  • ERROR (2): Raise an error if overflow occurs.
WRAP
Returns:

float or ndarray Fixed-point representation of the input, as integer(s).

Notes:

Uses ARM-style Q-format notation where a Qm.n format has: - m integer bits (qi) (including sign bit if signed=True) - n fractional bits (qf)

Examples:

>>> from pyfxp import fxpt
>>> from pyfxp.constants import TRUNC, WRAP
>>> import numpy as np
>>> np.pi
>>> 3.14159265358979
>>> fxpt(np.pi, 3, 4, signed=True, rnd=TRUNC, ovf=WRAP)  # Q3.4 format (3 integer bits, 4 fractional bits)
3.125