From c5d223a79b7917f82f0e35487629a283ccfd1c2e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 22 Oct 2019 11:34:51 +0200
Subject: [PATCH] Fix semantics of ExprNode::maxLag(), maxLead() and
 maxLagWithDiffsExpanded() with constants
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Those methods can return a negative value in some cases. For example,
maxLead(x₋₁) = −1.

But constants were always returning a value of zero, which means that we had
inconsistent behaviour like maxLead(x₋₁ + 2) = 0.

This commits fixes the behaviour by making these methods return the smallest
possible integer when called on constants.
---
 src/ExprNode.cc | 7 ++++---
 src/ExprNode.hh | 6 ++++--
 2 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/ExprNode.cc b/src/ExprNode.cc
index 8969d448..6eb9c506 100644
--- a/src/ExprNode.cc
+++ b/src/ExprNode.cc
@@ -22,6 +22,7 @@
 #include <cassert>
 #include <cmath>
 #include <utility>
+#include <limits>
 
 #include "ExprNode.hh"
 #include "DataTree.hh"
@@ -559,19 +560,19 @@ NumConstNode::maxExoLag() const
 int
 NumConstNode::maxLead() const
 {
-  return 0;
+  return numeric_limits<int>::min();
 }
 
 int
 NumConstNode::maxLag() const
 {
-  return 0;
+  return numeric_limits<int>::min();
 }
 
 int
 NumConstNode::maxLagWithDiffsExpanded() const
 {
-  return 0;
+  return numeric_limits<int>::min();
 }
 
 expr_t
diff --git a/src/ExprNode.hh b/src/ExprNode.hh
index c7326d02..007459e6 100644
--- a/src/ExprNode.hh
+++ b/src/ExprNode.hh
@@ -407,12 +407,14 @@ class ExprNode
 
       //! Returns the maximum lead of endo/exo/exodet in this expression
       /*! A negative value means that the expression contains only lagged
-          variables. */
+          variables. A value of numeric_limits<int>::min() means that there is
+          no variable. */
       virtual int maxLead() const = 0;
 
       //! Returns the maximum lag of endo/exo/exodet in this expression
       /*! A negative value means that the expression contains only leaded
-          variables. */
+          variables. A value of numeric_limits<int>::min() means that there is
+          no variable. */
       virtual int maxLag() const = 0;
 
       //! Returns the maximum lag of endo/exo/exodet, as if diffs were expanded
-- 
GitLab