Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recognize overflow bugs in Matlab?

Tags:

matlab

I spent part of yesterday and today tracking down a bug in some Matlab code. I had thought my problem was indexing (with many structures that I didn't define and am still getting used to), but it turned out to be an overflow bug. I missed this for a very specific reason:

>> uint8(2) - uint8(1)

ans =

    1

>> uint8(2) - uint8(2)

ans =

    0

>> uint8(2) - uint8(3)

ans =

    0

I would have expected the last one to be something like -1 (or 255). In the middle of a big vector, the erroneous 0s were difficult to detect, but a 255 would have stood out easily.

Any tips on how to detect these problems easily in the future? (Ideally, I'd like to turn off the overflow checking to make it work like C.) Changing to double works, of course, but if I don't realize it's a uint8 to begin with, that doesn't help.

like image 340
Benjamin Oakes Avatar asked Mar 02 '10 19:03

Benjamin Oakes


People also ask

How do you stop overflow in Matlab?

To disable integer overflow support: In a code generation configuration object for MEX or standalone code (static library, dynamically linked library, or executable program), set the SaturateOnIntegerOverflow property to false . In the MATLAB Coder™ app, set Saturate on integer overflow to No .

How do computers detect overflow?

Most computers have two dedicated processor flags to check for overflow conditions. The carry flag is set when the result of an addition or subtraction, considering the operands and result as unsigned numbers, does not fit in the given number of bits.

What can we do to avoid overflow errors if we need to deal with large numbers?

look into bignum arithmetic libraries. they'll be a bit slower than standard C arithmetic but at least you won't get overflow. also many standard math functions (exp etc) will set errno to ERANGE in case of overflow. Usually, it is prevented by thinking carefully when writing code.


2 Answers

You can start by turning on integer warnings:

intwarning('on')

This will give you a warning when integer arithmetic overflows.

Beware though, as outlined here, this does slow down integer arithmetic so only use this during debug.

like image 60
neuroguy123 Avatar answered Oct 15 '22 06:10

neuroguy123


Starting with release R2010b and later, the function INTWARNING has been removed, along with these warning messages for integer math and conversion:

  • MATLAB:intConvertNaN
  • MATLAB:intConvertNonIntVal
  • MATLAB:intConvertOverflow
  • MATLAB:intMathOverflow

So using INTWARNING is no longer a viable option for determining when integer overflows occur. An alternative is to use the CLASS function to test the class of your data and recast it accordingly before performing the operation. Here's an example:

if strcmp(class(data),'uint8')  %# Check if data is a uint8
  data = double(data);          %# Convert data to a double
end

You could also use the ISA function as well:

if ~isa(data,'single')  %# Check if data is not a single
  data = single(data);  %# Convert data to a single
end
like image 41
gnovice Avatar answered Oct 15 '22 07:10

gnovice