diff --git a/src/core/include/mp-units/framework/unit.h b/src/core/include/mp-units/framework/unit.h index cd1d9eda18..b8cc12348b 100644 --- a/src/core/include/mp-units/framework/unit.h +++ b/src/core/include/mp-units/framework/unit.h @@ -147,7 +147,7 @@ struct unit_interface { * Multiplication by `1` returns the same unit, otherwise `scaled_unit` is being returned. */ template - [[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator*(M, U u) + [[nodiscard]] friend constexpr Unit auto operator*(M, U u) { if constexpr (std::is_same_v)>) return u; @@ -171,7 +171,7 @@ struct unit_interface { * Returns the result of multiplication with an inverse unit. */ template - [[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator/(M mag, U u) + [[nodiscard]] friend constexpr Unit auto operator/(M mag, U u) { return mag * inverse(u); } @@ -182,7 +182,7 @@ struct unit_interface { * to the derived unit and the magnitude remains outside forming another scaled unit as a result of the operation. */ template - [[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator*(Lhs lhs, Rhs rhs) + [[nodiscard]] friend constexpr Unit auto operator*(Lhs lhs, Rhs rhs) { return expr_multiply(lhs, rhs); } @@ -193,7 +193,7 @@ struct unit_interface { * to the derived unit and the magnitude remains outside forming another scaled unit as a result of the operation. */ template - [[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator/(Lhs lhs, Rhs rhs) + [[nodiscard]] friend constexpr Unit auto operator/(Lhs lhs, Rhs rhs) { return expr_divide(lhs, rhs); } @@ -593,7 +593,7 @@ template MP_UNITS_EXPORT_BEGIN -[[nodiscard]] MP_UNITS_CONSTEVAL Unit auto inverse(Unit auto u) { return one / u; } +[[nodiscard]] constexpr Unit auto inverse(Unit auto u) { return one / u; } /** * @brief Computes the value of a unit raised to the `Num/Den` power diff --git a/test/runtime/math_test.cpp b/test/runtime/math_test.cpp index 3ef0013439..0305077cfd 100644 --- a/test/runtime/math_test.cpp +++ b/test/runtime/math_test.cpp @@ -589,4 +589,31 @@ TEST_CASE("math operations", "[math]") REQUIRE_THAT(atan2(1. * isq::length[km], 1000. * isq::length[m]), AlmostEquals(45. * angle[deg])); } } + + SECTION("inverse functions") + { + SECTION("inverse of time quantity returns frequency") + { + auto period = 2.0 * isq::time[s]; + auto frequency = inverse(period); + REQUIRE(frequency == 0.5 * isq::frequency[Hz]); + } + + SECTION("inverse works with runtime values") + { + // Test the specific case that fails with consteval + double runtime_value = 3.0; + auto period = runtime_value * isq::time[s]; + auto frequency = inverse(period); + auto expected = (1.0 / 3.0) * isq::frequency[Hz]; + REQUIRE_THAT(frequency, AlmostEquals(expected)); + } + + SECTION("inverse with different input units") + { + auto period_ms = 500.0 * isq::time[ms]; + auto frequency = inverse(period_ms); + REQUIRE(frequency == 2.0 * isq::frequency[Hz]); + } + } }