feat: New formatting specification implemented

This commit is contained in:
Mateusz Pusz committed 2024-04-18 22:29:09 +01:00
1 parent 1e3a93d10c
commit fa04e93687
8 files changed
+376 -329

No files matched your search

+4 -4
View File
@@ -118,7 +118,7 @@ void calcs_comparison()
const auto L1A = 2.f * fm;
const auto L2A = 3.f * fm;
const auto LrA = L1A + L2A;
std::cout << MP_UNITS_STD_FMT::format("{:{%N:.30} %U}\n + {:{%N:.30} %U}\n = {:{%N:.30} %U}\n\n", L1A, L2A, LrA);
std::cout << MP_UNITS_STD_FMT::format("{::N[.30]}\n + {::N[.30]}\n = {::N[.30]}\n\n", L1A, L2A, LrA);
std::cout << "The single unit method must convert large\n"
"or small values in other units to the base unit.\n"
@@ -127,17 +127,17 @@ void calcs_comparison()
const auto L1B = L1A.in(m);
const auto L2B = L2A.in(m);
const auto LrB = L1B + L2B;
std::cout << MP_UNITS_STD_FMT::format("{:{%N:.30e} %U}\n + {:{%N:.30e} %U}\n = {:{%N:.30e} %U}\n\n", L1B, L2B, LrB);
std::cout << MP_UNITS_STD_FMT::format("{::N[.30e]}\n + {::N[.30e]}\n = {::N[.30e]}\n\n", L1B, L2B, LrB);
std::cout << "In multiplication and division:\n\n";
const quantity<isq::area[square(fm)], float> ArA = L1A * L2A;
std::cout << MP_UNITS_STD_FMT::format("{:{%N:.30} %U}\n * {:{%N:.30} %U}\n = {:{%N:.30} %U}\n\n", L1A, L2A, ArA);
std::cout << MP_UNITS_STD_FMT::format("{::N[.30]}\n * {::N[.30]}\n = {::N[.30]}\n\n", L1A, L2A, ArA);
std::cout << "similar problems arise\n\n";
const quantity<isq::area[m2], float> ArB = L1B * L2B;
std::cout << MP_UNITS_STD_FMT::format("{:{%N:.30e} %U}\n * {:{%N:.30e} %U}\n = {:{%N:.30e} %U}\n\n", L1B, L2B, ArB);
std::cout << MP_UNITS_STD_FMT::format("{::N[.30e]}\n * {::N[.30e]}\n = {::N[.30e]}\n\n", L1B, L2B, ArB);
}
} // namespace
+9 -9
View File
@@ -87,7 +87,7 @@ void print(const R& gliders)
std::cout << "- Polar:\n";
for (const auto& p : g.polar) {
const auto ratio = glide_ratio(g.polar[0]).force_in(one);
std::cout << MP_UNITS_STD_FMT::format(" * {:{%N:.4} %U} @ {:{%N:.1} %U} -> {:{%N:.1} %U} ({:{%N:.1} %U})\n",
std::cout << MP_UNITS_STD_FMT::format(" * {::N[.4]} @ {::N[.1]} -> {::N[.1]} ({::N[.1]})\n",
p.climb, p.v, ratio,
// TODO is it possible to make ADL work below (we need another set of trig
// functions for strong angle in a different namespace)
@@ -106,8 +106,8 @@ void print(const R& conditions)
for (const auto& c : conditions) {
std::cout << "- " << c.first << "\n";
const auto& w = c.second;
std::cout << " * Cloud base: " << MP_UNITS_STD_FMT::format("{:{%N:.0} %U}", w.cloud_base) << " AGL\n";
std::cout << " * Thermals strength: " << MP_UNITS_STD_FMT::format("{:{%N:.1} %U}", w.thermal_strength) << "\n";
std::cout << " * Cloud base: " << MP_UNITS_STD_FMT::format("{::N[.0]}", w.cloud_base) << " AGL\n";
std::cout << " * Thermals strength: " << MP_UNITS_STD_FMT::format("{::N[.1]}", w.thermal_strength) << "\n";
std::cout << "\n";
}
}
@@ -119,7 +119,7 @@ void print(const R& waypoints)
std::cout << "Waypoints:\n";
std::cout << "==========\n";
for (const auto& w : waypoints)
std::cout << MP_UNITS_STD_FMT::format("- {}: {} {}, {:{%N:.1} %U}\n", w.name, w.pos.lat, w.pos.lon, w.alt);
std::cout << MP_UNITS_STD_FMT::format("- {}: {} {}, {::N[.1]}\n", w.name, w.pos.lat, w.pos.lon, w.alt);
std::cout << "\n";
}
@@ -130,12 +130,12 @@ void print(const task& t)
std::cout << "- Start: " << t.get_start().name << "\n";
std::cout << "- Finish: " << t.get_finish().name << "\n";
std::cout << "- Length: " << MP_UNITS_STD_FMT::format("{:{%N:.1} %U}", t.get_distance()) << "\n";
std::cout << "- Length: " << MP_UNITS_STD_FMT::format("{::N[.1]}", t.get_distance()) << "\n";
std::cout << "- Legs: "
<< "\n";
for (const auto& l : t.get_legs())
std::cout << MP_UNITS_STD_FMT::format(" * {} -> {} ({:{%N:.1} %U})\n", l.begin().name, l.end().name,
std::cout << MP_UNITS_STD_FMT::format(" * {} -> {} ({::N[.1]})\n", l.begin().name, l.end().name,
l.get_distance());
std::cout << "\n";
}
@@ -144,7 +144,7 @@ void print(const safety& s)
{
std::cout << "Safety:\n";
std::cout << "=======\n";
std::cout << "- Min AGL separation: " << MP_UNITS_STD_FMT::format("{:{%N:.0} %U}", s.min_agl_height) << "\n";
std::cout << "- Min AGL separation: " << MP_UNITS_STD_FMT::format("{::N[.0]}", s.min_agl_height) << "\n";
std::cout << "\n";
}
@@ -153,8 +153,8 @@ void print(const aircraft_tow& tow)
std::cout << "Tow:\n";
std::cout << "====\n";
std::cout << "- Type: aircraft\n";
std::cout << "- Height: " << MP_UNITS_STD_FMT::format("{:{%N:.0} %U}", tow.height_agl) << "\n";
std::cout << "- Performance: " << MP_UNITS_STD_FMT::format("{:{%N:.1} %U}", tow.performance) << "\n";
std::cout << "- Height: " << MP_UNITS_STD_FMT::format("{::N[.0]}", tow.height_agl) << "\n";
std::cout << "- Performance: " << MP_UNITS_STD_FMT::format("{::N[.1]}", tow.performance) << "\n";
std::cout << "\n";
}
@@ -82,9 +82,7 @@ void print(std::string_view phase_name, timestamp start_ts, const glide_computer
const glide_computer::flight_point& new_point)
{
std::cout << MP_UNITS_STD_FMT::format(
"| {:<12} | {:>9{%N:.1} %U} (Total: {:>9{%N:.1} %U}) | {:>8{%N:.1} %U} (Total: {:>8{%N:.1} %U}) | {:>7{%N:.0} %U} "
"({:>6{%N:.0} %U}) "
"|\n",
"| {:<12} | {:>9:N[.1]} (Total: {:>9:N[.1]}) | {:>8:N[.1]} (Total: {:>8:N[.1]}) | {:>7:N[.0]} ({:>6:N[.0]}) |\n",
phase_name, value_cast<si::minute>(new_point.ts - point.ts), value_cast<si::minute>(new_point.ts - start_ts),
new_point.dist - point.dist, new_point.dist, new_point.alt - point.alt, new_point.alt);
}
+7 -7
View File
@@ -58,11 +58,11 @@ int main()
constexpr quantity v6 = value_cast<m / s>(v4);
constexpr quantity v7 = value_cast<int>(v6);
std::cout << v1 << '\n'; // 110 km/h
std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h
std::cout << MP_UNITS_STD_FMT::format("{:*^10}\n", v3); // *110 km/h*
std::cout << MP_UNITS_STD_FMT::format("{:%N in %U of %D}\n", v4); // 70 in mi/h of LT⁻¹
std::cout << MP_UNITS_STD_FMT::format("{:{%N:.2f}%?%U}\n", v5); // 30.56 in m/s
std::cout << MP_UNITS_STD_FMT::format("{:{%N:.2f}%?{%U:dn}}\n", v6); // 31.29 in m⋅s⁻¹
std::cout << MP_UNITS_STD_FMT::format("{:%N}\n", v7); // 31
std::cout << v1 << '\n'; // 110 km/h
std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h
std::cout << MP_UNITS_STD_FMT::format("{:*^10}\n", v3); // *110 km/h*
std::cout << MP_UNITS_STD_FMT::format("{:%N in %U of %D}\n", v4); // 70 in mi/h of LT⁻¹
std::cout << MP_UNITS_STD_FMT::format("{::N[.2f]}\n", v5); // 30.56 m/s
std::cout << MP_UNITS_STD_FMT::format("{::N[.2f]U[dn]}\n", v6); // 31.29 m⋅s⁻¹
std::cout << MP_UNITS_STD_FMT::format("{:%N}\n", v7); // 31
}
+6 -6
View File
@@ -45,19 +45,19 @@ int main()
using namespace mp_units::si::unit_symbols;
std::cout << "The seven defining constants of the SI and the seven corresponding units they define:\n";
std::cout << MP_UNITS_STD_FMT::format("- hyperfine transition frequency of Cs: {} = {:{%N:.0} %U}\n",
std::cout << MP_UNITS_STD_FMT::format("- hyperfine transition frequency of Cs: {} = {::N[.0]}\n",
1. * si2019::hyperfine_structure_transition_frequency_of_cs,
(1. * si2019::hyperfine_structure_transition_frequency_of_cs).in(Hz));
std::cout << MP_UNITS_STD_FMT::format("- speed of light in vacuum: {} = {:{%N:.0} %U}\n",
std::cout << MP_UNITS_STD_FMT::format("- speed of light in vacuum: {} = {::N[.0]}\n",
1. * si2019::speed_of_light_in_vacuum,
(1. * si2019::speed_of_light_in_vacuum).in(m / s));
std::cout << MP_UNITS_STD_FMT::format("- Planck constant: {} = {:{%N:.8e} %U}\n",
std::cout << MP_UNITS_STD_FMT::format("- Planck constant: {} = {::N[.8e]}\n",
1. * si2019::planck_constant, (1. * si2019::planck_constant).in(J * s));
std::cout << MP_UNITS_STD_FMT::format("- elementary charge: {} = {:{%N:.9e} %U}\n",
std::cout << MP_UNITS_STD_FMT::format("- elementary charge: {} = {::N[.9e]}\n",
1. * si2019::elementary_charge, (1. * si2019::elementary_charge).in(C));
std::cout << MP_UNITS_STD_FMT::format("- Boltzmann constant: {} = {:{%N:.6e} %U}\n",
std::cout << MP_UNITS_STD_FMT::format("- Boltzmann constant: {} = {::N[.6e]}\n",
1. * si2019::boltzmann_constant, (1. * si2019::boltzmann_constant).in(J / K));
std::cout << MP_UNITS_STD_FMT::format("- Avogadro constant: {} = {:{%N:.8e} %U}\n",
std::cout << MP_UNITS_STD_FMT::format("- Avogadro constant: {} = {::N[.8e]}\n",
1. * si2019::avogadro_constant, (1. * si2019::avogadro_constant).in(one / mol));
std::cout << MP_UNITS_STD_FMT::format("- luminous efficacy: {} = {}\n",
1. * si2019::luminous_efficacy, (1. * si2019::luminous_efficacy).in(lm / W));
+1 -1
View File
@@ -170,6 +170,6 @@ int main()
};
waypoint wpt = {"EPPR", {54.24772_N, 18.6745_E}, mean_sea_level + 16. * ft};
std::cout << MP_UNITS_STD_FMT::format("{}: {} {}, {:{%N:.2} %U}, {:{%N:.2} %U}\n", wpt.name, wpt.pos.lat, wpt.pos.lon,
std::cout << MP_UNITS_STD_FMT::format("{}: {} {}, {::N[.2]}, {::N[.2]}\n", wpt.name, wpt.pos.lat, wpt.pos.lon,
wpt.msl_alt, to_hae<earth_gravity_model::egm2008_1>(wpt.msl_alt, wpt.pos));
}