mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-07 14:14:27 +02:00
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:
|
`mp_units` namespace:
|
||||||
|
|
||||||
```cpp title="avg_speed.cpp" linenums="1"
|
```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
|
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
|
and integral and floating-point representation types, respectively, and a third function
|
||||||
that we introduced in the [previous example](hello_units.md):
|
that we introduced in the [previous example](hello_units.md):
|
||||||
|
|
||||||
```cpp title="avg_speed.cpp" linenums="12"
|
```cpp title="avg_speed.cpp" linenums="16"
|
||||||
--8<-- "example/avg_speed.cpp:40:54"
|
--8<-- "example/avg_speed.cpp:44:58"
|
||||||
```
|
```
|
||||||
|
|
||||||
We also added a simple utility to print our results:
|
We also added a simple utility to print our results:
|
||||||
|
|
||||||
```cpp title="avg_speed.cpp" linenums="27"
|
```cpp title="avg_speed.cpp" linenums="31"
|
||||||
--8<-- "example/avg_speed.cpp:56:62"
|
--8<-- "example/avg_speed.cpp:60:66"
|
||||||
```
|
```
|
||||||
|
|
||||||
Now, let's analyze how those three utility functions behave with different sets of arguments.
|
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:
|
First, we are going to use quantities of SI units and integral representation:
|
||||||
|
|
||||||
```cpp title="avg_speed.cpp" linenums="27"
|
```cpp title="avg_speed.cpp" linenums="38"
|
||||||
--8<-- "example/avg_speed.cpp:64:78"
|
--8<-- "example/avg_speed.cpp:68:82"
|
||||||
```
|
```
|
||||||
|
|
||||||
The above provides the following output:
|
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
|
The second scenario is really similar to the previous one, but this time, function arguments
|
||||||
have floating-point representation types:
|
have floating-point representation types:
|
||||||
|
|
||||||
```cpp title="avg_speed.cpp" linenums="42"
|
```cpp title="avg_speed.cpp" linenums="53"
|
||||||
--8<-- "example/avg_speed.cpp:80:91"
|
--8<-- "example/avg_speed.cpp:84:95"
|
||||||
```
|
```
|
||||||
|
|
||||||
Conversion from floating-point to integral representation types is
|
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
|
Next, let's do the same for integral and floating-point representations, but this time
|
||||||
using US Customary units:
|
using US Customary units:
|
||||||
|
|
||||||
```cpp title="avg_speed.cpp" linenums="54"
|
```cpp title="avg_speed.cpp" linenums="65"
|
||||||
--8<-- "example/avg_speed.cpp:93:124"
|
--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
|
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:
|
In the end, we repeat the scenario for CGS units:
|
||||||
|
|
||||||
```cpp title="avg_speed.cpp" linenums="86"
|
```cpp title="avg_speed.cpp" linenums="98"
|
||||||
--8<-- "example/avg_speed.cpp:126:155"
|
--8<-- "example/avg_speed.cpp:131:161"
|
||||||
```
|
```
|
||||||
|
|
||||||
Again, we observe `value_cast` being used in the same places and consistent truncation errors
|
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:
|
The example file ends with a simple `main()` function:
|
||||||
|
|
||||||
```cpp title="avg_speed.cpp" linenums="116"
|
```cpp title="avg_speed.cpp" linenums="129"
|
||||||
--8<-- "example/avg_speed.cpp:157:"
|
--8<-- "example/avg_speed.cpp:163:"
|
||||||
```
|
```
|
||||||
|
@@ -18,20 +18,20 @@ First, we include the headers for:
|
|||||||
- text and stream output support
|
- text and stream output support
|
||||||
|
|
||||||
```cpp title="hello_units.cpp" linenums="1"
|
```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.
|
Also, to shorten the definitions, we "import" `mp_units` namespace.
|
||||||
|
|
||||||
```cpp title="hello_units.cpp" linenums="7"
|
```cpp title="hello_units.cpp" linenums="12"
|
||||||
--8<-- "example/hello_units.cpp:35:36"
|
--8<-- "example/hello_units.cpp:40:41"
|
||||||
```
|
```
|
||||||
|
|
||||||
Next we define a simple function that calculates average speed based on the provided
|
Next we define a simple function that calculates average speed based on the provided
|
||||||
arguments of length and time:
|
arguments of length and time:
|
||||||
|
|
||||||
```cpp title="hello_units.cpp" linenums="8"
|
```cpp title="hello_units.cpp" linenums="13"
|
||||||
--8<-- "example/hello_units.cpp:37:40"
|
--8<-- "example/hello_units.cpp:42:45"
|
||||||
```
|
```
|
||||||
|
|
||||||
The above function template takes any quantities implicitly convertible to `isq::length`
|
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
|
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`.
|
of what to expect from a function than just using `auto`.
|
||||||
|
|
||||||
```cpp title="hello_units.cpp" linenums="12"
|
```cpp title="hello_units.cpp" linenums="17"
|
||||||
--8<-- "example/hello_units.cpp:42:45"
|
--8<-- "example/hello_units.cpp:47:50"
|
||||||
```
|
```
|
||||||
|
|
||||||
The above lines explicitly opt-in to use unit symbols from two systems of units.
|
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
|
As this introduces a lot of short identifiers into the current scope, it is not done
|
||||||
implicitly while including a header file.
|
implicitly while including a header file.
|
||||||
|
|
||||||
```cpp title="hello_units.cpp" linenums="16"
|
```cpp title="hello_units.cpp" linenums="21"
|
||||||
--8<-- "example/hello_units.cpp:47:53"
|
--8<-- "example/hello_units.cpp:52:58"
|
||||||
```
|
```
|
||||||
|
|
||||||
- Lines `16` & `17` create a quantity of kind `isq::length / isq::time` with the numbers
|
- 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)
|
- 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`.
|
of changing the underlying representation type from `double` to `int`.
|
||||||
|
|
||||||
```cpp title="hello_units.cpp" linenums="23"
|
```cpp title="hello_units.cpp" linenums="28"
|
||||||
--8<-- "example/hello_units.cpp:55"
|
--8<-- "example/hello_units.cpp:60"
|
||||||
```
|
```
|
||||||
|
|
||||||
The above presents [various ways to print a quantity](../framework_basics/text_output.md).
|
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.
|
work in practice.
|
||||||
|
|
||||||
```cpp title="si_constants.cpp" linenums="1"
|
```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
|
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)
|
[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.
|
to be able to express vector quantities with simple scalar types.
|
||||||
|
|
||||||
```cpp title="si_constants.cpp" linenums="1"
|
```cpp title="si_constants.cpp" linenums="13"
|
||||||
--8<-- "example/si_constants.cpp:36:"
|
--8<-- "example/si_constants.cpp:41:"
|
||||||
```
|
```
|
||||||
|
|
||||||
The main part of the example prints all of the SI-defining constants. While analyzing the output of
|
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