Preprocessor: more explicit error message when users enter invalid floating
point numbers (like "1e-10000")issue#70
parent
f25b069a00
commit
47b828489b
|
@ -51,7 +51,7 @@ DataTree::~DataTree()
|
|||
}
|
||||
|
||||
expr_t
|
||||
DataTree::AddNonNegativeConstant(const string &value)
|
||||
DataTree::AddNonNegativeConstant(const string &value) throw (NumericalConstants::InvalidFloatingPointNumberException)
|
||||
{
|
||||
int id = num_constants.AddNonNegativeConstant(value);
|
||||
|
||||
|
|
|
@ -110,7 +110,7 @@ public:
|
|||
};
|
||||
|
||||
//! 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
|
||||
/*! The default implementation of the method refuses any lag != 0 */
|
||||
virtual VariableNode *AddVariable(int symb_id, int lag = 0);
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2003-2010 Dynare Team
|
||||
* Copyright (C) 2003-2011 Dynare Team
|
||||
*
|
||||
* This file is part of Dynare.
|
||||
*
|
||||
|
@ -26,7 +26,7 @@
|
|||
#include "NumericalConstants.hh"
|
||||
|
||||
int
|
||||
NumericalConstants::AddNonNegativeConstant(const string &iConst)
|
||||
NumericalConstants::AddNonNegativeConstant(const string &iConst) throw (InvalidFloatingPointNumberException)
|
||||
{
|
||||
map<string, int>::const_iterator iter = numConstantsIndex.find(iConst);
|
||||
|
||||
|
@ -39,7 +39,11 @@ NumericalConstants::AddNonNegativeConstant(const string &iConst)
|
|||
|
||||
errno = 0;
|
||||
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
|
||||
double_vals.push_back(val);
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2003-2010 Dynare Team
|
||||
* Copyright (C) 2003-2011 Dynare Team
|
||||
*
|
||||
* This file is part of Dynare.
|
||||
*
|
||||
|
@ -37,8 +37,15 @@ 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);
|
||||
int AddNonNegativeConstant(const string &iConst) throw (InvalidFloatingPointNumberException);
|
||||
//! Get a constant in string form
|
||||
string get(int ID) const;
|
||||
//! Get a constant in double form
|
||||
|
|
|
@ -227,7 +227,15 @@ ParsingDriver::add_equation_tags(string *key, string *value)
|
|||
expr_t
|
||||
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;
|
||||
return id;
|
||||
}
|
||||
|
@ -703,10 +711,18 @@ void
|
|||
ParsingDriver::add_value(string *v)
|
||||
{
|
||||
expr_t id;
|
||||
if (v->at(0) == '-')
|
||||
id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos)));
|
||||
else
|
||||
id = data_tree->AddNonNegativeConstant(*v);
|
||||
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);
|
||||
}
|
||||
|
||||
delete v;
|
||||
det_shocks_values.push_back(id);
|
||||
}
|
||||
|
@ -817,10 +833,18 @@ void
|
|||
ParsingDriver::add_to_row_const(string *v)
|
||||
{
|
||||
expr_t id;
|
||||
if (v->at(0) == '-')
|
||||
id = data_tree->AddUMinus(data_tree->AddNonNegativeConstant(v->substr(1, string::npos)));
|
||||
else
|
||||
id = data_tree->AddNonNegativeConstant(*v);
|
||||
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);
|
||||
}
|
||||
|
||||
delete v;
|
||||
sigmae_row.push_back(id);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue