FixedPoint¶
- class FixedPoint(value, fractional_bits=28, bit_width=32)[source][source]¶
Bases:
objectFixed-point numeric type mirroring QUA’s
fixedarithmetic.Values are stored as signed integers scaled by
2**fractional_bitswithin a configurable bit width (default: 32 bits, 28 fractional, i.e. range[-8, 8)).Overflow follows QUA: results wrap around modulo
2**bit_widthand aFixedPointOverflowWarningis 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 about1e-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 aFixedPointOverflowWarning. This is whatassign(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 throughassign.- 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).
- classmethod from_int(int_value, fractional_bits=28, bit_width=32)[source][source]¶
Construct a
FixedPointfrom 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
FixedPointinstance.