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
|
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions. The dimensions can be of any number, and be simple dimensions, composite dimensions and/or special names of dimensions. All base and derived scalar unit classes inherit from scalar_unit. Initialization string example: "1 N". More...
#include <scalar_unit.hpp>
Public Member Functions | |
scalar_unit () | |
Default constructor, the value is 0 and the dimensions are empty. | |
scalar_unit (const scalar_unit &x) | |
Copy constructor, copies the value and the dimensions. | |
scalar_unit (scalar_unit &&x) | |
Move constructor, moves the value and the dimensions. | |
scalar_unit (float new_value, dimension::type new_dimension, prefix::type new_prefix, dimension::position new_position=dimension::NUMERATOR) | |
scalar_unit (double new_value, dimension::type new_dimension, prefix::type new_prefix, dimension::position new_position=dimension::NUMERATOR) | |
scalar_unit (long double new_value, dimension::type new_dimension, prefix::type new_prefix, dimension::position new_position=dimension::NUMERATOR) | |
scalar_unit (int new_value, dimension::type new_dimension, prefix::type new_prefix, dimension::position new_position=dimension::NUMERATOR) | |
scalar_unit (float new_value, const string &init_dimensions) | |
Creates a new scalar_unit with the value and with the dimensions specified in the string. | |
scalar_unit (double new_value, const string &init_dimensions) | |
Creates a new scalar_unit, the double is casted to a float. | |
scalar_unit (long double new_value, const string &init_dimensions) | |
Creates a new scalar_unit, the long double is casted to a float. | |
scalar_unit (int new_value, const string &init_dimensions) | |
Creates a new scalar_unit, the int is casted to a float. | |
scalar_unit (float new_value, const vector< dimension > &new_dimensions) | |
Creates a new scalar_unit with the value given and copying the dimensions. | |
scalar_unit (double new_value, const vector< dimension > &new_dimensions) | |
Creates a new scalar_unit, the double is casted to a float, the dimensions are copied. | |
scalar_unit (long double new_value, const vector< dimension > &new_dimensions) | |
Creates a new scalar_unit, the long double is casted to a float, the dimensions are copied. | |
scalar_unit (int new_value, const vector< dimension > &new_dimensions) | |
Creates a new scalar_unit, the int is casted to a float, the dimensions are copied. | |
scalar_unit (const string &init_scalar) | |
Creates a new scalar_unit, the initialization string specifies both the value and the dimensions. | |
scalar_unit & | operator= (const scalar_unit &x) |
Copy assignment, it assigns a copy of the scalar_unit. | |
scalar_unit & | operator= (scalar_unit &&x) |
Move assignment, it moves the scalar_unit. | |
scalar_unit & | operator= (const string &init_scalar) |
Assignment with an initialization string, the value and the dimensions are both specified. | |
operator float () const | |
Cast the scalar_unit to their float equivalent, it discards the dimensions and copies the value to a float. | |
bool | operator== (scalar_unit x) const |
Comparison operator, two scalar_unit classes are considered equivalent if they have the same value given the same dimensions. | |
scalar_unit | operator+ (scalar_unit x) const |
Addition operator, it sums two scalar_unit classes, their dimensions are changed to be equal first. If the basic dimensions are different, it returns an empty scalar_unit class. | |
scalar_unit | operator- (scalar_unit x) const |
Substraction operator, it substracts one scalar_unit from the other, their dimensions are changed to be equal first. If the basic dimensions are different, it returns an empty scalar_unit class. | |
scalar_unit | operator* (scalar_unit x) const |
Multiplication operator, it multiplies two scalar_unit classes, their dimensions are also multiplied. | |
scalar_unit | operator/ (scalar_unit x) const |
Division operator, it divides one scalar_unit class with the other, their dimensions are also divided. | |
scalar_unit | operator^ (const scalar_unit &x) const |
Power operator, it powers a scalar_unit class with another, if that second scalar_unit class, which is the exponent, as empty dimensions. If the exponent doesn't has empty dimensions it returns an empty scalar_unit class. | |
void | operator+= (scalar_unit x) |
Addition operator, it adds a scalar_unit class to another, by converting their dimensions to be equal first. If their basic dimensions are different, it doesn't sums to the value. | |
void | operator-= (scalar_unit x) |
Substraction operator, it substracts a scalar_unit class to another, by converting their dimensions to be equal first. If their basic dimensions are different, it doesn't substracts to the value. | |
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type> | |
scalar_unit | operator+ (T y) const |
Addition operator, it sums the numeric type to the value, independent of the dimensions. | |
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type> | |
scalar_unit | operator- (T y) const |
Substraction operator, it substracts the numeric type to the value, independent of the dimensions. | |
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type> | |
scalar_unit | operator* (T y) const |
Multiplication operator, it multiplies the numeric type with the value, independent of the dimensions. | |
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type> | |
scalar_unit | operator/ (T y) const |
Division operator, it divides the value with the numeric type, independent of the dimensions. | |
template<typename T , typename = typename enable_if<is_integer_number<T>::value>::type> | |
scalar_unit | operator^ (T y) const |
Power operator, it powers the value with the numeric type, independent of the dimensions. | |
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type> | |
void | operator+= (T y) |
Addition operator, it adds the numeric type to the value, independent of the dimensions. | |
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type> | |
void | operator-= (T y) |
Substraction operator, it substracts the numeric type to the value, independent of the dimensions. | |
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type> | |
void | operator*= (T y) |
Multiplication operator, it multiplies the numeric type to the value, independent of the dimensions. | |
template<typename T , typename = typename enable_if<scifir::is_number<T>::value>::type> | |
void | operator/= (T y) |
Division operator, it divides the numeric type to the value, independent of the dimensions. | |
scalar_unit & | operator++ () |
Increment operator, it increases the value by one. | |
scalar_unit & | operator++ (int) |
Increment operator, it increases the value by one. | |
scalar_unit & | operator-- () |
Decrement operator, it decreases the value by one. | |
scalar_unit & | operator-- (int) |
Decrement operator, it decreases the value by one. | |
void | change_dimensions (const string &init_dimensions) |
Changes the dimensions to the dimensions specified by the initialization string of dimensions. | |
void | change_dimensions (const scalar_unit &x) |
Changes the dimensions to the same dimensions of the scalar_unit. | |
bool | has_dimensions (const string &init_dimensions) const |
Checks if the basic dimensions are the same as the initialization string of dimensions. | |
bool | has_dimensions (const vector< dimension > &x_dimensions) const |
Checks if the basic dimensions are the same as some set of dimensions. | |
bool | has_dimensions (const scalar_unit &x) const |
Checks if the basic dimensions are the same as other scalar_unit. | |
bool | has_empty_dimensions () const |
Checks if there aren't base dimensions. | |
bool | is_dimensionless () const |
Returns true if there aren't dimensions or if all dimensions are dimensionless. | |
bool | has_simple_dimensions () const |
Returns true if there's only a simple dimension. | |
bool | has_single_dimensions () const |
Returns true if there's only one dimension, which can be simple or composite. | |
bool | has_composite_dimensions () const |
Returns true is there's more than one simple dimension. | |
string | display_dimensions () const |
Generates an string of the dimensions of the scalar_unit, with the same format as the initialization string of dimensions. | |
dimension::type | get_single_dimension_type () const |
Returns the dimension::type if there's only one dimension, returns dimension::NONE if there's more than one dimension or if the dimensions are empty. | |
vector< dimension > | get_base_dimensions () const |
Generates a set of the base dimensions of the dimensions of the scalar_unit. | |
const vector< dimension > & | get_dimensions () const |
Read-only getter of the dimensions. | |
const float & | get_value () const |
Read-only getter of the value. | |
string | display (int number_of_decimals=2, bool with_brackets=false, bool use_close_prefix=false) const |
Generates a string representation of the scalar_unit, with the value and the dimensions. The dimensions can be enclosed by brackets, and the value can be set to have the closest prefix. | |
string | base_display (int number_of_decimals=2, bool with_brackets=false, bool use_close_prefix=false) const |
Generates a string representation of the scalar_unit, with its dimensions converted to their base counterpart. | |
string | custom_display (const string &init_dimensions, int number_of_decimals=2, bool with_brackets=false) const |
Generates a string representation of the scalar_unit, with the dimensions changed to any set of dimensions, specified by an initialization string of dimensions. | |
string | to_latex (const string &init_dimensions, int number_of_decimals=2, bool with_brackets=false) const |
Protected Member Functions | |
void | add_dimension (const dimension &new_dimension) |
Internal function. It adds a dimension, changing the value according to the conversion factor of the added dimension and the prefix. | |
void | remove_dimension (const dimension &old_dimension) |
Internal function. It removes a dimension, changing the value according to the conversion factor of the removed dimension and the prefix. | |
void | initialize_from_string (string init_scalar, const vector< dimension > &real_dimensions) |
Internal function. It sets the value and the dimensions of the scalar_unit to the value and dimensions specified by the initialization string of scalar_unit. | |
void | check_dimensions (const vector< dimension > &real_dimensions) |
Protected Attributes | |
vector< dimension > | dimensions |
Dimensions of the scalar_unit. They can be simple dimensions, composite dimensions or special names. | |
float | value |
Value of the scalar_unit. It changes automatically when the dimensions change. | |
Class that allows to create scalar units, which are composed of a value (as a float) and dimensions. The dimensions can be of any number, and be simple dimensions, composite dimensions and/or special names of dimensions. All base and derived scalar unit classes inherit from scalar_unit. Initialization string example: "1 N".
scalar_unit is a class that allows to have a scalar unit, which is composed of a value and one or more dimensions. All dimensions of the SI system of units are supported, and also all of its special names. The special names can be converted to its equivalent dimensions, too. scalar_unit classes have operators with all numeric types, and have operators with themselves. They can be displayed by its dimensions, its base dimensions, or any custom combination of dimensions. When they change dimensions, like, for example, from meters to kilometers, their numeric value is changed accordingly. POSSIBLE FEATURES At the present scalar_unit classes use a vector<dimension> to allow to have any number of dimensions, which can be simple, composite or special names. In order to size less, it's possible for some scalar_unit classes to have only one dimension, instead of a vector, that saves memory. With that approach, the dimension needs only 3 bytes. The possibility to switch between a single dimension and a vector<dimension> doesn't yet exists in C++, and so this important feature has been postposed.
Definition at line 201 of file scalar_unit.hpp.
scalar_unit::scalar_unit | ( | ) |
Default constructor, the value is 0 and the dimensions are empty.
Definition at line 23 of file scalar_unit.cpp.
scalar_unit::scalar_unit | ( | const scalar_unit & | x | ) |
Copy constructor, copies the value and the dimensions.
x | scalar_unit to be copied. |
Definition at line 26 of file scalar_unit.cpp.
scalar_unit::scalar_unit | ( | scalar_unit && | x | ) |
Move constructor, moves the value and the dimensions.
x | scalar_unit to be moved. |
Definition at line 29 of file scalar_unit.cpp.
|
explicit |
Definition at line 32 of file scalar_unit.cpp.
|
explicit |
Definition at line 35 of file scalar_unit.cpp.
|
explicit |
Definition at line 38 of file scalar_unit.cpp.
|
explicit |
Definition at line 41 of file scalar_unit.cpp.
Creates a new scalar_unit with the value and with the dimensions specified in the string.
Definition at line 44 of file scalar_unit.cpp.
Creates a new scalar_unit, the double is casted to a float.
Definition at line 47 of file scalar_unit.cpp.
Creates a new scalar_unit, the long double is casted to a float.
Definition at line 50 of file scalar_unit.cpp.
Creates a new scalar_unit, the int is casted to a float.
Definition at line 53 of file scalar_unit.cpp.
Creates a new scalar_unit with the value given and copying the dimensions.
Definition at line 56 of file scalar_unit.cpp.
Creates a new scalar_unit, the double is casted to a float, the dimensions are copied.
Definition at line 59 of file scalar_unit.cpp.
|
explicit |
Creates a new scalar_unit, the long double is casted to a float, the dimensions are copied.
Definition at line 62 of file scalar_unit.cpp.
Creates a new scalar_unit, the int is casted to a float, the dimensions are copied.
Definition at line 65 of file scalar_unit.cpp.
Creates a new scalar_unit, the initialization string specifies both the value and the dimensions.
Definition at line 68 of file scalar_unit.cpp.
Internal function. It adds a dimension, changing the value according to the conversion factor of the added dimension and the prefix.
Definition at line 664 of file scalar_unit.cpp.
string scalar_unit::base_display | ( | int | number_of_decimals = 2 , |
bool | with_brackets = false , |
||
bool | use_close_prefix = false |
||
) | const |
Generates a string representation of the scalar_unit, with its dimensions converted to their base counterpart.
Definition at line 475 of file scalar_unit.cpp.
void scalar_unit::change_dimensions | ( | const scalar_unit & | x | ) |
Changes the dimensions to the same dimensions of the scalar_unit.
Definition at line 287 of file scalar_unit.cpp.
Changes the dimensions to the dimensions specified by the initialization string of dimensions.
Definition at line 234 of file scalar_unit.cpp.
Definition at line 731 of file scalar_unit.cpp.
string scalar_unit::custom_display | ( | const string & | init_dimensions, |
int | number_of_decimals = 2 , |
||
bool | with_brackets = false |
||
) | const |
Generates a string representation of the scalar_unit, with the dimensions changed to any set of dimensions, specified by an initialization string of dimensions.
Definition at line 497 of file scalar_unit.cpp.
string scalar_unit::display | ( | int | number_of_decimals = 2 , |
bool | with_brackets = false , |
||
bool | use_close_prefix = false |
||
) | const |
Generates a string representation of the scalar_unit, with the value and the dimensions. The dimensions can be enclosed by brackets, and the value can be set to have the closest prefix.
Definition at line 446 of file scalar_unit.cpp.
string scalar_unit::display_dimensions | ( | ) | const |
Generates an string of the dimensions of the scalar_unit, with the same format as the initialization string of dimensions.
Definition at line 419 of file scalar_unit.cpp.
Generates a set of the base dimensions of the dimensions of the scalar_unit.
Definition at line 441 of file scalar_unit.cpp.
Read-only getter of the dimensions.
Definition at line 321 of file scalar_unit.hpp.
dimension::type scalar_unit::get_single_dimension_type | ( | ) | const |
Returns the dimension::type if there's only one dimension, returns dimension::NONE if there's more than one dimension or if the dimensions are empty.
Definition at line 429 of file scalar_unit.cpp.
Read-only getter of the value.
Definition at line 326 of file scalar_unit.hpp.
bool scalar_unit::has_composite_dimensions | ( | ) | const |
Returns true is there's more than one simple dimension.
Definition at line 403 of file scalar_unit.cpp.
bool scalar_unit::has_dimensions | ( | const scalar_unit & | x | ) | const |
Checks if the basic dimensions are the same as other scalar_unit.
Definition at line 350 of file scalar_unit.cpp.
Checks if the basic dimensions are the same as the initialization string of dimensions.
Definition at line 339 of file scalar_unit.cpp.
Checks if the basic dimensions are the same as some set of dimensions.
Definition at line 345 of file scalar_unit.cpp.
bool scalar_unit::has_empty_dimensions | ( | ) | const |
Checks if there aren't base dimensions.
Definition at line 355 of file scalar_unit.cpp.
bool scalar_unit::has_simple_dimensions | ( | ) | const |
Returns true if there's only a simple dimension.
Definition at line 386 of file scalar_unit.cpp.
bool scalar_unit::has_single_dimensions | ( | ) | const |
Returns true if there's only one dimension, which can be simple or composite.
Definition at line 398 of file scalar_unit.cpp.
|
protected |
Internal function. It sets the value and the dimensions of the scalar_unit to the value and dimensions specified by the initialization string of scalar_unit.
Definition at line 692 of file scalar_unit.cpp.
bool scalar_unit::is_dimensionless | ( | ) | const |
Returns true if there aren't dimensions or if all dimensions are dimensionless.
Definition at line 367 of file scalar_unit.cpp.
|
explicit |
Cast the scalar_unit to their float equivalent, it discards the dimensions and copies the value to a float.
Definition at line 107 of file scalar_unit.cpp.
scalar_unit scalar_unit::operator* | ( | scalar_unit | x | ) | const |
Multiplication operator, it multiplies two scalar_unit classes, their dimensions are also multiplied.
Definition at line 157 of file scalar_unit.cpp.
|
inline |
Multiplication operator, it multiplies the numeric type with the value, independent of the dimensions.
Definition at line 254 of file scalar_unit.hpp.
Multiplication operator, it multiplies the numeric type to the value, independent of the dimensions.
Definition at line 288 of file scalar_unit.hpp.
scalar_unit scalar_unit::operator+ | ( | scalar_unit | x | ) | const |
Addition operator, it sums two scalar_unit classes, their dimensions are changed to be equal first. If the basic dimensions are different, it returns an empty scalar_unit class.
Definition at line 125 of file scalar_unit.cpp.
|
inline |
Addition operator, it sums the numeric type to the value, independent of the dimensions.
Definition at line 238 of file scalar_unit.hpp.
scalar_unit & scalar_unit::operator++ | ( | ) |
Increment operator, it increases the value by one.
Definition at line 208 of file scalar_unit.cpp.
scalar_unit & scalar_unit::operator++ | ( | int | ) |
Increment operator, it increases the value by one.
Definition at line 214 of file scalar_unit.cpp.
void scalar_unit::operator+= | ( | scalar_unit | x | ) |
Addition operator, it adds a scalar_unit class to another, by converting their dimensions to be equal first. If their basic dimensions are different, it doesn't sums to the value.
Definition at line 186 of file scalar_unit.cpp.
Addition operator, it adds the numeric type to the value, independent of the dimensions.
Definition at line 276 of file scalar_unit.hpp.
scalar_unit scalar_unit::operator- | ( | scalar_unit | x | ) | const |
Substraction operator, it substracts one scalar_unit from the other, their dimensions are changed to be equal first. If the basic dimensions are different, it returns an empty scalar_unit class.
Definition at line 141 of file scalar_unit.cpp.
|
inline |
Substraction operator, it substracts the numeric type to the value, independent of the dimensions.
Definition at line 246 of file scalar_unit.hpp.
scalar_unit & scalar_unit::operator-- | ( | ) |
Decrement operator, it decreases the value by one.
Definition at line 221 of file scalar_unit.cpp.
scalar_unit & scalar_unit::operator-- | ( | int | ) |
Decrement operator, it decreases the value by one.
Definition at line 227 of file scalar_unit.cpp.
void scalar_unit::operator-= | ( | scalar_unit | x | ) |
Substraction operator, it substracts a scalar_unit class to another, by converting their dimensions to be equal first. If their basic dimensions are different, it doesn't substracts to the value.
Definition at line 197 of file scalar_unit.cpp.
Substraction operator, it substracts the numeric type to the value, independent of the dimensions.
Definition at line 282 of file scalar_unit.hpp.
scalar_unit scalar_unit::operator/ | ( | scalar_unit | x | ) | const |
Division operator, it divides one scalar_unit class with the other, their dimensions are also divided.
Definition at line 165 of file scalar_unit.cpp.
|
inline |
Division operator, it divides the value with the numeric type, independent of the dimensions.
Definition at line 262 of file scalar_unit.hpp.
Division operator, it divides the numeric type to the value, independent of the dimensions.
Definition at line 294 of file scalar_unit.hpp.
scalar_unit & scalar_unit::operator= | ( | const scalar_unit & | x | ) |
Copy assignment, it assigns a copy of the scalar_unit.
Definition at line 73 of file scalar_unit.cpp.
scalar_unit & scalar_unit::operator= | ( | const string & | init_scalar | ) |
Assignment with an initialization string, the value and the dimensions are both specified.
Definition at line 101 of file scalar_unit.cpp.
scalar_unit & scalar_unit::operator= | ( | scalar_unit && | x | ) |
Move assignment, it moves the scalar_unit.
Definition at line 87 of file scalar_unit.cpp.
bool scalar_unit::operator== | ( | scalar_unit | x | ) | const |
Comparison operator, two scalar_unit classes are considered equivalent if they have the same value given the same dimensions.
Definition at line 112 of file scalar_unit.cpp.
scalar_unit scalar_unit::operator^ | ( | const scalar_unit & | x | ) | const |
Power operator, it powers a scalar_unit class with another, if that second scalar_unit class, which is the exponent, as empty dimensions. If the exponent doesn't has empty dimensions it returns an empty scalar_unit class.
Definition at line 173 of file scalar_unit.cpp.
|
inline |
Power operator, it powers the value with the numeric type, independent of the dimensions.
Definition at line 270 of file scalar_unit.hpp.
Internal function. It removes a dimension, changing the value according to the conversion factor of the removed dimension and the prefix.
Definition at line 678 of file scalar_unit.cpp.
string scifir::scalar_unit::to_latex | ( | const string & | init_dimensions, |
int | number_of_decimals = 2 , |
||
bool | with_brackets = false |
||
) | const |
Definition at line 581 of file scalar_unit.cpp.
|
protected |
Dimensions of the scalar_unit. They can be simple dimensions, composite dimensions or special names.
Definition at line 338 of file scalar_unit.hpp.
|
protected |
Value of the scalar_unit. It changes automatically when the dimensions change.
Definition at line 339 of file scalar_unit.hpp.