feat: common_unit support added

This commit is contained in:
Mateusz Pusz
2024-09-26 20:28:41 +02:00
parent 25af8c9d8f
commit 3190d15eba
5 changed files with 280 additions and 50 deletions

View File

@@ -260,3 +260,31 @@ This is why we provide both versions of identifiers for such units.
quantity resistance = 60 * kΩ;
quantity capacitance = 100 * µF;
```
## Common units
Adding or subtracting two quantities of different units will force the library to find a common
unit for those. This is to prevent data truncation. For the cases when one of the units is an
integral multiple of the another, the resulting quantity will use a "smaller" one in its result.
For example:
```cpp
static_assert((1 * kg + 1 * g).unit == g);
static_assert((1 * km + 1 * mm).unit == mm);
static_assert((1 * yd + 1 * mi).unit == yd);
```
However, in many cases an arithmetic on quantities of different units will result in a yet another
unit. This happens when none of the source units is an integral multiple of another. In such cases,
the library returns a special type that denotes that we are dealing with a common unit of such
an equation:
```cpp
quantity q = 1 * km + 1 * mi; // quantity<common_unit<international::mile, si::kilo_<si::metre>>{}, int>
```
!!! note
A user should never explicitly instantiate a `common_unit` class template. The library's
framework will do it based on the provided quantity equation.

View File

@@ -268,6 +268,31 @@ The above prints:
kg⋅m⋅s⁻²
```
## Symbols of common units
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
a minimum set of common units resulting from the addition or subtraction of multiple quantities,
we print all of them as a scaled version of the source unit. For example the following:
```cpp
std::cout << 1 * km + 1 * mi << "\n";
std::cout << 1 * nmi + 1 * mi << "\n";
std::cout << 1 * km / h + 1 * m / s << "\n";
```
will print:
```text
40771 ([1/25146] mi = [1/15625] km)
108167 ([1/50292] mi = [1/57875] nmi)
23 ([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
unit and its impact on the value stored in the quantity.
## `space_before_unit_symbol` customization point
The [SI Brochure](../../appendix/references.md#SIBrochure) says: