Commit 48672ad1 authored by Sébastien Villemot's avatar Sébastien Villemot
Browse files

Preprocessor: handle underflows and overflows as MATLAB and Octave do

i.e. don't fail when they happen and instead return 0 or Inf
parent 4d183aa9
...@@ -51,7 +51,7 @@ DataTree::~DataTree() ...@@ -51,7 +51,7 @@ DataTree::~DataTree()
} }
expr_t expr_t
DataTree::AddNonNegativeConstant(const string &value) throw (NumericalConstants::InvalidFloatingPointNumberException) DataTree::AddNonNegativeConstant(const string &value)
{ {
int id = num_constants.AddNonNegativeConstant(value); int id = num_constants.AddNonNegativeConstant(value);
......
...@@ -110,7 +110,7 @@ public: ...@@ -110,7 +110,7 @@ public:
}; };
//! Adds a non-negative numerical constant (possibly Inf or NaN) //! 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 //! Adds a variable
/*! The default implementation of the method refuses any lag != 0 */ /*! The default implementation of the method refuses any lag != 0 */
virtual VariableNode *AddVariable(int symb_id, int lag = 0); virtual VariableNode *AddVariable(int symb_id, int lag = 0);
......
...@@ -26,7 +26,7 @@ ...@@ -26,7 +26,7 @@
#include "NumericalConstants.hh" #include "NumericalConstants.hh"
int int
NumericalConstants::AddNonNegativeConstant(const string &iConst) throw (InvalidFloatingPointNumberException) NumericalConstants::AddNonNegativeConstant(const string &iConst)
{ {
map<string, int>::const_iterator iter = numConstantsIndex.find(iConst); map<string, int>::const_iterator iter = numConstantsIndex.find(iConst);
...@@ -37,12 +37,10 @@ NumericalConstants::AddNonNegativeConstant(const string &iConst) throw (InvalidF ...@@ -37,12 +37,10 @@ NumericalConstants::AddNonNegativeConstant(const string &iConst) throw (InvalidF
mNumericalConstants.push_back(iConst); mNumericalConstants.push_back(iConst);
numConstantsIndex[iConst] = id; numConstantsIndex[iConst] = id;
errno = 0;
double val = strtod(iConst.c_str(), NULL); double val = strtod(iConst.c_str(), NULL);
// We check that the number is valid (e.g. not like "1e-10000") /* Note that we allow underflows (will be converted to 0) and overflows (will
if (errno != 0) be converted to Inf), as MATLAB and Octave do. */
throw InvalidFloatingPointNumberException(iConst);
assert(val >= 0 || isnan(val)); // Check we have a positive constant or a NaN assert(val >= 0 || isnan(val)); // Check we have a positive constant or a NaN
double_vals.push_back(val); double_vals.push_back(val);
......
...@@ -37,15 +37,8 @@ private: ...@@ -37,15 +37,8 @@ private:
//! Map matching constants to their id //! Map matching constants to their id
map<string, int> numConstantsIndex; map<string, int> numConstantsIndex;
public: 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 //! 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 //! Get a constant in string form
string get(int ID) const; string get(int ID) const;
//! Get a constant in double form //! Get a constant in double form
......
...@@ -258,15 +258,7 @@ ParsingDriver::add_equation_tags(string *key, string *value) ...@@ -258,15 +258,7 @@ ParsingDriver::add_equation_tags(string *key, string *value)
expr_t expr_t
ParsingDriver::add_non_negative_constant(string *constant) ParsingDriver::add_non_negative_constant(string *constant)
{ {
expr_t id; expr_t id = data_tree->AddNonNegativeConstant(*constant);
try
{
id = data_tree->AddNonNegativeConstant(*constant);
}
catch (NumericalConstants::InvalidFloatingPointNumberException &e)
{
error("Invalid floating point number: " + *constant);
}
delete constant; delete constant;
return id; return id;
} }
...@@ -747,17 +739,11 @@ void ...@@ -747,17 +739,11 @@ void
ParsingDriver::add_value(string *v) ParsingDriver::add_value(string *v)
{ {
expr_t id; expr_t id;
try
{ if (v->at(0) == '-')
if (v->at(0) == '-') id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos)));
id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos))); else
else id = data_tree->AddNonNegativeConstant(*v);
id = data_tree->AddNonNegativeConstant(*v);
}
catch (NumericalConstants::InvalidFloatingPointNumberException &e)
{
error("Invalid floating point number: " + *v);
}
delete v; delete v;
det_shocks_values.push_back(id); det_shocks_values.push_back(id);
...@@ -1004,17 +990,11 @@ void ...@@ -1004,17 +990,11 @@ void
ParsingDriver::add_to_row_const(string *v) ParsingDriver::add_to_row_const(string *v)
{ {
expr_t id; expr_t id;
try
{ if (v->at(0) == '-')
if (v->at(0) == '-') id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos)));
id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos))); else
else id = data_tree->AddNonNegativeConstant(*v);
id = data_tree->AddNonNegativeConstant(*v);
}
catch (NumericalConstants::InvalidFloatingPointNumberException &e)
{
error("Invalid floating point number: " + *v);
}
delete v; delete v;
sigmae_row.push_back(id); sigmae_row.push_back(id);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment