From dba8b07eb5de1a466152161c0458e53a906fcd9e Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 3 Oct 2024 19:34:16 +0200 Subject: [PATCH] feat: added support for printing powers of magnitude constants --- src/core/include/mp-units/bits/text_tools.h | 2 ++ src/core/include/mp-units/framework/magnitude.h | 14 ++++++++------ test/static/unit_symbol_test.cpp | 4 ++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/core/include/mp-units/bits/text_tools.h b/src/core/include/mp-units/bits/text_tools.h index 0b1fa7f71..46e5fa9c5 100644 --- a/src/core/include/mp-units/bits/text_tools.h +++ b/src/core/include/mp-units/bits/text_tools.h @@ -146,6 +146,8 @@ constexpr Out copy_symbol_exponent(text_encoding encoding, bool negative_power, } constexpr auto txt = superscript(); return copy(txt, encoding, out); + } else { + return out; } } diff --git a/src/core/include/mp-units/framework/magnitude.h b/src/core/include/mp-units/framework/magnitude.h index a05354b85..c52074e8b 100644 --- a/src/core/include/mp-units/framework/magnitude.h +++ b/src/core/include/mp-units/framework/magnitude.h @@ -353,9 +353,12 @@ template Out, auto M, auto... Rest> [[nodiscard]] consteval auto mag_constants_text(Out out, magnitude, const unit_symbol_formatting& fmt, bool negative_power) { - return ((out = copy_symbol(get_base(M).symbol, fmt.encoding, negative_power, out)), ..., - (print_separator(out, fmt), - out = copy_symbol(get_base(Rest).symbol, fmt.encoding, negative_power, out))); + auto to_symbol = [&](T v) { + out = copy_symbol(get_base(v).symbol, fmt.encoding, negative_power, out); + constexpr ratio r = get_exponent(T{}); + return copy_symbol_exponent(fmt.encoding, negative_power, out); + }; + return (to_symbol(M), ..., (print_separator(out, fmt), to_symbol(Rest))); } template> { [[nodiscard]] friend consteval auto _denominator(magnitude) { return _numerator(pow<-1>(magnitude{})); } - [[nodiscard]] friend consteval auto _remove_positive_powers(magnitude) { return (magnitude<>{} * ... * detail::remove_positive_power(magnitude{})); @@ -717,8 +719,8 @@ constexpr Magnitude auto mag_ratio = detail::prime_factorization_v / detail:: /** * @brief Create a Magnitude which is some rational number raised to a rational power. */ -template - requires detail::gt_zero +template + requires detail::gt_zero constexpr Magnitude auto mag_power = pow(mag); /** diff --git a/test/static/unit_symbol_test.cpp b/test/static/unit_symbol_test.cpp index 648503272..bca0d25da 100644 --- a/test/static/unit_symbol_test.cpp +++ b/test/static/unit_symbol_test.cpp @@ -168,6 +168,10 @@ static_assert(unit_symbol(mag_ratio<1 "[1/(2 pi) m]"); static_assert(unit_symbol(mag_ratio<1, 2> * mag * metre) == "[𝜋/2 m]"); +static_assert(unit_symbol(mag_power * one) == "[𝜋²]"); +static_assert(unit_symbol(mag_power * one) == "[pi^2]"); +static_assert(unit_symbol(mag_power * metre) == "[𝜋^(1/2) m]"); +static_assert(unit_symbol(mag_power * metre) == "[pi^(1/2) m]"); static_assert(unit_symbol(mag * mag * one) == "[e 𝜋]"); static_assert(unit_symbol(mag * mag * one) == "[e 𝜋]");