mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-02 03:44:27 +02:00
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]`
This commit is contained in:
@@ -180,14 +180,7 @@ struct unit_interface {
|
|||||||
template<Unit Lhs, Unit Rhs>
|
template<Unit Lhs, Unit Rhs>
|
||||||
[[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator*(Lhs lhs, Rhs rhs)
|
[[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator*(Lhs lhs, Rhs rhs)
|
||||||
{
|
{
|
||||||
if constexpr (is_specialization_of_scaled_unit<Lhs> && is_specialization_of_scaled_unit<Rhs>)
|
return expr_multiply<derived_unit, struct one, type_list_of_unit_less>(lhs, rhs);
|
||||||
return (Lhs::mag * Rhs::mag) * (Lhs::reference_unit * Rhs::reference_unit);
|
|
||||||
else if constexpr (is_specialization_of_scaled_unit<Lhs>)
|
|
||||||
return Lhs::mag * (Lhs::reference_unit * rhs);
|
|
||||||
else if constexpr (is_specialization_of_scaled_unit<Rhs>)
|
|
||||||
return Rhs::mag * (lhs * Rhs::reference_unit);
|
|
||||||
else
|
|
||||||
return expr_multiply<derived_unit, struct one, type_list_of_unit_less>(lhs, rhs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -198,14 +191,7 @@ struct unit_interface {
|
|||||||
template<Unit Lhs, Unit Rhs>
|
template<Unit Lhs, Unit Rhs>
|
||||||
[[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator/(Lhs lhs, Rhs rhs)
|
[[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator/(Lhs lhs, Rhs rhs)
|
||||||
{
|
{
|
||||||
if constexpr (is_specialization_of_scaled_unit<Lhs> && is_specialization_of_scaled_unit<Rhs>)
|
return expr_divide<derived_unit, struct one, type_list_of_unit_less>(lhs, rhs);
|
||||||
return (Lhs::mag / Rhs::mag) * (Lhs::reference_unit / Rhs::reference_unit);
|
|
||||||
else if constexpr (is_specialization_of_scaled_unit<Lhs>)
|
|
||||||
return Lhs::mag * (Lhs::reference_unit / rhs);
|
|
||||||
else if constexpr (is_specialization_of_scaled_unit<Rhs>)
|
|
||||||
return mag<1> / Rhs::mag * (lhs / Rhs::reference_unit);
|
|
||||||
else
|
|
||||||
return expr_divide<derived_unit, struct one, type_list_of_unit_less>(lhs, rhs);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] friend consteval bool operator==(Unit auto lhs, Unit auto rhs)
|
[[nodiscard]] friend consteval bool operator==(Unit auto lhs, Unit auto rhs)
|
||||||
@@ -615,8 +601,6 @@ template<std::intmax_t Num, std::intmax_t Den = 1, Unit U>
|
|||||||
return one;
|
return one;
|
||||||
else if constexpr (detail::ratio{Num, Den} == 1)
|
else if constexpr (detail::ratio{Num, Den} == 1)
|
||||||
return u;
|
return u;
|
||||||
else if constexpr (detail::is_specialization_of_scaled_unit<U>)
|
|
||||||
return scaled_unit<pow<Num, Den>(U::mag), decltype(pow<Num, Den>(U::reference_unit))>{};
|
|
||||||
else if constexpr (is_specialization_of<U, derived_unit>)
|
else if constexpr (is_specialization_of<U, derived_unit>)
|
||||||
return detail::expr_pow<Num, Den, derived_unit, struct one, detail::type_list_of_unit_less>(u);
|
return detail::expr_pow<Num, Den, derived_unit, struct one, detail::type_list_of_unit_less>(u);
|
||||||
else if constexpr (Den == 1)
|
else if constexpr (Den == 1)
|
||||||
|
@@ -120,10 +120,15 @@ static_assert(unit_symbol(mag<6> * mag_power<10, 3> * metre) == "[6 × 10³ m]")
|
|||||||
static_assert(unit_symbol<unit_symbol_formatting{.encoding = ascii}>(mag<6000> * metre) == "[6 x 10^3 m]");
|
static_assert(unit_symbol<unit_symbol_formatting{.encoding = ascii}>(mag<6000> * metre) == "[6 x 10^3 m]");
|
||||||
static_assert(unit_symbol(mag<10600> * metre) == "[10600 m]");
|
static_assert(unit_symbol(mag<10600> * metre) == "[10600 m]");
|
||||||
static_assert(unit_symbol(mag<60> * second) == "[60 s]");
|
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, 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, 18> * (metre / second)) == "[1/18 m/s]");
|
||||||
static_assert(unit_symbol(mag_ratio<1, 18000> * metre / second) == "[1/18 × 10⁻³ 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<unit_symbol_formatting{.encoding = ascii}>(mag_ratio<1, 18000> * metre / second) ==
|
static_assert(unit_symbol<unit_symbol_formatting{.encoding = ascii}>(mag_ratio<1, 18000> * metre / second) ==
|
||||||
|
"[1/18 x 10^-3 m]/s");
|
||||||
|
static_assert(unit_symbol<unit_symbol_formatting{.encoding = ascii}>(mag_ratio<1, 18000> * (metre / second)) ==
|
||||||
"[1/18 x 10^-3 m/s]");
|
"[1/18 x 10^-3 m/s]");
|
||||||
|
|
||||||
// common units
|
// 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<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<unit_symbol_formatting{.solidus = never}>(pow<1, 2>(metre / second)) == "m^(1/2) s^-(1/2)");
|
static_assert(unit_symbol<unit_symbol_formatting{.solidus = never}>(pow<1, 2>(metre / second)) == "m^(1/2) s^-(1/2)");
|
||||||
|
static_assert(unit_symbol(litre / (mag<100> * kilo<metre>)) == "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
|
// dimensionless unit
|
||||||
static_assert(unit_symbol(radian) == "rad");
|
static_assert(unit_symbol(radian) == "rad");
|
||||||
|
@@ -384,7 +384,7 @@ static_assert(is_of_type<get_canonical_unit(standard_gravity / speed_of_light_in
|
|||||||
|
|
||||||
// operations commutativity
|
// operations commutativity
|
||||||
constexpr auto u1 = mag<1000> * kilometre / hour;
|
constexpr auto u1 = mag<1000> * kilometre / hour;
|
||||||
static_assert(is_of_type<u1, scaled_unit<mag<1000>, derived_unit<kilo_<metre_>, per<hour_>>>>);
|
static_assert(is_of_type<u1, derived_unit<scaled_unit<mag<1000>, kilo_<metre_>>, per<hour_>>>);
|
||||||
static_assert(is_of_type<get_canonical_unit(u1).reference_unit, derived_unit<metre_, per<second_>>>);
|
static_assert(is_of_type<get_canonical_unit(u1).reference_unit, derived_unit<metre_, per<second_>>>);
|
||||||
static_assert(get_canonical_unit(u1).mag == mag_ratio<1'000'000, 3'600>);
|
static_assert(get_canonical_unit(u1).mag == mag_ratio<1'000'000, 3'600>);
|
||||||
|
|
||||||
@@ -394,7 +394,7 @@ static_assert(is_of_type<get_canonical_unit(u2).reference_unit, derived_unit<met
|
|||||||
static_assert(get_canonical_unit(u2).mag == mag_ratio<1'000'000, 3'600>);
|
static_assert(get_canonical_unit(u2).mag == mag_ratio<1'000'000, 3'600>);
|
||||||
|
|
||||||
constexpr auto u3 = one / hour * (mag<1000> * kilometre);
|
constexpr auto u3 = one / hour * (mag<1000> * kilometre);
|
||||||
static_assert(is_of_type<u3, scaled_unit<mag<1000>, derived_unit<kilo_<metre_>, per<hour_>>>>);
|
static_assert(is_of_type<u3, derived_unit<scaled_unit<mag<1000>, kilo_<metre_>>, per<hour_>>>);
|
||||||
static_assert(is_of_type<get_canonical_unit(u3).reference_unit, derived_unit<metre_, per<second_>>>);
|
static_assert(is_of_type<get_canonical_unit(u3).reference_unit, derived_unit<metre_, per<second_>>>);
|
||||||
static_assert(get_canonical_unit(u3).mag == mag_ratio<1'000'000, 3'600>);
|
static_assert(get_canonical_unit(u3).mag == mag_ratio<1'000'000, 3'600>);
|
||||||
|
|
||||||
@@ -515,8 +515,7 @@ static_assert(is_of_type<kilometre * kilometre, derived_unit<power<kilo_<metre_>
|
|||||||
static_assert(is_of_type<pow<2>(kilometre), derived_unit<power<kilo_<metre_>, 2>>>);
|
static_assert(is_of_type<pow<2>(kilometre), derived_unit<power<kilo_<metre_>, 2>>>);
|
||||||
static_assert(is_of_type<pow<2>(kilo<metre>), derived_unit<power<kilo_<metre_>, 2>>>);
|
static_assert(is_of_type<pow<2>(kilo<metre>), derived_unit<power<kilo_<metre_>, 2>>>);
|
||||||
static_assert(is_of_type<pow<2>(hour), derived_unit<power<hour_, 2>>>);
|
static_assert(is_of_type<pow<2>(hour), derived_unit<power<hour_, 2>>>);
|
||||||
static_assert(
|
static_assert(is_of_type<pow<2>(mag<3600>* second), derived_unit<power<scaled_unit<mag<3600>, second_>, 2>>>);
|
||||||
is_of_type<pow<2>(mag<3600>* second), scaled_unit<mag<3600> * mag<3600>, derived_unit<power<second_, 2>>>>);
|
|
||||||
|
|
||||||
// get_common_unit
|
// get_common_unit
|
||||||
static_assert(is_of_type<get_common_unit(gram, gram), gram_>);
|
static_assert(is_of_type<get_common_unit(gram, gram), gram_>);
|
||||||
|
Reference in New Issue
Block a user