This example shows how to detect overflows using the MATLAB® Coder™ app. At the numerical testing stage in the conversion process, you choose to simulate the fixed-point code using scaled doubles. The app then reports which expressions in the generated code produce values that overflow the fixed-point data type.
This example requires the following products:
Create a local working folder, for example,
Change to the
docroot\toolbox\fixpoint\examples folder. At the MATLAB command line,
cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples'))
to your local working folder.
|Function code||Entry-point MATLAB function|
|Test file||MATLAB script that tests |
Navigate to the work folder that contains the file for this example.
On the MATLAB Toolstrip Apps tab, under Code Generation, click the app icon.
To add the entry-point function
the project, browse to the file
then click Open. By default, the app saves
information and settings for this project in the current folder in
a file named
Set Numeric Conversion to
to fixed point.
Click Next to go to the Define Input Types step.
The app screens
overflow.m for code violations
and code generation readiness issues. The app does not find issues
On the Define Input Types page,
overflow_test as a test file, browse to
and then click Open.
Click Autodefine Input Types.
The test file runs. The app determines from the test file that the input type of
Click Next to go to the Check for Run-Time Issues step.
The Check for Run-Time Issues step generates
instrumented MEX. It runs the test file
overflow with calls to the generated
MEX function. If the app finds issues, it provides warning and error
messages. You can click a message to highlight the problematic code
in a pane where you can edit the code.
On the Check for Run-Time Issues page,
the app populates the test file field with
the test file that you used to define the input types.
Click Check for Issues.
The app does not detect issues.
Click Next to go to the Convert to Fixed Point step.
The app displays compiled information — type, size, and complexity — for variables in your code. For more information, see View and Modify Variable Information.
On the Function Replacements tab the app displays functions that are not supported for fixed-point conversion. See Running a Simulation.
To view the fimath settings, click the Settings arrow
. Set the fimath Product
mode and Sum mode to
KeepLSB. These settings model the behavior of
integer operations in the C language.
The test file,
overflow_test, runs. The app
displays simulation minimum and maximum ranges on the Variables tab.
Using the simulation range data, the software proposes fixed-point
types for each variable based on the default type proposal settings,
and displays them in the Proposed Type column.
To convert the floating-point algorithm to fixed point, click Convert.
The software validates the proposed types and generates a fixed-point version of the entry-point function.
If errors and warnings occur during validation, the app displays them on the Output tab. See Validating Types.
Click the Test arrow . Verify that the test file is
overflow_test.m. Select Use scaled doubles
to detect overflows, and then click
The app runs the test file that you used to define input types to test the fixed-point MATLAB code. Because you selected to detect overflows, it also runs the simulation using scaled double versions of the proposed fixed-point types. Scaled doubles store their data in double-precision floating-point, so they carry out arithmetic in full range. Because they retain their fixed-point settings, they can report when a computation goes out of the range of the fixed-point type.
The simulation runs. The app detects an overflow. The app reports the overflow on the Overflow tab. To highlight the expression that overflowed, click the overflow.
Determine whether it was the sum or the multiplication that overflowed.
In the fimath settings, set Product
FullPrecision, and then
repeat the conversion and test the fixed-point code again.
The overflow still occurs, indicating that it is the addition in the expression that is overflowing.