From: Andy on
"John D'Errico" <woodchips(a)rochester.rr.com> wrote in message <i27go8$q2s$1(a)fred.mathworks.com>...
> "Brandon Moore" <moorebj.nospamplease(a)umich.edu> wrote in message <i27ebu$mkb$1(a)fred.mathworks.com>...
> > Wondering if there is some easy way to adjust the tolerances on the relational operators (==, <, etc.) in MATLAB so I don't have things like sum([0.4 0.3 0.2 0.1])==1 come back false. I.e., I don't want to have to include a +tolerance or -tolerance term every time I use a relational operator in order for it to not have these sort of errors (or rather, to error more on the side of logical true instead of logical false).
>
> No, there is no easy way to do this automatically,
> beyond overloading the ops themselves. And that
> might be a VERY dangerous thing to do.
>
> Note that all of matlab would then be using your
> overloaded relops, possibly causing incredibly
> difficult to find bugs in the base matlab codes.
> In some cases, they may actually be relying on
> exact behaviors for those operators. An author
> who is careful may choose to take advantage of
> such things.
>
> At the very least, this will cause all of matlab to
> now run more slowly. So I would strongly
> recommend NOT going down this overloading
> path. It will come back to bite you in the butt
> one day.
>
> You might choose to implement your own
> distinct tests, passing in an explicit tolerance,
> and use these new functions where you wish
> to employ a test with a tolerance. This will be
> safe for all concerned. And if there is a bug, it
> will be easier for you to track down.
>
> John

I don't think it would be dangerous to overload eq with a new function call syntax, e.g. eq(A,B,'tol',tolerance). Without the additional arguments, other calls to eq would function as normal.

That said, if you don't want to worry, you can just create your own wrapper function:

function tf = isClose(A,B,tolerance)
tf = (A-B) <= tolerance;
end

This would at least save some typing in the long run, while making your code easy to read (not risking any confusion that might occur with an overloaded eq).
From: Walter Roberson on
John D'Errico wrote:

> No, there is no easy way to do this automatically,
> beyond overloading the ops themselves. And that
> might be a VERY dangerous thing to do.
>
> Note that all of matlab would then be using your
> overloaded relops, possibly causing incredibly
> difficult to find bugs in the base matlab codes.
> In some cases, they may actually be relying on
> exact behaviors for those operators. An author
> who is careful may choose to take advantage of
> such things.

As an example of what John describes:

histc() reserves the final output bin to count values that are exact equal to
the last provided edge. If comparisons suddenly became slightly fuzzy in the
manner described ("erring on the side of logical true"), then

x < x

and

x == x

would both be true and it would then be ambiguous as to whether the values
exactly matching the last edge should be counted towards the second-last bin
or toward the final bin or both.
From: Walter Roberson on
Andy wrote:

> That said, if you don't want to worry, you can just create your own
> wrapper function:
>
> function tf = isClose(A,B,tolerance)
> tf = (A-B) <= tolerance;
> end

You probably meant to type

tf = abs(A-B) <= tolerance;