From 92750072b0ee56116fa49f8c04835cbd89269239 Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Thu, 7 Jul 2022 16:44:19 +0000 Subject: [PATCH] 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. --- src/core/include/units/bits/unit_text.h | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/core/include/units/bits/unit_text.h b/src/core/include/units/bits/unit_text.h index 49c1c3e1..736492e1 100644 --- a/src/core/include/units/bits/unit_text.h +++ b/src/core/include/units/bits/unit_text.h @@ -32,12 +32,19 @@ namespace units::detail { inline constexpr basic_symbol_text base_multiplier("\u00D7 10", "x 10"); -template -constexpr auto ratio_text() +template +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(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(num); + constexpr auto den_value = get_value(den); + if constexpr (num_value == 1 && den_value == 1 && exp10 != 0) { return base_multiplier + superscript(); @@ -62,6 +69,12 @@ constexpr auto ratio_text() } } +template +constexpr auto ratio_text() +{ + return magnitude_text()>(); +} + template constexpr auto prefix_or_ratio_text() {