Units Library
The units library is a zero-overhead implementation of the International System of Units (SI) in C++. It provides strong typing of physical quantities with compile-time dimensional analysis.
namespace nin::inline units {
struct dimension;
template <dimension D, std::floating_point T = double> class quantity;
}
dimension
namespace nin::inline units {
struct dimension;
}
Represents the physical dimension of a quantity as a tuple of seven integer exponents, one for each SI base quantity.
Data members
α
β
γ
δ
ε
ζ
η
SI base dimension exponents
α
β
γ
δ
ε
ζ
η
SI base dimension exponents
|
Time (second) |
|
Length (metre) |
|
Mass (kilogram) |
|
Electric current (ampere) |
|
Temperature (kelvin) |
|
Amount of substance (mole) |
|
Luminous intensity (candela) |
All default-initialized to 0 (dimensionless).
Member functions
operator ==
equality comparison
operator ==
equality comparison
|
(1) |
Returns true if all seven exponents are equal. Defaulted.
operator *=
operator /=
dimension arithmetic assignment
operator *=
operator /=
dimension arithmetic assignment
|
(1) |
|
(2) |
-
(1) Adds the exponents of
rhsto*this(dimension multiplication). -
(2) Subtracts the exponents of
rhsfrom*this(dimension division).
sqrt
square root of a dimension
sqrt
square root of a dimension
|
(1) |
Returns a dimension with all exponents halved.
Throws std::domain_error if any exponent is odd.
is_sqrt_ok
check if square root is valid
is_sqrt_ok
check if square root is valid
|
(1) |
Returns true if all exponents are even.
quantity
namespace nin::inline units {
template <dimension D, std::floating_point T = double>
class quantity;
}
A physical quantity with a magnitude and a dimension. The magnitude is always stored internally in SI units, regardless of how the quantity was created.
Template parameters
-
D— Adimensionspecifying the physical dimension -
T— Astd::floating_pointtype for the magnitude (default:double)
Member functions
(Constructor)
constructs a quantity
(Constructor)
constructs a quantity
|
(1) |
|
(2) |
-
(1) Default constructor. Magnitude is zero.
-
(2) Converting constructor from a quantity with the same dimension but different floating-point type. Explicit if the conversion is narrowing.
operator <⇒
operator ==
comparison operators
operator <⇒
operator ==
comparison operators
|
(1) |
|
(2) |
-
(1) Equality comparison (cross-type).
-
(2) Three-way comparison. Generates
<,⇐,>,>=.
operator +=
operator -=
addition and subtraction assignment
operator +=
operator -=
addition and subtraction assignment
|
(1) |
|
(2) |
Adds or subtracts rhs from *this. The operands must have the same dimension.
operator *
operator /
multiplication and division with other quantities
operator *
operator /
multiplication and division with other quantities
|
(1) |
|
(2) |
-
(1) Returns a quantity whose dimension is the product of the two dimensions.
-
(2) Returns a quantity whose dimension is the quotient of the two dimensions.
There are no *= or /= operators for quantities with different dimensions,
since the result type differs from *this.
|
operator *=
operator /=
scalar multiplication and division assignment
operator *=
operator /=
scalar multiplication and division assignment
|
(1) |
|
(2) |
Multiplies or divides the magnitude by a scalar. Dimension is unchanged.
SI
magnitude in SI units
SI
magnitude in SI units
|
(1) |
Returns the magnitude in the International System of Units.
CGS
magnitude in CGS units
CGS
magnitude in CGS units
|
(1) |
Returns the magnitude in the centimetre-gram-second system.
in
magnitude in a given unit
in
magnitude in a given unit
|
(1) |
Returns the magnitude expressed in target_unit.
The target must have the same dimension.
auto d = 1_km;
d.in(1_mi); // ≈ 0.6214
degK
degC
degF
temperature conversions
degK
degC
degF
temperature conversions
|
(1) |
|
(2) |
|
(3) |
Available only for quantities with temperature dimension.
-
(1) Returns the temperature in kelvin (same as
SI()). -
(2) Returns the temperature in degrees Celsius.
-
(3) Returns the temperature in degrees Fahrenheit.
operator T
implicit conversion to scalar
operator T
implicit conversion to scalar
|
(1) |
Available only for dimensionless quantities. Returns the magnitude as a scalar.
operator std::chrono::duration
conversion to chrono duration
operator std::chrono::duration
conversion to chrono duration
|
(1) |
Available only for time quantities. Enables interoperability with <chrono>.
Non-member functions
operator +
operator -
unary operators
operator +
operator -
unary operators
|
(1) |
|
(2) |
-
(1) Returns
rhsunchanged. -
(2) Returns
rhswith negated magnitude.
operator +
operator -
binary addition and subtraction
operator +
operator -
binary addition and subtraction
|
(1) |
|
(2) |
Adds or subtracts two quantities of the same dimension.
operator *
operator /
scalar multiplication and division
operator *
operator /
scalar multiplication and division
|
(1) |
|
(2) |
|
(3) |
|
(4) |
-
(1)-(3) Scale the quantity by a scalar. Dimension unchanged.
-
(4) Dividing a scalar by a quantity inverts the dimension.
abs
fabs
absolute value
abs
fabs
absolute value
|
(1) |
|
(2) |
Returns the absolute value. Dimension unchanged.
sqrt
square root
sqrt
square root
|
(1) |
Returns the square root of the quantity. Halves each dimension exponent. Only available when all exponents are even.
hypot
hypotenuse / norm
hypot
hypotenuse / norm
|
(1) |
|
(2) |
-
(1) Returns \(\sqrt{v_1^2 + v_2^2}\).
-
(2) Returns \(\sqrt{v_1^2 + v_2^2 + v_3^2}\).
asin
acos
atan
atan2
inverse trigonometric functions
asin
acos
atan
atan2
inverse trigonometric functions
|
(1) |
|
(2) |
|
(3) |
|
(4) |
|
(5) |
-
(1)-(3) Take a scalar and return an angle.
-
(4) Takes a dimensionless quantity and returns an angle.
-
(5) Takes two quantities of the same dimension and returns an angle.
Literal operators
The library provides user-defined literal operators for all SI units with their prefixes. Considering all combinations of underlying types, prefixes, and units, there are more than 50,000 literals defined.
Factory functions
Factory functions provide the same functionality as literal operators but with a function call syntax. They are named as plurals or descriptive names.
auto d = nin::centimetres(3.0); // 3 cm
auto t = nin::seconds(1.5); // 1.5 s
auto v = nin::metres_v<float>(2.0f); // 2 m as float
Type aliases
For each physical quantity, the library provides template and concrete aliases:
| Pattern | Example |
|---|---|
|
|
|
|
|
|
|
|
|
|
Common type aliases
| Alias | Unit | Dimension |
|---|---|---|
|
second |
T |
|
metre |
L |
|
kilogram |
M |
|
ampere |
I |
|
kelvin |
Θ |
|
mole |
N |
|
candela |
J |
|
radian |
(dimensionless) |
|
hertz |
T-1 |
|
newton |
M·L·T-2 |
|
pascal |
M·L-1·T-2 |
|
joule |
M·L2·T-2 |
|
watt |
M·L2·T-3 |
|
coulomb |
T·I |
|
volt |
M·L2·T-3·I-1 |
Physical constants
Mathematical and physical constants are provided as constexpr variables.
SI defining constants
| Name | Description |
|---|---|
|
Speed of light (299,792,458 m/s) |
|
Planck constant |
|
Elementary charge |
|
Boltzmann constant |
|
Avogadro constant |
|
Luminous efficacy |
Derived constants
| Name | Description |
|---|---|
|
Reduced Planck constant |
|
Gravitational constant |
|
Permeability of free space |
|
Permittivity of free space |
|
Molar gas constant |
|
Faraday constant |
|
Stefan-Boltzmann constant |
|
Standard acceleration due to gravity |
|
Electron mass |
|
Proton mass |
Conformance
The units and fundamental constants are taken from:
"The International System of Units" 9th edition (2019).
Other physical constants are from:
Tiesinga, E., Mohr, P. J., Newell, D. B., & Taylor, B. N. (2021). CODATA recommended quantities of the fundamental physical constants: 2018. Reviews of Modern Physics, 93(2), 025010.
Any deviation from these documents shall be considered a bug.