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-bf33cf982152
issue#70
sebastien 2009-04-29 11:05:40 +00:00
parent 63c018a64f
commit d65978ff94
2 changed files with 16 additions and 7 deletions

View File

@ -19,6 +19,7 @@
#include <cstdlib>
#include <cassert>
#include <cmath>
#include <iostream>
#include "NumericalConstants.hh"
@ -26,16 +27,21 @@
int
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())
return iter->second;
assert(atof(iConst.c_str()) >= 0);
int id = (int) mNumericalConstants.size();
mNumericalConstants.push_back(iConst);
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;
}
@ -47,7 +53,8 @@ NumericalConstants::get(int ID) const
}
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]);
}

View File

@ -32,15 +32,17 @@ class NumericalConstants
private:
//! Vector of numerical constants
vector<string> mNumericalConstants;
//! Double values of these constants
vector<double> double_vals;
//! Map matching constants to their id
map<string, int> numConstantsIndex;
public:
//! Adds a constant and returns its ID
int AddConstant(const string &iConst);
//! Get a constant in string form
string get(int iID) const;
string get(int ID) const;
//! Get a constant in double form
double getDouble(int iID) const;
double getDouble(int ID) const;
};
#endif