From 51559d094be4f2d265adfa7b90c4c6c41c32831a Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 6 Feb 2025 16:55:37 +0100 Subject: [PATCH] refactor: `might_store_converted_value` refactored to `overflows_non_zero_values` --- .../include/mp-units/framework/quantity.h | 12 ++++---- .../include/mp-units/framework/value_cast.h | 28 +++++++++---------- src/core/include/mp-units/math.h | 2 +- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/core/include/mp-units/framework/quantity.h b/src/core/include/mp-units/framework/quantity.h index de2621ff..3432a626 100644 --- a/src/core/include/mp-units/framework/quantity.h +++ b/src/core/include/mp-units/framework/quantity.h @@ -69,7 +69,7 @@ concept ValuePreservingTo = Representation> && Repr Unit && std::assignable_from && (IsFloatingPoint || (!IsFloatingPoint> && integral_conversion_factor(FromUnit, ToUnit) && - might_store_converted_value(FromUnit, ToUnit))); + !overflows_non_zero_values(FromUnit, ToUnit))); template concept QuantityConvertibleTo = @@ -103,10 +103,10 @@ using common_quantity_for = quantity>; template -[[nodiscard]] consteval bool might_store_converted_common_value(U1 u1, U2 u2) +[[nodiscard]] consteval bool overflows_non_zero_common_values(U1 u1, U2 u2) { constexpr Unit auto cu = get_common_unit(U1{}, U2{}); - return might_store_converted_value(u1, cu) && might_store_converted_value(u2, cu); + return overflows_non_zero_values(u1, cu) || overflows_non_zero_values(u2, cu); } template @@ -115,8 +115,8 @@ concept CommonlyInvocableQuantities = std::convertible_to> && std::convertible_to> && InvocableQuantities && - might_store_converted_common_value>(Q1::unit, - Q2::unit); + (!overflows_non_zero_common_values>(Q1::unit, + Q2::unit)); template concept SameValueAs = (equivalent(get_unit(R1), get_unit(R2))) && std::convertible_to; @@ -651,7 +651,7 @@ template { mp_units::get_common_reference(Q1::reference, Q2::reference) } -> mp_units::Reference; typename std::common_type_t; requires( - might_store_converted_common_value>(Q1::unit, Q2::unit)); + !overflows_non_zero_common_values>(Q1::unit, Q2::unit)); requires mp_units::RepresentationOf, mp_units::get_common_quantity_spec(Q1::quantity_spec, Q2::quantity_spec)>; } diff --git a/src/core/include/mp-units/framework/value_cast.h b/src/core/include/mp-units/framework/value_cast.h index c5bece13..aaa1ed9c 100644 --- a/src/core/include/mp-units/framework/value_cast.h +++ b/src/core/include/mp-units/framework/value_cast.h @@ -38,22 +38,22 @@ namespace mp_units { namespace detail { template -[[nodiscard]] consteval bool might_store_converted_value(UFrom from, UTo to) +[[nodiscard]] consteval bool overflows_non_zero_values(UFrom from, UTo to) { if constexpr (is_same_v || treat_as_floating_point) - return true; + return false; else if constexpr (std::totally_ordered_with && requires(Rep v) { representation_values::max(); }) { constexpr auto factor = get_value(numerator(get_canonical_unit(from).mag / get_canonical_unit(to).mag)); if constexpr (std::is_integral_v) - return std::in_range(factor); + return !std::in_range(factor); else - return factor <= representation_values::max(); + return factor > representation_values::max(); } else // if the representation is not totally ordered with std::uintmax_t or does not have max() defined // then we assume that it might store any value - return true; + return false; } } // namespace detail @@ -70,7 +70,7 @@ template */ template> requires detail::UnitCompatibleWith && - (detail::might_store_converted_value(Q::unit, ToU)) + (!detail::overflows_non_zero_values(Q::unit, ToU)) [[nodiscard]] constexpr Quantity auto value_cast(FwdQ&& q) { return detail::sudo_cast>( @@ -107,7 +107,7 @@ template> requires detail::UnitCompatibleWith && - (detail::might_store_converted_value(Q::unit, ToU)) && + (!detail::overflows_non_zero_values(Q::unit, ToU)) && RepresentationOf && std::constructible_from [[nodiscard]] constexpr Quantity auto value_cast(FwdQ&& q) { @@ -116,7 +116,7 @@ template> requires detail::UnitCompatibleWith && - (detail::might_store_converted_value(Q::unit, ToU)) && + (!detail::overflows_non_zero_values(Q::unit, ToU)) && RepresentationOf && std::constructible_from [[nodiscard]] constexpr Quantity auto value_cast(FwdQ&& q) { @@ -140,7 +140,7 @@ template> requires detail::UnitCompatibleWith && - (detail::might_store_converted_value(Q::unit, ToQ::unit)) && + (!detail::overflows_non_zero_values(Q::unit, ToQ::unit)) && (ToQ::quantity_spec == Q::quantity_spec) && std::constructible_from [[nodiscard]] constexpr Quantity auto value_cast(FwdQ&& q) { @@ -159,7 +159,7 @@ template> */ template> requires detail::UnitCompatibleWith && - (detail::might_store_converted_value(QP::unit, ToU)) + (!detail::overflows_non_zero_values(QP::unit, ToU)) [[nodiscard]] constexpr QuantityPoint auto value_cast(FwdQP&& qp) { return quantity_point{value_cast(std::forward(qp).quantity_from_origin_is_an_implementation_detail_), @@ -197,7 +197,7 @@ template> requires detail::UnitCompatibleWith && - (detail::might_store_converted_value(QP::unit, ToU)) && + (!detail::overflows_non_zero_values(QP::unit, ToU)) && RepresentationOf && std::constructible_from [[nodiscard]] constexpr QuantityPoint auto value_cast(FwdQP&& qp) { @@ -208,7 +208,7 @@ template> requires detail::UnitCompatibleWith && - (detail::might_store_converted_value(QP::unit, ToU)) && + (!detail::overflows_non_zero_values(QP::unit, ToU)) && RepresentationOf && std::constructible_from [[nodiscard]] constexpr QuantityPoint auto value_cast(FwdQP&& qp) { @@ -233,7 +233,7 @@ template> requires detail::UnitCompatibleWith && - (detail::might_store_converted_value(QP::unit, ToQ::unit)) && + (!detail::overflows_non_zero_values(QP::unit, ToQ::unit)) && (ToQ::quantity_spec == QP::quantity_spec) && std::constructible_from [[nodiscard]] constexpr QuantityPoint auto value_cast(FwdQP&& qp) { @@ -271,7 +271,7 @@ template> requires detail::UnitCompatibleWith && - (detail::might_store_converted_value(QP::unit, ToQP::unit)) && + (!detail::overflows_non_zero_values(QP::unit, ToQP::unit)) && (ToQP::quantity_spec == QP::quantity_spec) && (detail::same_absolute_point_origins(ToQP::point_origin, QP::point_origin)) && std::constructible_from diff --git a/src/core/include/mp-units/math.h b/src/core/include/mp-units/math.h index de1dc2e3..62186c98 100644 --- a/src/core/include/mp-units/math.h +++ b/src/core/include/mp-units/math.h @@ -405,7 +405,7 @@ template */ template [[nodiscard]] constexpr Quantity auto inverse(const quantity& q) - requires(detail::might_store_converted_value(one / get_unit(R), To)) && requires { + requires(!detail::overflows_non_zero_values(one / get_unit(R), To)) && requires { representation_values::one(); value_cast(representation_values::one() / q); }