Minsky
unitsExpressionWalker.h
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2012
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
25 #ifndef UNITEXPRESSIONWALKER_H
26 #define UNITEXPRESSIONWALKER_H
27 #include <variableType.h>
28 
29 namespace minsky
30 {
32  {
34  static bool check;
35  double value=std::nan("");
37  UnitsExpressionWalker(double x): value(x) {}
38  UnitsExpressionWalker(const std::string& units): units(units) {}
39 
40  void checkSameDims(const UnitsExpressionWalker& x) const
41  {if (check && x.units!=units) throw std::runtime_error("Incommensurate units");}
42  void checkDimensionless() const
43  {if (check && !units.empty()) throw std::runtime_error("Should be dimensionless");}
45  {checkSameDims(x); return *this;}
47  {checkSameDims(x); return *this;}
49  {checkSameDims(x); return *this;}
51  {checkSameDims(x); return *this;}
52 
54  {
55  for (auto& i: x.units)
56  units[i.first]+=i.second;
57  units.normalise();
58  return *this;
59  }
61  {auto tmp=*this; return tmp*=x;}
63  {
64  for (auto& i: x.units)
65  units[i.first]-=i.second;
66  units.normalise();
67  return *this;
68  }
70  {auto tmp=*this; return tmp/=x;}
72  {return x;}
74  {checkSameDims(x);return x;}
76  {checkSameDims(x);return x;}
78  {checkSameDims(x);return x;}
79  bool operator<(const UnitsExpressionWalker& x) const
80  {checkSameDims(x); return value<x.value;}
81  bool operator>(const UnitsExpressionWalker& x) const
82  {checkSameDims(x); return value>x.value;}
83  bool operator<=(const UnitsExpressionWalker& x) const
84  {checkSameDims(x); return value<=x.value;}
85  bool operator>=(const UnitsExpressionWalker& x) const
86  {checkSameDims(x); return value>=x.value;}
87  bool operator==(const UnitsExpressionWalker& x) const
88  {return x.value==value;}
89  bool operator!=(const UnitsExpressionWalker& x) const
90  {return !operator==(x);}
91  };
92 
93 }
94 #endif
UnitsExpressionWalker operator-=(const UnitsExpressionWalker &x)
bool operator==(const UnitsExpressionWalker &x) const
UnitsExpressionWalker operator/(const UnitsExpressionWalker &x) const
UnitsExpressionWalker operator%(const UnitsExpressionWalker &x) const
UnitsExpressionWalker operator+(const UnitsExpressionWalker &x) const
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
represents the units (in sense of dimensional analysis) of a variable.
Definition: units.h:34
UnitsExpressionWalker operator*=(const UnitsExpressionWalker &x)
bool operator<=(const UnitsExpressionWalker &x) const
void checkSameDims(const UnitsExpressionWalker &x) const
void normalise()
Definition: units.h:42
UnitsExpressionWalker operator*(const UnitsExpressionWalker &x) const
UnitsExpressionWalker operator+=(const UnitsExpressionWalker &x)
bool operator>(const UnitsExpressionWalker &x) const
UnitsExpressionWalker operator/=(const UnitsExpressionWalker &x)
bool operator>=(const UnitsExpressionWalker &x) const
bool operator!=(const UnitsExpressionWalker &x) const
UnitsExpressionWalker operator &&(const UnitsExpressionWalker &x) const
UnitsExpressionWalker operator||(const UnitsExpressionWalker &x) const
bool operator<(const UnitsExpressionWalker &x) const
UnitsExpressionWalker(const std::string &units)
UnitsExpressionWalker operator^(const UnitsExpressionWalker &x) const
UnitsExpressionWalker operator-(const UnitsExpressionWalker &x) const