Xquantity

Polymorphic dimensioned quantity with runtime unit checking/conversion

Context

_images/ditaa-f1cc7f6e5ec2ac0d2813d9d173a2217870a3bbf1.png
#include <xo/unit/xquantity.hpp>

allowmixing

object qty1<<xquantity>>
qty1 : scale = 1.23
qty1 : unit = unit

object unit<<scaled_unit>>
unit : is_natural() = true

qty1 o-- unit

Class

Class with run-time unit representation.

template<typename Repr = double, typename Int = std::int64_t>
class xquantity

represent a scalar quantity with polymorphic units.

  • Repr type used represent a dimensionless multiple of a natural unit.

Constexpr implementation, but units are explicitly represented:

sizeof(xquantity) > sizeof(xquantity::repr_type)

Explicit unit representation allows introducing units at runtime, for example in python bindings. See for example xo-pyutil

See quantity for implementation with units established at compile time

Require:

  • Repr supports numeric operations (+, -, *, /)

  • Repr supports conversion from double.

Member Variables

group Xquantity-instance-vars

Variables

Repr scale_ = Repr{}

quantity represents this multiple of a unit amount

natural_unit<Int> unit_

unit for this quantity

Constructors

group xquantity constructors

Functions

inline constexpr xquantity()

create dimensionless, zero quantity

inline constexpr xquantity(Repr scale, const natural_unit<Int> &unit)

create quantity representing multiple of scale times unit

inline constexpr xquantity(Repr scale, const scaled_unit<Int> &unit)

create quantity representing multiple of scale times unit.

Collects outer scalefactors (if any) from unit, so for example:

using namespace xo::qty;
xquantity q(123, u::meter * u::millimeter);

q.scale() --> 0.123

Access Methods

group xquantity access methods

Functions

inline const repr_type &scale() const

get member scale_

inline const unit_type &unit() const

get member unit_

inline bool is_dimensionless() const

true iff this quantity has no dimension

inline nu_abbrev_type abbrev() const

return abbreviation for quantities with this unit

Constants

group static xquantity constants

Variables

static bool always_constexpr_unit = false

false since unit information may be unknown at compile time. Coordinates with quantity::always_constexpr_unit

Conversion Methods

group Xquantity-unit-conversion

Functions

inline auto rescale(const natural_unit<Int> &unit2) const

create quantity representing the same value, but in units of unit2

inline auto rescale_ext(const scaled_unit<Int> &unit2) const

Arithmetic

group xquantity operators

Functions

template<typename Quantity2>
inline xquantity &operator+=(const Quantity2 &x)

add x in-place, converting units if necessary

inline xquantity operator-() const

unary negation; preserves unit information

template<typename Quantity2>
inline xquantity &operator-=(const Quantity2 &x)

subtract x in-place, converting units if necessary

template<typename Quantity2>
inline xquantity &operator*=(const Quantity2 &x)

multiply x in-place, converting units if necessary

Note

: unlike quantity::operator*=, may change dimension of lhs

template<typename Quantity2>
inline xquantity &operator/=(const Quantity2 &x)

divide x in-place, converting units if necessary

Note

: unlike quantity::operator/=, may change dimension of lhs

Support methods for arithmetic operations

group Xquantity-arithmetic-support

Functions

inline xquantity unit_qty() const

create unit quantity with same unit as this

inline xquantity zero_qty() const

create zero quantity with same unit as this

inline xquantity reciprocal() const

create quantity representing reciprocal of this

template<typename Dimensionless>
inline auto scale_by(Dimensionless x) const

create quantity representing this value scaled by dimensionless mutliplier x

template<typename Dimensionless>
inline auto divide_by(Dimensionless x) const

create quantity representing this value scaled by dimensionless multiplier 1/x

template<typename Dimensionless>
inline auto divide_into(Dimensionless x) const

create quantity representing dimensionless numerator x divided by this value

template<typename Quantity2>
static inline auto multiply(const xquantity &x, const Quantity2 &y)

multiply two xquantity values, or a mixed (xquantity, quantity) pair

template<typename Quantity2>
static inline auto divide(const xquantity &x, const Quantity2 &y)

compute quotient x / y, where x and y are xquantities

template<typename Quantity2>
static inline auto add(const xquantity &x, const Quantity2 &y)

compute sum x + y, where x and y are xquantities

template<typename Quantity2>
static inline auto subtract(const xquantity &x, const Quantity2 &y)

compute difference x - y, where x and y are xquantities

Comparison

Support methods for comparison operators

group xquantity comparison support methods

Functions

template<typename Quantity2>
static inline auto compare(const xquantity &x, const Quantity2 &y)

perform 3-way comparison between xquantity values x and y