diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 31cf8ea7..3d192e1f 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -55,8 +55,6 @@ add_mp_units_module( include/mp-units/framework/dimension.h include/mp-units/framework/dimension_concepts.h include/mp-units/framework/expression_template.h - include/mp-units/framework/magnitude.h - include/mp-units/framework/magnitude_concepts.h include/mp-units/framework/quantity.h include/mp-units/framework/quantity_cast.h include/mp-units/framework/quantity_concepts.h @@ -71,6 +69,8 @@ add_mp_units_module( include/mp-units/framework/system_reference.h include/mp-units/framework/unit.h include/mp-units/framework/unit_concepts.h + include/mp-units/framework/unit_magnitude.h + include/mp-units/framework/unit_magnitude_concepts.h include/mp-units/framework/unit_symbol_formatting.h include/mp-units/framework/value_cast.h include/mp-units/compat_macros.h diff --git a/src/core/include/mp-units/bits/sudo_cast.h b/src/core/include/mp-units/bits/sudo_cast.h index 1264cc0a..b2d1f275 100644 --- a/src/core/include/mp-units/bits/sudo_cast.h +++ b/src/core/include/mp-units/bits/sudo_cast.h @@ -23,10 +23,10 @@ #pragma once #include -#include #include #include #include +#include namespace mp_units::detail { @@ -49,7 +49,7 @@ using maybe_common_type = * @tparam Rep1 first quantity representation type * @tparam Rep2 second quantity representation type */ -template +template struct conversion_type_traits { using c_rep_type = maybe_common_type; using c_mag_type = common_magnitude_type; @@ -74,11 +74,11 @@ struct conversion_type_traits { * @tparam M common magnitude between the two quantities * @tparam T common multiplier representation type */ -template +template struct conversion_value_traits { - static constexpr Magnitude auto num = _numerator(M); - static constexpr Magnitude auto den = _denominator(M); - static constexpr Magnitude auto irr = M * (den / num); + static constexpr UnitMagnitude auto num = _numerator(M); + static constexpr UnitMagnitude auto den = _denominator(M); + static constexpr UnitMagnitude auto irr = M * (den / num); static constexpr T num_mult = _get_value(num); static constexpr T den_mult = _get_value(den); static constexpr T irr_mult = _get_value(irr); @@ -108,7 +108,7 @@ template; using multiplier_type = typename type_traits::multiplier_type; // TODO the below crashed nearly every compiler I tried it on @@ -170,7 +170,7 @@ template; using value_traits = conversion_value_traits; using c_rep_type = typename type_traits::c_rep_type; diff --git a/src/core/include/mp-units/framework.h b/src/core/include/mp-units/framework.h index 2de63579..1a1e972c 100644 --- a/src/core/include/mp-units/framework.h +++ b/src/core/include/mp-units/framework.h @@ -29,8 +29,8 @@ #include #include #include -#include -#include +#include +#include #include #include #include diff --git a/src/core/include/mp-units/framework/unit.h b/src/core/include/mp-units/framework/unit.h index 311d5aac..3ff330e8 100644 --- a/src/core/include/mp-units/framework/unit.h +++ b/src/core/include/mp-units/framework/unit.h @@ -34,11 +34,11 @@ #include #include #include -#include #include #include #include #include +#include #include #ifndef MP_UNITS_IN_MODULE_INTERFACE @@ -61,7 +61,7 @@ namespace mp_units { namespace detail { -template +template struct scaled_unit_impl; template @@ -84,7 +84,7 @@ struct derived_unit_impl; * @tparam U a unit to use as a `reference_unit` * @tparam M a Magnitude representing an absolute scaling factor of this unit */ -template +template struct canonical_unit { M mag; U reference_unit; @@ -92,7 +92,7 @@ struct canonical_unit { #if MP_UNITS_COMP_CLANG -template +template canonical_unit(M, U) -> canonical_unit; #endif @@ -167,7 +167,7 @@ struct unit_interface { /** * Multiplication by `1` returns the same unit, otherwise `scaled_unit` is being returned. */ - template + template [[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator*(M, U u) { if constexpr (std::is_same_v)>) @@ -181,7 +181,7 @@ struct unit_interface { return scaled_unit{}; } - [[nodiscard]] friend consteval Unit auto operator*(Unit auto, Magnitude auto) + [[nodiscard]] friend consteval Unit auto operator*(Unit auto, UnitMagnitude auto) #if __cpp_deleted_function = delete("To scale a unit use `mag * unit` syntax"); #else @@ -191,7 +191,7 @@ struct unit_interface { /** * Returns the result of multiplication with an inverse unit. */ - template + template [[nodiscard]] friend MP_UNITS_CONSTEVAL Unit auto operator/(M mag, U u) { return mag * inverse(u); @@ -246,10 +246,10 @@ struct propagate_point_origin { static constexpr auto _point_origin_ = U::_point_origin_; }; -template +template struct scaled_unit_impl : detail::unit_interface, detail::propagate_point_origin { using _base_type_ = scaled_unit_impl; // exposition only - static constexpr Magnitude auto _mag_ = M; + static constexpr UnitMagnitude auto _mag_ = M; static constexpr U _reference_unit_{}; }; @@ -264,8 +264,8 @@ struct scaled_unit_impl : detail::unit_interface, detail::propagate_point_origin * @note User should not instantiate this type! It is not exported from the C++ module. The library will * instantiate this type automatically based on the unit arithmetic equation provided by the user. */ -template - requires(M != magnitude<>{} && M != mag<1>) +template + requires(M != unit_magnitude<>{} && M != mag<1>) struct scaled_unit final : detail::scaled_unit_impl {}; namespace detail { @@ -425,7 +425,7 @@ struct named_unit : decltype(U)::_base_type_ { * @tparam M scaling factor of the prefix * @tparam U a named unit to be prefixed */ -MP_UNITS_EXPORT template +MP_UNITS_EXPORT template requires(!Symbol.empty()) struct prefixed_unit : decltype(M * U)::_base_type_ { using _base_type_ = prefixed_unit; // exposition only @@ -594,9 +594,9 @@ template template [[nodiscard]] consteval auto get_canonical_unit_impl(const type_list&) { - auto magnitude = (mp_units::mag<1> * ... * get_canonical_unit_impl(Us{}, Us{}).mag); + auto unit_magnitude = (mp_units::mag<1> * ... * get_canonical_unit_impl(Us{}, Us{}).mag); auto u = (one * ... * get_canonical_unit_impl(Us{}, Us{}).reference_unit); - return canonical_unit{magnitude, u}; + return canonical_unit{unit_magnitude, u}; } template @@ -810,7 +810,7 @@ template return mag<1>; }; constexpr auto canonical_u = get_canonical_unit(u); - constexpr Magnitude auto cmag = get_magnitude() / canonical_u.mag; + constexpr UnitMagnitude auto cmag = get_magnitude() / canonical_u.mag; if constexpr (cmag == mag<1>) return u; else diff --git a/src/core/include/mp-units/framework/unit_concepts.h b/src/core/include/mp-units/framework/unit_concepts.h index c66f0b72..cb4e210d 100644 --- a/src/core/include/mp-units/framework/unit_concepts.h +++ b/src/core/include/mp-units/framework/unit_concepts.h @@ -25,9 +25,9 @@ // IWYU pragma: private, include #include #include -#include #include #include +#include namespace mp_units { @@ -45,8 +45,8 @@ struct unit_interface; MP_UNITS_EXPORT template concept Unit = detail::SymbolicConstant && std::derived_from; -template - requires(M != magnitude<>{} && M != mag<1>) +template + requires(M != unit_magnitude<>{} && M != mag<1>) struct scaled_unit; MP_UNITS_EXPORT template diff --git a/src/core/include/mp-units/framework/magnitude.h b/src/core/include/mp-units/framework/unit_magnitude.h similarity index 75% rename from src/core/include/mp-units/framework/magnitude.h rename to src/core/include/mp-units/framework/unit_magnitude.h index 5a05bb1e..013f666e 100644 --- a/src/core/include/mp-units/framework/magnitude.h +++ b/src/core/include/mp-units/framework/unit_magnitude.h @@ -32,8 +32,8 @@ #include #include #include -#include #include +#include #include #ifndef MP_UNITS_IN_MODULE_INTERFACE @@ -214,42 +214,42 @@ template // The largest integer which can be extracted from any magnitude with only a single basis vector. template -[[nodiscard]] consteval auto integer_part(magnitude); +[[nodiscard]] consteval auto integer_part(unit_magnitude); [[nodiscard]] consteval std::intmax_t integer_part(ratio r) { return r.num / r.den; } template -[[nodiscard]] consteval auto remove_positive_power(magnitude m); +[[nodiscard]] consteval auto remove_positive_power(unit_magnitude m); template -[[nodiscard]] consteval auto remove_mag_constants(magnitude m); +[[nodiscard]] consteval auto remove_mag_constants(unit_magnitude m); template -[[nodiscard]] consteval auto only_positive_mag_constants(magnitude m); +[[nodiscard]] consteval auto only_positive_mag_constants(unit_magnitude m); template -[[nodiscard]] consteval auto only_negative_mag_constants(magnitude m); +[[nodiscard]] consteval auto only_negative_mag_constants(unit_magnitude m); template requires(detail::get_base_value(Base) > 0) -[[nodiscard]] consteval Magnitude auto mag_power_lazy(); +[[nodiscard]] consteval UnitMagnitude auto mag_power_lazy(); template struct magnitude_base {}; template -struct magnitude_base> { +struct magnitude_base> { template - [[nodiscard]] friend consteval Magnitude auto _multiply_impl(magnitude, magnitude) + [[nodiscard]] friend consteval UnitMagnitude auto _multiply_impl(unit_magnitude, unit_magnitude) { if constexpr (mag_less(H, H2)) { if constexpr (sizeof...(T) == 0) { // Shortcut for the "pure prepend" case, which makes it easier to implement some of the other cases. - return magnitude{}; + return unit_magnitude{}; } else { - return magnitude{} * (magnitude{} * magnitude{}); + return unit_magnitude{} * (unit_magnitude{} * unit_magnitude{}); } } else if constexpr (mag_less(H2, H)) { - return magnitude

{} * (magnitude{} * magnitude{}); + return unit_magnitude

{} * (unit_magnitude{} * unit_magnitude{}); } else { if constexpr (is_same_v) { - constexpr auto partial_product = magnitude{} * magnitude{}; + constexpr auto partial_product = unit_magnitude{} * unit_magnitude{}; if constexpr (get_exponent(H) + get_exponent(H2) == 0) { return partial_product; } else { @@ -259,7 +259,7 @@ struct magnitude_base> { if constexpr (get_exponent(new_head) == 0) { return partial_product; } else { - return magnitude{} * partial_product; + return unit_magnitude{} * partial_product; } } } @@ -284,30 +284,32 @@ struct magnitude_base> { // Thus, we make the _simplest_ choice which reproduces the correct convention in the rational case: namely, taking // the minimum power for each base (where absent bases implicitly have a power of 0). template - [[nodiscard]] friend consteval auto _common_magnitude(magnitude, magnitude) + [[nodiscard]] friend consteval auto _common_magnitude(unit_magnitude, unit_magnitude) { using detail::remove_positive_power; if constexpr (detail::get_base_value(H) < detail::get_base_value(H2)) { // When H1 has the smaller base, prepend to result from recursion. - return remove_positive_power(magnitude{}) * _common_magnitude(magnitude{}, magnitude{}); + return remove_positive_power(unit_magnitude{}) * + _common_magnitude(unit_magnitude{}, unit_magnitude{}); } else if constexpr (detail::get_base_value(H2) < detail::get_base_value(H)) { // When H2 has the smaller base, prepend to result from recursion. - return remove_positive_power(magnitude

{}) * _common_magnitude(magnitude{}, magnitude{}); + return remove_positive_power(unit_magnitude

{}) * + _common_magnitude(unit_magnitude{}, unit_magnitude{}); } else { // When the bases are equal, pick whichever has the lower power. - constexpr auto common_tail = _common_magnitude(magnitude{}, magnitude{}); + constexpr auto common_tail = _common_magnitude(unit_magnitude{}, unit_magnitude{}); if constexpr (detail::get_exponent(H) < detail::get_exponent(H2)) { - return magnitude{} * common_tail; + return unit_magnitude{} * common_tail; } else { - return magnitude

{} * common_tail; + return unit_magnitude

{} * common_tail; } } } }; template -[[nodiscard]] consteval std::size_t magnitude_list_size(magnitude) +[[nodiscard]] consteval std::size_t magnitude_list_size(unit_magnitude) { return sizeof...(Ms); } @@ -329,13 +331,13 @@ constexpr Out print_separator(Out out, const unit_symbol_formatting& fmt) template Out, auto... Ms> requires(sizeof...(Ms) == 0) -[[nodiscard]] constexpr auto mag_constants_text(Out out, magnitude, const unit_symbol_formatting&, bool) +[[nodiscard]] constexpr auto mag_constants_text(Out out, unit_magnitude, const unit_symbol_formatting&, bool) { return out; } template Out, auto M, auto... Rest> -[[nodiscard]] constexpr auto mag_constants_text(Out out, magnitude, const unit_symbol_formatting& fmt, +[[nodiscard]] constexpr auto mag_constants_text(Out out, unit_magnitude, const unit_symbol_formatting& fmt, bool negative_power) { auto to_symbol = [&](T v) { @@ -346,8 +348,8 @@ template Out, auto M, auto... Rest> return (to_symbol(M), ..., (print_separator(out, fmt), to_symbol(Rest))); } -template Out> +template Out> constexpr Out magnitude_symbol_impl(Out out, const unit_symbol_formatting& fmt) { bool numerator = false; @@ -419,31 +421,40 @@ constexpr Out magnitude_symbol_impl(Out out, const unit_symbol_formatting& fmt) * rational powers, and compare for equality. */ template -struct magnitude : detail::magnitude_base> { - template - [[nodiscard]] friend consteval Magnitude auto operator*(magnitude lhs, M rhs) +struct unit_magnitude : detail::magnitude_base> { + template + [[nodiscard]] friend consteval UnitMagnitude auto operator*(unit_magnitude lhs, M rhs) { if constexpr (sizeof...(Ms) == 0) return rhs; - else if constexpr (is_same_v>) + else if constexpr (is_same_v>) return lhs; else return _multiply_impl(lhs, rhs); } - [[nodiscard]] friend consteval auto operator/(magnitude lhs, Magnitude auto rhs) { return lhs * _pow<-1>(rhs); } - - template - [[nodiscard]] friend consteval bool operator==(magnitude, Rhs) + [[nodiscard]] friend consteval auto operator/(unit_magnitude lhs, UnitMagnitude auto rhs) { - return is_same_v; + return lhs * _pow<-1>(rhs); + } + + template + [[nodiscard]] friend consteval bool operator==(unit_magnitude, Rhs) + { + return is_same_v; } private: // all below functions should in fact be in a `detail` namespace but are placed here to benefit from the ADL - [[nodiscard]] friend consteval bool _is_integral(const magnitude&) { return (detail::is_integral_impl(Ms) && ...); } - [[nodiscard]] friend consteval bool _is_rational(const magnitude&) { return (detail::is_rational_impl(Ms) && ...); } - [[nodiscard]] friend consteval bool _is_positive_integral_power(const magnitude&) + [[nodiscard]] friend consteval bool _is_integral(const unit_magnitude&) + { + return (detail::is_integral_impl(Ms) && ...); + } + [[nodiscard]] friend consteval bool _is_rational(const unit_magnitude&) + { + return (detail::is_rational_impl(Ms) && ...); + } + [[nodiscard]] friend consteval bool _is_positive_integral_power(const unit_magnitude&) { return (detail::is_positive_integral_power_impl(Ms) && ...); } @@ -453,7 +464,7 @@ private: */ template requires((detail::is_integral_impl(Ms) && ...)) || treat_as_floating_point - [[nodiscard]] friend consteval T _get_value(const magnitude&) + [[nodiscard]] friend consteval T _get_value(const unit_magnitude&) { // Force the expression to be evaluated in a constexpr context, to catch, e.g., overflow. constexpr T result = detail::checked_static_cast((detail::compute_base_power(Ms) * ... * T{1})); @@ -463,55 +474,57 @@ private: //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Magnitude rational powers implementation. template - [[nodiscard]] friend consteval auto _pow(magnitude) + [[nodiscard]] friend consteval auto _pow(unit_magnitude) { if constexpr (Num == 0) { - return magnitude<>{}; + return unit_magnitude<>{}; } else { - return magnitude< + return unit_magnitude< detail::power_v_or_T()...>{}; } } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Magnitude numerator and denominator implementation. - [[nodiscard]] friend consteval auto _numerator(magnitude) + [[nodiscard]] friend consteval auto _numerator(unit_magnitude) { - return (detail::integer_part(magnitude{}) * ... * magnitude<>{}); + return (detail::integer_part(unit_magnitude{}) * ... * unit_magnitude<>{}); } - [[nodiscard]] friend consteval auto _denominator(magnitude) { return _numerator(_pow<-1>(magnitude{})); } + [[nodiscard]] friend consteval auto _denominator(unit_magnitude) { return _numerator(_pow<-1>(unit_magnitude{})); } - [[nodiscard]] friend consteval auto _remove_positive_powers(magnitude) + [[nodiscard]] friend consteval auto _remove_positive_powers(unit_magnitude) { - return (magnitude<>{} * ... * detail::remove_positive_power(magnitude{})); + return (unit_magnitude<>{} * ... * detail::remove_positive_power(unit_magnitude{})); } - [[nodiscard]] friend consteval auto _common_magnitude_type_impl(magnitude) + [[nodiscard]] friend consteval auto _common_magnitude_type_impl(unit_magnitude) { return (std::intmax_t{} * ... * decltype(detail::get_base_value(Ms)){}); } - [[nodiscard]] friend consteval auto _extract_components(magnitude) + [[nodiscard]] friend consteval auto _extract_components(unit_magnitude) { - constexpr auto ratio = (magnitude<>{} * ... * detail::remove_mag_constants(magnitude{})); - if constexpr (ratio == magnitude{}) - return std::tuple{ratio, magnitude<>{}, magnitude<>{}}; + constexpr auto ratio = (unit_magnitude<>{} * ... * detail::remove_mag_constants(unit_magnitude{})); + if constexpr (ratio == unit_magnitude{}) + return std::tuple{ratio, unit_magnitude<>{}, unit_magnitude<>{}}; else { - constexpr auto num_constants = (magnitude<>{} * ... * detail::only_positive_mag_constants(magnitude{})); - constexpr auto den_constants = (magnitude<>{} * ... * detail::only_negative_mag_constants(magnitude{})); + constexpr auto num_constants = + (unit_magnitude<>{} * ... * detail::only_positive_mag_constants(unit_magnitude{})); + constexpr auto den_constants = + (unit_magnitude<>{} * ... * detail::only_negative_mag_constants(unit_magnitude{})); return std::tuple{ratio, num_constants, den_constants}; } } template - [[nodiscard]] friend consteval detail::ratio _get_power([[maybe_unused]] T base, magnitude) + [[nodiscard]] friend consteval detail::ratio _get_power([[maybe_unused]] T base, unit_magnitude) { return ((detail::get_base_value(Ms) == base ? detail::get_exponent(Ms) : detail::ratio{0}) + ... + detail::ratio{0}); } - [[nodiscard]] friend consteval std::intmax_t _extract_power_of_10(magnitude m) + [[nodiscard]] friend consteval std::intmax_t _extract_power_of_10(unit_magnitude m) { const auto power_of_2 = _get_power(2, m); const auto power_of_5 = _get_power(5, m); @@ -522,29 +535,29 @@ private: } template Out> - friend constexpr Out _magnitude_symbol(Out out, magnitude, const unit_symbol_formatting& fmt) + friend constexpr Out _magnitude_symbol(Out out, unit_magnitude, const unit_symbol_formatting& fmt) { - if constexpr (magnitude{} == magnitude<1>{}) { + if constexpr (unit_magnitude{} == unit_magnitude<1>{}) { return out; } else { - constexpr auto extract_res = _extract_components(magnitude{}); - constexpr Magnitude auto ratio = std::get<0>(extract_res); - constexpr Magnitude auto num_constants = std::get<1>(extract_res); - constexpr Magnitude auto den_constants = std::get<2>(extract_res); + constexpr auto extract_res = _extract_components(unit_magnitude{}); + constexpr UnitMagnitude auto ratio = std::get<0>(extract_res); + constexpr UnitMagnitude auto num_constants = std::get<1>(extract_res); + constexpr UnitMagnitude auto den_constants = std::get<2>(extract_res); constexpr std::intmax_t exp10 = _extract_power_of_10(ratio); if constexpr (detail::abs(exp10) < 3) { // print the value as a regular number (without exponent) - constexpr Magnitude auto num = _numerator(magnitude{}); - constexpr Magnitude auto den = _denominator(magnitude{}); + constexpr UnitMagnitude auto num = _numerator(unit_magnitude{}); + constexpr UnitMagnitude auto den = _denominator(unit_magnitude{}); // TODO address the below static_assert(ratio == num / den, "Printing rational powers not yet supported"); return detail::magnitude_symbol_impl(out, fmt); } else { // print the value as a number with exponent // if user wanted a regular number for this magnitude then probably a better scaled unit should be used - constexpr Magnitude auto base = ratio / detail::mag_power_lazy<10, exp10>(); - constexpr Magnitude auto num = _numerator(base); - constexpr Magnitude auto den = _denominator(base); + constexpr UnitMagnitude auto base = ratio / detail::mag_power_lazy<10, exp10>(); + constexpr UnitMagnitude auto num = _numerator(base); + constexpr UnitMagnitude auto den = _denominator(base); // TODO address the below static_assert(base == num / den, "Printing rational powers not yet supported"); @@ -554,67 +567,73 @@ private: } }; -[[nodiscard]] consteval auto _common_magnitude(magnitude<>, Magnitude auto m) { return _remove_positive_powers(m); } -[[nodiscard]] consteval auto _common_magnitude(Magnitude auto m, magnitude<>) { return _remove_positive_powers(m); } -[[nodiscard]] consteval auto _common_magnitude(magnitude<> m, magnitude<>) { return m; } +[[nodiscard]] consteval auto _common_magnitude(unit_magnitude<>, UnitMagnitude auto m) +{ + return _remove_positive_powers(m); +} +[[nodiscard]] consteval auto _common_magnitude(UnitMagnitude auto m, unit_magnitude<>) +{ + return _remove_positive_powers(m); +} +[[nodiscard]] consteval auto _common_magnitude(unit_magnitude<> m, unit_magnitude<>) { return m; } namespace detail { // The largest integer which can be extracted from any magnitude with only a single basis vector. template -[[nodiscard]] consteval auto integer_part(magnitude) +[[nodiscard]] consteval auto integer_part(unit_magnitude) { constexpr auto power_num = get_exponent(M).num; constexpr auto power_den = get_exponent(M).den; if constexpr (std::is_integral_v && (power_num >= power_den)) { // largest integer power - return magnitude()>{}; // Note: integer division intended + return unit_magnitude()>{}; // Note: integer division intended } else { - return magnitude<>{}; + return unit_magnitude<>{}; } } template -[[nodiscard]] consteval auto remove_positive_power(magnitude m) +[[nodiscard]] consteval auto remove_positive_power(unit_magnitude m) { if constexpr (get_exponent(M).num < 0) { return m; } else { - return magnitude<>{}; + return unit_magnitude<>{}; } } template -[[nodiscard]] consteval auto remove_mag_constants(magnitude m) +[[nodiscard]] consteval auto remove_mag_constants(unit_magnitude m) { if constexpr (MagConstant) - return magnitude<>{}; + return unit_magnitude<>{}; else return m; } template -[[nodiscard]] consteval auto only_positive_mag_constants(magnitude m) +[[nodiscard]] consteval auto only_positive_mag_constants(unit_magnitude m) { if constexpr (MagConstant && get_exponent(M) >= 0) return m; else - return magnitude<>{}; + return unit_magnitude<>{}; } template -[[nodiscard]] consteval auto only_negative_mag_constants(magnitude m) +[[nodiscard]] consteval auto only_negative_mag_constants(unit_magnitude m) { if constexpr (MagConstant && get_exponent(M) < 0) return m; else - return magnitude<>{}; + return unit_magnitude<>{}; } // Returns the most precise type to express the magnitude factor -template +template using common_magnitude_type = decltype(_common_magnitude_type_impl(M)); } // namespace detail @@ -643,23 +662,23 @@ struct prime_factorization { static constexpr std::intmax_t remainder = remove_power(first_base, first_power, N); static constexpr auto value = - magnitude()>{} * prime_factorization::value; + unit_magnitude()>{} * prime_factorization::value; }; // Specialization for the prime factorization of 1 (base case). template<> struct prime_factorization<1> { - static constexpr magnitude<> value{}; + static constexpr unit_magnitude<> value{}; }; template constexpr auto prime_factorization_v = prime_factorization::value; template -[[nodiscard]] consteval Magnitude auto make_magnitude() +[[nodiscard]] consteval UnitMagnitude auto make_magnitude() { if constexpr (MagConstant) - return magnitude{}; + return unit_magnitude{}; else return prime_factorization_v; } @@ -670,18 +689,18 @@ MP_UNITS_EXPORT_BEGIN template requires(detail::get_base_value(V) > 0) -constexpr Magnitude auto mag = detail::make_magnitude(); +constexpr UnitMagnitude auto mag = detail::make_magnitude(); template requires detail::gt_zero -constexpr Magnitude auto mag_ratio = detail::prime_factorization_v / detail::prime_factorization_v; +constexpr UnitMagnitude auto mag_ratio = detail::prime_factorization_v / detail::prime_factorization_v; /** * @brief Create a Magnitude which is some rational number raised to a rational power. */ template requires(detail::get_base_value(Base) > 0) -constexpr Magnitude auto mag_power = _pow(mag); +constexpr UnitMagnitude auto mag_power = _pow(mag); /** * @brief A convenient Magnitude constant for pi, which we can manipulate like a regular number. @@ -696,16 +715,16 @@ inline constexpr struct pi final : } pi; inline constexpr auto π /* U+03C0 GREEK SMALL LETTER PI */ = pi; -[[deprecated("Use `mag` instead")]] inline constexpr Magnitude auto mag_pi = mag; +[[deprecated("Use `mag` instead")]] inline constexpr UnitMagnitude auto mag_pi = mag; MP_UNITS_EXPORT_END namespace detail { -// This is introduced to break the dependency cycle between `magnitude::_magnitude_text` and `prime_factorization` +// This is introduced to break the dependency cycle between `unit_magnitude::_magnitude_text` and `prime_factorization` template requires(detail::get_base_value(Base) > 0) -[[nodiscard]] consteval Magnitude auto mag_power_lazy() +[[nodiscard]] consteval UnitMagnitude auto mag_power_lazy() { return _pow(mag); } diff --git a/src/core/include/mp-units/framework/magnitude_concepts.h b/src/core/include/mp-units/framework/unit_magnitude_concepts.h similarity index 92% rename from src/core/include/mp-units/framework/magnitude_concepts.h rename to src/core/include/mp-units/framework/unit_magnitude_concepts.h index a979923a..75587166 100644 --- a/src/core/include/mp-units/framework/magnitude_concepts.h +++ b/src/core/include/mp-units/framework/unit_magnitude_concepts.h @@ -50,12 +50,12 @@ MP_UNITS_EXPORT template concept MagConstant = detail::SymbolicConstant && is_derived_from_specialization_of_v; template -struct magnitude; +struct unit_magnitude; /** - * @brief Concept to detect whether T is a valid Magnitude. + * @brief Concept to detect whether T is a valid UnitMagnitude. */ MP_UNITS_EXPORT template -concept Magnitude = is_specialization_of_v; +concept UnitMagnitude = is_specialization_of_v; } // namespace mp_units diff --git a/src/systems/include/mp-units/systems/si/chrono.h b/src/systems/include/mp-units/systems/si/chrono.h index 59bb4439..fb4e3aa4 100644 --- a/src/systems/include/mp-units/systems/si/chrono.h +++ b/src/systems/include/mp-units/systems/si/chrono.h @@ -122,7 +122,7 @@ struct quantity_point_like_traits(_numerator(m)), _get_value(_denominator(m))>{}; diff --git a/test/static/CMakeLists.txt b/test/static/CMakeLists.txt index f1b225cd..48bd4a05 100644 --- a/test/static/CMakeLists.txt +++ b/test/static/CMakeLists.txt @@ -47,7 +47,6 @@ add_library( international_test.cpp isq_test.cpp isq_angle_test.cpp - # magnitude_test.cpp natural_test.cpp prime_test.cpp quantity_point_test.cpp @@ -58,8 +57,9 @@ add_library( symbol_text_test.cpp type_list_test.cpp typographic_test.cpp - unit_test.cpp + # unit_magnitude_test.cpp unit_symbol_test.cpp + unit_test.cpp usc_test.cpp ) diff --git a/test/static/magnitude_test.cpp b/test/static/unit_magnitude_test.cpp similarity index 86% rename from test/static/magnitude_test.cpp rename to test/static/unit_magnitude_test.cpp index 72bc8af3..28456bd9 100644 --- a/test/static/magnitude_test.cpp +++ b/test/static/unit_magnitude_test.cpp @@ -23,8 +23,8 @@ #ifdef MP_UNITS_MODULES import mp_units; #else -#include #include +#include #include #endif @@ -60,7 +60,7 @@ namespace { // template // void check_ratio_round_trip_is_identity() // { -// constexpr Magnitude auto m = mag(); +// constexpr UnitMagnitude auto m = mag(); // constexpr ratio round_trip = ratio{ // get_value(numerator(m)), // get_value(denominator(m)), @@ -68,16 +68,16 @@ namespace { // CHECK(round_trip == R); // } -inline constexpr struct mag_2_ : magnitude<2> { +inline constexpr struct mag_2_ : unit_magnitude<2> { } mag_2; -// inline constexpr struct mag_2_other : magnitude<2> { +// inline constexpr struct mag_2_other : unit_magnitude<2> { // } mag_2_other; -// inline constexpr struct mag_3 : magnitude<2> { +// inline constexpr struct mag_3 : unit_magnitude<2> { // } mag_3; // concepts verification -static_assert(Magnitude)>); -static_assert(Magnitude); +static_assert(UnitMagnitude)>); +static_assert(UnitMagnitude); // is_named_magnitude static_assert(!is_named_magnitude)>); @@ -162,15 +162,15 @@ static_assert(std::is_same_v{})), mag_2_> // { // SECTION("Performs prime factorization when denominator is 1") // { -// CHECK(mag<1>() == magnitude<>{}); -// CHECK(mag<2>() == magnitude{}); -// CHECK(mag<3>() == magnitude{}); -// CHECK(mag<4>() == magnitude{}); +// CHECK(mag<1>() == unit_magnitude<>{}); +// CHECK(mag<2>() == unit_magnitude{}); +// CHECK(mag<3>() == unit_magnitude{}); +// CHECK(mag<4>() == unit_magnitude{}); -// CHECK(mag<792>() == magnitude{}); +// CHECK(mag<792>() == unit_magnitude{}); // } -// SECTION("Supports fractions") { CHECK(mag_ratio<5, 8>() == magnitude{}); } +// SECTION("Supports fractions") { CHECK(mag_ratio<5, 8>() == unit_magnitude{}); } // SECTION("Can handle prime factor which would be large enough to overflow int") // { @@ -179,7 +179,7 @@ static_assert(std::is_same_v{})), mag_2_> // mag_ratio<16'605'390'666'050, 10'000'000'000'000>(); // } -// TEST_CASE("magnitude converts to numerical value") +// TEST_CASE("unit_magnitude converts to numerical value") // { // SECTION("Positive integer powers of integer bases give integer values") // { @@ -267,14 +267,14 @@ static_assert(std::is_same_v{})), mag_2_> // TEST_CASE("Multiplication works for magnitudes") // { -// SECTION("Reciprocals reduce to null magnitude") { CHECK(mag_ratio<3, 4>() * mag_ratio<4, 3>() == mag<1>()); } +// SECTION("Reciprocals reduce to null unit_magnitude") { CHECK(mag_ratio<3, 4>() * mag_ratio<4, 3>() == mag<1>()); } // SECTION("Products work as expected") { CHECK(mag_ratio<4, 5>() * mag_ratio<4, 3>() == mag_ratio<16, 15>()); } // SECTION("Products handle pi correctly") // { // CHECK(pi_to_the<1>() * mag_ratio<2, 3>() * pi_to_the() == -// magnitude{ratio{1, 2}}>{}); +// unit_magnitude{ratio{1, 2}}>{}); // } // SECTION("Supports constexpr") @@ -307,7 +307,7 @@ static_assert(std::is_same_v{})), mag_2_> // TEST_CASE("Division works for magnitudes") // { -// SECTION("Dividing anything by itself reduces to null magnitude") +// SECTION("Dividing anything by itself reduces to null unit_magnitude") // { // CHECK(mag_ratio<3, 4>() / mag_ratio<3, 4>() == mag<1>()); // CHECK(mag<15>() / mag<15>() == mag<1>()); @@ -350,11 +350,11 @@ static_assert(std::is_same_v{})), mag_2_> // { // SECTION("Integer magnitudes are integral and rational") // { -// auto check_rational_and_integral = [](Magnitude auto m) { +// auto check_rational_and_integral = [](UnitMagnitude auto m) { // CHECK(is_integral(m)); // CHECK(is_rational(m)); // }; -// check_rational_and_integral(magnitude<>{}); +// check_rational_and_integral(unit_magnitude<>{}); // check_rational_and_integral(mag<1>()); // check_rational_and_integral(mag<3>()); // check_rational_and_integral(mag<8>()); @@ -364,7 +364,7 @@ static_assert(std::is_same_v{})), mag_2_> // SECTION("Fractional magnitudes are rational, but not integral") // { -// auto check_rational_but_not_integral = [](Magnitude auto m) { +// auto check_rational_but_not_integral = [](UnitMagnitude auto m) { // CHECK(!is_integral(m)); // CHECK(is_rational(m)); // }; @@ -373,7 +373,7 @@ static_assert(std::is_same_v{})), mag_2_> // } // } -// TEST_CASE("Constructing ratio from rational magnitude") +// TEST_CASE("Constructing ratio from rational unit_magnitude") // { // SECTION("Round trip is identity") // { @@ -427,27 +427,27 @@ static_assert(std::is_same_v{})), mag_2_> // { // SECTION("integer_part of non-integer base is identity magnitude") // { -// CHECK(integer_part(pi_to_the<1>()) == magnitude<>{}); -// CHECK(integer_part(pi_to_the<-8>()) == magnitude<>{}); -// CHECK(integer_part(pi_to_the()) == magnitude<>{}); +// CHECK(integer_part(pi_to_the<1>()) == unit_magnitude<>{}); +// CHECK(integer_part(pi_to_the<-8>()) == unit_magnitude<>{}); +// CHECK(integer_part(pi_to_the()) == unit_magnitude<>{}); // } // SECTION("integer_part of integer base to negative power is identity magnitude") // { -// CHECK(integer_part(magnitude{}) == magnitude<>{}); -// CHECK(integer_part(magnitude{}) == magnitude<>{}); +// CHECK(integer_part(unit_magnitude{}) == unit_magnitude<>{}); +// CHECK(integer_part(unit_magnitude{}) == unit_magnitude<>{}); // } // SECTION("integer_part of integer base to fractional power is identity magnitude") // { -// CHECK(integer_part(magnitude{}) == magnitude<>{}); +// CHECK(integer_part(unit_magnitude{}) == unit_magnitude<>{}); // } // SECTION("integer_part of integer base to power at least one takes integer part") // { -// CHECK(integer_part(magnitude{}) == magnitude{}); -// CHECK(integer_part(magnitude{}) == magnitude{}); -// CHECK(integer_part(magnitude{}) == magnitude{}); +// CHECK(integer_part(unit_magnitude{}) == unit_magnitude{}); +// CHECK(integer_part(unit_magnitude{}) == unit_magnitude{}); +// CHECK(integer_part(unit_magnitude{}) == unit_magnitude{}); // } // } @@ -470,22 +470,22 @@ static_assert(std::is_same_v{})), mag_2_> // TEST_CASE("Prime factorization") // { -// SECTION("1 factors into the null magnitude") { CHECK(prime_factorization_v<1> == magnitude<>{}); } +// SECTION("1 factors into the null unit_magnitude") { CHECK(prime_factorization_v<1> == unit_magnitude<>{}); } // SECTION("Prime numbers factor into themselves") // { -// CHECK(prime_factorization_v<2> == magnitude{}); -// CHECK(prime_factorization_v<3> == magnitude{}); -// CHECK(prime_factorization_v<5> == magnitude{}); -// CHECK(prime_factorization_v<7> == magnitude{}); -// CHECK(prime_factorization_v<11> == magnitude{}); +// CHECK(prime_factorization_v<2> == unit_magnitude{}); +// CHECK(prime_factorization_v<3> == unit_magnitude{}); +// CHECK(prime_factorization_v<5> == unit_magnitude{}); +// CHECK(prime_factorization_v<7> == unit_magnitude{}); +// CHECK(prime_factorization_v<11> == unit_magnitude{}); -// CHECK(prime_factorization_v<41> == magnitude{}); +// CHECK(prime_factorization_v<41> == unit_magnitude{}); // } // SECTION("Prime factorization finds factors and multiplicities") // { -// CHECK(prime_factorization_v<792> == magnitude{}); +// CHECK(prime_factorization_v<792> == unit_magnitude{}); // } // }