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 — A dimension specifying the physical dimension

  • T — A std::floating_point type for the magnitude (default: double)

Nested types

  • dimensions — The dimension D

  • floating_point_type — Same as T

Member functions

(Constructor)

constructs a quantity

quantity()

(1)

explicit(narrowing) quantity(quantity<D, T1> other)

(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

bool operator ==(quantity<D, T2> rhs) const

(1)

partial_ordering operator <⇒(quantity rhs) const

(2)

  • (1) Equality comparison (cross-type).

  • (2) Three-way comparison. Generates <, , >, >=.

operator +=
operator -=

addition and subtraction assignment

quantity & operator +=(quantity<D, T2> rhs)

(1)

quantity & operator -=(quantity<D, T2> rhs)

(2)

Adds or subtracts rhs from *this. The operands must have the same dimension.

operator *
operator /

multiplication and division with other quantities

quantity<D*D2> operator *(quantity<D2, T2> rhs) const

(1)

quantity<D/D2> operator /(quantity<D2, T2> rhs) const

(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

quantity & operator *=(T rhs)

(1)

quantity & operator /=(T rhs)

(2)

Multiplies or divides the magnitude by a scalar. Dimension is unchanged.

SI

magnitude in SI units

T SI() const

(1)

Returns the magnitude in the International System of Units.

CGS

magnitude in CGS units

T CGS() const

(1)

Returns the magnitude in the centimetre-gram-second system.

in

magnitude in a given unit

T in(quantity<D, T2> target_unit) const

(1)

Returns the magnitude expressed in target_unit. The target must have the same dimension.

Example
auto d = 1_km;
d.in(1_mi);  // ≈ 0.6214

degK
degC
degF

temperature conversions

T degK() const

(1)

T degC() const

(2)

T degF() const

(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() const

(1)

Available only for dimensionless quantities. Returns the magnitude as a scalar.

operator std::chrono::duration

conversion to chrono duration

operator std::chrono::duration<Rep, Period>() const

(1)

Available only for time quantities. Enables interoperability with <chrono>.

Non-member functions

operator +
operator -

unary operators

quantity operator +(quantity rhs)

(1)

quantity operator -(quantity rhs)

(2)

  • (1) Returns rhs unchanged.

  • (2) Returns rhs with negated magnitude.

operator +
operator -

binary addition and subtraction

quantity operator +(quantity lhs, quantity rhs)

(1)

quantity operator -(quantity lhs, quantity rhs)

(2)

Adds or subtracts two quantities of the same dimension.

operator *
operator /

scalar multiplication and division

quantity operator *(quantity lhs, arithmetic rhs)

(1)

quantity operator *(arithmetic lhs, quantity rhs)

(2)

quantity operator /(quantity lhs, arithmetic rhs)

(3)

quantity<1/D> operator /(arithmetic lhs, quantity rhs)

(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

quantity abs(quantity v)

(1)

quantity fabs(quantity v)

(2)

Returns the absolute value. Dimension unchanged.

sqrt

square root

quantity<D.sqrt()> sqrt(quantity v)

(1)

Returns the square root of the quantity. Halves each dimension exponent. Only available when all exponents are even.

hypot

hypotenuse / norm

quantity hypot(quantity v1, quantity v2)

(1)

quantity hypot(quantity v1, quantity v2, quantity v3)

(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

angle asin(T value)

(1)

angle acos(T value)

(2)

angle atan(T value)

(3)

angle atan(quantity<{}, T> value)

(4)

angle atan2(quantity num, quantity denom)

(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.

Formatter

std::formatter<quantity<D, T>> supports format specifications:

  • Default: prints magnitude followed by the SI dimension string

  • Unit spec (e.g. {:_cm}): converts to the given unit before printing

  • Float spec (e.g. {:.3f}): applied to the magnitude