Skip to content

Commit 9ee98d6

Browse files
committed
Even more modernization and snippets
1 parent 29740c2 commit 9ee98d6

10 files changed

Lines changed: 315 additions & 36 deletions

File tree

code_snippets/chapter12/chapter12.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ $GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fs
3636
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_04-004_euler_gamma_approx.cpp -o ./bin/chapter12_04-004_euler_gamma_approx.exe
3737
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_05-001_complex.cpp -o ./bin/chapter12_05-001_complex.exe
3838
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_05-002_complex.cpp -o ./bin/chapter12_05-002_complex.exe
39-
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_05-003_complex_sinc_function.cpp -o ./bin/chapter12_05-003_complex_sinc_function.exe
39+
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_05-003_complex.cpp -o ./bin/chapter12_05-003_complex.exe
40+
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_05-004_complex_sinc_function.cpp -o ./bin/chapter12_05-004_complex_sinc_function.exe
4041
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_06-001_constexpr_sine.cpp -o ./bin/chapter12_06-001_constexpr_sine.exe
4142
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_07-001_derivative.cpp -o ./bin/chapter12_07-001_derivative.exe
43+
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_07-002_deriv_quadratic.cpp -o ./bin/chapter12_07-002_deriv_quadratic.exe
44+
$GCC -std=$STD -Wall -Wextra -Wpedantic -Werror -O3 -m64 -fsanitize=address -fsanitize=shift -fsanitize=shift-exponent -fsanitize=shift-base -fsanitize=integer-divide-by-zero -fsanitize=null -fsanitize=signed-integer-overflow -fsanitize=bounds -fsanitize=alignment -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow -fsanitize=enum ./chapter12_07-003_integral_j2.cpp -o ./bin/chapter12_07-003_integral_j2.exe
4245

4346
ls -la \
4447
./bin/chapter12_01-001_area_of_circle.exe \
@@ -53,9 +56,12 @@ ls -la \
5356
./bin/chapter12_04-004_euler_gamma_approx.exe \
5457
./bin/chapter12_05-001_complex.exe \
5558
./bin/chapter12_05-002_complex.exe \
56-
./bin/chapter12_05-003_complex_sinc_function.exe \
59+
./bin/chapter12_05-003_complex.exe \
60+
./bin/chapter12_05-004_complex_sinc_function.exe \
5761
./bin/chapter12_06-001_constexpr_sine.exe \
58-
./bin/chapter12_07-001_derivative.exe
62+
./bin/chapter12_07-001_derivative.exe \
63+
./bin/chapter12_07-002_deriv_quadratic.exe \
64+
./bin/chapter12_07-003_integral_j2.exe
5965

6066
result_ls=$?
6167

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// Copyright Christopher Kormanyos 2017 - 2025.
3+
// Distributed under the Boost Software License,
4+
// Version 1.0. (See accompanying file LICENSE_1_0.txt
5+
// or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
8+
// chapter12_05-003_complex.cpp
9+
10+
#include <complex>
11+
#include <iomanip>
12+
#include <iostream>
13+
#include <limits>
14+
#include <sstream>
15+
16+
auto main() -> int;
17+
18+
auto main() -> int
19+
{
20+
std::complex<float> z { }; // (0 + 0 I)
21+
22+
z.real(1.23F); // Set the real part.
23+
z.imag(3.45F); // Set the imag part.
24+
25+
const float zr { z.real() }; // Get the real part, 1.23.
26+
const float zi { z.imag() }; // Get the imag part, 3.45.
27+
28+
static_cast<void>(zr);
29+
static_cast<void>(zi);
30+
31+
const float nz { std::norm(z) }; // 13.4154
32+
33+
std::stringstream strm { };
34+
35+
strm << "nz: " << std::setprecision(std::numeric_limits<float>::digits10) << nz << '\n';
36+
37+
std::cout << strm.str() << std::endl;
38+
}

code_snippets/chapter12/chapter12_05-003_complex_sinc_function.cpp renamed to code_snippets/chapter12/chapter12_05-004_complex_sinc_function.cpp

File renamed without changes.

code_snippets/chapter12/chapter12_07-001_derivative.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// chapter12_07-001_derivative.cpp
99

10-
// See also https://godbolt.org/z/cra4qo4ab
10+
// See also https://godbolt.org/z/baWzcrd1T
1111

1212
#include <cmath>
1313
#include <iomanip>
@@ -43,10 +43,7 @@ auto derivative(const ValueType x,
4343
return ((fifteen_m1 - six_m2) + m3) / ten_dx1;
4444
}
4545

46-
template<typename T>
47-
constexpr T pi = static_cast<T>(3.1415926535'8979323846'2643383279L);
48-
49-
const auto x = static_cast<float>(pi<float> / static_cast<float>(3.0L));
46+
const auto x = static_cast<float>(std::numbers::pi_v<float> / static_cast<float>(3.0L));
5047

5148
// Should be very near 0.5.
5249
const auto y =
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// Copyright Christopher Kormanyos 2017 - 2025.
3+
// Distributed under the Boost Software License,
4+
// Version 1.0. (See accompanying file LICENSE_1_0.txt
5+
// or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
8+
// chapter12_07-002_deriv_quadratic.cpp
9+
10+
#include <cmath>
11+
#include <iomanip>
12+
#include <iostream>
13+
#include <limits>
14+
#include <sstream>
15+
16+
template<typename ValueType,
17+
typename FunctionType>
18+
auto derivative(const ValueType x,
19+
const ValueType dx,
20+
FunctionType function) -> ValueType
21+
{
22+
using value_type = ValueType;
23+
24+
// Compute the derivative using a three point
25+
// central difference rule of O(dx^6).
26+
27+
const auto dx1 = dx;
28+
const auto dx2 = static_cast<value_type>(dx1 * 2);
29+
const auto dx3 = static_cast<value_type>(dx1 * 3);
30+
31+
const auto m1 = static_cast<value_type>((function(x + dx1) - function(x - dx1)) / 2);
32+
33+
const auto m2 = static_cast<value_type>((function(x + dx2) - function(x - dx2)) / 4);
34+
35+
const auto m3 = static_cast<value_type>((function(x + dx3) - function(x - dx3)) / 6);
36+
37+
const auto fifteen_m1 = static_cast<value_type>(15 * m1);
38+
const auto six_m2 = static_cast<value_type>( 6 * m2);
39+
const auto ten_dx1 = static_cast<value_type>(10 * dx1);
40+
41+
return ((fifteen_m1 - six_m2) + m3) / ten_dx1;
42+
}
43+
44+
template<typename T>
45+
class quadratic
46+
{
47+
public:
48+
const T a;
49+
const T b;
50+
const T c;
51+
52+
quadratic(const T& a_,
53+
const T& b_,
54+
const T& c_) : a(a_),
55+
b(b_),
56+
c(c_) { }
57+
58+
auto operator()(const T& x) const -> T
59+
{
60+
return ((a * x + b) * x) + c;
61+
}
62+
};
63+
64+
const float x { 0.5F };
65+
66+
// Should be very near 4.6.
67+
const float y =
68+
derivative(x,
69+
0.01F,
70+
quadratic<float>(1.2F, 3.4F, 5.6F));
71+
72+
auto main() -> int;
73+
74+
auto main() -> int
75+
{
76+
std::stringstream strm { };
77+
78+
// 4.6
79+
strm << std::setprecision(std::numeric_limits<float>::digits10) << y << '\n';
80+
81+
using std::fabs;
82+
83+
const auto delta = fabs(static_cast<float>(1.0L) - static_cast<float>(y / static_cast<float>(4.6L)));
84+
85+
const auto result_close_fraction_is_ok = (delta < static_cast<float>(std::numeric_limits<float>::epsilon() * 128));
86+
87+
strm << "result_close_fraction_is_ok: " << std::boolalpha << result_close_fraction_is_ok << '\n';
88+
89+
std::cout << strm.str() << std::endl;
90+
91+
return (result_close_fraction_is_ok ? 0 : -1);
92+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
///////////////////////////////////////////////////////////////////////////////
2+
// Copyright Christopher Kormanyos 2017 - 2025.
3+
// Distributed under the Boost Software License,
4+
// Version 1.0. (See accompanying file LICENSE_1_0.txt
5+
// or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
8+
// chapter12_07-003_integral_j2.cpp
9+
10+
#include <cmath>
11+
#include <cstdint>
12+
#include <iomanip>
13+
#include <iostream>
14+
#include <limits>
15+
#include <numbers>
16+
#include <sstream>
17+
18+
template<typename real_value_type,
19+
typename real_function_type>
20+
auto integral(
21+
const real_value_type& a,
22+
const real_value_type& b,
23+
const real_value_type& tol,
24+
real_function_type real_function) -> real_value_type
25+
{
26+
std::uint_fast32_t n2(1);
27+
28+
real_value_type step = ((b - a) / 2U);
29+
30+
real_value_type result =
31+
(real_function(a) + real_function(b)) * step;
32+
33+
const std::uint_fast8_t k_max = UINT8_C(32);
34+
35+
for(std::uint_fast8_t k(0U); k < k_max; ++k)
36+
{
37+
real_value_type sum(0);
38+
39+
for(std::uint_fast32_t j(0U); j < n2; ++j)
40+
{
41+
const std::uint_fast32_t two_j_plus_one =
42+
(j * UINT32_C(2)) + UINT32_C(1);
43+
44+
sum +=
45+
real_function(a + (step * two_j_plus_one));
46+
}
47+
48+
const real_value_type tmp = result;
49+
50+
result = (result / 2U) + (step * sum);
51+
52+
const real_value_type ratio =
53+
std::abs(tmp / result);
54+
55+
const real_value_type delta = std::abs(ratio - 1U);
56+
57+
if((k > UINT8_C(1)) && (delta < tol))
58+
{
59+
break;
60+
}
61+
62+
n2 *= 2U;
63+
64+
step /= 2U;
65+
}
66+
67+
return result;
68+
}
69+
70+
template<typename float_type>
71+
auto cyl_bessel_j(const std::uint_fast8_t n,
72+
const float_type& x) -> float_type
73+
{
74+
constexpr float_type eps
75+
{ std::numeric_limits<float_type>::epsilon() };
76+
77+
const float_type tol { std::sqrt(eps) };
78+
79+
const float_type jn =
80+
integral
81+
(
82+
float_type(0),
83+
std::numbers::pi_v<float_type>,
84+
tol,
85+
[&x,&n](const float_type& t) -> float_type
86+
{
87+
return std::cos(x * std::sin(t) - (n * t));
88+
}
89+
)
90+
/ std::numbers::pi_v<float_type>;
91+
92+
return jn;
93+
}
94+
95+
auto main() -> int;
96+
97+
auto main() -> int
98+
{
99+
const float j2 { cyl_bessel_j(UINT8_C(2), 1.23F) };
100+
101+
// Computed result: 0.166369
102+
// Known value: 0.1663693837...
103+
104+
std::stringstream strm { };
105+
106+
strm << "j2: " << std::setprecision(std::numeric_limits<float>::digits10) << j2 << '\n';
107+
108+
std::cout << strm.str() << std::endl;
109+
}

code_snippets/chapter12/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ This folder contains the code snippets for Chapter 12.
66
## Try it at _godbolt_
77

88
<p align="center">
9-
<a href="https://godbolt.org/z/cra4qo4ab" alt="godbolt">
9+
<a href="https://godbolt.org/z/baWzcrd1T" alt="godbolt">
1010
<img src="https://img.shields.io/badge/try%20it%20on-godbolt-green" /></a>
1111
</p>
1212

1313
Try the [`chapter12_07-001_derivative.cpp`](./chapter12_07-001_derivative.cpp)
1414
program at this
15-
[short link](https://godbolt.org/z/cra4qo4ab) to [godbolt](https://godbolt.org/).
15+
[short link](https://godbolt.org/z/baWzcrd1T) to [godbolt](https://godbolt.org/).

0 commit comments

Comments
 (0)