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
percentage.cpp
Go to the documentation of this file.
1#include "./percentage.hpp"
2
3#include "../util/types.hpp"
4
5#include "boost/algorithm/string.hpp"
6
7#include <cmath>
8#include <iostream>
9#include <sstream>
10#include <string>
11
12using namespace std;
13
14namespace scifir
15{
16 percentage::percentage() : value(0.0f)
17 {}
18
19 percentage::percentage(const percentage& x) : value(x.get_value())
20 {}
21
22 percentage::percentage(percentage&& x) : value(std::move(x.get_value()))
23 {}
24
26 {
28 {
30 }
31 else if (init_type == percentage::RATIO)
32 {
33 value = new_value * 100.0f;
34 }
36 {
37 value = new_value / 10000.0f;
38 }
39 else
40 {
41 value = 0.0f;
42 }
43 }
44
46 {
48 {
50 }
51 else if (init_type == percentage::RATIO)
52 {
53 value = float(new_value) * 100.0f;
54 }
56 {
57 value = float(new_value) / 10000.0f;
58 }
59 else
60 {
61 value = 0.0f;
62 }
63 }
64
66 {
68 {
70 }
71 else if (init_type == percentage::RATIO)
72 {
73 value = float(new_value) * 100.0f;
74 }
76 {
77 value = float(new_value) / 10000.0f;
78 }
79 else
80 {
81 value = 0.0f;
82 }
83 }
84
86 {
88 {
90 }
92 {
93 value = float(new_value) / 10000.0f;
94 }
95 else
96 {
97 value = 0.0f;
98 }
99 }
100
102 {
103 if (init_type == "%")
104 {
106 }
107 else if (init_type == "ppm")
108 {
109 value = new_value / 10000.0f;
110 }
111 else
112 {
113 value = 0.0f;
114 }
115 /*else if (init_type == "ppb")
116 {
117 value = new_value / 10000000.0f;
118 }
119 else if (init_type == "ppt")
120 {
121 value = new_value / 10000000000.0f;
122 }
123 else if (init_type == "ppq")
124 {
125 value = new_value / 10000000000000.0f;
126 }*/
127 }
128
130 {
131 if (init_type == "%")
132 {
134 }
135 else if (init_type == "ppm")
136 {
137 value = float(new_value) / 10000.0f;
138 }
139 else
140 {
141 value = 0.0f;
142 }
143 /*else if (init_type == "ppb")
144 {
145 value = float(new_value) / 10000000.0f;
146 }
147 else if (init_type == "ppt")
148 {
149 value = float(new_value) / 10000000000.0f;
150 }
151 else if (init_type == "ppq")
152 {
153 value = float(new_value) / 10000000000000.0f;
154 }*/
155 }
156
157 percentage::percentage(long double new_value,const string& init_type)
158 {
159 if (init_type == "%")
160 {
162 }
163 else if (init_type == "ppm")
164 {
165 value = float(new_value) / 10000.0f;
166 }
167 else
168 {
169 value = 0.0f;
170 }
171 /*else if (init_type == "ppb")
172 {
173 value = float(new_value) / 10000000.0f;
174 }
175 else if (init_type == "ppt")
176 {
177 value = float(new_value) / 10000000000.0f;
178 }
179 else if (init_type == "ppq")
180 {
181 value = float(new_value) / 10000000000000.0f;
182 }*/
183 }
184
186 {
187 if (init_type == "%")
188 {
190 }
191 else if (init_type == "ppm")
192 {
193 value = float(new_value) / 10000.0f;
194 }
195 else
196 {
197 value = 0.0f;
198 }
199 /*else if (init_type == "ppb")
200 {
201 value = float(new_value) / 10000000.0f;
202 }
203 else if (init_type == "ppt")
204 {
205 value = float(new_value) / 10000000000.0f;
206 }
207 else if (init_type == "ppq")
208 {
209 value = float(new_value) / 10000000000000.0f;
210 }*/
211 }
212
217
219 {
220 if (x.has_empty_dimensions())
221 {
222 value = float(x);
223 }
224 else
225 {
226 cerr << "A percentage cannot be initialized with dimensions" << endl;
227 value = 0.0f;
228 }
229 }
230
232 {
233 value = x.get_value();
234 return *this;
235 }
236
238 {
239 value = std::move(x.get_value());
240 return *this;
241 }
242
244 {
246 return *this;
247 }
248
250 {
252 return *this;
253 }
254
256 {
258 return *this;
259 }
260
262 {
264 return *this;
265 }
266
272
274 {
275 if (x.has_empty_dimensions())
276 {
277 value = x.get_value();
278 }
279 else
280 {
281 cerr << "A percentage cannot be initialized with dimensions" << endl;
282 value = 0.0f;
283 }
284 return *this;
285 }
286
288 {
289 return percentage(value + x.get_value());
290 }
291
293 {
294 return percentage(value - x.get_value());
295 }
296
298 {
299 return percentage(value * x.get_value() / 100.0f);
300 }
301
303 {
304 return percentage(100.0f * value / x.get_value());
305 }
306
308 {
309 value += x.get_value();
310 }
311
313 {
314 value -= x.get_value();
315 }
316
318 {
319 value *= (x.get_value() / 100.0f);
320 }
321
323 {
324 value *= 100.0f / x.get_value();
325 }
326
328 {
329 value++;
330 return *this;
331 }
332
334 {
335 percentage tmp = percentage(*this);
336 operator ++();
337 return tmp;
338 }
339
341 {
342 value--;
343 return *this;
344 }
345
347 {
348 percentage tmp = percentage(*this);
349 operator --();
350 return tmp;
351 }
352
354 {
355 return get_factor() * x;
356 }
357
359 {
360 return get_factor() / x;
361 }
362
364 {
365 return value / 100.0f;
366 }
367
369 {
370 return value * 10000.0f;
371 }
372
373 /*float percentage::get_ppb() const
374 {
375 return value * 10000000.0f;
376 }
377
378 float percentage::get_ppt() const
379 {
380 return value * 10000000000.0f;
381 }
382
383 float percentage::get_ppq() const
384 {
385 return value * 10000000000000.0f;
386 }*/
387
389 {
392 return out.str();
393 }
394
396 {
398 out << display_float(get_ppm()) << " ppm";
399 return out.str();
400 }
401
402 /*string percentage::display_ppb() const
403 {
404 ostringstream out;
405 out << display_float(get_ppb()) << " ppb";
406 return out.str();
407 }
408
409 string percentage::display_ppt() const
410 {
411 ostringstream out;
412 out << display_float(get_ppt()) << " ppt";
413 return out.str();
414 }
415
416 string percentage::display_ppq() const
417 {
418 ostringstream out;
419 out << display_float(get_ppq()) << " ppq";
420 return out.str();
421 }*/
422
424 {
425 if (init_percentage[init_percentage.length() - 1] == '%')
426 {
427 value = stof(init_percentage.substr(0,init_percentage.length() - 1));
428 }
429 else if (init_percentage.find(" ") != string::npos)
430 {
431 string percentage_unit = init_percentage.substr(init_percentage.length() - 4,4);
432 if (percentage_unit == " ppm")
433 {
434 value = stof(init_percentage.substr(0,init_percentage.length() - 4)) / 10000.0f;
435 }
436 else
437 {
438 value = 0.0f;
439 }
440 /*else if (percentage_unit == " ppb")
441 {
442 value = stof(init_percentage.substr(0,init_percentage.length() - 4)) / 10000000.0f;
443 }
444 else if (percentage_unit == " ppt")
445 {
446 value = stof(init_percentage.substr(0,init_percentage.length() - 4)) / 10000000000.0f;
447 }
448 else if (percentage_unit == " ppq")
449 {
450 value = stof(init_percentage.substr(0,init_percentage.length() - 4)) / 10000000000000.0f;
451 }*/
452 }
453 else if (init_percentage.length() > 3)
454 {
455 string percentage_unit = init_percentage.substr(init_percentage.length() - 3,3);
456 if (percentage_unit == "ppm")
457 {
458 value = stof(init_percentage.substr(0,init_percentage.length() - 3)) / 10000.0f;
459 }
460 /*else if (percentage_unit == "ppb")
461 {
462 value = stof(init_percentage.substr(0,init_percentage.length() - 3)) / 10000000.0f;
463 }
464 else if (percentage_unit == "ppt")
465 {
466 value = stof(init_percentage.substr(0,init_percentage.length() - 3)) / 10000000000.0f;
467 }
468 else if (percentage_unit == "ppq")
469 {
470 value = stof(init_percentage.substr(0,init_percentage.length() - 3)) / 10000000000000.0f;
471 }*/
472 else
473 {
474 value = 0.0f;
475 }
476 }
477 else
478 {
479 value = 0.0f;
480 }
481 }
482
483 string to_string(const percentage& x)
484 {
485 return x.display_percentage();
486 }
487
488 bool is_percentage(const string& init_percentage)
489 {
490 int iteration_limit;
491 if (init_percentage.back() == '%')
492 {
493 iteration_limit = int(init_percentage.length()) - 1;
494 }
495 else
496 {
497 string percentage_unit = init_percentage.substr(init_percentage.length() - 4,4);
498 if (percentage_unit == " ppm"/* or percentage_unit == " ppb" or percentage_unit == " ppt" or percentage_unit == " ppq"*/)
499 {
500 iteration_limit = int(init_percentage.length()) - 4;
501 }
502 else
503 {
504 percentage_unit = init_percentage.substr(init_percentage.length() - 3,3);
505 if (percentage_unit == "ppm"/* or percentage_unit == "ppb" or percentage_unit == "ppt" or percentage_unit == "ppq"*/)
506 {
507 iteration_limit = int(init_percentage.length()) - 3;
508 }
509 else
510 {
511 return false;
512 }
513 }
514 }
515 bool dot_present = false;
516 for (int i = 0; i < iteration_limit; i++)
517 {
518 if (init_percentage[i] == '.')
519 {
520 if (dot_present)
521 {
522 return false;
523 }
524 else
525 {
526 dot_present = true;
527 }
528 }
529 else if (!std::isdigit(init_percentage[i]))
530 {
531 return false;
532 }
533 }
534 return true;
535 }
536}
537
539{
540 return x * y.get_factor();
541}
542
544{
545 return x / y.get_factor();
546}
547
549{
550 if(x.get_value() == y.get_value())
551 {
552 return true;
553 }
554 else
555 {
556 return false;
557 }
558}
559
561{
562 return !(x == y);
563}
564
566{
567 if(x.get_value() < y.get_value())
568 {
569 return true;
570 }
571 else
572 {
573 return false;
574 }
575}
576
578{
579 if(x.get_value() > y.get_value())
580 {
581 return true;
582 }
583 else
584 {
585 return false;
586 }
587}
588
590{
591 return !(x > y);
592}
593
595{
596 return !(x < y);
597}
598
599bool operator ==(const scifir::percentage& x, const string& init_percentage)
600{
601 scifir::percentage y = scifir::percentage(init_percentage);
602 return (x == y);
603}
604
605bool operator !=(const scifir::percentage& x, const string& init_percentage)
606{
607 return !(x == init_percentage);
608}
609
610bool operator ==(const string& init_percentage, const scifir::percentage& x)
611{
612 scifir::percentage y = scifir::percentage(init_percentage);
613 return (x == y);
614}
615
616bool operator !=(const string& init_percentage, const scifir::percentage& x)
617{
618 return !(init_percentage == x);
619}
620
621void operator +=(string& x, const scifir::percentage& y)
622{
623 ostringstream output;
624 output << y;
625 x += output.str();
626}
627
628string operator +(const string& x, const scifir::percentage& y)
629{
630 ostringstream output;
631 output << x;
632 output << y;
633 return output.str();
634}
635
636string operator +(const scifir::percentage& y, const string& x)
637{
638 ostringstream output;
639 output << y;
640 output << x;
641 return output.str();
642}
643
644ostream& operator <<(ostream& os, const scifir::percentage& x)
645{
646 return os << to_string(x);
647}
648
649istream& operator >>(istream& is, scifir::percentage& x)
650{
651 char a[256];
652 is.getline(a, 256);
653 string b(a);
654 boost::trim(b);
655 x = scifir::percentage(b);
656 return is;
657}
Class that allows to handle percentages and do calculations with it easy. It supports normal percenta...
percentage operator*(const percentage &x) const
Creates a new percentage as the multiplication of two percentages.
percentage operator/(const percentage &x) const
Creates a new percentage as the division of two percentages.
void initialize_from_string(const string &init_percentage)
Internal function. Initializes the member-variables with an initialization string of percentage.
percentage operator+(const percentage &x) const
Creates a new percentage as the addition of two percentages.
float get_factor() const
Returns the factor of the percentage, not his value. It equals is value divided by 100.
percentage()
Default constructor. The value is 0.
percentage & operator=(const percentage &x)
Copy assignment. The value is copied from the percentage x.
percentage & operator--()
Decrements the value by one.
type
Type of percentage.
@ PARTS_PER_MILLION
Percentage, with the ppm symbol. Commonly used in chemistry, it represents 1 in 10^6.
@ RATIO
The normal percentage obtained by a direct ratio of some proportion, you don't need to multiply by 10...
@ PERCENTAGE
Normal percentage, with the % symbol.
void operator+=(const percentage &x)
Sums the value with the value of the percentage x.
string display_ppm() const
Displays the percentage as ppm, with the ppm symbol.
float get_ppm() const
Returns the factor of the percentage in ppm, not his value. It equals is value multiplied by 10,...
string display_percentage(int number_of_decimals=2) const
Displays the percentage as normal percentage, with the % symbol.
void operator*=(const percentage &x)
Multiplies the value with the value of the percentage x.
const float & get_value() const
It returns the value.
percentage operator-(const percentage &x) const
Creates a new percentage as the difference of two percentages.
float value
The value of the percentage, in numbers related to a 100%. A percentage of 30% has a value of 30,...
void operator/=(const percentage &x)
Divides the value with the value of the percentage x.
void operator-=(const percentage &x)
Substracts the value with the value of the percentage x.
percentage & operator++()
Increments the value by one.
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions....
bool has_empty_dimensions() const
Checks if there aren't base dimensions.
const float & get_value() const
Read-only getter of the value.
The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
Definition address.cpp:6
string to_string(const aid &x)
Creates a string representation of aid, it's for aid equivalent to the display() function of aid.
Definition aid.cpp:582
string display_float(const float &value, int number_of_decimals)
Definition types.cpp:36
bool is_percentage(const string &init_percentage)
Checks if a string is an initialization string of percentage.
bool operator==(const scifir::percentage &x, const scifir::percentage &y)
Returns true if the value of percentage x is equal to the value of percentage y.
string operator+(const string &x, const scifir::percentage &y)
Concatenates x with the string representation of percentage y.
bool operator<(const scifir::percentage &x, const scifir::percentage &y)
Returns true if the value of percentage x is lower than the value of percentage y.
bool operator!=(const scifir::percentage &x, const scifir::percentage &y)
Returns true if the value of percentage x is not equal to the value of percentage y.
ostream & operator<<(ostream &os, const scifir::percentage &x)
Adds the string representation of the percentage x to an output stream os.
scifir::scalar_unit operator/(const scifir::scalar_unit &x, const scifir::percentage &y)
Multiplies the scalar_unit x by the percentage equal to the inverse of percentage y.
bool operator<=(const scifir::percentage &x, const scifir::percentage &y)
Returns true if the value of percentage x is lower or equal than the value of percentage y.
void operator+=(string &x, const scifir::percentage &y)
Concatenates the string representation of percentage y to the string x.
istream & operator>>(istream &is, scifir::percentage &x)
Allows that an istream initializes by string a percentage x.
bool operator>(const scifir::percentage &x, const scifir::percentage &y)
Returns true if the value of percentage x is greather than the value of percentage y.
scifir::scalar_unit operator*(const scifir::scalar_unit &x, const scifir::percentage &y)
Multiplies the percentage with the scalar_unit x to create a percentage of that scalar_unit.
bool operator>=(const scifir::percentage &x, const scifir::percentage &y)
Returns true if the value of percentage x is equal or greather than the value of percentage y.