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.hpp
Go to the documentation of this file.
1#ifndef SCIFIR_UNITS_SPECIAL_UNITS_PERCENTAGE_HPP_INCLUDED
2#define SCIFIR_UNITS_SPECIAL_UNITS_PERCENTAGE_HPP_INCLUDED
3
4#include "../util/is_number.hpp"
5#include "../units/scalar_unit.hpp"
6
7#include "boost/math/constants/constants.hpp"
8
9#include <cmath>
10#include <string>
11
12using namespace std;
13
14namespace scifir
15{
17 {
18 public:
20
21 percentage();
22 percentage(const percentage& x);
28 explicit percentage(float new_value,const string& init_type);
29 explicit percentage(double new_value,const string& init_type);
30 explicit percentage(long double new_value,const string& init_type);
31 explicit percentage(int new_value,const string& init_type);
32 explicit percentage(const string& init_percentage);
33 explicit percentage(const scalar_unit& x);
34
39 percentage& operator =(long double new_value);
43
44 explicit operator float() const
45 {
46 return float(value);
47 }
48
49 inline const float& get_value() const
50 {
51 return value;
52 }
53
54 percentage operator +(const percentage& x) const;
55 percentage operator -(const percentage& x) const;
56 percentage operator *(const percentage& x) const;
57 percentage operator /(const percentage& x) const;
58 void operator +=(const percentage& x);
59 void operator -=(const percentage& x);
60 void operator *=(const percentage& x);
61 void operator /=(const percentage& x);
62
64 void operator =(T x)
65 {
66 value = x;
67 }
68
71 {
72 return percentage(value + x);
73 }
74
77 {
78 return percentage(value - x);
79 }
80
82 float operator *(T x) const
83 {
84 return float(value * x / 100);
85 }
86
88 float operator /(T x) const
89 {
90 return float(value / (100 * x));
91 }
92
94 void operator +=(T x)
95 {
96 value += x;
97 }
98
101 {
102 value -= x;
103 }
104
107 {
108 value *= x;
109 }
110
113 {
114 value /= x;
115 }
116
121
122 scalar_unit operator *(const scalar_unit& x) const;
123 scalar_unit operator /(const scalar_unit& x) const;
124
125 float get_factor() const;
126
127 float get_ppm() const;
128 /*float get_ppb() const;
129 float get_ppt() const;
130 float get_ppq() const;*/
131
132 string display_percentage(int number_of_decimals = 2) const;
133 string display_ppm() const;
134 /*string display_ppb() const;
135 string display_ppt() const;
136 string display_ppq() const;*/
137
138 private:
139 float value;
140
141 void initialize_from_string(const string& init_percentage);
142
143 };
144
145 string to_string(const percentage& x);
146 bool is_percentage(const string& init_percentage);
147}
148
149template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
154
155template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
160
161template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
162float operator *(T x, const scifir::percentage& y)
163{
164 return float(x * y.get_value() / 100);
165}
166
167template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
168float operator /(T x, const scifir::percentage& y)
169{
170 return float(100 * x / y.get_value());
171}
172
173template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
175{
176 return (x == y.get_value());
177}
178
179template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
181{
182 return (x != y.get_value());
183}
184
185template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
187{
188 return (x < y.get_value());
189}
190
191template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
193{
194 return (x > y.get_value());
195}
196
197template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
199{
200 return (x <= y.get_value());
201}
202
203template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
205{
206 return (x >= y.get_value());
207}
208
209template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
211{
212 return (x == y.get_value());
213}
214
215template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
217{
218 return (x != y.get_value());
219}
220
221template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
223{
224 return (y.get_value() < x);
225}
226
227template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
229{
230 return (y.get_value() > x);
231}
232
233template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
235{
236 return (y.get_value() <= x);
237}
238
239template<typename T, typename = typename enable_if<scifir::is_number<T>::value>::type>
241{
242 return (y.get_value() >= x);
243}
244
247
248bool operator ==(const scifir::percentage& x, const scifir::percentage& y);
249bool operator !=(const scifir::percentage& x, const scifir::percentage& y);
250bool operator <(const scifir::percentage& x, const scifir::percentage& y);
251bool operator >(const scifir::percentage& x, const scifir::percentage& y);
252bool operator <=(const scifir::percentage& x, const scifir::percentage& y);
253bool operator >=(const scifir::percentage& x, const scifir::percentage& y);
254
255bool operator ==(const scifir::percentage& x, const string& init_percentage);
256bool operator !=(const scifir::percentage& x, const string& init_percentage);
257
258bool operator ==(const string& init_percentage, const scifir::percentage& x);
259bool operator !=(const string& init_percentage, const scifir::percentage& x);
260
261void operator +=(string& x, const scifir::percentage& y);
262string operator +(const string& x, const scifir::percentage& y);
263string operator +(const scifir::percentage& y, const string& x);
264
265ostream& operator <<(ostream& os, const scifir::percentage& x);
266istream& operator >>(istream& is, scifir::percentage& x);
267
268#endif // SCIFIR_UNITS_SPECIAL_UNITS_PERCENTAGE_HPP_INCLUDED
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...
@ PARTS_PER_TRILLION
Percentage, with the ppt symbol. Commonly used in chemistry, it represents 1 in 10^12.
@ PERCENTAGE
Normal percentage, with the % symbol.
@ PARTS_PER_QUATRILLION
Percentage, with the ppq symbol. Commonly used in chemistry, it represents 1 in 10^15.
@ PARTS_PER_BILLION
Percentage, with the ppb symbol. Commonly used in chemistry, it represents 1 in 10^9.
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....
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
bool is_percentage(const string &init_percentage)
Checks if a string is an initialization string of percentage.
float operator*(T x, const scifir::percentage &y)
Creates a new percentage as the multiplication of the percentage with the numeric type x.
bool operator!=(T x, const scifir::percentage &y)
Returns true if the value of percentage is not equal to the numeric type x.
bool operator<(T x, const scifir::percentage &y)
Returns true if the numeric type x is lower than the value of percentage y.
scifir::percentage operator-(T x, const scifir::percentage &y)
Creates a new percentage as the difference of the percentage with the numeric type x.
bool operator>(T x, const scifir::percentage &y)
Returns true if the numeric type x is greather than the value of percentage y.
bool operator<=(T x, const scifir::percentage &y)
Returns true if the numeric type x is lower or equal than the value of percentage y.
bool operator>=(T x, const scifir::percentage &y)
Returns true if the numeric type x is equal or greather than the value of percentage y.
scifir::percentage operator+(T x, const scifir::percentage &y)
Creates a new percentage as the addition of the percentage with the numeric type x.
float operator/(T x, const scifir::percentage &y)
Creates a new percentage as the division of the percentage with the numeric type x.
ostream & operator<<(ostream &os, const scifir::percentage &x)
Adds the string representation of the percentage x to an output stream os.
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==(T x, const scifir::percentage &y)
Returns true if the value of percentage is equal to the numeric type x.