主要内容

Resolve Unexpected Values from Data Type Conversion

When you represent numbers with finite precision, not every number can be represented exactly. Quantization must occur to use fixed-point data types in Simulink® which can sometimes yield unexpected values. You can use strategies to improve the numerical accuracy of fixed-point calculations. For a more detailed explanation of how quantization occurs, see Understand Unexpected Results from Data Type Conversion.

Issue

Even if you use data types that can accurately represent values in the simulation range, your model might produce an unexpected result due to quantization. For example, this model shows the value 0.5 converted from sfix16_Sp1 to sfix16_En7. The new data type should be able to represent the value 0.5, but the value is quantized to 0.4921875.

A Simulink model converts a constant with the value 0.5 from one fixed-point data type to another fixed-point data type using a data type conversion block. A display block shows the value of the constant after data type conversion.

In some cases, you might expect that the fixed-point data types used in your model cannot represent all the values in the range exactly, but the result you see is still further from the ideal value than you expect. For example, this model shows the value 9.81 converted from sfix16_Sp00125 to sfix32_En15.

A Simulink model converts a constant with the value 9.81 from one fixed-point data type to another fixed-point data type using a data type conversion block. A display block shows the value of the constant after data type conversion.

This number line shows stored integers and corresponding values that can be used to approximate 9.81 using the sfix32_En15 data type. The value shown in the shaded area of the number line is the real-world value calculated in this data type conversion operation, which is several stored integers away from the most accurate available value.

A number line shows the ideal value 9.81 among several values that could be used to represent the ideal with the fixed-point data type. The value calculated from data type conversion is shown in red. There are six values between the ideal value and the calculated value.

Possible Solutions

Adjust Rounding Method

Changing the Rounding Method value of the Data Type Conversion block to Nearest, Round, or Convergent can produce more accurate results. In the case of the first example model, using Nearest rounding produces the expected value 0.5.

A Simulink model converts a constant with the value 0.5 from one fixed-point data type to another fixed-point data type using a data type conversion block. A display block shows the value of the constant after data type conversion.

Adjusting the rounding method does not work in every case, though. In the second example, no change occurs from using Nearest rounding.

A Simulink model converts a constant with the value 9.81 from one fixed-point data type to another fixed-point data type using a data type conversion block. A display block shows the value of the constant after data type conversion.

Additionally, if you are generating code, enforcing a rounding method rather than leaving it set to Simplest can be less efficient, as it results in additional generated code.

Use Division for Net Slope Computation

The model parameter Use division for fixed-point net slope computation can improve accuracy because it uses a rational approximation for the net scaling. The resulting integer division, multiplication, and addition avoid rounding, which can reduce quantization error. Setting this parameter to 'On'improves the accuracy for both example models.

In the first example, the result of data type conversion is now the expected value 0.5

A Simulink model converts a constant with the value 0.5 from one fixed-point data type to another fixed-point data type using a data type conversion block. A display block shows the value of the constant after data type conversion.

In the second example model, enabling the parameter now produces the expected value when converting between fixed-point types.

A Simulink model converts a constant with the value 9.81 from one fixed-point data type to another fixed-point data type using a data type conversion block. A display block shows the value of the constant after data type conversion.

The resulting real-world value and stored integer are shown in the shaded area on the number line. The data type conversion now uses a stored integer that is as close as possible to the ideal value for the data type sfix32_En15.

A number line shows the ideal value 9.81 among several values that could be used to represent the ideal with the fixed-point data type. The value calculated from data type conversion is shown in green. There calculated value is the closest possible value to the ideal value.

The example models in this topic explore issues related specifically to Data Type Conversion blocks. However, similar principles apply when data types change between other Simulink blocks, such as Gain and Product. You should start troubleshooting unexpected values from fixed-point data types by adjusting rounding methods and exploring settings that can reduce rounding errors.

See Also

|