feat: common_unit support added
This commit is contained in:
1 parent
25af8c9d8f
commit
3190d15eba
5 files changed
+280
-50
No files matched your search
@@ -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.
|
||||
Reference in new issue
Block a user