-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunits_goodie.tex
203 lines (158 loc) · 5.43 KB
/
units_goodie.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
\begin{cpp}[\tiny]
#include "antioch/antioch_asserts.h"
#include "antioch/units.h"
#include "antioch/unit_defs.h"
namespace Antioch{
template <typename CoeffType>
class PhysicalQuantity{
public:
PhysicalQuantity();
PhysicalQuantity(const CoeffType & value, const std::string & unit = std::string() );
~PhysicalQuantity();
/* getter/setter */
void set_value(const CoeffType & value);
void set_unit(const std::string & unit);
void set_unit(const Units<CoeffType> & unit);
const CoeffType value() const;
const std::string & unit() const;
const Units<CoeffType> & unit_object() const;
/* changers */
void change_unit_to(const std::string &target);
void change_unit_to_SI();
/* operators */
PhysicalQuantity<CoeffType> & operator+=(const PhysicalQuantity<CoeffType> &rhs);
PhysicalQuantity<CoeffType> & operator-=(const PhysicalQuantity<CoeffType> &rhs);
PhysicalQuantity<CoeffType> & operator*=(const PhysicalQuantity<CoeffType> &rhs);
PhysicalQuantity<CoeffType> & operator/=(const PhysicalQuantity<CoeffType> &rhs);
PhysicalQuantity<CoeffType> operator* (const PhysicalQuantity<CoeffType> &rhs) const;
PhysicalQuantity<CoeffType> operator/ (const PhysicalQuantity<CoeffType> &rhs) const;
PhysicalQuantity<CoeffType> operator+ (const PhysicalQuantity<CoeffType> &rhs) const;
PhysicalQuantity<CoeffType> operator- (const PhysicalQuantity<CoeffType> &rhs) const;
private:
Units<CoeffType> _unit;
CoeffType _value;
};
template <typename CoeffType>
PhysicalQuantity<CoeffType>::PhysicalQuantity():
_unit(),
_value(0.)
{
return;
}
template <typename CoeffType>
PhysicalQuantity<CoeffType>::PhysicalQuantity(const CoeffType & value, const std::string & unit):
_unit(unit),
_value(value)
{
return;
}
template <typename CoeffType>
PhysicalQuantity<CoeffType>::~PhysicalQuantity()
{
return;
}
template <typename CoeffType>
void PhysicalQuantity<CoeffType>::set_value(const CoeffType & value)
{
_value = value;
}
template <typename CoeffType>
void PhysicalQuantity<CoeffType>::set_unit(const std::string & unit)
{
_unit = unit;
}
template <typename CoeffType>
void PhysicalQuantity<CoeffType>::set_unit(const Units<CoeffType> & unit)
{
_unit = unit;
}
template <typename CoeffType>
const CoeffType PhysicalQuantity<CoeffType>::value() const
{
return _value;
}
template <typename CoeffType>
const std::string & PhysicalQuantity<CoeffType>::unit() const
{
return _unit.symbol();
}
template <typename CoeffType>
const Units<CoeffType> & PhysicalQuantity<CoeffType>::unit_object() const
{
return _unit;
}
template <typename CoeffType>
void PhysicalQuantity<CoeffType>::change_unit_to(const std::string &target)
{
if(!_unit.is_homogeneous(target))antioch_error();
CoeffType t = _unit.translator_to_some_unit(target);
CoeffType v = _unit.factor_to_some_unit(target);
_value = v * _value + t;
_unit.set_unit(target);
}
template <typename CoeffType>
void PhysicalQuantity<CoeffType>::change_unit_to_SI()
{
CoeffType t = _unit.get_SI_translator();
CoeffType v = _unit.get_SI_factor();
_value = v * _value + t;
_unit.set_unit(_unit.get_SI_symb());
}
template <typename CoeffType>
PhysicalQuantity<CoeffType> &
PhysicalQuantity<CoeffType>::operator+=(const PhysicalQuantity<CoeffType> &rhs)
{
if(!rhs.unit().is_homogeneous(this->unit()))antioch_error();
this->set_value(this->value() + rhs.value());
return *this;
}
template <typename CoeffType>
PhysicalQuantity<CoeffType> &
PhysicalQuantity<CoeffType>::operator-=(const PhysicalQuantity<CoeffType> &rhs)
{
if(!rhs.unit().is_homogeneous(this->unit()))antioch_error();
this->set_value(this->value() - rhs.value());
return *this;
}
template <typename CoeffType>
PhysicalQuantity<CoeffType> &
PhysicalQuantity<CoeffType>::operator*=(const PhysicalQuantity<CoeffType> &rhs)
{
this->set_value(this->value() * rhs.value());
this->set_unit(this->unit() + rhs.unit());
return *this;
}
template <typename CoeffType>
PhysicalQuantity<CoeffType> &
PhysicalQuantity<CoeffType>::operator/=(const PhysicalQuantity<CoeffType> &rhs)
{
this->set_value(this->value() / rhs.value());
this->set_unit(this->unit() - rhs.unit());
return *this;
}
template <typename CoeffType>
PhysicalQuantity<CoeffType>
PhysicalQuantity<CoeffType>::operator*(const PhysicalQuantity<CoeffType> &rhs) const
{
return (PhysicalQuantity<CoeffType>(*this) *= rhs);
}
template <typename CoeffType>
PhysicalQuantity<CoeffType>
PhysicalQuantity<CoeffType>::operator/(const PhysicalQuantity<CoeffType> &rhs) const
{
return (PhysicalQuantity<CoeffType>(*this) /= rhs);
}
template <typename CoeffType>
PhysicalQuantity<CoeffType>
PhysicalQuantity<CoeffType>::operator+(const PhysicalQuantity<CoeffType> &rhs) const
{
return (PhysicalQuantity<CoeffType>(*this) += rhs);
}
template <typename CoeffType>
PhysicalQuantity<CoeffType>
PhysicalQuantity<CoeffType>::operator-(const PhysicalQuantity<CoeffType> &rhs) const
{
return (PhysicalQuantity<CoeffType>(*this) -= rhs);
}
}
\end{cpp}