Reimplement ratio_text() as magnitude_text()

The algorithm is exactly the same; we just get our values for
`num_value`, `den_value`, and `exp10` in different ways.

The old printing logic assumed the ratio/Magnitude was an exact rational
number.  Formerly, we couldn't even _represent_ anything else, but with
Magnitude, we can.  Thus, we guard this assumption with a
`static_assert`.  Later on, we can figure out how we want to print
irrational bases and/or rational powers.  The point of this change is to
unblock us from moving to the new infrastructure.
This commit is contained in:
Chip Hogg
2022-07-07 16:44:19 +00:00
parent e1a17ab1f0
commit 92750072b0

View File

@@ -32,12 +32,19 @@ namespace units::detail {
inline constexpr basic_symbol_text base_multiplier("\u00D7 10", "x 10");
template<ratio R>
constexpr auto ratio_text()
template<Magnitude auto M>
constexpr auto magnitude_text()
{
constexpr auto num_value = R.num;
constexpr auto den_value = R.den;
constexpr auto exp10 = R.exp;
constexpr auto exp10 = extract_power_of_10(M);
constexpr Magnitude auto base = M / pow<exp10>(as_magnitude<10>());
constexpr Magnitude auto num = numerator(base);
constexpr Magnitude auto den = denominator(base);
static_assert(base == num / den, "Printing rational powers, or irrational bases, not yet supported");
constexpr auto num_value = get_value<std::intmax_t>(num);
constexpr auto den_value = get_value<std::intmax_t>(den);
if constexpr (num_value == 1 && den_value == 1 && exp10 != 0) {
return base_multiplier + superscript<exp10>();
@@ -62,6 +69,12 @@ constexpr auto ratio_text()
}
}
template<ratio R>
constexpr auto ratio_text()
{
return magnitude_text<as_magnitude<R>()>();
}
template<Unit U, ratio R, std::size_t SymbolLen>
constexpr auto prefix_or_ratio_text()
{