scifir-units 2.0.0
scifir-units is a library of units of measurement, angles, coordinates, fields, and related data, all lightweight, that help in the development of scientific software and scientific machines
Loading...
Searching...
No Matches
prefix.cpp
Go to the documentation of this file.
1#include "./prefix.hpp"
2
3#include <iostream>
4
5using namespace std;
6
7namespace scifir
8{
9 prefix::prefix(): prefix_type(prefix::NONE)
10 {}
11
12 prefix::prefix(const prefix& x) : prefix_type(x.prefix_type)
13 {}
14
15 prefix::prefix(prefix&& x) : prefix_type(std::move(x.prefix_type))
16 {}
17
20
21 prefix::prefix(const string& new_type) : prefix_type(prefix_string(new_type))
22 {}
23
25 {
27 return *this;
28 }
29
31 {
32 prefix_type = std::move(x.prefix_type);
33 return *this;
34 }
35
37 {
38 switch (prefix_type)
39 {
40 case prefix::QUETTA:
41 return 30;
42 case prefix::RONNA:
43 return 27;
44 case prefix::YOTTA:
45 return 24;
46 case prefix::ZETTA:
47 return 21;
48 case prefix::EXA:
49 return 18;
50 case prefix::PETA:
51 return 15;
52 case prefix::TERA:
53 return 12;
54 case prefix::GIGA:
55 return 9;
56 case prefix::MEGA:
57 return 6;
58 case prefix::KILO:
59 return 3;
60 case prefix::HECTO:
61 return 2;
62 case prefix::DECA:
63 return 1;
64 case prefix::NONE:
65 return 0;
66 case prefix::DECI:
67 return -1;
68 case prefix::CENTI:
69 return -2;
70 case prefix::MILLI:
71 return -3;
72 case prefix::MICRO:
73 return -6;
74 case prefix::NANO:
75 return -9;
76 case prefix::PICO:
77 return -12;
78 case prefix::FEMTO:
79 return -15;
80 case prefix::ATTO:
81 return -18;
82 case prefix::ZEPTO:
83 return -21;
84 case prefix::YOCTO:
85 return -24;
86 case prefix::RONTO:
87 return -27;
88 case prefix::QUECTO:
89 return -30;
90 }
91 return 0;
92 }
93
94 string prefix::get_name() const
95 {
96 switch (prefix_type)
97 {
98 case prefix::QUETTA:
99 return "quetta";
100 case prefix::RONNA:
101 return "ronna";
102 case prefix::YOTTA:
103 return "yotta";
104 case prefix::ZETTA:
105 return "zetta";
106 case prefix::EXA:
107 return "exa";
108 case prefix::PETA:
109 return "peta";
110 case prefix::TERA:
111 return "tera";
112 case prefix::GIGA:
113 return "giga";
114 case prefix::MEGA:
115 return "mega";
116 case prefix::KILO:
117 return "kilo";
118 case prefix::HECTO:
119 return "hecto";
120 case prefix::DECA:
121 return "deca";
122 case prefix::NONE:
123 return "";
124 case prefix::DECI:
125 return "deci";
126 case prefix::CENTI:
127 return "centi";
128 case prefix::MILLI:
129 return "milli";
130 case prefix::MICRO:
131 return "micro";
132 case prefix::NANO:
133 return "nano";
134 case prefix::PICO:
135 return "pico";
136 case prefix::FEMTO:
137 return "femto";
138 case prefix::ATTO:
139 return "atto";
140 case prefix::ZEPTO:
141 return "zepto";
142 case prefix::YOCTO:
143 return "yocto";
144 case prefix::RONTO:
145 return "ronto";
146 case prefix::QUECTO:
147 return "quecto";
148 }
149 return "";
150 }
151
152 string prefix::get_symbol() const
153 {
154 switch (prefix_type)
155 {
156 case prefix::QUETTA:
157 return "Q";
158 case prefix::RONNA:
159 return "R";
160 case prefix::YOTTA:
161 return "Y";
162 case prefix::ZETTA:
163 return "Z";
164 case prefix::EXA:
165 return "E";
166 case prefix::PETA:
167 return "P";
168 case prefix::TERA:
169 return "T";
170 case prefix::GIGA:
171 return "G";
172 case prefix::MEGA:
173 return "M";
174 case prefix::KILO:
175 return "k";
176 case prefix::HECTO:
177 return "h";
178 case prefix::DECA:
179 return "da";
180 case prefix::NONE:
181 return "";
182 case prefix::DECI:
183 return "d";
184 case prefix::CENTI:
185 return "c";
186 case prefix::MILLI:
187 return "m";
188 case prefix::MICRO:
189 return "µ";
190 case prefix::NANO:
191 return "n";
192 case prefix::PICO:
193 return "p";
194 case prefix::FEMTO:
195 return "f";
196 case prefix::ATTO:
197 return "a";
198 case prefix::ZEPTO:
199 return "z";
200 case prefix::YOCTO:
201 return "y";
202 case prefix::RONTO:
203 return "r";
204 case prefix::QUECTO:
205 return "q";
206 }
207 return "";
208 }
209
211 {
213 }
214
216 {
217 if (x == "Q")
218 {
219 return prefix::QUETTA;
220 }
221 else if (x == "R")
222 {
223 return prefix::RONNA;
224 }
225 else if (x == "Y")
226 {
227 return prefix::YOTTA;
228 }
229 else if (x == "Z")
230 {
231 return prefix::ZETTA;
232 }
233 else if (x == "E")
234 {
235 return prefix::EXA;
236 }
237 else if (x == "P")
238 {
239 return prefix::PETA;
240 }
241 else if (x == "T")
242 {
243 return prefix::TERA;
244 }
245 else if(x == "G")
246 {
247 return prefix::GIGA;
248 }
249 else if(x == "M")
250 {
251 return prefix::MEGA;
252 }
253 else if(x == "k")
254 {
255 return prefix::KILO;
256 }
257 else if(x == "h")
258 {
259 return prefix::HECTO;
260 }
261 else if(x == "da")
262 {
263 return prefix::DECA;
264 }
265 else if(x == "d")
266 {
267 return prefix::DECI;
268 }
269 else if(x == "c")
270 {
271 return prefix::CENTI;
272 }
273 else if(x == "m")
274 {
275 return prefix::MILLI;
276 }
277 else if(x == "u" or x == "µ")
278 {
279 return prefix::MICRO;
280 }
281 else if(x == "n")
282 {
283 return prefix::NANO;
284 }
285 else if(x == "p")
286 {
287 return prefix::PICO;
288 }
289 else if(x == "f")
290 {
291 return prefix::FEMTO;
292 }
293 else if(x == "a")
294 {
295 return prefix::ATTO;
296 }
297 else if(x == "z")
298 {
299 return prefix::ZEPTO;
300 }
301 else if(x == "y")
302 {
303 return prefix::YOCTO;
304 }
305 else if(x == "r")
306 {
307 return prefix::RONTO;
308 }
309 else if(x == "q")
310 {
311 return prefix::QUECTO;
312 }
313 else if (x == "")
314 {
315 return prefix::NONE;
316 }
317 else
318 {
319 return prefix::NONE;
320 }
321 }
322
329
331 {
332 if (factor == 0)
333 {
334 return prefix(prefix::NONE);
335 }
336 else if (factor == 1)
337 {
338 return prefix(prefix::DECA);
339 }
340 else if (factor == 2)
341 {
342 return prefix(prefix::HECTO);
343 }
344 else if (factor == -1)
345 {
346 return prefix(prefix::DECI);
347 }
348 else if (factor == -2)
349 {
350 return prefix(prefix::CENTI);
351 }
352 else if (factor >= 3 and factor < 6)
353 {
354 return prefix(prefix::KILO);
355 }
356 else if (factor >= 6 and factor < 9)
357 {
358 return prefix(prefix::MEGA);
359 }
360 else if (factor >= 9 and factor < 12)
361 {
362 return prefix(prefix::GIGA);
363 }
364 else if (factor >= 12 and factor < 15)
365 {
366 return prefix(prefix::TERA);
367 }
368 else if (factor >= 15 and factor < 18)
369 {
370 return prefix(prefix::PETA);
371 }
372 else if (factor >= 18 and factor < 21)
373 {
374 return prefix(prefix::EXA);
375 }
376 else if (factor >= 21 and factor < 24)
377 {
378 return prefix(prefix::ZETTA);
379 }
380 else if (factor >= 24 and factor < 27)
381 {
382 return prefix(prefix::YOTTA);
383 }
384 else if (factor >= 27 and factor < 30)
385 {
386 return prefix(prefix::RONNA);
387 }
388 else if (factor >= 30)
389 {
390 return prefix(prefix::QUETTA);
391 }
392 else if (factor <= -3 and factor > -6)
393 {
394 return prefix(prefix::MILLI);
395 }
396 else if (factor <= -6 and factor > -9)
397 {
398 return prefix(prefix::MICRO);
399 }
400 else if (factor <= -9 and factor > -12)
401 {
402 return prefix(prefix::NANO);
403 }
404 else if (factor <= -12 and factor > -15)
405 {
406 return prefix(prefix::PICO);
407 }
408 else if (factor <= -15 and factor > -18)
409 {
410 return prefix(prefix::FEMTO);
411 }
412 else if (factor <= -18 and factor > -21)
413 {
414 return prefix(prefix::ATTO);
415 }
416 else if (factor <= -21 and factor > -24)
417 {
418 return prefix(prefix::ZEPTO);
419 }
420 else if (factor <= -24 and factor > -27)
421 {
422 return prefix(prefix::YOCTO);
423 }
424 else if (factor <= -27 and factor > -30)
425 {
426 return prefix(prefix::RONTO);
427 }
428 else if (factor <= -30)
429 {
430 return prefix(prefix::QUECTO);
431 }
432 return prefix();
433 }
434}
435
437{
439}
440
442{
443 return !(x == y);
444}
445
447{
449}
450
452{
454}
455
457{
459}
460
461ostream& operator <<(ostream& os, const scifir::prefix& x)
462{
463 return os << x.get_symbol();
464}
Class that represents prefixes of the SI system of units. Each prefix sizes 1 byte....
Definition prefix.hpp:14
int get_conversion_factor() const
Returns the conversion factor of the prefix given the prefix_type. It uses the SI system of units for...
Definition prefix.cpp:36
prefix::type prefix_type
It stores the prefix type currently being used.
Definition prefix.hpp:33
prefix()
Default constructor. The prefix_type is set to NONE.
Definition prefix.cpp:9
string get_symbol() const
Symbol of the prefix given the prefix_type. The symbol of micro is supported in his Unicode version.
Definition prefix.cpp:152
string get_name() const
Name of the prefix given the prefix_type.
Definition prefix.cpp:94
prefix & operator=(const prefix &x)
Copy assignment. The prefix_type is copied from the prefix x.
Definition prefix.cpp:24
bool operator<(const scifir::prefix &x) const
This operator allows to order the prefixes from the lower to the greather.
Definition prefix.cpp:210
type
Represents a prefix of the SI system of units. All the prefixes of the SI system of units are support...
Definition prefix.hpp:16
@ ATTO
Prefix of a factor of 10^-18.
Definition prefix.hpp:16
@ YOCTO
Prefix of a factor of 10^-24.
Definition prefix.hpp:16
@ RONNA
Prefix of a factor of 10^27.
Definition prefix.hpp:16
@ NANO
Prefix of a factor of 10^-9.
Definition prefix.hpp:16
@ MILLI
Prefix of a factor of 10^-3.
Definition prefix.hpp:16
@ NONE
There is no prefix. Then, the dimension is not increased or decreased by some factor.
Definition prefix.hpp:16
@ EXA
Prefix of a factor of 10^18.
Definition prefix.hpp:16
@ QUETTA
Prefix of a factor of 10^30.
Definition prefix.hpp:16
@ KILO
Prefix of a factor of 10^3.
Definition prefix.hpp:16
@ MEGA
Prefix of a factor of 10^6.
Definition prefix.hpp:16
@ HECTO
Prefix of a factor of 10^2.
Definition prefix.hpp:16
@ CENTI
Prefix of a factor of 10^-2.
Definition prefix.hpp:16
@ PETA
Prefix of a factor of 10^15.
Definition prefix.hpp:16
@ RONTO
Prefix of a factor of 10^-27.
Definition prefix.hpp:16
@ DECA
Prefix of a factor of 10^1.
Definition prefix.hpp:16
@ ZEPTO
Prefix of a factor of 10^-21.
Definition prefix.hpp:16
@ YOTTA
Prefix of a factor of 10^24.
Definition prefix.hpp:16
@ FEMTO
Prefix of a factor of 10^-15.
Definition prefix.hpp:16
@ DECI
Prefix of a factor of 10^-1.
Definition prefix.hpp:16
@ MICRO
Prefix of a factor of 10^-6.
Definition prefix.hpp:16
@ GIGA
Prefix of a factor of 10^9.
Definition prefix.hpp:16
@ QUECTO
Prefix of a factor of 10^-30.
Definition prefix.hpp:16
@ PICO
Prefix of a factor of 10^-12.
Definition prefix.hpp:16
@ ZETTA
Prefix of a factor of 10^21.
Definition prefix.hpp:16
@ TERA
Prefix of a factor of 10^12.
Definition prefix.hpp:16
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
@ NONE
No predefined astronomical body selected.
prefix::type prefix_string(const string &x)
Returns the value of the enum prefix::type associated with the string x given.
Definition prefix.cpp:215
prefix closest_prefix(const prefix &actual_prefix, int actual_scale)
Returns the closes prefix related to the scale of the current value. It is used when displaying a sca...
Definition prefix.cpp:323
prefix create_prefix_by_factor(int factor)
Creates the prefix of the factor given, which is always between a range.
Definition prefix.cpp:330
bool operator!=(const scifir::prefix &x, const scifir::prefix &y)
Comparison operator. Compares if two prefixes are different, that's, if they don't have the same pref...
Definition prefix.cpp:441
bool operator<=(const scifir::prefix &x, const scifir::prefix &y)
Checks if prefix x is of a lower or equal factor than prefix y.
Definition prefix.cpp:446
bool operator>=(const scifir::prefix &x, const scifir::prefix &y)
Checks if prefix x is of an equal or greather factor than prefix y.
Definition prefix.cpp:456
bool operator>(const scifir::prefix &x, const scifir::prefix &y)
Checks if prefix x is of a greather factor than prefix y.
Definition prefix.cpp:451
ostream & operator<<(ostream &os, const scifir::prefix &x)
Adds the string representation of the prefix x to an output stream.
Definition prefix.cpp:461
bool operator==(const scifir::prefix &x, const scifir::prefix &y)
Comparison operator. Compares if two prefixes are the same, that's, if they have the same prefix::typ...
Definition prefix.cpp:436