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
Classes | Namespaces | Functions
percentage.hpp File Reference
#include "../util/is_number.hpp"
#include "../units/scalar_unit.hpp"
#include "boost/math/constants/constants.hpp"
#include <cmath>
#include <string>
Include dependency graph for percentage.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  scifir::percentage
 Class that allows to handle percentages and do calculations with it easy. It supports normal percentages (with %), ppm, ppb, ppt and ppq. The types ppb, ppt and ppq are currently not supported because the float type in percentage class can't store so lower values. More...
 

Namespaces

namespace  scifir
 The namespace scifir contains all scifir-units, excepting the string literals, which are outside.
 

Functions

string scifir::to_string (const percentage &x)
 Returns a string representation of percentage x.
 
bool scifir::is_percentage (const string &init_percentage)
 Checks if a string is an initialization string of percentage.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
scifir::percentage operator+ (T x, const scifir::percentage &y)
 Creates a new percentage as the addition of the percentage with the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
scifir::percentage operator- (T x, const scifir::percentage &y)
 Creates a new percentage as the difference of the percentage with the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
float operator* (T x, const scifir::percentage &y)
 Creates a new percentage as the multiplication of the percentage with the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
float operator/ (T x, const scifir::percentage &y)
 Creates a new percentage as the division of the percentage with the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator== (T x, const scifir::percentage &y)
 Returns true if the value of percentage is equal to the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator!= (T x, const scifir::percentage &y)
 Returns true if the value of percentage is not equal to the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator< (T x, const scifir::percentage &y)
 Returns true if the numeric type x is lower than the value of percentage y.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator> (T x, const scifir::percentage &y)
 Returns true if the numeric type x is greather than the value of percentage y.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
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.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
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.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator== (const scifir::percentage &y, T x)
 Returns true if the value of percentage is equal to the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator!= (const scifir::percentage &y, T x)
 Returns true if the value of percentage is not equal to the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator< (const scifir::percentage &y, T x)
 Returns true if the value of percentage y is lower than the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator> (const scifir::percentage &y, T x)
 Returns true if the value of percentage y is greather than the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator<= (const scifir::percentage &y, T x)
 Returns true if the value of percentage y is lower or equal than the numeric type x.
 
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator>= (const scifir::percentage &y, T x)
 Returns true if the value of percentage y is equal or greather than the numeric type x.
 
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.
 
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 equal to 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.
 
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 greather 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 lower or equal 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 equal or greather than the value of percentage y.
 
bool operator== (const scifir::percentage &x, const string &init_percentage)
 Returns true if x and the percentage constructed with init_percentage are equal.
 
bool operator!= (const scifir::percentage &x, const string &init_percentage)
 Returns true if x and the percentage constructed with init_percentage are different.
 
bool operator== (const string &init_percentage, const scifir::percentage &x)
 Returns true if x and the percentage constructed with init_percentage are equal.
 
bool operator!= (const string &init_percentage, const scifir::percentage &x)
 Returns true if x and the percentage constructed with init_percentage are different.
 
void operator+= (string &x, const scifir::percentage &y)
 Concatenates the string representation of percentage y to the string x.
 
string operator+ (const string &x, const scifir::percentage &y)
 Concatenates x with the string representation of percentage y.
 
string operator+ (const scifir::percentage &y, const string &x)
 Concatenates x with the string representation of percentage y.
 
ostream & operator<< (ostream &os, const scifir::percentage &x)
 Adds the string representation of the percentage x to an output stream os.
 
istream & operator>> (istream &is, scifir::percentage &x)
 Allows that an istream initializes by string a percentage x.
 

Function Documentation

◆ operator!=() [1/5]

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.

Definition at line 560 of file percentage.cpp.

561{
562 return !(x == y);
563}

◆ operator!=() [2/5]

bool operator!= ( const scifir::percentage x,
const string &  init_percentage 
)

Returns true if x and the percentage constructed with init_percentage are different.

