forked from mpusz/mp-units
docs: update example source lines
This commit is contained in:
committed by
Mateusz Pusz
parent
52b0c03346
commit
ceb1522a1e
@@ -18,28 +18,28 @@ First, we include all the necessary header files and import all the identifiers
|
||||
`mp_units` namespace:
|
||||
|
||||
```cpp title="avg_speed.cpp" linenums="1"
|
||||
--8<-- "example/avg_speed.cpp:28:38"
|
||||
--8<-- "example/avg_speed.cpp:28:42"
|
||||
```
|
||||
|
||||
Next, we define two functions calculating average speed based on quantities of fixed units
|
||||
and integral and floating-point representation types, respectively, and a third function
|
||||
that we introduced in the [previous example](hello_units.md):
|
||||
|
||||
```cpp title="avg_speed.cpp" linenums="12"
|
||||
--8<-- "example/avg_speed.cpp:40:54"
|
||||
```cpp title="avg_speed.cpp" linenums="16"
|
||||
--8<-- "example/avg_speed.cpp:44:58"
|
||||
```
|
||||
|
||||
We also added a simple utility to print our results:
|
||||
|
||||
```cpp title="avg_speed.cpp" linenums="27"
|
||||
--8<-- "example/avg_speed.cpp:56:62"
|
||||
```cpp title="avg_speed.cpp" linenums="31"
|
||||
--8<-- "example/avg_speed.cpp:60:66"
|
||||
```
|
||||
|
||||
Now, let's analyze how those three utility functions behave with different sets of arguments.
|
||||
First, we are going to use quantities of SI units and integral representation:
|
||||
|
||||
```cpp title="avg_speed.cpp" linenums="27"
|
||||
--8<-- "example/avg_speed.cpp:64:78"
|
||||
```cpp title="avg_speed.cpp" linenums="38"
|
||||
--8<-- "example/avg_speed.cpp:68:82"
|
||||
```
|
||||
|
||||
The above provides the following output:
|
||||
@@ -61,8 +61,8 @@ can be easily observed in the first case where we deal with integral representat
|
||||
The second scenario is really similar to the previous one, but this time, function arguments
|
||||
have floating-point representation types:
|
||||
|
||||
```cpp title="avg_speed.cpp" linenums="42"
|
||||
--8<-- "example/avg_speed.cpp:80:91"
|
||||
```cpp title="avg_speed.cpp" linenums="53"
|
||||
--8<-- "example/avg_speed.cpp:84:95"
|
||||
```
|
||||
|
||||
Conversion from floating-point to integral representation types is
|
||||
@@ -82,8 +82,8 @@ Average speed of a car that makes 220 km in 2 h is 110 km/h.
|
||||
Next, let's do the same for integral and floating-point representations, but this time
|
||||
using US Customary units:
|
||||
|
||||
```cpp title="avg_speed.cpp" linenums="54"
|
||||
--8<-- "example/avg_speed.cpp:93:124"
|
||||
```cpp title="avg_speed.cpp" linenums="65"
|
||||
--8<-- "example/avg_speed.cpp:97:129"
|
||||
```
|
||||
|
||||
One important difference here is the fact that as it is not possible to make a lossless conversion
|
||||
@@ -108,8 +108,8 @@ Please note how the first and third results get truncated using integral represe
|
||||
|
||||
In the end, we repeat the scenario for CGS units:
|
||||
|
||||
```cpp title="avg_speed.cpp" linenums="86"
|
||||
--8<-- "example/avg_speed.cpp:126:155"
|
||||
```cpp title="avg_speed.cpp" linenums="98"
|
||||
--8<-- "example/avg_speed.cpp:131:161"
|
||||
```
|
||||
|
||||
Again, we observe `value_cast` being used in the same places and consistent truncation errors
|
||||
@@ -129,6 +129,6 @@ Average speed of a car that makes 2.2e+07 cm in 7200 s is 110 km/h.
|
||||
|
||||
The example file ends with a simple `main()` function:
|
||||
|
||||
```cpp title="avg_speed.cpp" linenums="116"
|
||||
--8<-- "example/avg_speed.cpp:157:"
|
||||
```cpp title="avg_speed.cpp" linenums="129"
|
||||
--8<-- "example/avg_speed.cpp:163:"
|
||||
```
|
||||
|
@@ -18,20 +18,20 @@ First, we include the headers for:
|
||||
- text and stream output support
|
||||
|
||||
```cpp title="hello_units.cpp" linenums="1"
|
||||
--8<-- "example/hello_units.cpp:28:33"
|
||||
--8<-- "example/hello_units.cpp:28:39"
|
||||
```
|
||||
|
||||
Also, to shorten the definitions, we "import" `mp_units` namespace.
|
||||
|
||||
```cpp title="hello_units.cpp" linenums="7"
|
||||
--8<-- "example/hello_units.cpp:35:36"
|
||||
```cpp title="hello_units.cpp" linenums="12"
|
||||
--8<-- "example/hello_units.cpp:40:41"
|
||||
```
|
||||
|
||||
Next we define a simple function that calculates average speed based on the provided
|
||||
arguments of length and time:
|
||||
|
||||
```cpp title="hello_units.cpp" linenums="8"
|
||||
--8<-- "example/hello_units.cpp:37:40"
|
||||
```cpp title="hello_units.cpp" linenums="13"
|
||||
--8<-- "example/hello_units.cpp:42:45"
|
||||
```
|
||||
|
||||
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 really useful 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="12"
|
||||
--8<-- "example/hello_units.cpp:42:45"
|
||||
```cpp title="hello_units.cpp" linenums="17"
|
||||
--8<-- "example/hello_units.cpp:47:50"
|
||||
```
|
||||
|
||||
The above lines explicitly opt-in to use 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="16"
|
||||
--8<-- "example/hello_units.cpp:47:53"
|
||||
```cpp title="hello_units.cpp" linenums="21"
|
||||
--8<-- "example/hello_units.cpp:52:58"
|
||||
```
|
||||
|
||||
- Lines `16` & `17` create a quantity of kind `isq::length / isq::time` with the numbers
|
||||
@@ -74,8 +74,8 @@ implicitly while including a header file.
|
||||
- Line `22` 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="23"
|
||||
--8<-- "example/hello_units.cpp:55"
|
||||
```cpp title="hello_units.cpp" linenums="28"
|
||||
--8<-- "example/hello_units.cpp:60"
|
||||
```
|
||||
|
||||
The above presents [various ways to print a quantity](../framework_basics/text_output.md).
|
||||
|
@@ -13,7 +13,7 @@ how [Faster-than-lightspeed Constants](../framework_basics/faster_than_lightspee
|
||||
work in practice.
|
||||
|
||||
```cpp title="si_constants.cpp" linenums="1"
|
||||
--8<-- "example/si_constants.cpp:28:34"
|
||||
--8<-- "example/si_constants.cpp:28:39"
|
||||
```
|
||||
|
||||
As always, we start with the inclusion of all the needed header files. After that, for
|
||||
@@ -21,8 +21,8 @@ the simplicity of this example, we
|
||||
[hack the character of quantities](../framework_basics/character_of_a_quantity.md#hacking-the-character)
|
||||
to be able to express vector quantities with simple scalar types.
|
||||
|
||||
```cpp title="si_constants.cpp" linenums="1"
|
||||
--8<-- "example/si_constants.cpp:36:"
|
||||
```cpp title="si_constants.cpp" linenums="13"
|
||||
--8<-- "example/si_constants.cpp:41:"
|
||||
```
|
||||
|
||||
The main part of the example prints all of the SI-defining constants. While analyzing the output of
|
||||
|
Reference in New Issue
Block a user