Skip to content
Snippets Groups Projects
Commit aa7c7190 authored by Johannes Pfeifer's avatar Johannes Pfeifer
Browse files

:bug: getPowerDeriv.m: fix first-order derivative of 0^0

Evaluated to NaN instead of 0
parent a890e6d5
Branches
No related tags found
1 merge request!2358:bug: getPowerDeriv.m: fix first-order derivative of 0^0
...@@ -30,7 +30,7 @@ function dxp=getPowerDeriv(x,p,k) ...@@ -30,7 +30,7 @@ function dxp=getPowerDeriv(x,p,k)
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>. % along with Dynare. If not, see <https://www.gnu.org/licenses/>.
if (abs(x) < 1e-12) && (p > 0) && (k > p) && (abs(p - round(p)) < 1e-12) if (abs(x) < 1e-12) && (p >= 0) && (k > p) && (abs(p - round(p)) < 1e-12)
dxp = 0; dxp = 0;
else else
dxp = x^(p-k); dxp = x^(p-k);
...@@ -39,4 +39,23 @@ else ...@@ -39,4 +39,23 @@ else
p = p-1; p = p-1;
end end
end end
end
return % --*-- Unit tests --*--
%@test:1
x=getPowerDeriv(2,3,1);
t(1)=all(abs(x-3*4)<1e-10)
x=getPowerDeriv(0,2,2);
t(2)=all(abs(x-2)<1e-10)
x=getPowerDeriv(0,2,3); %special case evaluates to 0
t(3)=all(abs(x-0)<1e-10)
x=getPowerDeriv(1e-13,2,3-1e-13); %0 within tolerance
t(4)=all(abs(x-0)<1e-10)
x=getPowerDeriv(0,0,1);
t(5)=all(abs(x-0)<1e-10)
x=getPowerDeriv(0,0,0);
t(6)=all(abs(x-1)<1e-10);
x=getPowerDeriv(0,1/3,1); %derivative evaluating to Inf due to division by 0
t(7)= isinf(x)
T = all(t);
%@eof:1
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment