SyDEVS  v0.6.7
Multiscale Simulation and Systems Modeling Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
scale.h
Go to the documentation of this file.
1 #pragma once
2 #ifndef SYDEVS_SCALE_H_
3 #define SYDEVS_SCALE_H_
4 
6 #include <iosfwd>
7 
8 namespace sydevs {
9 
10 
70 class scale
71 {
72 public:
73  using level_type = int8;
74 
78  constexpr scale();
79 
89  explicit constexpr scale(int64 level);
90 
91  constexpr scale(const scale&) = default;
92  scale& operator=(const scale&) = default;
93  scale(scale&&) = default;
94  scale& operator=(scale&&) = default;
95  ~scale() = default;
96 
97  constexpr int64 level() const;
98  constexpr float64 approx() const;
99 
100  scale& operator++();
101  scale operator++(int);
102  scale& operator--();
103  scale operator--(int);
104  scale& operator+=(int64 rhs);
105  scale& operator-=(int64 rhs);
106 
107  constexpr const scale operator+() const;
108  constexpr const scale operator-() const;
109 
110  constexpr const scale operator+(int64 rhs) const;
111  constexpr const scale operator-(int64 rhs) const;
112  constexpr int64 operator-(scale rhs) const;
113  constexpr float64 operator/(scale rhs) const;
114 
115  constexpr bool operator==(scale rhs) const;
116  constexpr bool operator!=(scale rhs) const;
117  constexpr bool operator< (scale rhs) const;
118  constexpr bool operator> (scale rhs) const;
119  constexpr bool operator<=(scale rhs) const;
120  constexpr bool operator>=(scale rhs) const;
121 
133  char symbol() const;
134 
135 private:
136  level_type level_;
137 
138 };
139 
140 
141 constexpr scale::scale()
142  : level_(0)
143 {
144 }
145 
146 
147 constexpr scale::scale(int64 level)
148  : level_(level_type(level))
149 {
150 }
151 
152 
153 constexpr scale no_scale = scale(std::numeric_limits<scale::level_type>::max());
154 
155 constexpr scale yocto = scale(-8);
156 constexpr scale zepto = scale(-7);
157 constexpr scale atto = scale(-6);
158 constexpr scale femto = scale(-5);
159 constexpr scale pico = scale(-4);
160 constexpr scale nano = scale(-3);
161 constexpr scale micro = scale(-2);
162 constexpr scale milli = scale(-1);
163 constexpr scale unit = scale(0);
164 constexpr scale kilo = scale(1);
165 constexpr scale mega = scale(2);
166 constexpr scale giga = scale(3);
167 constexpr scale tera = scale(4);
168 constexpr scale peta = scale(5);
169 constexpr scale exa = scale(6);
170 constexpr scale zetta = scale(7);
171 constexpr scale yotta = scale(8);
172 
173 
174 constexpr int64 scale::level() const
175 {
176  return level_;
177 }
178 
179 
180 constexpr float64 scale::approx() const
181 {
182  return level_ == 0 ? 1.0 :
183  level_ < 0 ? 0.001*scale(level_ + 1).approx() :
184  1000.0*scale(level_ - 1).approx();
185 }
186 
187 
188 constexpr const scale scale::operator+() const
189 {
190  return scale(level_);
191 }
192 
193 
194 constexpr const scale scale::operator-() const
195 {
196  return scale(-level_);
197 }
198 
199 
200 constexpr const scale scale::operator+(int64 rhs) const
201 {
202  return scale(level_ + rhs);
203 }
204 
205 
206 constexpr const scale scale::operator-(int64 rhs) const
207 {
208  return scale(level_ - rhs);
209 }
210 
211 
212 constexpr int64 scale::operator-(scale rhs) const
213 {
214  return level_ - rhs.level_;
215 }
216 
217 
218 constexpr float64 scale::operator/(scale rhs) const
219 {
220  return level_ == rhs.level_ ? 1.0 :
221  level_ < rhs.level_ ? 0.001*(scale(level_ + 1)/rhs) :
222  1000.0*(scale(level_ - 1)/rhs);
223 }
224 
225 
226 constexpr bool scale::operator==(scale rhs) const
227 {
228  return level_ == rhs.level_;
229 }
230 
231 
232 constexpr bool scale::operator!=(scale rhs) const
233 {
234  return level_ != rhs.level_;
235 }
236 
237 
238 constexpr bool scale::operator<(scale rhs) const
239 {
240  return level_ < rhs.level_;
241 }
242 
243 
244 constexpr bool scale::operator>(scale rhs) const
245 {
246  return level_ > rhs.level_;
247 }
248 
249 
250 constexpr bool scale::operator<=(scale rhs) const
251 {
252  return level_ <= rhs.level_;
253 }
254 
255 
256 constexpr bool scale::operator>=(scale rhs) const
257 {
258  return level_ >= rhs.level_;
259 }
260 
261 
262 constexpr const scale operator+(int64 lhs, scale rhs)
263 {
264  return rhs + lhs;
265 }
266 
267 
268 std::ostream& operator<<(std::ostream& os, const scale& rhs);
269 
270 
271 } // namespace
272 
273 #endif
constexpr bool operator>=(scale rhs) const
Returns true if the scale value is at least rhs.
Definition: scale.h:256
constexpr scale pico
Definition: scale.h:159
arraynd< T, ndims > operator+(const arraynd< T, ndims > &rhs)
Definition: arraynd.h:724
constexpr bool operator<(scale rhs) const
Returns true if the scale value is less than rhs.
Definition: scale.h:238
constexpr scale exa
Definition: scale.h:169
constexpr scale yotta
Definition: scale.h:171
std::ostream & operator<<(std::ostream &os, const arraynd< T, ndims > &rhs)
Definition: arraynd.h:1156
constexpr scale atto
Definition: scale.h:157
constexpr const scale operator+() const
Returns a copy of the scale value.
Definition: scale.h:188
constexpr bool operator<=(scale rhs) const
Returns true if the scale value is at most rhs.
Definition: scale.h:250
scale & operator=(const scale &)=default
Copy assignment.
constexpr scale nano
Definition: scale.h:160
constexpr scale yocto
Definition: scale.h:155
scale & operator++()
Increments (prefix) the level integer.
Definition: scale.cpp:9
constexpr scale no_scale
Definition: scale.h:153
scale & operator--()
Decrements (prefix) the level integer.
Definition: scale.cpp:24
constexpr scale zetta
Definition: scale.h:170
constexpr scale milli
Definition: scale.h:162
constexpr scale peta
Definition: scale.h:168
constexpr bool operator>(scale rhs) const
Returns true if the scale value is greater than rhs.
Definition: scale.h:244
constexpr scale micro
Definition: scale.h:161
constexpr const scale operator-() const
Returns the negation of the scale value.
Definition: scale.h:194
A data type which represents the general concept of scale as a dimensionless power of 1000...
Definition: scale.h:70
constexpr int64 level() const
Returns the level integer.
Definition: scale.h:174
int8 level_type
The type used to store the "level" integer internally.
Definition: scale.h:73
char symbol() const
Provdes the metric prefix symbol.
Definition: scale.cpp:53
scale & operator+=(int64 rhs)
Adds rhs to the level integer.
Definition: scale.cpp:39
constexpr scale tera
Definition: scale.h:167
constexpr scale zepto
Definition: scale.h:156
constexpr bool operator==(scale rhs) const
Returns true if the scale value equals rhs.
Definition: scale.h:226
constexpr float64 operator/(scale rhs) const
Returns 1000 to the power of level() divided by 1000 to the power of rhs.level(). ...
Definition: scale.h:218
constexpr scale giga
Definition: scale.h:166
scale & operator-=(int64 rhs)
Subtracts rhs from the level integer.
Definition: scale.cpp:46
constexpr scale kilo
Definition: scale.h:164
constexpr bool operator!=(scale rhs) const
Returns true if the scale value does not equal rhs.
Definition: scale.h:232
~scale()=default
Destructor.
constexpr scale unit
Definition: scale.h:163
int64_t int64
Definition: number_types.h:14
int8_t int8
Definition: number_types.h:11
constexpr float64 approx() const
Returns 1000 to the power of level(), rounded if necessary.
Definition: scale.h:180
double float64
Definition: number_types.h:22
constexpr scale femto
Definition: scale.h:158
constexpr scale mega
Definition: scale.h:165
constexpr scale()
Constructs a scale value with a level integer of zero.
Definition: scale.h:141