Using Simulink | ![]() ![]() |
Typecasting Parameters
In general, during simulation, Simulink silently converts parameter data types to signal data types (if they differ) when computing block outputs that are a function of an input signal and a parameter. The following exceptions occur to this rule:
This model uses a Gain block to amplify a constant input signal. Computing the output of the Gain block requires computing the product of the input signal and the gain. Such a computation requires that the two values be of the same data type. However, in this case, the data type of the signal, uint8
(unsigned 8-bit word), differs from the data type of the gain parameter, int32
(signed 32-bit integer). Thus computing the output of the Gain block entails a type conversion.
When making such conversions, Simulink always casts the parameter type to the signal type. Thus, in this case, Simulink must convert the Gain block's gain value to the data type of the input signal. Simulink can make this conversion only if the input signal's data type (uint8
) can represent the gain. In this case, Simulink can make the conversion because the gain is 255, which is within the range of the uint8
data type (0 to 255). Thus, this model simulates without error. However, if the gain were slightly larger (for example, 256), Simulink would signal an out-of-range error if you attempted to simulate the model.
In this example, the signal type accommodates only integer values, while the gain value has a fractional component. Simulating this model causes Simulink to truncate the gain to the nearest integral value (2) and issue a loss-of-precision warning. On the other hand, if the gain were 2.0, Simulink would simulate the model without complaint because in this case the conversion entails no loss of precision.
![]() | Enabling Strict Boolean Type Checking | Working with Data Objects | ![]() |