What are the best practices for floating-point comparisons in Matlab?

Solution 1:

I think it's most likely going to have to be a function you write yourself. I use three things pretty constantly for running computational vector tests so to speak:

Maximum absolute error

return max(abs(result(:) - expected(:))) < tolerance

This calculates maximum absolute error point-wise and tells you whether that's less than some tolerance.

Maximum excessive error count

return sum( (abs(result(:) - expected(:))) < tolerance )

This returns the number of points that fall outside your tolerance range. It's also easy to modify to return percentage.

Root mean squared error

return norm(result(:) - expected(:)) < rmsTolerance

Since these and many other criteria exist for comparing arrays of floats, I would suggest writing a function which would accept the calculation result, the expected result, the tolerance and the comparison method. This way you can make your checks very compact, and it's going to be much less ugly than trying to explain what it is that you're doing in comments.

Solution 2:

Any fixed tolerance will fail if you put in very large or very small numbers, simplest solution is to use eps to get the double precision:

abs(A-B)<eps(A)*4

The 4 is a totally arbitrary number, which is sufficient in most cases.

Solution 3:

Don't know any special build in solution. Maybe something with using eps function?

For example as you probably know this will give False (i.e. 0) as a result:

>> 0.1 + 0.1 + 0.1 == 0.3

ans =

     0

But with eps you could do the following and the result is as expected:

>> (0.1+0.1+0.1) - 0.3  < eps     

ans =

     1