scicpp::units::quantity

Defined in header <scicpp/core.hpp>


template<typename T, typename Dim, typename Scale, typename Offset = std::ratio<0>>
class quantity

An arithmetic-like type representing a quantity. The class only stores a value of type T.

It performs compile-time Dimensional analysis to validate Dimensional homogeneity. The quantity dimension Dim is a compile-time constant representing the quantity dimension (for example L^2 T / L). See Implementation notes.

It also provides conversion between units (for example meters to miles), whith conversion factor calculated at compile-time. Scale and Offset are compile-time rational constant representing the affine transform coefficients to convert between units.

Operators

  • Assignment operator: =
  • Arithmetic operators: ++, --, +=, -=, *=, /=, +, -, *, /
  • Comparison operators: ==, !=, >=, <=, >, <

Additions, substractions and comparisons can only be performed between quantities of same dimension Dim.

Member functions

constexpr T value() const

Return the underlying value of the quantity (ex. 1_km.value() returns 1.0). Should mostly be used for printing.

constexpr T eval() const

Return the value of a quantity evaluated in the base unit (ex. 1_km.eval() returns 1000.0). Should mostly be used for printing.

Non-member functions

template<typename ToQuantity, typename T, typename Dim, typename Scale, typename Offset>
constexpr ToQuantity quantity_cast(const quantity<T, Dim, Scale, Offset> &qty)

Convert qty to a quantity of type ToQuantity. Used to convert between units when implicit conversion is not allowed, that is if it would result in a loss of precision.

Type traits

template<class T>
constexpr bool is_quantity_v<T>

true if T is a quantity. For example, is_quantity_v<meter<>> == true, but is_quantity_v<double> == false.


template<typename T, typename Dim, typename Scale1, typename Scale2, typename Offset1, typename Offset2>
class common_quantity_t

Determines the common type of two units.


template<class T>
class representation_t

Return the representation type if T is a quantity, else simply return T.

For example, representation_t<meter<int>> is int and representation_t<double> is double.


Quantity types arithmetic

template<typename Quantity1, typename Quantity2>
class quantity_multiply
template<typename Quantity1, typename Quantity2>
class quantity_divide
template<typename Quantity>
class quantity_invert
template<intmax_t Root, typename Quantity>
class quantity_root

Use to build derived new quantities from existing ones:

// Energy = Power x Time
template <typename T, typename Scale>
using energy = quantity_multiply<power<T, Scale>, time<T>>;

// Pressure = Force / Area
template <typename T, typename Scale>
using pressure = quantity_divide<force<T, Scale>, area<T>>;

// Frequency = 1 / Time
template <typename T, typename Scale>
using frequency = quantity_invert<time<T, Scale>>;

// Voltage noise density = Electric Potential / sqrt(Bandwidth)
template <typename T, typename Scale>
using voltage_noise_density = quantity_divide<electric_potential<T, Scale>,
                                              quantity_root<2, frequency<T>>>;