Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/core/include/mp-units/framework/unit.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ struct unit_interface {
* Multiplication by `1` returns the same unit, otherwise `scaled_unit` is being returned.
*/
template<UnitMagnitude M, Unit U>
[[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<M, MP_UNITS_NONCONST_TYPE(mp_units::mag<1>)>)
return u;
Expand All @@ -171,7 +171,7 @@ struct unit_interface {
* Returns the result of multiplication with an inverse unit.
*/
template<UnitMagnitude M, Unit U>
[[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);
}
Expand All @@ -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<Unit Lhs, Unit Rhs>
[[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<derived_unit, struct one>(lhs, rhs);
}
Expand All @@ -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<Unit Lhs, Unit Rhs>
[[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<derived_unit, struct one>(lhs, rhs);
}
Expand Down Expand Up @@ -593,7 +593,7 @@ template<Unit T, typename... Expr>

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
Expand Down
27 changes: 27 additions & 0 deletions test/runtime/math_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<si::hertz>(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<si::hertz>(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<si::hertz>(period_ms);
REQUIRE(frequency == 2.0 * isq::frequency[Hz]);
}
}
}
Loading