Preprocessor: more explicit error message when users enter invalid floating

point numbers (like "1e-10000")
time-shift
Sébastien Villemot 2011-02-16 11:08:40 +01:00
parent d27b6522b9
commit 89b5022f9b
5 changed files with 51 additions and 16 deletions

View File

@ -51,7 +51,7 @@ DataTree::~DataTree()
} }
expr_t expr_t
DataTree::AddNonNegativeConstant(const string &value) DataTree::AddNonNegativeConstant(const string &value) throw (NumericalConstants::InvalidFloatingPointNumberException)
{ {
int id = num_constants.AddNonNegativeConstant(value); int id = num_constants.AddNonNegativeConstant(value);

View File

@ -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); expr_t AddNonNegativeConstant(const string &value) throw (NumericalConstants::InvalidFloatingPointNumberException);
//! 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);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2003-2010 Dynare Team * Copyright (C) 2003-2011 Dynare Team
* *
* This file is part of Dynare. * This file is part of Dynare.
* *
@ -26,7 +26,7 @@
#include "NumericalConstants.hh" #include "NumericalConstants.hh"
int int
NumericalConstants::AddNonNegativeConstant(const string &iConst) NumericalConstants::AddNonNegativeConstant(const string &iConst) throw (InvalidFloatingPointNumberException)
{ {
map<string, int>::const_iterator iter = numConstantsIndex.find(iConst); map<string, int>::const_iterator iter = numConstantsIndex.find(iConst);
@ -39,7 +39,11 @@ NumericalConstants::AddNonNegativeConstant(const string &iConst)
errno = 0; errno = 0;
double val = strtod(iConst.c_str(), NULL); double val = strtod(iConst.c_str(), NULL);
assert(errno == 0); // Check that the conversion succeeded
// We check that the number is valid (e.g. not like "1e-10000")
if (errno != 0)
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);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2003-2010 Dynare Team * Copyright (C) 2003-2011 Dynare Team
* *
* This file is part of Dynare. * This file is part of Dynare.
* *
@ -37,8 +37,15 @@ 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); int AddNonNegativeConstant(const string &iConst) throw (InvalidFloatingPointNumberException);
//! 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

View File

@ -227,7 +227,15 @@ 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 = data_tree->AddNonNegativeConstant(*constant); expr_t id;
try
{
id = data_tree->AddNonNegativeConstant(*constant);
}
catch (NumericalConstants::InvalidFloatingPointNumberException &e)
{
error("Invalid floating point number: " + *constant);
}
delete constant; delete constant;
return id; return id;
} }
@ -703,10 +711,18 @@ void
ParsingDriver::add_value(string *v) ParsingDriver::add_value(string *v)
{ {
expr_t id; expr_t id;
if (v->at(0) == '-') try
id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos))); {
else if (v->at(0) == '-')
id = data_tree->AddNonNegativeConstant(*v); 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);
}
delete v; delete v;
det_shocks_values.push_back(id); det_shocks_values.push_back(id);
} }
@ -817,10 +833,18 @@ void
ParsingDriver::add_to_row_const(string *v) ParsingDriver::add_to_row_const(string *v)
{ {
expr_t id; expr_t id;
if (v->at(0) == '-') try
id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos))); {
else if (v->at(0) == '-')
id = data_tree->AddNonNegativeConstant(*v); 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);
}
delete v; delete v;
sigmae_row.push_back(id); sigmae_row.push_back(id);
} }