Skip to content

Q function

pyfxp.Q

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

Create a fixed-point format specification using ARM-style Q-format notation.

The Q function defines a fixed-point representation by specifying the number of integer and fractional bits, whether the format is signed, and how rounding and overflow are handled. It returns an FxpSpec object that can be passed to other functions (e.g., fxp) for actual numeric conversion.

Parameters:

Name Type Description Default
qi int

Number of integer bits.

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 toward 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 toward positive infinity
  • HALF_DOWN (5): Round to nearest; ties toward negative infinity
  • HALF_EVEN (6): Round to nearest; ties to even
  • HALF_ZERO (7): Round to nearest; ties toward zero
  • HALF_AWAY (8): Round to nearest; ties away from zero
TRUNC
ovf int

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

Supported methods:

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

Returns:

Type Description
FxpSpec

A specification object describing the fixed-point format.

Notes

This function uses ARM-style Q-format notation:

  • Qm.n indicates m integer bits and n fractional bits (ARM style).
  • The returned FxpSpec can be reused for consistent conversions across multiple values.

Examples:

>>> spec = Q(3, 1)  # Q3.5 format (3 integer bits, 5 fractional bits)
>>> spec.qi, spec.qf
(3, 1)
>>> fxp(1.25, spec)
1.0