Main Content

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.

  1. To open the ex_scaled_double model, at the MATLAB® command line, enter:

    addpath(fullfile(docroot,'toolbox','fixpoint','examples'))
    ex_scaled_double

    In this model:

    • The Constant block Output data type is fixdt(1,8,4).

    • The Bitwise Operator block uses the AND operator and the bit mask 0xFF to 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.

  2. From the Simulink® Apps tab, select Fixed-Point Tool.

  3. In the Fixed-Point Tool, select Collect Ranges > Use current settings. Click Collect Ranges.

    The Display block displays 4.125 as 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 4.1 exactly.

  4. 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.1 as 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 fixdt(1,8,4).

    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.

    Note

    You cannot use a data type override setting of Double-precision because the Bitwise Operator block does not support floating-point data types.

Related Topics