Overloaded inbuilt operators causing maximum recursion errors
古いコメントを表示
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 件のコメント
JMP Phillips
2015 年 12 月 18 日
Walter Roberson
2015 年 12 月 18 日
What happened when you fixed your routine to return logicals?
Guillaume
2015 年 12 月 18 日
@JMP, which version of matlab? I cannot reproduce in 2015b.
"It seems no one knows why recursion errors come up when overloading built-in functions" It's more that no one really care as it's not something you should do for built-in types.
Walter Roberson
2015 年 12 月 21 日
So when someone said "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" was that something other than an explanation for why the recursion errors were occurring?
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
2015 年 12 月 21 日
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!