trunk preprocessor: in NumericalConstants
* fixed bug introduced in r2642 with NaN * added a check to verify that the conversion to double succeeded (use strtod() instead of atof()) * store the double values in a vector git-svn-id: https://www.dynare.org/svn/dynare/trunk@2647 ac1d8469-bf42-47a9-8791-bf33cf982152issue#70
parent
63c018a64f
commit
d65978ff94
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "NumericalConstants.hh"
|
#include "NumericalConstants.hh"
|
||||||
|
@ -26,16 +27,21 @@
|
||||||
int
|
int
|
||||||
NumericalConstants::AddConstant(const string &iConst)
|
NumericalConstants::AddConstant(const string &iConst)
|
||||||
{
|
{
|
||||||
map<string, int>::iterator iter = numConstantsIndex.find(iConst);
|
map<string, int>::const_iterator iter = numConstantsIndex.find(iConst);
|
||||||
|
|
||||||
if (iter != numConstantsIndex.end())
|
if (iter != numConstantsIndex.end())
|
||||||
return iter->second;
|
return iter->second;
|
||||||
|
|
||||||
assert(atof(iConst.c_str()) >= 0);
|
|
||||||
|
|
||||||
int id = (int) mNumericalConstants.size();
|
int id = (int) mNumericalConstants.size();
|
||||||
mNumericalConstants.push_back(iConst);
|
mNumericalConstants.push_back(iConst);
|
||||||
numConstantsIndex[iConst] = id;
|
numConstantsIndex[iConst] = id;
|
||||||
|
|
||||||
|
char *endptr;
|
||||||
|
double val = strtod(iConst.c_str(), &endptr);
|
||||||
|
assert(endptr != iConst.c_str()); // Check that the conversion succeeded
|
||||||
|
assert(val >= 0 || isnan(val)); // Check we have a positive constant or a NaN
|
||||||
|
double_vals.push_back(val);
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,7 +53,8 @@ NumericalConstants::get(int ID) const
|
||||||
}
|
}
|
||||||
|
|
||||||
double
|
double
|
||||||
NumericalConstants::getDouble(int iID) const
|
NumericalConstants::getDouble(int ID) const
|
||||||
{
|
{
|
||||||
return(atof(get(iID).c_str()));
|
assert(ID >= 0 && ID < (int) double_vals.size());
|
||||||
|
return(double_vals[ID]);
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,15 +32,17 @@ class NumericalConstants
|
||||||
private:
|
private:
|
||||||
//! Vector of numerical constants
|
//! Vector of numerical constants
|
||||||
vector<string> mNumericalConstants;
|
vector<string> mNumericalConstants;
|
||||||
|
//! Double values of these constants
|
||||||
|
vector<double> double_vals;
|
||||||
//! Map matching constants to their id
|
//! Map matching constants to their id
|
||||||
map<string, int> numConstantsIndex;
|
map<string, int> numConstantsIndex;
|
||||||
public:
|
public:
|
||||||
//! Adds a constant and returns its ID
|
//! Adds a constant and returns its ID
|
||||||
int AddConstant(const string &iConst);
|
int AddConstant(const string &iConst);
|
||||||
//! Get a constant in string form
|
//! Get a constant in string form
|
||||||
string get(int iID) const;
|
string get(int ID) const;
|
||||||
//! Get a constant in double form
|
//! Get a constant in double form
|
||||||
double getDouble(int iID) const;
|
double getDouble(int ID) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue