How does the Compare To Constant block in Simulink choose type of the Constant value, and what are the implications for fixed-point operations?

21 views (last 30 days)
How can I detect and address questionable fixed-point operations in Compare To Constant block?
This example model generates code that does not function as intended. It appears that value 15 is cast to fixdt(1,8,-5) as sfix8_E5, and quantized to zero.

Accepted Answer

MathWorks Fixed Point Team
MathWorks Fixed Point Team on 28 Aug 2025 at 15:27
Edited: MathWorks Fixed Point Team on 28 Aug 2025 at 15:28
The Compare To Constant block in Simulink casts the Constant value parameter to the input signal's data type before performing the comparison. This ensures that the comparison is performed between values of the same data type, which is important in fixed-point designs to avoid unexpected behavior due to type mismatches.
Implications for Fixed-Point Operations:
  • Underflow: If the constant value cannot be represented in the input's data type (e.g., a negative constant with an unsigned input type, or a fractional constant with an integer input type), the value is quantized (rounded or saturated) to the nearest representable value. This process is called parameter quantization. The Parameter Quantization Advisor can help you detect and understand these cases, and will generate a warning in the diagnostic window if quantization occurs.
  • Overflow: If the constant value exceeds the representable range of the input data type, it is saturated to the maximum or minimum value of the data type. This can lead to "questionable fixed-point operations," as the comparison may not reflect the original intent. The Model Advisor has specific checks to identify Compare To Constant blocks where this occurs (see the "Identify Compare to Constant blocks for questionable fixed-point operations" section).
In the model shown here, the input constant is quantized to zero. The constant value 15 is also quantized to zero. Hence the output of True in the setup.
fi(1,1,8,-5)
fi(15,1,8,-5)
ans =
0
DataTypeMode: Fixed-point: binary point scaling
Signedness: Signed
WordLength: 8
FractionLength: -5
Hope this clarified the behaviors.

More Answers (0)

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!