From c431ca637c272a20cae960b3ede409e7fb0337b2 Mon Sep 17 00:00:00 2001 From: sebastien Date: Wed, 29 Apr 2009 11:05:40 +0000 Subject: [PATCH] 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 --- preprocessor/NumericalConstants.cc | 17 ++++++++++++----- preprocessor/NumericalConstants.hh | 6 ++++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/preprocessor/NumericalConstants.cc b/preprocessor/NumericalConstants.cc index eacb3434d..de53e4a03 100644 --- a/preprocessor/NumericalConstants.cc +++ b/preprocessor/NumericalConstants.cc @@ -19,6 +19,7 @@ #include #include +#include #include #include "NumericalConstants.hh" @@ -26,16 +27,21 @@ int NumericalConstants::AddConstant(const string &iConst) { - map::iterator iter = numConstantsIndex.find(iConst); + map::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]); } diff --git a/preprocessor/NumericalConstants.hh b/preprocessor/NumericalConstants.hh index de7712709..49adc774a 100644 --- a/preprocessor/NumericalConstants.hh +++ b/preprocessor/NumericalConstants.hh @@ -32,15 +32,17 @@ class NumericalConstants private: //! Vector of numerical constants vector mNumericalConstants; + //! Double values of these constants + vector double_vals; //! Map matching constants to their id map 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