From df6cfbb25c5c491fdc1485ab34634f98cb87b2d3 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 7 May 2024 10:38:38 -0400 Subject: [PATCH] xo-unit: promote to common repr in mixed-type operator* w/ quantity Avoid truncating when, for example, multiplying quantity * double. Add targeted unit test for this --- include/xo/unit/quantity.hpp | 4 +++- utest/quantity.test.cpp | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/xo/unit/quantity.hpp b/include/xo/unit/quantity.hpp index e41e96cf..a386b843 100644 --- a/include/xo/unit/quantity.hpp +++ b/include/xo/unit/quantity.hpp @@ -106,7 +106,9 @@ namespace xo { template requires std::is_arithmetic_v constexpr auto scale_by(Dimensionless x) const { - return quantity(x * this->scale_); + using r_repr_type = std::common_type_t; + + return quantity(x * this->scale_); } // divide_by diff --git a/utest/quantity.test.cpp b/utest/quantity.test.cpp index 0e0582f4..603a24d8 100644 --- a/utest/quantity.test.cpp +++ b/utest/quantity.test.cpp @@ -421,6 +421,25 @@ namespace xo { } /*TEST_CASE(quantity.mult2)*/ + TEST_CASE("quantity.mult3", "[quantity.mult]") { + constexpr auto ng = qty::nanogram; + + constexpr auto l_qty = 7.55 * ng; + constexpr auto r_qty = ng * 7.55; + + static_assert(l_qty.unit().n_bpu() == 1); + static_assert(l_qty.unit().bpu_v_[0].native_dim() == dim::mass); + static_assert(l_qty.unit().bpu_v_[0].power() == xo::ratio::ratio(1,1)); + static_assert(l_qty.unit().bpu_v_[0].scalefactor() == xo::ratio::ratio(1,1000000000)); + static_assert(l_qty.scale() == 7.55); + + static_assert(r_qty.unit().n_bpu() == 1); + static_assert(r_qty.unit().bpu_v_[0].native_dim() == dim::mass); + static_assert(r_qty.unit().bpu_v_[0].power() == xo::ratio::ratio(1,1)); + static_assert(r_qty.unit().bpu_v_[0].scalefactor() == xo::ratio::ratio(1,1000000000)); + static_assert(r_qty.scale() == 7.55); + } + TEST_CASE("quantity.div2", "[quantity.div]") { constexpr auto ms = qty::milliseconds(1.0);