/** @file quantity.hpp * * Author: Roland Conybeare **/ #pragma once #include "quantity_ops.hpp" #include "natural_unit.hpp" #include "scaled_unit.hpp" namespace xo { namespace qty { /** @class quantity * @brief represent a scalar quantity with associated units. * * Enforce dimensional consistency at compile time. * sizeof(quantity) == sizeof(Repr). **/ template < auto /*natural_unit*/ NaturalUnit, typename Repr = double, typename Int2x = detail::width2x_t > class quantity { public: using repr_type = Repr; using unit_type = decltype(NaturalUnit); using ratio_int_type = decltype(NaturalUnit)::ratio_int_type; using ratio_int2x_type = Int2x; public: constexpr quantity() : scale_{0} {} explicit constexpr quantity(Repr scale) : scale_{scale} {} static constexpr bool always_constexpr_unit = true; constexpr const repr_type & scale() const { return scale_; } constexpr const unit_type & unit() const { return s_unit; } constexpr bool is_dimensionless() const { return s_unit.is_dimensionless(); } // unit_qty // zero_qty constexpr auto reciprocal() const { return quantity(1.0 / scale_); } template constexpr auto with_repr() const { return quantity(scale_); } /* parallel implementation to Quantity::rescale(), * except that NaturalUnit2 is a compile-time-only template-argument * * NOTE: constexpr as long as no fractional units involved. */ template NaturalUnit2> constexpr auto rescale() const { /* conversion factor from .unit -> unit2*/ auto rr = detail::su_ratio(NaturalUnit, NaturalUnit2); if (rr.natural_unit_.is_dimensionless()) { repr_type r_scale = (((rr.outer_scale_sq_ == 1.0) ? 1.0 : ::sqrt(rr.outer_scale_sq_)) * rr.outer_scale_factor_.template convert_to() * this->scale_); return quantity(r_scale); } else { return quantity(std::numeric_limits::quiet_NaN()); } } template ScaledUnit2> constexpr auto rescale_ext() const { /* conversion factor from .unit -> unit2*/ auto rr = detail::su_ratio(NaturalUnit, ScaledUnit2.natural_unit_); if (rr.natural_unit_.is_dimensionless()) { /* NOTE: test for unit .outer_scale_sq values to get constexpr result with c++23 * and integer dimension powers. */ repr_type r_scale = ((((rr.outer_scale_sq_ == 1.0) && (ScaledUnit2.outer_scale_sq_ == 1.0)) ? 1.0 : ::sqrt(rr.outer_scale_sq_ / ScaledUnit2.outer_scale_sq_)) * rr.outer_scale_factor_.template convert_to() * this->scale_ / ScaledUnit2.outer_scale_factor_.template convert_to()); return quantity(r_scale); } else { return quantity(std::numeric_limits::quiet_NaN()); } } template requires std::is_arithmetic_v constexpr auto scale_by(Dimensionless x) const { return quantity(x * this->scale_); } // divide_by // divide_into // divide // add // subtract /* parallel implementation to Quantity */ template static constexpr auto compare(const quantity &x, const Quantity2 & y) { quantity y2 = y.template rescale(); return x.scale() <=> y2.scale(); } // operator- // operator+= // operator-= // operator*= // operator/= constexpr nu_abbrev_type abbrev() const { return s_unit.abbrev(); } quantity & operator=(const quantity & x) { this->scale_ = x.scale_; return *this; } template requires(quantity_concept && Q2::always_constexpr_unit) quantity & operator=(const Q2 & x) { auto x2 = x.template rescale(); this->scale_ = x2.scale(); return *this; } template requires(quantity_concept && Q2::always_constexpr_unit) constexpr operator Q2() const { return this->template rescale().template with_repr(); } constexpr operator Repr() const requires (NaturalUnit.is_dimensionless()) { return scale_; } public: /* need public members so that a quantity instance can be a non-type template parameter (is a structural type) */ static constexpr natural_unit s_unit = NaturalUnit; Repr scale_ = Repr{}; }; template constexpr auto rescale(const Quantity & x, const scaled_unit & su) { return x.template rescale(); } namespace detail { struct quantity_util { /* parallel implementation to xquantity multiply, * but return type will have dimension computed at compile-time */ template requires (quantity_concept && quantity_concept && Q1::always_constexpr_unit && Q2::always_constexpr_unit) static constexpr auto multiply(Q1 x, Q2 y) { using r_repr_type = std::common_type_t; using r_int_type = std::common_type_t; using r_int2x_type = std::common_type_t; constexpr auto rr = detail::su_product(x.unit(), y.unit()); r_repr_type r_scale = (((rr.outer_scale_sq_ == 1.0) ? 1.0 : ::sqrt(rr.outer_scale_sq_)) * rr.outer_scale_factor_.template convert_to() * static_cast(x.scale()) * static_cast(y.scale())); return quantity(r_scale); } template requires (quantity_concept && quantity_concept && Q1::always_constexpr_unit && Q2::always_constexpr_unit) static constexpr auto divide(Q1 x, Q2 y) { using r_repr_type = std::common_type_t; using r_int_type = std::common_type_t; using r_int2x_type = std::common_type_t; constexpr auto rr = detail::su_ratio(x.unit(), y.unit()); r_repr_type r_scale = (((rr.outer_scale_sq_ == 1.0) ? 1.0 : ::sqrt(rr.outer_scale_sq_)) * rr.outer_scale_factor_.template convert_to() * static_cast(x.scale()) / static_cast(y.scale())); return quantity(r_scale); } template requires(quantity_concept && quantity_concept && Q1::always_constexpr_unit && Q2::always_constexpr_unit) static constexpr auto add(Q1 x, Q2 y) { using r_repr_type = std::common_type_t; using r_int_type = std::common_type_t; using r_int2x_type = std::common_type_t; /* conversion to get y in same units as x: multiply by y/x */ auto rr = detail::su_ratio(y.unit(), x.unit()); if (rr.natural_unit_.is_dimensionless()) { r_repr_type r_scale = (static_cast(x.scale()) + (::sqrt(rr.outer_scale_sq_) * rr.outer_scale_factor_.template convert_to() * static_cast(y.scale()))); return quantity(r_scale); } else { /* units don't match! */ return quantity(std::numeric_limits::quiet_NaN()); } } template requires(quantity_concept && quantity_concept && Q1::always_constexpr_unit && Q2::always_constexpr_unit) static constexpr auto subtract(Q1 x, Q2 y) { using r_repr_type = std::common_type_t; using r_int_type = std::common_type_t; using r_int2x_type = std::common_type_t; /* conversion to get y in same units as x: multiply by y/x */ auto rr = detail::su_ratio(y.unit(), x.unit()); if (rr.natural_unit_.is_dimensionless()) { r_repr_type r_scale = (static_cast(x.scale()) - (::sqrt(rr.outer_scale_sq_) * rr.outer_scale_factor_.template convert_to() * static_cast(y.scale()))); return quantity(r_scale); } else { /* units don't match! */ return quantity(std::numeric_limits::quiet_NaN()); } } }; } /*namespace detail*/ template requires(quantity_concept && Q1::always_constexpr_unit) constexpr auto with_units(const Q1 & x) { return x.template rescale_ext(); } template requires (quantity_concept && quantity_concept && Q1::always_constexpr_unit && Q2::always_constexpr_unit) constexpr auto with_units_from(const Q1 & x, const Q2 & y) { return x.template rescale(); } template requires (quantity_concept && Q1::always_constexpr_unit) constexpr auto with_repr(const Q1 & x) { return x.template with_repr(); } /** note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr) **/ template requires (quantity_concept && quantity_concept && Q1::always_constexpr_unit && Q2::always_constexpr_unit) constexpr auto operator* (const Q1 & x, const Q2 & y) { return detail::quantity_util::multiply(x, y); } /** note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr) **/ template requires (quantity_concept && quantity_concept && Q1::always_constexpr_unit && Q2::always_constexpr_unit) constexpr auto operator/ (const Q1 & x, const Q2 & y) { return detail::quantity_util::divide(x, y); } /** note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr) **/ template requires (quantity_concept && quantity_concept && Q1::always_constexpr_unit && Q2::always_constexpr_unit) constexpr auto operator+ (const Q1 & x, const Q2 & y) { return detail::quantity_util::add(x, y); } /** note: won't have constexpr result w/ fractional dimension until c++26 (when ::sqrt(), ::pow() are constexpr) **/ template requires (quantity_concept && quantity_concept && Q1::always_constexpr_unit && Q2::always_constexpr_unit) constexpr auto operator- (const Q1 & x, const Q2 & y) { return detail::quantity_util::subtract(x, y); } namespace qty { // ----- mass ----- template inline constexpr auto picograms(Repr x) { return quantity(x); } template inline constexpr auto nanograms(Repr x) { return quantity(x); } template inline constexpr auto micrograms(Repr x) { return quantity(x); } template inline constexpr auto milligrams(Repr x) { return quantity(x); } template inline constexpr auto grams(Repr x) { return quantity(x); } template inline constexpr auto kilograms(Repr x) { return quantity(x); } template inline constexpr auto tonnes(Repr x) { return quantity(x); } template inline constexpr auto kilotonnes(Repr x) { return quantity(x); } template inline constexpr auto megatonnes(Repr x) { return quantity(x); } template inline constexpr auto gigatonnes(Repr x) { return quantity(x); } // ----- distance ----- template inline constexpr auto picometers(Repr x) { return quantity(x); } template inline constexpr auto nanometers(Repr x) { return quantity(x); } template inline constexpr auto micrometers(Repr x) { return quantity(x); } template inline constexpr auto millimeters(Repr x) { return quantity(x); } template inline constexpr auto meters(Repr x) { return quantity(x); } template inline constexpr auto kilometers(Repr x) { return quantity(x); } template inline constexpr auto megameters(Repr x) { return quantity(x); } template inline constexpr auto gigameters(Repr x) { return quantity(x); } template inline constexpr auto lightseconds(Repr x) { return quantity(x); } template inline constexpr auto astronomicalunits(Repr x) { return quantity(x); } static constexpr auto meter = meters(1); // ----- time ----- template inline constexpr auto picoseconds(Repr x) { return quantity(x); } template inline constexpr auto nanoseconds(Repr x) { return quantity(x); } template inline constexpr auto microseconds(Repr x) { return quantity(x); } template inline constexpr auto milliseconds(Repr x) { return quantity(x); } template inline constexpr auto seconds(Repr x) { return quantity(x); } template inline constexpr auto minutes(Repr x) { return quantity(x); } template inline constexpr auto hours(Repr x) { return quantity(x); } template inline constexpr auto days(Repr x) { return quantity(x); } template inline constexpr auto weeks(Repr x) { return quantity(x); } template inline constexpr auto months(Repr x) { return quantity(x); } template inline constexpr auto years(Repr x) { return quantity(x); } template inline constexpr auto year250s(Repr x) { return quantity(x); } template inline constexpr auto year360s(Repr x) { return quantity(x); } template inline constexpr auto year365s(Repr x) { return quantity(x); } static constexpr auto second = seconds(1); // ----- volatility ----- /* variance expressed has dimension 1/t; * volatility ~ sqrt(variance), has dimension 1/sqrt(t) */ template inline constexpr auto volatility_30d(Repr x) { return quantity(x); } template inline constexpr auto volatility_250d(Repr x) { return quantity(x); } template inline constexpr auto volatility_360d(Repr x) { return quantity(x); } } /* reminder: see [quantity_ops.hpp] for operator* etc */ } /*namespace qty*/ } /*namespace xo*/ /** end quantity.hpp **/