Overloaded inbuilt operators causing maximum recursion errors

2 ビュー (過去 30 日間)
JMP Phillips
JMP Phillips 2015 年 12 月 17 日
コメント済み: JMP Phillips 2015 年 12 月 21 日
I am trying to do overloaded inbuilt operators, such as overloading == (eq) for A == B (or eq(A,B)).
I did this: In my working directory I created a new folder 'overloads' , within that directory created a folder @double, and then the function eq.m which is my overloaded function:
function [result] = eq(A,B)
if abs(A-B)<1e-12
result = 1;
else
result = 0;
end
This seems to work fine. The problem is I am getting weird behavior after creating this overloaded function. For example whenever I subtract two numbers in the command line, such as 4-5, or 2 - 3 (any numbers really), I get this error: "maximum recursion limit of 500 reached... etc'. Whenever I try to use any operators, even ones I have not overloaded, I get this weird behavior .
Any ideas?
  6 件のコメント
Adam
Adam 2015 年 12 月 21 日
Your code will also say two numbers with difference smaller than 1e-12 are equal even if those numbers themselves are of that order of magnitude and are percentage-wise very different, but I guess if you never work with small numbers and you are happy to accept any consequences of Matlab's algorithms also using your definition of equality that is 'ok'
JMP Phillips
JMP Phillips 2015 年 12 月 21 日
Walter, if you mean that MATLAB's comparison routine involves some sort of recursion then that may explain it but I don't understand it.
Changing to logicals did not resolve the issue (was working yesterday, not today), so must be something else.
Additionally, when I try to open the function file from within matlab, it also gives a recursion error.
Anyway thanks for your answers much appreciated.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2015 年 12 月 17 日
function [result] = eq(A,B)
result = abs(A-B)<1e-12);
end
Notice this returns logical not double. In your code the double 0 and double 1 you returned would have had to have been compared against 0 by MATLAB in order to try to figure out whether the value you had returned was true or false.
I recommend, by the way, that you specifically comment how your code will deal with NaN and with vectors or arrays and with comparing values of different size()
I also recommend that you consider making the tolerance a relative tolerance based upon eps.
  5 件のコメント
Adam
Adam 2015 年 12 月 17 日
In addition to Guillaume's comment, Matlab uses its operators within many of its own other builtin functions so if you overload them you can easily end up with your overloaded function being called in all sorts of places you don't expect rather than just the ones you explicitly use yourself.
Walter Roberson
Walter Roberson 2015 年 12 月 17 日
編集済み: Walter Roberson 2015 年 12 月 18 日
When you overload an existing class operator you are not making any explicit references, you are messing with ALL use of the operator (except for nested functions in the original implementation, those take local precedence.) Overloading is nearly unscoped.
But the poster did not ask us if this was a good idea, or ask us how to confine the effects to particular routines. The poster has already gone as far as to keep the power supply on while opening the cover of the No User Serviceable Parts section, and I am okay with them finding out experimentally what can go wrong.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by