diff --git a/src/core/include/mp-units/math.h b/src/core/include/mp-units/math.h index a023333070..744c24a1e1 100644 --- a/src/core/include/mp-units/math.h +++ b/src/core/include/mp-units/math.h @@ -413,13 +413,15 @@ template } { #if MP_UNITS_API_NATURAL_UNITS - if constexpr (!MP_UNITS_ASSOCIATED_UNIT) - return (representation_values::one() * one).force_in(To * q.unit) / q; - else + if constexpr (!MP_UNITS_ASSOCIATED_UNIT) { + constexpr Unit auto unit = To * quantity::unit; + return (representation_values::one() * one).force_in(unit) / q; + } else #endif { constexpr QuantitySpec auto qs = get_quantity_spec(To) * quantity::quantity_spec; - return qs(representation_values::one() * one).force_in(To * q.unit) / q; + constexpr Unit auto unit = To * quantity::unit; + return qs(representation_values::one() * one).force_in(unit) / q; } } 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]); + } + } }