FixedPoint

class FixedPoint(value, fractional_bits=28, bit_width=32)[source][source]

Bases: object

Fixed-point numeric type mirroring QUA’s fixed arithmetic.

Values are stored as signed integers scaled by 2**fractional_bits within a configurable bit width (default: 32 bits, 28 fractional, i.e. range [-8, 8)).

Overflow follows QUA: results wrap around modulo 2**bit_width and a FixedPointOverflowWarning is emitted. Multiplication and right shifts truncate toward negative infinity (arithmetic shift), as the controller does.

Checked bit for bit on hardware: +, -, *, <<, >>, & and | (including overflow). Division is not bit-exact: the controller computes an approximation (relative error up to about 1e-6, i.e. a few tens of LSB in 4.28), whereas __truediv__() returns the exact floored quotient, and a quotient that overflows is not reproduced.

Create a fixed-point value from a float or integer scale.

The value is rounded to the nearest representable fixed-point number, as QUA does for declare(fixed, value=...) (verified on hardware). A value outside the representable range [-8, 8) wraps around and emits a FixedPointOverflowWarning. This is what assign(x, constant) does on the controller; declare(fixed, value=constant) instead makes the program fail to execute, so such a constant can only be mirrored through assign.

Parameters:
  • value – Initial value (float interpreted in fixed-point units).

  • fractional_bits – Number of fractional bits in the representation.

  • bit_width – Total signed bit width including the sign bit.

to_int() → int[source][source]

Return the integer part, floored toward negative infinity (arithmetic shift).

to_unsafe_int() → int[source][source]

Return the raw scaled integer without fractional-bit shifting.

to_float() → float[source][source]

Return the value as a Python float.

classmethod from_int(int_value, fractional_bits=28, bit_width=32)[source][source]

Construct a FixedPoint from an integer fixed-point representation.

Parameters:
  • int_value – Scaled integer value (already in fixed-point units).

  • fractional_bits – Number of fractional bits.

  • bit_width – Total signed bit width.

Returns:

New FixedPoint instance.