Detect Overflows

This example shows how to detect overflows using the Fixed-Point Converter 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 would overflow the fixed-point data type.


To complete this example, you must install the following products:


  • Fixed-Point Designer™

  • C compiler (for most platforms, a default C compiler is supplied with MATLAB) For a list of supported compilers, see Supported Compilers

    You can use mex -setup to change the default compiler. See Changing Default Compiler.

Create a New Folder and Copy Relevant Files

  1. Create a local working folder, for example, c:\overflow.

  2. Change to the docroot\toolbox\fixpoint\examples folder. At the MATLAB command line, enter:

    cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples')) 
  3. Copy the overflow.m and overflow_test.m files to your local working folder.

    It is best practice to create a separate test script to do all the pre- and post-processing such as loading inputs, setting up input values, calling the function under test, and outputting test results. For more information, see Create a Test File.

    Function codeoverflow.mEntry-point MATLAB function
    Test fileoverflow_test.mMATLAB script that tests overflow.m

 The overflow Function

 The overflow_test Function

Open the Fixed-Point Converter App

  1. Navigate to the work folder that contains the file for this example.

  2. On the MATLAB Apps tab, in the Code Generation group, select Fixed-Point Converter.

    Alternatively, at the MATLAB command line, enter


Select Source Files

Click Select Source Files. Browse to the file overflow.m and then click Open to add the file to the project and check the file for potential code generation issues.

By default, the app saves information and settings for this project in the current folder in a file named overflow.prj. It detects no potential issues with the overflow.m file.

Define Input Types

  1. Click the arrow on the right side of the app to navigate to the next step in the workflow.

  2. Browse to add overflow_test as a test file and then click .

    The test file runs and displays the outputs of the filter for each of the input signals.

    The app determines from the test file that the input type of b is double(1x12), x is double(256x1), and reset is logical(1x1).

Convert to Fixed Point

  1. Click the arrow on the right side of the app to navigate to the next step in the workflow.

    The Fixed-Point Converter app generates an instrumented MEX function for your entry-point MATLAB function. After generating the MEX function, the app displays compiled information — type, size, and complexity — for variables in your code. For more information, see View and Modify Variable Information.

    If the MEX function generation fails, the app provides error message links to help you navigate to the code that caused the build issues. If your code contains functions that are not supported for fixed-point conversion, the app displays these on the Function Replacements tab. For more information, see Running a Simulation.

  2. In the Fixed-Point Converter app, click to view the advanced settings. Set the fimath Product mode and Sum mode to KeepLSB. These settings model the behavior of integer operations in the C language.

  3. Click Run and Detect Ranges.

    The test file, overflow_test, runs and 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.

  4. 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 the errors or warnings occur during validation, they are displayed on the Type Validation Output tab. For more information, see Validating Types.

Test Numerics and Check for Overflows

  1. Click the arrow to the right of Test, select Use scaled doubles to detect overflows, and then click Test.

    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. They also retain their fixed-point settings, so they are able to report when a computation goes out of the range of the fixed-point type.

    The simulation runs and the app detects an overflow. The app reports on the Overflow tab and underlines the expression that overflowed in the code window.

  2. Determine whether it was the sum or the multiplication that overflowed.

    In the Advanced settings, set Product mode to 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.

Was this topic helpful?