Doug’s MATLAB Video Tutorials

December 1st, 2008

Video tutorial: Tolerances in comparisons

Sometimes you will do a calculation where the answer looks like ‘0′ or ‘1′ or some other number like that. The natural thing to do is to do a test like this:

if (answer == 1)
     Do something
end
However, if the calculation was done in double precision, answer might actually be 1.000000001 or something like that. This video discusses a strategy for overcoming this:

if abs(answer - 1) < 1e-14
     Do something
end

MATLAB Central Files Icon

4 Responses to “Video tutorial: Tolerances in comparisons”

  1. paramesh replied on :

    Hi Doug,

    Nice point. But instead, won’t the function “round” work? Say, “if round(answer) == 1″.

  2. matt fig replied on :

    paramesh,

    Your solution will work if the integer is known, as it is in this case (1). However Doug’s solution is more general in that it can be applied to determine if an integer is present when the integer may be unknown. As in:

    abs(round(N) - N)<tol

    Where N can be an array of possible integers which resulted from a previous calculation. This will be true when integers are found in N and false where not.

  3. Seth Popinchalk replied on :

    @paramesh - I don’t like the ROUND solution at all. That would return true for 1.4 and .6! As matt fig points out, you need to set a tolerance bound on your comparison.

  4. Leonardo Glavina replied on :

    That’s true Seth, but you can use a type of trick to make it more precise, like [i]round(100*w)/100[/i] or even [i]round(1000*w)/1000[/i].

    Anyway, great solution Doug… I was having a little bit of problem with that, when I was making comparisons to find the intersection points of plots, and this method can be better than mine.

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.