From eeece8a3566136ab4c6f7ff9870bc89cf211fc65 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 2 Oct 2024 16:10:29 +0200 Subject: [PATCH] feat: `scaled_unit` does not have a priority over `derived_unit` anymore We want `pow<2>(mag<3600> * second)` to print `[3600 s]^2` and `42 * (mag<10> * metre) / (mag<20> * second)` to print `42 [10 m]/[20 s]` --- src/core/include/mp-units/framework/unit.h | 20 ++------------------ test/static/unit_symbol_test.cpp | 14 +++++++++++--- test/static/unit_test.cpp | 7 +++---- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/src/core/include/mp-units/framework/unit.h b/src/core/include/mp-units/framework/unit.h index b5ed5837..1a8a69c7 100644 --- a/src/core/include/mp-units/framework/unit.h +++ b/src/core/include/mp-units/framework/unit.h @@ -180,14 +180,7 @@ struct unit_interface { template [[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator*(Lhs lhs, Rhs rhs) { - if constexpr (is_specialization_of_scaled_unit && is_specialization_of_scaled_unit) - return (Lhs::mag * Rhs::mag) * (Lhs::reference_unit * Rhs::reference_unit); - else if constexpr (is_specialization_of_scaled_unit) - return Lhs::mag * (Lhs::reference_unit * rhs); - else if constexpr (is_specialization_of_scaled_unit) - return Rhs::mag * (lhs * Rhs::reference_unit); - else - return expr_multiply(lhs, rhs); + return expr_multiply(lhs, rhs); } /** @@ -198,14 +191,7 @@ struct unit_interface { template [[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator/(Lhs lhs, Rhs rhs) { - if constexpr (is_specialization_of_scaled_unit && is_specialization_of_scaled_unit) - return (Lhs::mag / Rhs::mag) * (Lhs::reference_unit / Rhs::reference_unit); - else if constexpr (is_specialization_of_scaled_unit) - return Lhs::mag * (Lhs::reference_unit / rhs); - else if constexpr (is_specialization_of_scaled_unit) - return mag<1> / Rhs::mag * (lhs / Rhs::reference_unit); - else - return expr_divide(lhs, rhs); + return expr_divide(lhs, rhs); } [[nodiscard]] friend consteval bool operator==(Unit auto lhs, Unit auto rhs) @@ -615,8 +601,6 @@ template return one; else if constexpr (detail::ratio{Num, Den} == 1) return u; - else if constexpr (detail::is_specialization_of_scaled_unit) - return scaled_unit(U::mag), decltype(pow(U::reference_unit))>{}; else if constexpr (is_specialization_of) return detail::expr_pow(u); else if constexpr (Den == 1) diff --git a/test/static/unit_symbol_test.cpp b/test/static/unit_symbol_test.cpp index c861dd68..1ba9aa69 100644 --- a/test/static/unit_symbol_test.cpp +++ b/test/static/unit_symbol_test.cpp @@ -120,10 +120,15 @@ static_assert(unit_symbol(mag<6> * mag_power<10, 3> * metre) == "[6 × 10³ m]") static_assert(unit_symbol(mag<6000> * metre) == "[6 x 10^3 m]"); static_assert(unit_symbol(mag<10600> * metre) == "[10600 m]"); static_assert(unit_symbol(mag<60> * second) == "[60 s]"); -static_assert(unit_symbol(mag_ratio<1, 18> * metre / second) == "[1/18 m/s]"); -static_assert(unit_symbol(mag_ratio<1, 1800> * metre / second) == "[1/1800 m/s]"); -static_assert(unit_symbol(mag_ratio<1, 18000> * metre / second) == "[1/18 × 10⁻³ m/s]"); +static_assert(unit_symbol(mag_ratio<1, 18> * metre / second) == "[1/18 m]/s"); +static_assert(unit_symbol(mag_ratio<1, 18> * (metre / second)) == "[1/18 m/s]"); +static_assert(unit_symbol(mag_ratio<1, 1800> * metre / second) == "[1/1800 m]/s"); +static_assert(unit_symbol(mag_ratio<1, 1800> * (metre / second)) == "[1/1800 m/s]"); +static_assert(unit_symbol(mag_ratio<1, 18000> * metre / second) == "[1/18 × 10⁻³ m]/s"); +static_assert(unit_symbol(mag_ratio<1, 18000> * (metre / second)) == "[1/18 × 10⁻³ m/s]"); static_assert(unit_symbol(mag_ratio<1, 18000> * metre / second) == + "[1/18 x 10^-3 m]/s"); +static_assert(unit_symbol(mag_ratio<1, 18000> * (metre / second)) == "[1/18 x 10^-3 m/s]"); // common units @@ -202,6 +207,9 @@ static_assert(unit_symbol(pow<1, 2>(metre)) == "m^(1/2)"); static_assert(unit_symbol(pow<3, 5>(metre)) == "m^(3/5)"); static_assert(unit_symbol(pow<1, 2>(metre / second)) == "m^(1/2)/s^(1/2)"); static_assert(unit_symbol(pow<1, 2>(metre / second)) == "m^(1/2) s^-(1/2)"); +static_assert(unit_symbol(litre / (mag<100> * kilo)) == "l/[100 km]"); +static_assert(unit_symbol((mag<10> * metre) / (mag<20> * second)) == "[10 m]/[20 s]"); +static_assert(unit_symbol(pow<2>(mag<3600> * second)) == "[3600 s]²"); // dimensionless unit static_assert(unit_symbol(radian) == "rad"); diff --git a/test/static/unit_test.cpp b/test/static/unit_test.cpp index 5a82aef3..7af8dae0 100644 --- a/test/static/unit_test.cpp +++ b/test/static/unit_test.cpp @@ -384,7 +384,7 @@ static_assert(is_of_type * kilometre / hour; -static_assert(is_of_type, derived_unit, per>>>); +static_assert(is_of_type, kilo_>, per>>); static_assert(is_of_type>>); static_assert(get_canonical_unit(u1).mag == mag_ratio<1'000'000, 3'600>); @@ -394,7 +394,7 @@ static_assert(is_of_type); constexpr auto u3 = one / hour * (mag<1000> * kilometre); -static_assert(is_of_type, derived_unit, per>>>); +static_assert(is_of_type, kilo_>, per>>); static_assert(is_of_type>>); static_assert(get_canonical_unit(u3).mag == mag_ratio<1'000'000, 3'600>); @@ -515,8 +515,7 @@ static_assert(is_of_type static_assert(is_of_type(kilometre), derived_unit, 2>>>); static_assert(is_of_type(kilo), derived_unit, 2>>>); static_assert(is_of_type(hour), derived_unit>>); -static_assert( - is_of_type(mag<3600>* second), scaled_unit * mag<3600>, derived_unit>>>); +static_assert(is_of_type(mag<3600>* second), derived_unit, second_>, 2>>>); // get_common_unit static_assert(is_of_type);