feat: common unit symbols now use EQUIV{u1, u2, ...} syntax

This commit is contained in:
Mateusz Pusz
2024-10-02 08:14:14 +02:00
parent 0d00be067a
commit 25e99fe0fa
3 changed files with 13 additions and 12 deletions

View File

@@ -272,8 +272,8 @@ kg⋅m⋅s⁻²
Some [common units](systems_of_units.md#common-units) expressed with a specialization of the Some [common units](systems_of_units.md#common-units) expressed with a specialization of the
`common_unit` class template need special printing rules for their symbols. As they represent `common_unit` class template need special printing rules for their symbols. As they represent
a minimum set of common units resulting from the addition or subtraction of multiple quantities, a minimum set of equivalent common units resulting from the addition or subtraction of multiple
we print all of them as a scaled version of the source unit. For example the following: quantities, we print all of them as a scaled version of the source unit. For example the following:
```cpp ```cpp
std::cout << 1 * km + 1 * mi << "\n"; std::cout << 1 * km + 1 * mi << "\n";
@@ -284,9 +284,9 @@ std::cout << 1 * km / h + 1 * m / s << "\n";
will print: will print:
```text ```text
40771 ([1/25146] mi = [1/15625] km) 40771 EQUIV{[1/25146] mi, [1/15625] km}
108167 ([1/50292] mi = [1/57875] nmi) 108167 EQUIV{[1/50292] mi, [1/57875] nmi}
23 ([1/5] km/h = [1/18] m/s) 23 EQUIV{[1/5] km/h, [1/18] m/s}
``` ```
Thanks to the above, it might be easier for the user to reason about the magnitude of the resulting Thanks to the above, it might be easier for the user to reason about the magnitude of the resulting

View File

@@ -854,18 +854,19 @@ template<typename CharT, std::output_iterator<CharT> Out, typename U, typename..
constexpr Out unit_symbol_impl(Out out, const common_unit<U, Rest...>&, const unit_symbol_formatting& fmt, constexpr Out unit_symbol_impl(Out out, const common_unit<U, Rest...>&, const unit_symbol_formatting& fmt,
bool negative_power) bool negative_power)
{ {
constexpr std::string_view separator(" = "); constexpr std::string_view prefix("EQUIV{");
constexpr std::string_view separator(", ");
auto print_unit = [&]<Unit Arg>(Arg) { auto print_unit = [&]<Unit Arg>(Arg) {
constexpr auto u = get_common_unit_in(common_unit<U, Rest...>{}, Arg{}); constexpr auto u = get_common_unit_in(common_unit<U, Rest...>{}, Arg{});
unit_symbol_impl<CharT>(out, u, fmt, negative_power); unit_symbol_impl<CharT>(out, u, fmt, negative_power);
}; };
*out++ = '('; detail::copy(std::begin(prefix), std::end(prefix), out);
print_unit(U{}); print_unit(U{});
for_each(std::tuple<Rest...>{}, [&]<Unit Arg>(Arg) { for_each(std::tuple<Rest...>{}, [&]<Unit Arg>(Arg) {
detail::copy(std::begin(separator), std::end(separator), out); detail::copy(std::begin(separator), std::end(separator), out);
print_unit(Arg{}); print_unit(Arg{});
}); });
*out++ = ')'; *out++ = '}';
return out; return out;
} }

View File

@@ -117,12 +117,12 @@ static_assert(unit_symbol<unit_symbol_formatting{.encoding = ascii}>(mag<60> * s
static_assert(unit_symbol(mag_ratio<1, 18> * metre / second) == "[1/18] m/s"); static_assert(unit_symbol(mag_ratio<1, 18> * metre / second) == "[1/18] m/s");
// common units // common units
static_assert(unit_symbol(get_common_unit(kilo<metre>, mile)) == "([1/25146] mi = [1/15625] km)"); static_assert(unit_symbol(get_common_unit(kilo<metre>, mile)) == "EQUIV{[1/25146] mi, [1/15625] km}");
static_assert(unit_symbol(get_common_unit(kilo<metre> / hour, metre / second)) == "([1/5] km/h = [1/18] m/s)"); static_assert(unit_symbol(get_common_unit(kilo<metre> / hour, metre / second)) == "EQUIV{[1/5] km/h, [1/18] m/s}");
static_assert(unit_symbol(get_common_unit(kilo<metre> / hour, metre / second) / second) == static_assert(unit_symbol(get_common_unit(kilo<metre> / hour, metre / second) / second) ==
"([1/5] km/h = [1/18] m/s)/s"); "EQUIV{[1/5] km/h, [1/18] m/s}/s");
static_assert(unit_symbol(get_common_unit(kilo<metre> / hour, metre / second) * second) == static_assert(unit_symbol(get_common_unit(kilo<metre> / hour, metre / second) * second) ==
"([1/5] km/h = [1/18] m/s) s"); "EQUIV{[1/5] km/h, [1/18] m/s} s");
// derived units // derived units
static_assert(unit_symbol(one) == ""); // NOLINT(readability-container-size-empty) static_assert(unit_symbol(one) == ""); // NOLINT(readability-container-size-empty)