Skip to content

Commit

Permalink
docs: code samples modernized
Browse files Browse the repository at this point in the history
  • Loading branch information
mpusz committed Jan 27, 2024
1 parent 05f481d commit 99167fd
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 116 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,18 @@ static_assert(10 * km / (5 * km) == 2 * one);
static_assert(1000 / (1 * s) == 1 * kHz);
```
_Try it on the [Compiler Explorer](https://godbolt.org/z/81Ev7qhTd)._
_Try it on the [Compiler Explorer](https://godbolt.org/z/ox8a8dGTz)._
This library heavily uses C++20 features (concepts, classes as NTTPs, ...). Thanks to
them the user gets a powerful but still easy to use interfaces and all unit conversions
and dimensional analysis can be performed without sacrificing on runtime performance or
accuracy. Please see the below example for a quick preview of basic library features:
```cpp
#include <format>
#include <iomanip>
#include <iostream>
#include <print>
import mp_units;
using namespace mp_units;
Expand All @@ -118,11 +120,11 @@ int main()
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
std::println("{:%N in %U}", v4); // 70 in mi/h
std::println("{:{%N:.2f}%?%U}", v5); // 30.56 m/s
std::println("{:{%N:.2f}%?{%U:n}}", v6); // 31.29 m s⁻¹
std::println("{:%N}", v7); // 31
}
```

_Try it on the [Compiler Explorer](https://godbolt.org/z/Tsesa1Pvq)._
_Try it on the [Compiler Explorer](https://godbolt.org/z/hWzxf1j1M)._
22 changes: 13 additions & 9 deletions docs/getting_started/look_and_feel.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ performed without sacrificing accuracy. Please see the below example for a quick
=== "C++ modules"

```cpp
#include <format>
#include <iomanip>
#include <iostream>
#include <print>
import mp_units;

using namespace mp_units;
Expand All @@ -97,10 +99,10 @@ performed without sacrificing accuracy. Please see the below example for a quick
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
std::println("{:%N in %U}", v4); // 70 in mi/h
std::println("{:{%N:.2f}%?%U}", v5); // 30.56 m/s
std::println("{:{%N:.2f}%?{%U:n}}", v6); // 31.29 m s⁻¹
std::println("{:%N}", v7); // 31
}
```

Expand All @@ -112,8 +114,10 @@ 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 <format>
#include <iomanip>
#include <iostream>
#include <print>

using namespace mp_units;

Expand All @@ -139,14 +143,14 @@ performed without sacrificing accuracy. Please see the below example for a quick
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
std::println("{:%N in %U}", v4); // 70 in mi/h
std::println("{:{%N:.2f}%?%U}", v5); // 30.56 m/s
std::println("{:{%N:.2f}%?{%U:n}}", v6); // 31.29 m s⁻¹
std::println("{:%N}", v7); // 31
}
```

!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/Tsesa1Pvq)"
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/hWzxf1j1M)"

!!! note

Expand Down
24 changes: 12 additions & 12 deletions docs/getting_started/quick_start.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,40 +149,40 @@ This introduces an additional type-safety.
=== "C++ modules"
```cpp
#include <iostream>
#include <print>
import mp_units;
int main()
{
using namespace mp_units;
using namespace mp_units::si::unit_symbols;
using namespace mp_units::usc::unit_symbols;
quantity_point temp{20. * deg_C};
std::cout << "Temperature: "
<< temp.quantity_from_zero() << " ("
<< temp.in(deg_F).quantity_from_zero() << ")\n";
std::println("Temperature: {} ({})",
temp.quantity_from_zero(),
temp.in(deg_F).quantity_from_zero());
}
```
=== "Header files"
```cpp
#include <mp-units/ostream.h>
#include <mp-units/format.h>
#include <mp-units/systems/si/si.h>
#include <mp-units/systems/usc/usc.h>
#include <iostream>
#include <print>
int main()
{
using namespace mp_units;
using namespace mp_units::si::unit_symbols;
using namespace mp_units::usc::unit_symbols;
quantity_point temp{20. * deg_C};
std::cout << "Temperature: "
<< temp.quantity_from_zero() << " ("
<< temp.in(deg_F).quantity_from_zero() << ")\n";
std::println("Temperature: {} ({})",
temp.quantity_from_zero(),
temp.in(deg_F).quantity_from_zero());
}
```
Expand Down
21 changes: 12 additions & 9 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ The library source code is hosted on [GitHub](https://github.com/mpusz/mp-units)

```cpp
#include <iostream>
#include <print>
import mp_units;

using namespace mp_units;
Expand All @@ -51,36 +52,38 @@ The library source code is hosted on [GitHub](https://github.com/mpusz/mp-units)
int main()
{
constexpr quantity dist = 364.4 * smoot;
std::cout << "Harvard Bridge length = " << dist << "(" << dist.in(usc::foot) << ", " << dist.in(si::metre) << ") ± 1 εar\n";
std::println("Harvard Bridge length = {:{%N:.5} %U} ({:{%N:.5} %U}, {:{%N:.5} %U}) ± 1 εar",
dist, dist.in(usc::foot), dist.in(si::metre));
}
```

=== "Header files"

```cpp
#include <mp-units/ostream.h>
#include <mp-units/format.h>
#include <mp-units/systems/si/si.h>
#include <mp-units/systems/usc/usc.h>
#include <iostream>

#include <print>
using namespace mp_units;

inline constexpr struct smoot : named_unit<"smoot", mag<67> * usc::inch> {} smoot;

int main()
{
constexpr quantity dist = 364.4 * smoot;
std::cout << "Harvard Bridge length = " << dist << "(" << dist.in(usc::foot) << ", " << dist.in(si::metre) << ") ± 1 εar\n";
std::println("Harvard Bridge length = {:{%N:.5} %U} ({:{%N:.5} %U}, {:{%N:.5} %U}) ± 1 εar",
dist, dist.in(usc::foot), dist.in(si::metre));
}
```

Output:

```txt
Harvard Bridge length = 364.4 smoot(2034.57 ft, 620.136 m) ± 1 εar
Harvard Bridge length = 364.4 smoot (2034.6 ft, 620.14 m) ± 1 εar
```

!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/K66zKsT89)"
!!! example "[Try it on Compiler Explorer](https://godbolt.org/z/x77WEWahs)"

??? question "What is `smoot`?"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ constexpr auto speed_of_light_in_vacuum = 1 * si::si2019::speed_of_light_in_vacu
QuantityOf<isq::permittivity_of_vacuum> auto q = 1 / (permeability_of_vacuum * pow<2>(speed_of_light_in_vacuum));
std::cout << "permittivity of vacuum = " << q << " = " << q.in(F / m) << "\n";
std::println("permittivity of vacuum = {} = {:{%N:.3e} %U}", q, q.in(F / m));
```

The above first prints the following:

```text
permittivity of vacuum = 1 μ₀⁻¹ c⁻² = 8.85419e-12 F/m
permittivity of vacuum = 1 μ₀⁻¹ c⁻² = 8.854e-12 F/m
```

As we can clearly see, all the calculations above were just about multiplying and dividing
Expand Down
Loading

0 comments on commit 99167fd

Please sign in to comment.