Initial support for printing multiple negative exponents added

This commit is contained in:
Mateusz Pusz
2019-12-26 12:07:08 +01:00
parent 83516346e7
commit bf5762dbbb
3 changed files with 30 additions and 13 deletions

View File

@@ -28,11 +28,11 @@
namespace units::detail { namespace units::detail {
template<bool Divide, std::size_t Idx> template<bool Divide, std::size_t NegativeExpCount, std::size_t Idx>
constexpr auto operator_text() constexpr auto operator_text()
{ {
if constexpr(Idx == 0) { if constexpr(Idx == 0) {
if constexpr(Divide) { if constexpr(Divide && NegativeExpCount == 1) {
return basic_fixed_string("1/"); return basic_fixed_string("1/");
} }
else { else {
@@ -40,7 +40,7 @@ constexpr auto operator_text()
} }
} }
else { else {
if constexpr(Divide) { if constexpr(Divide && NegativeExpCount == 1) {
return basic_fixed_string("/"); return basic_fixed_string("/");
} }
else { else {
@@ -49,28 +49,40 @@ constexpr auto operator_text()
} }
} }
template<typename E, basic_fixed_string Symbol, std::size_t Idx> template<typename E, basic_fixed_string Symbol, std::size_t NegativeExpCount, std::size_t Idx>
constexpr auto exp_text() constexpr auto exp_text()
{ {
// get calculation operator + symbol // get calculation operator + symbol
const auto txt = operator_text<E::num < 0, Idx>() + Symbol; const auto txt = operator_text<E::num < 0, NegativeExpCount, Idx>() + Symbol;
if constexpr(E::den != 1) { if constexpr(E::den != 1) {
// add root part // add root part
return txt + basic_fixed_string("^(") + regular<abs(E::num)>() + basic_fixed_string("/") + regular<E::den>() + basic_fixed_string(")"); return txt + basic_fixed_string("^(") + regular<abs(E::num)>() + basic_fixed_string("/") + regular<E::den>() + basic_fixed_string(")");
} }
else if constexpr(abs(E::num) != 1) { else if constexpr(E::num != 1) {
// add exponent part // add exponent part
return txt + superscript<abs(E::num)>(); if constexpr(NegativeExpCount > 1) { // no '/' sign here (only negative exponents)
return txt + superscript<E::num>();
}
else if constexpr(E::num != -1) { // -1 is replaced with '/' sign here
return txt + superscript<abs(E::num)>();
}
else {
return txt;
}
} }
else { else {
return txt; return txt;
} }
} }
template<typename... Es>
inline constexpr int negative_exp_count = ((Es::num < 0 ? 1 : 0) + ...);
template<typename... Us, typename... Es, std::size_t... Idxs> template<typename... Us, typename... Es, std::size_t... Idxs>
constexpr auto deduced_symbol_text(exp_list<Es...>, std::index_sequence<Idxs...>) constexpr auto deduced_symbol_text(exp_list<Es...>, std::index_sequence<Idxs...>)
{ {
return (exp_text<Es, Us::symbol, Idxs>() + ...); constexpr auto neg_exp = negative_exp_count<Es...>;
return (exp_text<Es, Us::symbol, neg_exp, Idxs>() + ...);
} }
template<DerivedDimension Dim, Unit... Us> template<DerivedDimension Dim, Unit... Us>

View File

@@ -41,21 +41,25 @@ template<> inline constexpr basic_fixed_string superscript_number<7> = "\u2077";
template<> inline constexpr basic_fixed_string superscript_number<8> = "\u2078"; template<> inline constexpr basic_fixed_string superscript_number<8> = "\u2078";
template<> inline constexpr basic_fixed_string superscript_number<9> = "\u2079"; template<> inline constexpr basic_fixed_string superscript_number<9> = "\u2079";
inline constexpr basic_fixed_string superscript_minus = "\u207b";
template<int Value> template<int Value>
requires (Value >= 0)
constexpr auto superscript() constexpr auto superscript()
{ {
if constexpr(Value < 10) if constexpr(Value < 0)
return superscript_minus + superscript<-Value>();
else if constexpr(Value < 10)
return superscript_number<Value>; return superscript_number<Value>;
else else
return superscript<Value / 10>() + superscript<Value % 10>(); return superscript<Value / 10>() + superscript<Value % 10>();
} }
template<int Value> template<int Value>
requires (Value >= 0)
constexpr auto regular() constexpr auto regular()
{ {
if constexpr(Value < 10) if constexpr(Value < 0)
return basic_fixed_string("-") + superscript<-Value>();
else if constexpr(Value < 10)
return basic_fixed_string(static_cast<char>('0' + Value)); return basic_fixed_string(static_cast<char>('0' + Value));
else else
return regular<Value / 10>() + regular<Value % 10>(); return regular<Value / 10>() + regular<Value % 10>();

View File

@@ -74,7 +74,8 @@ constexpr auto prefix_or_ratio_text()
template<typename... Es, std::size_t... Idxs> template<typename... Es, std::size_t... Idxs>
constexpr auto derived_dimension_unit_text(exp_list<Es...>, std::index_sequence<Idxs...>) constexpr auto derived_dimension_unit_text(exp_list<Es...>, std::index_sequence<Idxs...>)
{ {
return (exp_text<Es, dimension_unit<typename Es::dimension>::symbol, Idxs>() + ...); constexpr auto neg_exp = negative_exp_count<Es...>;
return (exp_text<Es, dimension_unit<typename Es::dimension>::symbol, neg_exp, Idxs>() + ...);
} }
template<typename... Es> template<typename... Es>