Defined in header <scicpp/core.hpp>
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.
=
++
, --
, +=
, -=
, *=
, /=
, +
, -
, *
, /
==
, !=
, >=
, <=
, >
, <
Additions, substractions and comparisons can only be performed between quantities of same dimension Dim
.
value
() const¶Return the underlying value of the quantity (ex. 1_km.value()
returns 1.0).
Should mostly be used for printing.
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.
ToQuantity
, typename T
, typename Dim
, typename Scale
, typename Offset
>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.
true
if T
is a quantity.
For example, is_quantity_v<meter<>> == true
, but is_quantity_v<double> == false
.
T
, typename Dim
, typename Scale1
, typename Scale2
, typename Offset1
, typename Offset2
>common_quantity_t
¶Determines the common type of two units.
T
>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
.
Quantity1
, typename Quantity2
>quantity_multiply
¶Quantity1
, typename Quantity2
>quantity_divide
¶Quantity
>quantity_invert
¶Root
, typename Quantity
>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>>>;