필터 지우기
필터 지우기

Why does Matlab not respect Function Precedence Order when I try to overload a builtin with no arguments (pi)?

조회 수: 4 (최근 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?

답변 (1개)

Yash
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:
  • 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.
Hope this helps!

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by