diff --git a/src/CodeInterpreter.hh b/src/CodeInterpreter.hh index 30ce42138b79b79ffdb285fda886dd08d9d4da06..fcd7ca4dd04c76a0a1868a0c0218619e9dc05153 100644 --- a/src/CodeInterpreter.hh +++ b/src/CodeInterpreter.hh @@ -39,10 +39,10 @@ # endif #endif -#define NEAR_ZERO (1e-12) - using namespace std; +const double near_zero{1e-12}; + /** * \enum Tags * \brief The differents flags of the bytecode diff --git a/src/DataTree.cc b/src/DataTree.cc index ca7273c4a467607afb0b71811fa42a72135ec6c7..91dacdaa9be9da25df2b557dd139ca7393372c29 100644 --- a/src/DataTree.cc +++ b/src/DataTree.cc @@ -722,7 +722,7 @@ DataTree::writePowerDeriv(ostream &output) const << "#ifdef _MSC_VER" << endl << "# define nearbyint(x) (fabs((x)-floor(x)) < fabs((x)-ceil(x)) ? floor(x) : ceil(x))" << endl << "#endif" << endl - << " if ( fabs(x) < " << NEAR_ZERO << " && p > 0 && k > p && fabs(p-nearbyint(p)) < " << NEAR_ZERO << " )" << endl + << " if ( fabs(x) < " << near_zero << " && p > 0 && k > p && fabs(p-nearbyint(p)) < " << near_zero << " )" << endl << " return 0.0;" << endl << " else" << endl << " {" << endl diff --git a/src/DataTree.hh b/src/DataTree.hh index 39c629ac6c44821e0e7e587435a2c53ba32392fc..ab0f2d4e1db095786e59ab5edf514fc074f9c20d 100644 --- a/src/DataTree.hh +++ b/src/DataTree.hh @@ -35,8 +35,6 @@ using namespace std; #include "ExternalFunctionsTable.hh" #include "ExprNode.hh" -#define CONSTANTS_PRECISION 16 - class DataTree { friend class ExprNode; @@ -118,6 +116,8 @@ protected: static string packageDir(const string &package); private: + const static int constants_precision{16}; + using node_list_t = list<expr_t>; //! The list of nodes node_list_t node_list; @@ -329,7 +329,7 @@ DataTree::AddPossiblyNegativeConstant(double v) neg = true; } ostringstream ost; - ost << setprecision(CONSTANTS_PRECISION) << v; + ost << setprecision(constants_precision) << v; expr_t cnode = AddNonNegativeConstant(ost.str()); diff --git a/src/DynamicModel.cc b/src/DynamicModel.cc index 9cbbd6486bc1a891a8b5398a3723e1f42b47c19e..8614dda5d119cc98962ba2d8d7c6b617b7c8b674 100644 --- a/src/DynamicModel.cc +++ b/src/DynamicModel.cc @@ -4860,7 +4860,7 @@ DynamicModel::testTrendDerivativesEqualToZero(const eval_context_t &eval_context equations[eq]->get_arg2()); // Do not run the test if the term inside the log is zero - if (fabs(homogeneq->eval(eval_context)) > ZERO_BAND) + if (fabs(homogeneq->eval(eval_context)) > zero_band) { expr_t testeq = AddLog(homogeneq); // F = log(lhs-rhs) testeq = testeq->getDerivative(it->second); // d F / d Trend @@ -4869,7 +4869,7 @@ DynamicModel::testTrendDerivativesEqualToZero(const eval_context_t &eval_context if (symbol_table.getType(endogit->first.first) == eEndogenous) { double nearZero = testeq->getDerivative(endogit->second)->eval(eval_context); // eval d F / d Trend d Endog - if (fabs(nearZero) > ZERO_BAND) + if (fabs(nearZero) > zero_band) { cerr << "WARNING: trends not compatible with balanced growth path; the second-order cross partial of equation " << eq + 1 << " (line " << equations_lineno[eq] << ") w.r.t. trend variable " @@ -5651,10 +5651,6 @@ DynamicModel::dynamicOnlyEquationsNbr() const return eqs.size(); } -#ifndef PRIVATE_BUFFER_SIZE -# define PRIVATE_BUFFER_SIZE 1024 -#endif - bool DynamicModel::isChecksumMatching(const string &basename) const { @@ -5712,10 +5708,11 @@ DynamicModel::isChecksumMatching(const string &basename) const } } - char private_buffer[PRIVATE_BUFFER_SIZE]; + const size_t private_buffer_size{1024}; + char private_buffer[private_buffer_size]; while (buffer) { - buffer.get(private_buffer, PRIVATE_BUFFER_SIZE); + buffer.get(private_buffer, private_buffer_size); result.process_bytes(private_buffer, strlen(private_buffer)); } diff --git a/src/DynamicModel.hh b/src/DynamicModel.hh index 79c726ce2d3cf3816c6acbdd79e35c54ce2a4dbd..cf38385ee7f8b7ebe556ca22e7e7df10a5c33010 100644 --- a/src/DynamicModel.hh +++ b/src/DynamicModel.hh @@ -21,7 +21,6 @@ #define _DYNAMICMODEL_HH using namespace std; -#define ZERO_BAND 1e-8 #include <fstream> #include <boost/crc.hpp> @@ -32,6 +31,8 @@ using namespace std; class DynamicModel : public ModelTree { private: + constexpr static double zero_band{1e-8}; + //! Stores equations declared as [static] /*! They will be used in toStatic() to replace equations marked as [dynamic] */ vector<BinaryOpNode *> static_only_equations; diff --git a/src/ExprNode.cc b/src/ExprNode.cc index 4e98111f68b51c815a8db36820cd42846090055c..4d542c99566d3f2ae4da345515d48d753544ed63 100644 --- a/src/ExprNode.cc +++ b/src/ExprNode.cc @@ -2162,7 +2162,7 @@ UnaryOpNode::computeTemporaryTerms(map<expr_t, pair<int, NodeTreeReference>> &re else { reference_count[this2] = { it->second.first + 1, it->second.second }; - if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > MIN_COST(is_matlab)) + if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > min_cost(is_matlab)) temp_terms_map[reference_count[this2].second].insert(this2); } } @@ -2186,7 +2186,7 @@ UnaryOpNode::computeTemporaryTerms(map<expr_t, int> &reference_count, else { reference_count[this2]++; - if (reference_count[this2] * cost(temporary_terms, false) > MIN_COST_C) + if (reference_count[this2] * cost(temporary_terms, false) > min_cost_c) { temporary_terms.insert(this2); v_temporary_terms[first_occurence[this2].first][first_occurence[this2].second].insert(this2); @@ -3763,7 +3763,7 @@ BinaryOpNode::cost(int cost, bool is_matlab) const return cost + 990; case oPower: case oPowerDeriv: - return cost + (MIN_COST_MATLAB/2+1); + return cost + (min_cost_matlab/2+1); case oEqual: return cost; } @@ -3790,7 +3790,7 @@ BinaryOpNode::cost(int cost, bool is_matlab) const case oPower: return cost + 520; case oPowerDeriv: - return cost + (MIN_COST_C/2+1);; + return cost + (min_cost_c/2+1);; case oEqual: return cost; } @@ -3819,7 +3819,7 @@ BinaryOpNode::computeTemporaryTerms(map<expr_t, pair<int, NodeTreeReference>> &r and declare it as a temporary term if it is too costly (except if it is an equal node: we don't want them as temporary terms) */ reference_count[this2] = { it->second.first + 1, it->second.second };; - if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > MIN_COST(is_matlab) + if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > min_cost(is_matlab) && op_code != oEqual) temp_terms_map[reference_count[this2].second].insert(this2); } @@ -3845,7 +3845,7 @@ BinaryOpNode::computeTemporaryTerms(map<expr_t, int> &reference_count, else { reference_count[this2]++; - if (reference_count[this2] * cost(temporary_terms, false) > MIN_COST_C + if (reference_count[this2] * cost(temporary_terms, false) > min_cost_c && op_code != oEqual) { temporary_terms.insert(this2); @@ -3870,9 +3870,9 @@ BinaryOpNode::eval_opcode(double v1, BinaryOpcode op_code, double v2, int derivO case oPower: return (pow(v1, v2)); case oPowerDeriv: - if (fabs(v1) < NEAR_ZERO && v2 > 0 + if (fabs(v1) < near_zero && v2 > 0 && derivOrder > v2 - && fabs(v2-nearbyint(v2)) < NEAR_ZERO) + && fabs(v2-nearbyint(v2)) < near_zero) return 0.0; else { @@ -5320,7 +5320,7 @@ TrinaryOpNode::computeTemporaryTerms(map<expr_t, pair<int, NodeTreeReference>> & // If the node has already been encountered, increment its ref count // and declare it as a temporary term if it is too costly reference_count[this2] = { it->second.first + 1, it->second.second };; - if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > MIN_COST(is_matlab)) + if (reference_count[this2].first * cost(temp_terms_map, is_matlab) > min_cost(is_matlab)) temp_terms_map[reference_count[this2].second].insert(this2); } } @@ -5346,7 +5346,7 @@ TrinaryOpNode::computeTemporaryTerms(map<expr_t, int> &reference_count, else { reference_count[this2]++; - if (reference_count[this2] * cost(temporary_terms, false) > MIN_COST_C) + if (reference_count[this2] * cost(temporary_terms, false) > min_cost_c) { temporary_terms.insert(this2); v_temporary_terms[first_occurence[this2].first][first_occurence[this2].second].insert(this2); diff --git a/src/ExprNode.hh b/src/ExprNode.hh index 0498ad7e86f08bf81e8f3787e40d5258e1e773b3..44999bc22c1dc00dd5203898dacd3e0ea7deb536 100644 --- a/src/ExprNode.hh +++ b/src/ExprNode.hh @@ -129,11 +129,6 @@ enum ExprNodeOutputType #define LEFT_PAR(output_type) (IS_LATEX(output_type) ? "\\left(" : "(") #define RIGHT_PAR(output_type) (IS_LATEX(output_type) ? "\\right)" : ")") -// Computing cost above which a node can be declared a temporary term -#define MIN_COST_MATLAB (40*90) -#define MIN_COST_C (40*4) -#define MIN_COST(is_matlab) ((is_matlab) ? MIN_COST_MATLAB : MIN_COST_C) - //! Base class for expression nodes class ExprNode { @@ -171,6 +166,10 @@ class ExprNode //! Used for caching of first order derivatives (when non-null) map<int, expr_t> derivatives; + const static int min_cost_matlab{40*90}; + const static int min_cost_c{40*4}; + inline static int min_cost(bool is_matlab) { return(is_matlab ? min_cost_matlab : min_cost_c); }; + //! Cost of computing current node /*! Nodes included in temporary_terms are considered having a null cost */ virtual int cost(int cost, bool is_matlab) const; diff --git a/src/ModFile.hh b/src/ModFile.hh index 5f76da3f8e20efb161936b7c7cce0ebe220b4990..d22203cbce989bdcc8bc4ac8327186ffc7ba263a 100644 --- a/src/ModFile.hh +++ b/src/ModFile.hh @@ -39,11 +39,6 @@ using namespace std; #include "WarningConsolidation.hh" #include "ExtendedPreprocessorTypes.hh" -// for checksum computation -#ifndef PRIVATE_BUFFER_SIZE -# define PRIVATE_BUFFER_SIZE 1024 -#endif - //! The abstract representation of a "mod" file class ModFile { diff --git a/src/ModelTree.cc b/src/ModelTree.cc index c9c950449f3c8f3d750b9d8c9e6d6e6a611e4dda..9551c9a0026df150b1c2f3c2faffc983b1b73bec 100644 --- a/src/ModelTree.cc +++ b/src/ModelTree.cc @@ -1126,7 +1126,7 @@ ModelTree::computeTemporaryTerms(bool is_matlab) { VariableNode *v = AddVariable(used_local_var); temporary_terms_mlv[v] = local_variables_table.find(used_local_var)->second; - reference_count[v] = { MIN_COST(is_matlab)+1, eResiduals }; + reference_count[v] = { ExprNode::min_cost(is_matlab)+1, eResiduals }; } map<NodeTreeReference, temporary_terms_t> temp_terms_map;