From dbec61ba62eb336458a1040cff1cda8b2be4523d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien.villemot@ens.fr>
Date: Thu, 12 Jul 2012 12:46:22 +0200
Subject: [PATCH] Preprocessor: handle underflows and overflows as MATLAB and
 Octave do

i.e. don't fail when they happen and instead return 0 or Inf
---
 DataTree.cc           |  2 +-
 DataTree.hh           |  2 +-
 NumericalConstants.cc |  8 +++-----
 NumericalConstants.hh |  9 +--------
 ParsingDriver.cc      | 42 +++++++++++-------------------------------
 5 files changed, 17 insertions(+), 46 deletions(-)

diff --git a/DataTree.cc b/DataTree.cc
index f95708e6..629365fb 100644
--- a/DataTree.cc
+++ b/DataTree.cc
@@ -51,7 +51,7 @@ DataTree::~DataTree()
 }
 
 expr_t
-DataTree::AddNonNegativeConstant(const string &value) throw (NumericalConstants::InvalidFloatingPointNumberException)
+DataTree::AddNonNegativeConstant(const string &value)
 {
   int id = num_constants.AddNonNegativeConstant(value);
 
diff --git a/DataTree.hh b/DataTree.hh
index e616f9cf..17823d23 100644
--- a/DataTree.hh
+++ b/DataTree.hh
@@ -110,7 +110,7 @@ public:
   };
 
   //! Adds a non-negative numerical constant (possibly Inf or NaN)
-  expr_t AddNonNegativeConstant(const string &value) throw (NumericalConstants::InvalidFloatingPointNumberException);
+  expr_t AddNonNegativeConstant(const string &value);
   //! Adds a variable
   /*! The default implementation of the method refuses any lag != 0 */
   virtual VariableNode *AddVariable(int symb_id, int lag = 0);
diff --git a/NumericalConstants.cc b/NumericalConstants.cc
index 6efe1fc0..dc2bd80f 100644
--- a/NumericalConstants.cc
+++ b/NumericalConstants.cc
@@ -26,7 +26,7 @@
 #include "NumericalConstants.hh"
 
 int
-NumericalConstants::AddNonNegativeConstant(const string &iConst) throw (InvalidFloatingPointNumberException)
+NumericalConstants::AddNonNegativeConstant(const string &iConst)
 {
   map<string, int>::const_iterator iter = numConstantsIndex.find(iConst);
 
@@ -37,12 +37,10 @@ NumericalConstants::AddNonNegativeConstant(const string &iConst) throw (InvalidF
   mNumericalConstants.push_back(iConst);
   numConstantsIndex[iConst] = id;
 
-  errno = 0;
   double val = strtod(iConst.c_str(), NULL);
 
-  // We check that the number is valid (e.g. not like "1e-10000")
-  if (errno != 0)
-    throw InvalidFloatingPointNumberException(iConst);
+  /* Note that we allow underflows (will be converted to 0) and overflows (will
+     be converted to Inf), as MATLAB and Octave do. */
 
   assert(val >= 0 || isnan(val)); // Check we have a positive constant or a NaN
   double_vals.push_back(val);
diff --git a/NumericalConstants.hh b/NumericalConstants.hh
index 0a54616a..cb50b0cb 100644
--- a/NumericalConstants.hh
+++ b/NumericalConstants.hh
@@ -37,15 +37,8 @@ private:
   //! Map matching constants to their id
   map<string, int> numConstantsIndex;
 public:
-  class InvalidFloatingPointNumberException
-  {
-  public:
-    const string fp;
-    InvalidFloatingPointNumberException(const string &fp_arg) : fp(fp_arg) {}
-  };
-
   //! Adds a non-negative constant (possibly Inf or NaN) and returns its ID
-  int AddNonNegativeConstant(const string &iConst) throw (InvalidFloatingPointNumberException);
+  int AddNonNegativeConstant(const string &iConst);
   //! Get a constant in string form
   string get(int ID) const;
   //! Get a constant in double form
diff --git a/ParsingDriver.cc b/ParsingDriver.cc
index a3078554..75dc4daa 100644
--- a/ParsingDriver.cc
+++ b/ParsingDriver.cc
@@ -258,15 +258,7 @@ ParsingDriver::add_equation_tags(string *key, string *value)
 expr_t
 ParsingDriver::add_non_negative_constant(string *constant)
 {
-  expr_t id;
-  try
-    {
-      id = data_tree->AddNonNegativeConstant(*constant);
-    }
-  catch (NumericalConstants::InvalidFloatingPointNumberException &e)
-    {
-      error("Invalid floating point number: " + *constant);
-    }
+  expr_t id = data_tree->AddNonNegativeConstant(*constant);
   delete constant;
   return id;
 }
@@ -747,17 +739,11 @@ void
 ParsingDriver::add_value(string *v)
 {
   expr_t id;
-  try
-    {
-      if (v->at(0) == '-')
-        id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos)));
-      else
-        id = data_tree->AddNonNegativeConstant(*v);
-    }
-  catch (NumericalConstants::InvalidFloatingPointNumberException &e)
-    {
-      error("Invalid floating point number: " + *v);
-    }
+
+  if (v->at(0) == '-')
+    id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos)));
+  else
+    id = data_tree->AddNonNegativeConstant(*v);
 
   delete v;
   det_shocks_values.push_back(id);
@@ -1004,17 +990,11 @@ void
 ParsingDriver::add_to_row_const(string *v)
 {
   expr_t id;
-  try
-    {
-      if (v->at(0) == '-')
-        id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos)));
-      else
-        id = data_tree->AddNonNegativeConstant(*v);
-    }
-  catch (NumericalConstants::InvalidFloatingPointNumberException &e)
-    {
-      error("Invalid floating point number: " + *v);
-    }
+
+  if (v->at(0) == '-')
+    id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos)));
+  else
+    id = data_tree->AddNonNegativeConstant(*v);
 
   delete v;
   sigmae_row.push_back(id);
-- 
GitLab