Overloaded inbuilt operators causing maximum recursion errors
Mostrar comentarios más antiguos
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 comentarios
JMP Phillips
el 18 de Dic. de 2015
Walter Roberson
el 18 de Dic. de 2015
What happened when you fixed your routine to return logicals?
Guillaume
el 18 de Dic. de 2015
@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
el 21 de Dic. de 2015
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
el 21 de Dic. de 2015
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
el 21 de Dic. de 2015
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Logical en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!