refactor: example applications refactored to a new formatting syntax
This commit is contained in:
1 parent
24e5ca8ad9
commit
a9deec9d59
14 files changed
+118
-115
No files matched your search
@@ -13,7 +13,7 @@ Here is a small example of operations possible on scalar quantities:
|
||||
// simple numeric operations
|
||||
static_assert(10 * km / 2 == 5 * km);
|
||||
|
||||
// unit conversions
|
||||
// conversions to common units
|
||||
static_assert(1 * h == 3600 * s);
|
||||
static_assert(1 * km + 1 * m == 1001 * m);
|
||||
|
||||
@@ -40,7 +40,7 @@ Here is a small example of operations possible on scalar quantities:
|
||||
// simple numeric operations
|
||||
static_assert(10 * km / 2 == 5 * km);
|
||||
|
||||
// unit conversions
|
||||
// conversions to common units
|
||||
static_assert(1 * h == 3600 * s);
|
||||
static_assert(1 * km + 1 * m == 1001 * m);
|
||||
|
||||
@@ -56,7 +56,7 @@ Here is a small example of operations possible on scalar quantities:
|
||||
static_assert(1000 / (1 * s) == 1 * kHz);
|
||||
```
|
||||
|
||||
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/81Ev7qhTd)"
|
||||
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/ox8a8dGTz)"
|
||||
|
||||
|
||||
This library requires some C++20 features ([concepts and constraints](https://en.cppreference.com/w/cpp/language/constraints),
|
||||
@@ -69,6 +69,7 @@ performed without sacrificing accuracy. Please see the below example for a quick
|
||||
=== "C++ modules"
|
||||
|
||||
```cpp
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
import mp_units;
|
||||
|
||||
@@ -93,13 +94,13 @@ performed without sacrificing accuracy. Please see the below example for a quick
|
||||
constexpr quantity v6 = value_cast<m / s>(v4);
|
||||
constexpr quantity v7 = value_cast<int>(v6);
|
||||
|
||||
std::cout << v1 << '\n'; // 110 km/h
|
||||
std::cout << v2 << '\n'; // 70 mi/h
|
||||
std::cout << std::format("{}", v3) << '\n'; // 110 km/h
|
||||
std::cout << std::format("{:*^14}", v4) << '\n'; // ***70 mi/h****
|
||||
std::cout << std::format("{:%Q in %q}", v5) << '\n'; // 30.5556 in m/s
|
||||
std::cout << std::format("{0:%Q} in {0:%q}", v6) << '\n'; // 31.2928 in m/s
|
||||
std::cout << std::format("{:%Q}", v7) << '\n'; // 31
|
||||
std::cout << v1 << '\n'; // 110 km/h
|
||||
std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h
|
||||
std::cout << std::format("{:*^10}\n", v3); // *110 km/h*
|
||||
std::cout << std::format("{:%N in %U}\n", v4); // 70 in mi/h
|
||||
std::cout << std::format("{:{%N:.2f}%?%U}\n", v5); // 30.56 in m/s
|
||||
std::cout << std::format("{:{%N:.2f}%?{%U:n}}\n", v6); // 31.29 in m s⁻¹
|
||||
std::cout << std::format("{:%N}\n", v7); // 31
|
||||
}
|
||||
```
|
||||
|
||||
@@ -111,6 +112,7 @@ performed without sacrificing accuracy. Please see the below example for a quick
|
||||
#include <mp-units/systems/international/international.h>
|
||||
#include <mp-units/systems/isq/isq.h>
|
||||
#include <mp-units/systems/si/si.h>
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
|
||||
using namespace mp_units;
|
||||
@@ -134,13 +136,13 @@ performed without sacrificing accuracy. Please see the below example for a quick
|
||||
constexpr quantity v6 = value_cast<m / s>(v4);
|
||||
constexpr quantity v7 = value_cast<int>(v6);
|
||||
|
||||
std::cout << v1 << '\n'; // 110 km/h
|
||||
std::cout << v2 << '\n'; // 70 mi/h
|
||||
std::cout << std::format("{}", v3) << '\n'; // 110 km/h
|
||||
std::cout << std::format("{:*^14}", v4) << '\n'; // ***70 mi/h****
|
||||
std::cout << std::format("{:%Q in %q}", v5) << '\n'; // 30.5556 in m/s
|
||||
std::cout << std::format("{0:%Q} in {0:%q}", v6) << '\n'; // 31.2928 in m/s
|
||||
std::cout << std::format("{:%Q}", v7) << '\n'; // 31
|
||||
std::cout << v1 << '\n'; // 110 km/h
|
||||
std::cout << std::setw(10) << std::setfill('*') << v2 << '\n'; // ***70 mi/h
|
||||
std::cout << std::format("{:*^10}\n", v3); // *110 km/h*
|
||||
std::cout << std::format("{:%N in %U}\n", v4); // 70 in mi/h
|
||||
std::cout << std::format("{:{%N:.2f}%?%U}\n", v5); // 30.56 in m/s
|
||||
std::cout << std::format("{:{%N:.2f}%?{%U:n}}\n", v6); // 31.29 in m s⁻¹
|
||||
std::cout << std::format("{:%N}\n", v7); // 31
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
@@ -18,20 +18,20 @@ First, we either import the `mp_units` module or include the headers for:
|
||||
- text formatting and stream output support
|
||||
|
||||
```cpp title="hello_units.cpp" linenums="1"
|
||||
--8<-- "example/hello_units.cpp:28:39"
|
||||
--8<-- "example/hello_units.cpp:28:40"
|
||||
```
|
||||
|
||||
Also, to shorten the definitions, we "import" all the symbols from the `mp_units` namespace.
|
||||
|
||||
```cpp title="hello_units.cpp" linenums="12"
|
||||
--8<-- "example/hello_units.cpp:40:41"
|
||||
```cpp title="hello_units.cpp" linenums="13"
|
||||
--8<-- "example/hello_units.cpp:41:42"
|
||||
```
|
||||
|
||||
Next, we define a simple function that calculates the average speed based on the provided
|
||||
arguments of length and time:
|
||||
|
||||
```cpp title="hello_units.cpp" linenums="13"
|
||||
--8<-- "example/hello_units.cpp:42:45"
|
||||
```cpp title="hello_units.cpp" linenums="14"
|
||||
--8<-- "example/hello_units.cpp:43:46"
|
||||
```
|
||||
|
||||
The above function template takes any quantities implicitly convertible to `isq::length`
|
||||
@@ -45,16 +45,16 @@ that its quantity type is implicitly convertible to `isq::speed`.
|
||||
type is beneficial for users of such a function as it provides more information
|
||||
of what to expect from a function than just using `auto`.
|
||||
|
||||
```cpp title="hello_units.cpp" linenums="17"
|
||||
--8<-- "example/hello_units.cpp:47:50"
|
||||
```cpp title="hello_units.cpp" linenums="18"
|
||||
--8<-- "example/hello_units.cpp:48:51"
|
||||
```
|
||||
|
||||
The above lines explicitly opt into using unit symbols from two systems of units.
|
||||
As this introduces a lot of short identifiers into the current scope, it is not done
|
||||
implicitly while including a header file.
|
||||
|
||||
```cpp title="hello_units.cpp" linenums="21"
|
||||
--8<-- "example/hello_units.cpp:52:58"
|
||||
```cpp title="hello_units.cpp" linenums="22"
|
||||
--8<-- "example/hello_units.cpp:53:59"
|
||||
```
|
||||
|
||||
- Lines `21` & `22` create a quantity of kind `isq::length / isq::time` with the numbers
|
||||
@@ -74,8 +74,8 @@ implicitly while including a header file.
|
||||
- Line `27` does a [value-truncating conversion](../framework_basics/value_conversions.md#value-truncating-conversions)
|
||||
of changing the underlying representation type from `double` to `int`.
|
||||
|
||||
```cpp title="hello_units.cpp" linenums="28"
|
||||
--8<-- "example/hello_units.cpp:60"
|
||||
```cpp title="hello_units.cpp" linenums="29"
|
||||
--8<-- "example/hello_units.cpp:61"
|
||||
```
|
||||
|
||||
The above presents [various ways to print a quantity](../framework_basics/text_output.md).
|
||||
|
||||
Reference in new issue
Block a user