Best practice for comparing a float in Matlab

Obviously, floating point comparisons are always difficult. In my (scientific) code, I check statements a lot, so very often I have to check the equality of sums to one and the like.

Is there a quick and easy / best way to do these checks?

The easiest way I can come up with is to create a custom function to compare with a fixed point with a fixed tolerance, but it seems pretty ugly to me. I would prefer an embedded solution, or at least something that is extremely clear and understandable.

+14
floating-point matlab
May 23 '14 at 8:38
source share
5 answers

I think this is likely to be a function that you write yourself. I use three things pretty constantly to do computational vector tests, so to speak:

Maximum absolute error

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

This calculates the maximum absolute point error and tells you that this is less than some tolerance.

Maximum number of errors when an error is exceeded

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

This returns the number of points out of range. It is also easy to change to return the percentage.

Square 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 that will take the result of the calculation, the expected result, the tolerance, and the comparison method. This way you can make your checks very compact, and it will be much less ugly than trying to explain what you are doing in the comments.

+10
May 23 '14 at 8:51
source share

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

 abs(AB)<eps(A)*4 

4 is an absolutely arbitrary number, which in most cases is sufficient.

+9
May 23 '14 at 9:02
source share

I do not know any special solutions. Maybe something using the 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 can do the following, and the result will be as expected:

 >> (0.1+0.1+0.1) - 0.3 < eps ans = 1 
+5
May 23 '14 at 8:49
source share

I had good experience with xUnit , unit test for Matlab. After installing it, you can use:

  • assertVectorsAlmostEqual(a,b) (checks normal proximity between vectors, custom absolute / relative tolerance, and normal default values)
  • assertElementsAlmostEqual(a,b) (the same check, but differently on each individual record - so [1 1e-12] and [1 -1e-9] will be compared with the former, but not with the last).

They are well tested, understandable and readable. The names of the functions are quite long, but with any decent editor (or with Matlab) you can write them as assertV<tab> .

0
May 23 '14 at 12:04
source share

For those who understand both MATLAB and Python (NumPy), it would be helpful to check the code for the following Python functions that do the job:

 numpy.allclose(a, b, rtol=1e-05, atol=1e-08) numpy.isclose(a, b, rtol=1e-05, atol=1e-08, equal_nan=False) 
-2
May 7 '15 at 17:54
source share



All Articles