Definition at line 605 of file percentage.cpp.

606{
607 return !(x == init_percentage);
608}

◆ operator!=() [3/5]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator!= ( const scifir::percentage y,
x 
)

Returns true if the value of percentage is not equal to the numeric type x.

Definition at line 216 of file percentage.hpp.

217{
218 return (x != y.get_value());
219}
const float & get_value() const
It returns the value.

◆ operator!=() [4/5]

bool operator!= ( const string &  init_percentage,
const scifir::percentage x 
)

Returns true if x and the percentage constructed with init_percentage are different.

Definition at line 616 of file percentage.cpp.

617{
618 return !(init_percentage == x);
619}

◆ operator!=() [5/5]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator!= ( x,
const scifir::percentage y 
)

Returns true if the value of percentage is not equal to the numeric type x.

Definition at line 180 of file percentage.hpp.

181{
182 return (x != y.get_value());
183}

◆ operator*() [1/2]

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.

Definition at line 538 of file percentage.cpp.

539{
540 return x * y.get_factor();
541}
float get_factor() const
Returns the factor of the percentage, not his value. It equals is value divided by 100.

◆ operator*() [2/2]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
float operator* ( x,
const scifir::percentage y 
)

Creates a new percentage as the multiplication of the percentage with the numeric type x.

Definition at line 162 of file percentage.hpp.

163{
164 return float(x * y.get_value() / 100);
165}

◆ operator+() [1/3]

string operator+ ( const scifir::percentage y,
const string &  x 
)

Concatenates x with the string representation of percentage y.

Definition at line 636 of file percentage.cpp.

637{
638 ostringstream output;
639 output << y;
640 output << x;
641 return output.str();
642}

◆ operator+() [2/3]

string operator+ ( const string &  x,
const scifir::percentage y 
)

Concatenates x with the string representation of percentage y.

Definition at line 628 of file percentage.cpp.

629{
630 ostringstream output;
631 output << x;
632 output << y;
633 return output.str();
634}

◆ operator+() [3/3]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
scifir::percentage operator+ ( x,
const scifir::percentage y 
)

Creates a new percentage as the addition of the percentage with the numeric type x.

Definition at line 150 of file percentage.hpp.

151{
152 return scifir::percentage(x + y.get_value());
153}
Class that allows to handle percentages and do calculations with it easy. It supports normal percenta...

◆ operator+=()

void operator+= ( string &  x,
const scifir::percentage y 
)

Concatenates the string representation of percentage y to the string x.

Definition at line 621 of file percentage.cpp.

622{
623 ostringstream output;
624 output << y;
625 x += output.str();
626}

◆ operator-()

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
scifir::percentage operator- ( x,
const scifir::percentage y 
)

Creates a new percentage as the difference of the percentage with the numeric type x.

Definition at line 156 of file percentage.hpp.

157{
158 return scifir::percentage(x - y.get_value());
159}

◆ operator/() [1/2]

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.

Definition at line 543 of file percentage.cpp.

544{
545 return x / y.get_factor();
546}

◆ operator/() [2/2]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
float operator/ ( x,
const scifir::percentage y 
)

Creates a new percentage as the division of the percentage with the numeric type x.

Definition at line 168 of file percentage.hpp.

169{
170 return float(100 * x / y.get_value());
171}

◆ operator<() [1/3]

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.

Definition at line 565 of file percentage.cpp.

566{
567 if(x.get_value() < y.get_value())
568 {
569 return true;
570 }
571 else
572 {
573 return false;
574 }
575}

◆ operator<() [2/3]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator< ( const scifir::percentage y,
x 
)

Returns true if the value of percentage y is lower than the numeric type x.

Definition at line 222 of file percentage.hpp.

223{
224 return (y.get_value() < x);
225}

◆ operator<() [3/3]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator< ( x,
const scifir::percentage y 
)

Returns true if the numeric type x is lower than the value of percentage y.

Definition at line 186 of file percentage.hpp.

187{
188 return (x < y.get_value());
189}

◆ operator<<()

ostream & operator<< ( ostream &  os,
const scifir::percentage x 
)

Adds the string representation of the percentage x to an output stream os.

Definition at line 644 of file percentage.cpp.

645{
646 return os << to_string(x);
647}
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

◆ operator<=() [1/3]

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.

Definition at line 589 of file percentage.cpp.

590{
591 return !(x > y);
592}

◆ operator<=() [2/3]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator<= ( const scifir::percentage y,
x 
)

Returns true if the value of percentage y is lower or equal than the numeric type x.

Definition at line 234 of file percentage.hpp.

235{
236 return (y.get_value() <= x);
237}

◆ operator<=() [3/3]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator<= ( x,
const scifir::percentage y 
)

Returns true if the numeric type x is lower or equal than the value of percentage y.

Definition at line 198 of file percentage.hpp.

199{
200 return (x <= y.get_value());
201}

◆ operator==() [1/5]

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.

Definition at line 548 of file percentage.cpp.

549{
550 if(x.get_value() == y.get_value())
551 {
552 return true;
553 }
554 else
555 {
556 return false;
557 }
558}

◆ operator==() [2/5]

bool operator== ( const scifir::percentage x,
const string &  init_percentage 
)

Returns true if x and the percentage constructed with init_percentage are equal.

Definition at line 599 of file percentage.cpp.

600{
601 scifir::percentage y = scifir::percentage(init_percentage);
602 return (x == y);
603}

◆ operator==() [3/5]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator== ( const scifir::percentage y,
x 
)

Returns true if the value of percentage is equal to the numeric type x.

Definition at line 210 of file percentage.hpp.

211{
212 return (x == y.get_value());
213}

◆ operator==() [4/5]

bool operator== ( const string &  init_percentage,
const scifir::percentage x 
)

Returns true if x and the percentage constructed with init_percentage are equal.

Definition at line 610 of file percentage.cpp.

611{
612 scifir::percentage y = scifir::percentage(init_percentage);
613 return (x == y);
614}

◆ operator==() [5/5]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator== ( x,
const scifir::percentage y 
)

Returns true if the value of percentage is equal to the numeric type x.

Definition at line 174 of file percentage.hpp.

175{
176 return (x == y.get_value());
177}

◆ operator>() [1/3]

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.

Definition at line 577 of file percentage.cpp.

578{
579 if(x.get_value() > y.get_value())
580 {
581 return true;
582 }
583 else
584 {
585 return false;
586 }
587}

◆ operator>() [2/3]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator> ( const scifir::percentage y,
x 
)

Returns true if the value of percentage y is greather than the numeric type x.

Definition at line 228 of file percentage.hpp.

229{
230 return (y.get_value() > x);
231}

◆ operator>() [3/3]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator> ( x,
const scifir::percentage y 
)

Returns true if the numeric type x is greather than the value of percentage y.

Definition at line 192 of file percentage.hpp.

193{
194 return (x > y.get_value());
195}

◆ operator>=() [1/3]

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.

Definition at line 594 of file percentage.cpp.

595{
596 return !(x < y);
597}

◆ operator>=() [2/3]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator>= ( const scifir::percentage y,
x 
)

Returns true if the value of percentage y is equal or greather than the numeric type x.

Definition at line 240 of file percentage.hpp.

241{
242 return (y.get_value() >= x);
243}

◆ operator>=() [3/3]

template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type>
bool operator>= ( x,
const scifir::percentage y 
)

Returns true if the numeric type x is equal or greather than the value of percentage y.

Definition at line 204 of file percentage.hpp.

205{
206 return (x >= y.get_value());
207}

◆ operator>>()

istream & operator>> ( istream &  is,
scifir::percentage x 
)

Allows that an istream initializes by string a percentage x.

Definition at line 649 of file percentage.cpp.

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}