qformatpy.qformat

qformatpy.qformat(x, qi: int, qf: int, signed: bool = True, rnd_method='Trunc', overflow_action='Wrap')[source]

Format a given numeric value ‘x’ into fixed-point representation with Q format.

The Q format is specified using ARM’s notation, where QI includes the sign bit.

Parameters:
xfloat or numpy.ndarray

Input numeric value or array to be formatted.

qiint

Number of integer bits in the Q format.

qfint

Number of fractional bits in the Q format.

signedbool, optional

Indicates whether the number is signed (True) or unsigned (False). Default is True.

rnd_methodstr, optional
Rounding method to be applied. Supported methods:
  • ‘Trunc’: Round towards -inf.

  • ‘Ceiling’: Round towards +inf.

  • ‘TowardsZero’: Round towards zero.

  • ‘AwayFromZero’: Round away from zero.

  • ‘HalfUp’: Round half up.

  • ‘HalfDown’: Round half down.

  • ‘HalfTowardsZero’: Round half towards zero.

  • ‘HalfAwayFromZero’: Round half away from zero.

Default is ‘Trunc’.

overflow_actionstr, optional
Action to be taken in case of overflow. Supported actions:
  • ‘Error’: Raise an OverflowError if overflow occurs.

  • ‘Wrap’: Wraparound overflow, values wrap around the representable range.

  • ‘Saturate’: Saturate overflow, values are clamped to the maximum or minimum representable value.

Default is ‘Wrap’.

Returns:
xfloat or numpy.ndarray

The formatted value(s) after applying rounding and overflow handling.