- Function shadowing: The function "pi" might be defined somewhere else on the MATLAB Path. This might result in conflicting definitions.
- Third party tool Interference: There might be a chance that the third party tool is causing the issue. There could be another definition of "pi" in the toolbox which is shadowing the function. To debug this, you can uninstall the tool and try whether the overloaed function is called or not.
Why does Matlab not respect Function Precedence Order when I try to overload a builtin with no arguments (pi)?
1 次查看(过去 30 天)
显示 更早的评论
I am using the multiprecision computing toolbox "advanpix". To make full use of the extra precision, I need to replace calls to pi with calls to mp('pi') (or similar). This would be a daunting task to do accross all the files in the repositories I am using, and it would cause issues to other people who use the same code. Even if I use their suggestion for precision independent code, this would require adding an extra function call, complete with global variables, which would likely cause problems for other users.
The simplest solution would be to overload pi, the only problem in my case, with a function like so:
function pi_ = pi()
disp('pi'); %This is just to confirm if the overloading is working
pi_ = mp('pi');
end
This function is in the same folder as another function, which uses pi. However, it does not call this overloaded function, instead it calls the builtin everytime. According to the Function Precedence Order, since the overloader is in the same path, it should get called instead of the builtin, but Matlab, as too often happens, has behaviour that does not match its documentation...
How should I solve this problem?
0 个评论
回答(1 个)
Yash
2024-3-4
Hello,
I tried to reproduce the issue on MATLAB R2021a. Since I do not have access to the third-party multiprecision toolbox "advanpix", I replaced "pi = mp('pi')" with "pi = 0".
I created a file "pi.m" with the following content:
function pi_ = pi()
disp('pi'); %This is just to confirm if the overloading is working
pi_ = 0;
end
The code is same as shared by you, just one line is changed. I then created another file in the same folder and called "pi" as follows:
x = pi
In the output, 'pi' is diplayed and the values of "x" becomes 0 which means that the overloaded function is called.
Without the access to your folder and files, it is not possible to determine the exact reason for the issue. The following could be the reasons for this behaviour:
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!