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.