From 3e86011f514b87d70d1c23e438939dcf6103b076 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Tue, 7 May 2024 10:35:54 -0400 Subject: [PATCH] xo-unit: simplify quantity Always get ratio numerator/denominator type from NaturalUnit --- include/xo/unit/quantity.hpp | 42 ++++++++++----------------- include/xo/unit/quantity_iostream.hpp | 4 +-- utest/quantity.test.cpp | 6 ++-- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/include/xo/unit/quantity.hpp b/include/xo/unit/quantity.hpp index 621b206b..e41e96cf 100644 --- a/include/xo/unit/quantity.hpp +++ b/include/xo/unit/quantity.hpp @@ -18,15 +18,14 @@ namespace xo { * sizeof(quantity) == sizeof(Repr). **/ template < - auto /*natural_unit*/ NaturalUnit, - typename Repr = double, - typename Int2x = detail::width2x_t > + auto NaturalUnit, + typename Repr = double> 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; + using ratio_int_type = unit_type::ratio_int_type; + using ratio_int2x_type = detail::width2x_t; public: constexpr quantity() : scale_{0} {} @@ -47,16 +46,13 @@ namespace xo { constexpr auto reciprocal() const { return quantity(1.0 / scale_); + repr_type>(1.0 / scale_); } template constexpr auto with_repr() const { - return quantity(scale_); + return quantity(scale_); } /* parallel implementation to Quantity::rescale(), @@ -77,9 +73,9 @@ namespace xo { : ::sqrt(rr.outer_scale_sq_)) * rr.outer_scale_factor_.template convert_to() * this->scale_); - return quantity(r_scale); + return quantity(r_scale); } else { - return quantity(std::numeric_limits::quiet_NaN()); + return quantity(std::numeric_limits::quiet_NaN()); } } @@ -101,9 +97,9 @@ namespace xo { * rr.outer_scale_factor_.template convert_to() * this->scale_ / ScaledUnit2.outer_scale_factor_.template convert_to()); - return quantity(r_scale); + return quantity(r_scale); } else { - return quantity(std::numeric_limits::quiet_NaN()); + return quantity(std::numeric_limits::quiet_NaN()); } } @@ -205,10 +201,7 @@ namespace xo { * static_cast(x.scale()) * static_cast(y.scale())); - return quantity(r_scale); + return quantity(r_scale); } template @@ -233,10 +226,7 @@ namespace xo { * static_cast(x.scale()) / static_cast(y.scale())); - return quantity(r_scale); + return quantity(r_scale); } template @@ -260,10 +250,10 @@ namespace xo { * rr.outer_scale_factor_.template convert_to() * static_cast(y.scale()))); - return quantity(r_scale); + return quantity(r_scale); } else { /* units don't match! */ - return quantity(std::numeric_limits::quiet_NaN()); + return quantity(std::numeric_limits::quiet_NaN()); } } @@ -288,10 +278,10 @@ namespace xo { * rr.outer_scale_factor_.template convert_to() * static_cast(y.scale()))); - return quantity(r_scale); + return quantity(r_scale); } else { /* units don't match! */ - return quantity(std::numeric_limits::quiet_NaN()); + return quantity(std::numeric_limits::quiet_NaN()); } } }; diff --git a/include/xo/unit/quantity_iostream.hpp b/include/xo/unit/quantity_iostream.hpp index e30dc1dc..4a1faf73 100644 --- a/include/xo/unit/quantity_iostream.hpp +++ b/include/xo/unit/quantity_iostream.hpp @@ -10,10 +10,10 @@ namespace xo { namespace qty { - template < auto NaturalUnit, typename Repr, typename Int2x > + template < auto NaturalUnit, typename Repr > inline std::ostream & operator<< (std::ostream & os, - const quantity & x) + const quantity & x) { os << x.scale() << x.abbrev(); return os; diff --git a/utest/quantity.test.cpp b/utest/quantity.test.cpp index ba477a39..0e0582f4 100644 --- a/utest/quantity.test.cpp +++ b/utest/quantity.test.cpp @@ -388,8 +388,7 @@ namespace xo { static_assert(rr.natural_unit_.n_bpu() == 1); constexpr auto q1 = quantity(ms.scale() * ms.scale()); + decltype(ms)::repr_type>(ms.scale() * ms.scale()); /* proof that q is constexpr */ static_assert(q1.scale() == 1.0); @@ -440,8 +439,7 @@ namespace xo { static_assert(rr.natural_unit_.n_bpu() == 0); constexpr auto q1 = quantity(ms.scale() * ms.scale()); + decltype(ms)::repr_type>(ms.scale() * ms.scale()); /* proof that q is constexpr */ static_assert(q1.scale() == 1.0);