Use Scaled Doubles to Avoid Precision Loss
This example shows how you can avoid precision loss by overriding the data types in your model with scaled doubles.
To open the
ex_scaled_doublemodel, at the MATLAB® command line, enter:
In this model:
The Constant block Output data type is
The Bitwise Operator block uses the
ANDoperator and the bit mask
0xFFto pass the input value to the output. Because the Treat mask as parameter is set to
Stored Integer, the block outputs the stored integer value, S, of its input. The encoding scheme is V = SQ+B, where V is the real-world value and Q is the stored integer value.
From the Simulink® Apps tab, select Fixed-Point Tool.
In the Fixed-Point Tool, select Collect Ranges > Use current settings. Click Collect Ranges.
The Display block displays
4.125as the output value of the Constant block. The Stored Integer Display block displays
(SI) bin 0100 0010, which is the binary equivalent of the stored integer value. Precision loss occurs because the output data type,
fixdt(1,8,4), cannot represent the output value
Override data types in the model with scaled doubles. In the Fixed-Point Tool, select Collect Ranges > Scaled double-precision. Click Collect Ranges.
The Display block correctly displays
4.1as the output value of the Constant block. The Stored Integer Display block displays
(SI) 65, which is the binary equivalent of the stored integer value. Because the model uses scaled doubles to override the data type
fixdt(1,8,4), the compiled output data type changes to
flts8_En4, which is the scaled doubles equivalent of
No precision loss occurs because the scaled doubles use a double to hold the stored value and retain information about the specified data type and scaling.
You cannot use a data type override setting of Double-precision because the Bitwise Operator block does not support floating-point data types.