xo-unit: + Quantity.add support

This commit is contained in:
Roland Conybeare 2024-04-23 15:36:11 -04:00
commit e1d7f62ca6
6 changed files with 120 additions and 13 deletions

View file

@ -44,13 +44,13 @@ namespace xo {
constexpr Quantity reciprocal() const { return Quantity(1.0 / scale_, unit_.reciprocal()); }
template <typename OtherQuantity>
template <typename Quantity2>
static constexpr
auto multiply(const Quantity & x, const OtherQuantity & y) {
auto multiply(const Quantity & x, const Quantity2 & y) {
using r_repr_type = std::common_type_t<typename Quantity::repr_type,
typename OtherQuantity::repr_type>;
typename Quantity2::repr_type>;
using r_int_type = std::common_type_t<typename Quantity::ratio_int_type,
typename OtherQuantity::ratio_int_type>;
typename Quantity2::ratio_int_type>;
auto rr = detail::nu_product(x.unit(), y.unit());
@ -85,6 +85,30 @@ namespace xo {
rr.natural_unit_);
}
template <typename Quantity2>
static constexpr
auto add(const Quantity & x, const Quantity2 & y) {
using r_repr_type = std::common_type_t<typename Quantity::repr_type,
typename Quantity2::repr_type>;
using r_int_type = std::common_type_t<typename Quantity::ratio_int_type,
typename Quantity2::ratio_int_type>;
/* conversion to get y in same units as x: multiply by y/x */
auto rr = detail::nu_ratio(y.unit(), x.unit());
if (rr.natural_unit_.is_dimensionless()) {
r_repr_type r_scale = (static_cast<r_repr_type>(x.scale())
+ (::sqrt(rr.outer_scale_sq_)
* rr.outer_scale_exact_.template to<r_repr_type>()
* static_cast<r_repr_type>(y.scale())));
return Quantity<r_repr_type, r_int_type>(r_scale, x.unit_.template to_repr<r_int_type>());
} else {
/* units don't match! */
return Quantity<r_repr_type, r_int_type>(std::numeric_limits<Repr>::quiet_NaN(),
x.unit_.template to_repr<r_int_type>());
}
}
private:
/** @brief quantity represents this multiple of a unit amount **/
@ -106,23 +130,47 @@ namespace xo {
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
**/
template <typename Quantity, typename OtherQuantity>
template <typename Repr = double,
typename Int = std::int64_t>
inline constexpr Quantity<Repr, Int>
natural_unit_qty(const natural_unit<Int> & nu) {
return Quantity<Repr, Int>(1.0, nu);
}
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
**/
template <typename Quantity, typename Quantity2>
requires quantity2_concept<Quantity> && quantity2_concept<Quantity2>
constexpr auto
operator* (const Quantity & x, const OtherQuantity & y)
operator* (const Quantity & x, const Quantity2 & y)
{
return Quantity::multiply(x, y);
}
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
**/
template <typename Quantity, typename OtherQuantity>
template <typename Quantity, typename Quantity2>
requires quantity2_concept<Quantity> && quantity2_concept<Quantity2>
constexpr auto
operator/ (const Quantity & x, const OtherQuantity & y)
operator/ (const Quantity & x, const Quantity2 & y)
{
return Quantity::divide(x, y);
}
/** note: won't have constexpr result until c++26 (when ::sqrt(), ::pow() are constexpr)
**/
template <typename Quantity, typename Quantity2>
requires quantity2_concept<Quantity> && quantity2_concept<Quantity2>
constexpr auto
operator+ (const Quantity & x, const Quantity2 & y)
{
return Quantity::add(x, y);
}
namespace unit {
constexpr auto nanogram = natural_unit_qty(nu2::nanogram);
}
} /*namespace qty*/
} /*namespace xo*/
/** end Quantity.hpp **/

View file

@ -1,4 +1,4 @@
/** @file basis_unit2.hpp **/
/** @file basis_unit.hpp **/
#pragma once
@ -211,5 +211,4 @@ namespace xo {
} /*namespace qty*/
} /*namespace xo*/
/** end basis_unit2.hpp **/
/** end basis_unit.hpp **/

