Skip to content

Commit

Permalink
feat: added support for printing powers of magnitude constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed Oct 3, 2024
1 parent d783c3c commit dba8b07
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/core/include/mp-units/bits/text_tools.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ constexpr Out copy_symbol_exponent(text_encoding encoding, bool negative_power,
}
constexpr auto txt = superscript<r.num>();
return copy<CharT>(txt, encoding, out);
} else {
return out;
}
}

Expand Down
14 changes: 8 additions & 6 deletions src/core/include/mp-units/framework/magnitude.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,12 @@ template<typename CharT, std::output_iterator<CharT> Out, auto M, auto... Rest>
[[nodiscard]] consteval auto mag_constants_text(Out out, magnitude<M, Rest...>, const unit_symbol_formatting& fmt,
bool negative_power)
{
return ((out = copy_symbol<CharT>(get_base(M).symbol, fmt.encoding, negative_power, out)), ...,
(print_separator<CharT>(out, fmt),
out = copy_symbol<CharT>(get_base(Rest).symbol, fmt.encoding, negative_power, out)));
auto to_symbol = [&]<typename T>(T v) {
out = copy_symbol<CharT>(get_base(v).symbol, fmt.encoding, negative_power, out);
constexpr ratio r = get_exponent(T{});
return copy_symbol_exponent<CharT, abs(r.num), r.den>(fmt.encoding, negative_power, out);
};
return (to_symbol(M), ..., (print_separator<CharT>(out, fmt), to_symbol(Rest)));
}

template<typename CharT, Magnitude auto Num, Magnitude auto Den, Magnitude auto NumConstants,
Expand Down Expand Up @@ -501,7 +504,6 @@ struct magnitude : detail::magnitude_base<magnitude<Ms...>> {

[[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<Ms>{}));
Expand Down Expand Up @@ -717,8 +719,8 @@ constexpr Magnitude auto mag_ratio = detail::prime_factorization_v<N> / detail::
/**
* @brief Create a Magnitude which is some rational number raised to a rational power.
*/
template<std::intmax_t Base, int Num, int Den = 1>
requires detail::gt_zero<Base>
template<MagArg auto Base, int Num, int Den = 1>
requires detail::gt_zero<detail::get_base_value(Base)>
constexpr Magnitude auto mag_power = pow<Num, Den>(mag<Base>);

/**
Expand Down
4 changes: 4 additions & 0 deletions test/static/unit_symbol_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ static_assert(unit_symbol<usf{.encoding = ascii, .solidus = always}>(mag_ratio<1
"[1/(2 pi) m]");
static_assert(unit_symbol(mag_ratio<1, 2> * mag<pi> * metre) == "[πœ‹/2 m]");

static_assert(unit_symbol(mag_power<pi, 2> * one) == "[πœ‹Β²]");
static_assert(unit_symbol<usf{.encoding = ascii}>(mag_power<pi, 2> * one) == "[pi^2]");
static_assert(unit_symbol(mag_power<pi, 1, 2> * metre) == "[πœ‹^(1/2) m]");
static_assert(unit_symbol<usf{.encoding = ascii}>(mag_power<pi, 1, 2> * metre) == "[pi^(1/2) m]");

static_assert(unit_symbol(mag<pi> * mag<e> * one) == "[e πœ‹]");
static_assert(unit_symbol(mag<e> * mag<pi> * one) == "[e πœ‹]");
Expand Down

0 comments on commit dba8b07

Please sign in to comment.