From 30c205f418459bd6d2003378f06bea7d38cfc562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org> Date: Wed, 4 Jul 2018 14:52:37 +0200 Subject: [PATCH] C++11: replace calls to std::atof() by std::stod() --- src/ComputingTasks.cc | 2 +- src/NumericalConstants.cc | 7 ++++--- src/NumericalInitialization.cc | 2 +- src/ParsingDriver.cc | 8 ++++---- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/ComputingTasks.cc b/src/ComputingTasks.cc index bd1e5dbf..a4b8fe17 100644 --- a/src/ComputingTasks.cc +++ b/src/ComputingTasks.cc @@ -3261,7 +3261,7 @@ MarkovSwitchingStatement::writeOutput(ostream &output, const string &basename, b itDuration = options_list.num_options.find("ms.duration"); assert(itDuration != options_list.num_options.end()); - if (atof(itDuration->second.c_str()) || infStr.compare(itDuration->second) == 0) + if (stod(itDuration->second) || infStr.compare(itDuration->second) == 0) isDurationAVec = false; output << "options_.ms.duration = " << itDuration->second << ";" << endl; diff --git a/src/NumericalConstants.cc b/src/NumericalConstants.cc index bd69d370..37be1a7f 100644 --- a/src/NumericalConstants.cc +++ b/src/NumericalConstants.cc @@ -37,10 +37,11 @@ NumericalConstants::AddNonNegativeConstant(const string &iConst) mNumericalConstants.push_back(iConst); numConstantsIndex[iConst] = id; - double val = strtod(iConst.c_str(), nullptr); - /* Note that we allow underflows (will be converted to 0) and overflows (will - be converted to Inf), as MATLAB and Octave do. */ + be converted to Inf), as MATLAB and Octave do. As a consequence, we + cannot use std::stod() here, since it does not allow distinguishing + between underflow and overflow. */ + double val = strtod(iConst.c_str(), nullptr); assert(val >= 0 || isnan(val)); // Check we have a positive constant or a NaN double_vals.push_back(val); diff --git a/src/NumericalInitialization.cc b/src/NumericalInitialization.cc index b720bda8..013060a0 100644 --- a/src/NumericalInitialization.cc +++ b/src/NumericalInitialization.cc @@ -633,5 +633,5 @@ void LoadParamsAndSteadyStateStatement::fillEvalContext(eval_context_t &eval_context) const { for (const auto & it : content) - eval_context[it.first] = atof(it.second.c_str()); + eval_context[it.first] = stod(it.second); } diff --git a/src/ParsingDriver.cc b/src/ParsingDriver.cc index 0af02909..1549aba6 100644 --- a/src/ParsingDriver.cc +++ b/src/ParsingDriver.cc @@ -629,7 +629,7 @@ void ParsingDriver::add_VAR_restriction_equation_or_crossequation(string *numberstr) { assert(var_restriction_eq_or_crosseq.size() > 0 && var_restriction_eq_or_crosseq.size() < 3); - double number = atof(numberstr->c_str()); + double number = stod(*numberstr); if (var_restriction_eq_or_crosseq.size() == 1) var_restriction_equation_or_crossequation = { { var_restriction_eq_or_crosseq[0], { { -1, { -1, -1 } }, nullptr } }, number }; else @@ -673,7 +673,7 @@ ParsingDriver::add_VAR_covariance_number_restriction(string *name1, string *name { int symb_id1 = mod_file->symbol_table.getID(*name1); int symb_id2 = mod_file->symbol_table.getID(*name2); - double value = atof(valuestr->c_str()); + double value = stod(*valuestr); covariance_number_restriction[{ symb_id1, symb_id2 }] = value; delete name1; delete name2; @@ -858,7 +858,7 @@ ParsingDriver::add_generate_irfs_exog_element(string *exo, string *value) if (generate_irf_exos.find(*exo) != generate_irf_exos.end()) error("You have set the exogenous variable " + *exo + " twice."); - generate_irf_exos[*exo] = atof(value->c_str()); + generate_irf_exos[*exo] = stod(*value); delete exo; delete value; @@ -920,7 +920,7 @@ ParsingDriver::differentiate_forward_vars_some() void ParsingDriver::cutoff(string *value) { - double val = atof(value->c_str()); + double val = stod(*value); mod_file->dynamic_model.cutoff = val; mod_file->static_model.cutoff = val; delete value; -- GitLab