View file

@ -102,6 +102,13 @@ namespace xo {
return bpu<Int>(native_dim(), scalefactor(), power_.negate());
}
template <typename Int2>
constexpr bpu<Int2> to_repr() const {
return bpu<Int2>(this->native_dim(),
this->scalefactor(),
ratio::ratio<Int2>(power_.num(), power_.den()));
}
/** @brief this unit represents native dimension taken to this power **/
power_ratio_type power_;
};

View file

@ -33,6 +33,8 @@ namespace xo {
constexpr natural_unit() : n_bpu_{0} {}
constexpr std::size_t n_bpu() const { return n_bpu_; }
constexpr bool is_dimensionless() const { return n_bpu_ == 0; }
constexpr bpu<Int> * bpu_v() const { return bpu_v_; }
constexpr nu_abbrev_type abbrev() const {
@ -63,6 +65,17 @@ namespace xo {
constexpr bpu<Int> & operator[](std::size_t i) { return bpu_v_[i]; }
constexpr const bpu<Int> & operator[](std::size_t i) const { return bpu_v_[i]; }
template <typename Int2>
constexpr natural_unit<Int2> to_repr() const {
natural_unit<Int2> retval;
std::size_t i = 0;
for (; i < n_bpu_; ++i)
retval.push_back(bpu_v_[i].template to_repr<Int2>());
return retval;
}
private:
/** @brief the number of occupied slots in @c bpu_v_ **/
std::size_t n_bpu_;

View file

@ -13,7 +13,8 @@ namespace xo {
typename Quantity::unit_type;
typename Quantity::repr_type;
{ qty.scale() } -> std::same_as<typename Quantity::repr_type>;
{ qty.scale() } -> std::same_as<const typename Quantity::repr_type &>;
{ qty.unit() } -> std::same_as<const typename Quantity::unit_type &>;
//{ Quantity::unit_cstr() } -> std::same_as<char const *>;
//{ Quantity::unit_quantity() } -> std::same_as<Quantity>;
//{ Quantity::promote(repr) } -> std::same_as<Quantity>;

View file

@ -1119,6 +1119,45 @@ namespace xo {
//REQUIRE(ng2.scale() == 1);
} /*TEST_CASE(Quantity5)*/
TEST_CASE("Quantity6", "[Quantity]") {
constexpr bool c_debug_flag = true;
// can get bits from /dev/random by uncommenting the 2nd line below
//uint64_t seed = xxx;
//rng::Seed<xoshio256ss> seed;
//auto rng = xo::rng::xoshiro256ss(seed);
scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.Quantity6"));
//log && log("(A)", xtag("foo", foo));
/* not constexpr until c++26 */
Quantity ng = unit_qty(su2::nanogram);
Quantity ug = unit_qty(su2::microgram);
{
auto sum1 = ng + ug;
log && log(xtag("ng+ug", sum1));
/* units will be nanograms, since that's on lhs */
REQUIRE(sum1.unit().n_bpu() == 1);
REQUIRE(sum1.unit()[0].scalefactor() == scalefactor_ratio_type(1, 1000000000));
REQUIRE(sum1.scale() == 1001.0);
}
{
auto sum2 = ug + ng;
log && log(xtag("ug+ng", sum2));
/* units will be micrograms, since that's on rhs */
REQUIRE(sum2.unit().n_bpu() == 1);
REQUIRE(sum2.unit()[0].scalefactor() == scalefactor_ratio_type(1, 1000000));
REQUIRE(sum2.scale() == 1.001);
}
//REQUIRE(ng2.scale() == 1);
} /*TEST_CASE(Quantity6)*/
} /*namespace ut*/
} /*namespace xo*/