Main Content

Detect Overflows

This example shows how to detect overflows using theFixed-Point Converterapp. 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.

Prerequisites

This example requires the following products:

Create a New Folder and Copy Relevant Files

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

  2. Change to thedocroot\toolbox\fixpoint\examplesfolder. At the MATLAB command line, enter:

    cd(fullfile(docroot, 'toolbox', 'fixpoint', 'examples'))

  3. Copy theoverflow.mandoverflow_test.mfiles to your local working folder.

    It is a best practice is to create a separate test script to do pre- and post-processing, such as:

    • Loading inputs.

    • Setting up input values.

    • Outputting test results.

    For more information, see创建一个测试文件.

    Type Name Description
    Function code overflow.m Entry-point MATLAB function
    Test file overflow_test.m MATLAB script that testsoverflow.m

The overflow Function

The overflow_test Function

Open theFixed-Point Converter应用程序

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

  2. On the MATLAB Toolstrip应用程序stab, underCode Generation, click the app icon.

Select Source Files

  1. To add the entry-point functionoverflowto the project, browse to the fileoverflow.m, and then clickOpen. By default, the app saves information and settings for this project in the current folder in a file namedoverflow.prj.

  2. ClickNextto go to theDefine Input Typesstep.

    The app screensoverflow.mfor code violations and fixed-point conversion readiness issues. The app does not find issues inoverflow.m.

Define Input Types

  1. On theDefine Input Typespage, to addoverflow_testas a test file, browse tooverflow_test.m, and then clickOpen.

  2. ClickAutodefine Input Types.

    The test file runs. The app determines from the test file that the input type ofbisdouble(1x12),xisdouble(256x1), andresetislogical(1x1).

  3. ClickNextto go to theConvert to Fixed Pointstep.

Convert to Fixed Point

  1. The app generates an instrumented MEX function for your entry-point MATLAB function.The app displays compiled information — type, size, and complexity — for variables in your code. For more information, seeView and Modify Variable Information.

    On theFunction Replacementstab the app displays functions that are not supported for fixed-point conversion. SeeRunning a Simulation.

  2. To view the fimath settings, click theSettingsarrow. Set the fimathProduct modeandSum modetoKeepLSB. These settings model the behavior of integer operations in the C language.

  3. ClickAnalyze.

    The test file,overflow_test, runs. The app displays simulation minimum and maximum ranges on theVariables选项卡。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 theProposed Typecolumn.

  4. To convert the floating-point algorithm to fixed point, clickConvert.

    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 theOutput选项卡。SeeValidating Types.

Test Numerics and Check for Overflows

  1. Click theTestarrow. Verify that the test file isoverflow_test.m. SelectUse scaled doubles to detect overflows, and then clickTest.

    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 theOverflow选项卡。强调溢出的表达式,click the overflow.

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

    In thefimathsettings, setProduct modetoFullPrecision, 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.