From 5c8250ba2bd063d1ae02eba2b5d10388541cd27b Mon Sep 17 00:00:00 2001
From: Michel Juillard <michel.juillard@mjui.fr>
Date: Fri, 18 Nov 2011 22:12:36 +0100
Subject: [PATCH] introducing new test for 0/0 case in mjdgges (cherry-picked
 by hand from 1cd827b5adc78669b6525fed57cfa383e6ffd5fb)

---
 matlab/DsgeLikelihood.m       |  4 ++--
 matlab/dr1.m                  |  9 +++++++--
 matlab/print_info.m           |  4 ++--
 mex/sources/mjdgges/mjdgges.c | 10 +++++++++-
 4 files changed, 20 insertions(+), 7 deletions(-)

diff --git a/matlab/DsgeLikelihood.m b/matlab/DsgeLikelihood.m
index e20a13f3b..c0ecd0bb3 100644
--- a/matlab/DsgeLikelihood.m
+++ b/matlab/DsgeLikelihood.m
@@ -126,7 +126,7 @@ M_.H = H;
 %------------------------------------------------------------------------------
 [T,R,SteadyState,info] = dynare_resolve('restrict');
 
-if info(1) == 1 || info(1) == 2 || info(1) == 5
+if info(1) == 1 || info(1) == 2 || info(1) == 7 || info(1) == 5
     fval = bayestopt_.penalty+1;
     cost_flag = 0;
     return
@@ -271,4 +271,4 @@ end
 % ------------------------------------------------------------------------------
 lnprior = priordens(xparam1,bayestopt_.pshape,bayestopt_.p6,bayestopt_.p7,bayestopt_.p3,bayestopt_.p4);
 fval    = (likelihood-lnprior);
-options_.kalman_algo = kalman_algo;
\ No newline at end of file
+options_.kalman_algo = kalman_algo;
diff --git a/matlab/dr1.m b/matlab/dr1.m
index 562ef7e90..b1c006dfb 100644
--- a/matlab/dr1.m
+++ b/matlab/dr1.m
@@ -352,8 +352,13 @@ else  % use original Dynare solver
     mexErrCheck('mjdgges', err);
 
     if info1
-        info(1) = 2;
-        info(2) = info1;
+        if info1 == -30
+            info(1) = 7;
+        else
+            info(1) = 2;
+            info(2) = info1;
+            info(3) = size(e,2);
+        end
         return
     end
 
diff --git a/matlab/print_info.m b/matlab/print_info.m
index a1e22d1bd..40c1d7ee9 100644
--- a/matlab/print_info.m
+++ b/matlab/print_info.m
@@ -48,8 +48,8 @@ if ~noprint
         error(['The Jacobian matrix evaluated at the steady state contains elements ' ...
                'that are not real or are infinite'])
       case 7
-        error(['One of the eigenvalues is is close to 0/0 (the absolute ' ...
-               'value of numerator and denominator is smaller than 1e-6).'])
+        error(['One of the eigenvalues is close to 0/0 (the absolute ' ...
+               'value of numerator and denominator is smaller than 1e-6)'])
       case 19
         error('The steadystate file did not compute the steady state (inconsistent deep parameters).')
       case 20
diff --git a/mex/sources/mjdgges/mjdgges.c b/mex/sources/mjdgges/mjdgges.c
index e42b8234c..bbd91a40c 100644
--- a/mex/sources/mjdgges/mjdgges.c
+++ b/mex/sources/mjdgges/mjdgges.c
@@ -60,7 +60,15 @@ mjdgges(double *a, double *b, double *z, double *n, double *sdim, double *eval_r
   pei = eval_i;
   for (per = eval_r; per <= &eval_r[i_n-1]; ++per)
     {
-      *per = *par / *pb;
+      if ((fabs(*par) > 1e-6) || (fabs(*pb) > 1e-6)) 
+	*per = *par / *pb;
+      else
+	{
+	  /* the ratio is too close to 0/0;
+	     returns specific error number only if no other error */
+	  if (i_info == 0)
+	    *info = -30;
+	}
       if (*pai == 0.0 && *pb == 0.0)
         *pei = 0.0;
       else
-- 
GitLab