Skip to content

Commit c01c750

Browse files
authored
Merge pull request #713 from ckormanyos/more_syntax
More syntax
2 parents c8ff9a6 + 0c22b2e commit c01c750

3 files changed

Lines changed: 53 additions & 25 deletions

File tree

code_snippets/chapter12/chapter12_07-001_derivative.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2017 - 2025.
2+
// Copyright Christopher Kormanyos 2017 - 2026.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -13,13 +13,15 @@
1313
#include <iomanip>
1414
#include <iostream>
1515
#include <limits>
16+
#include <numbers>
1617
#include <sstream>
1718

1819
template<typename ValueType,
1920
typename FunctionType>
20-
auto derivative(const ValueType x,
21-
const ValueType dx,
22-
FunctionType function) -> ValueType
21+
constexpr auto derivative(ValueType x,
22+
ValueType dx,
23+
FunctionType function)
24+
-> ValueType
2325
{
2426
using value_type = ValueType;
2527

@@ -43,13 +45,13 @@ auto derivative(const ValueType x,
4345
return ((fifteen_m1 - six_m2) + m3) / ten_dx1;
4446
}
4547

46-
const auto x = static_cast<float>(std::numbers::pi_v<float> / static_cast<float>(3.0L));
48+
constexpr float xval { static_cast<float>(std::numbers::pi_v<float> / static_cast<float>(3.0L)) };
4749

4850
// Should be very near 0.5.
4951
const auto y =
50-
derivative(x,
51-
static_cast<float>(0.01L),
52-
[](const float& x) -> float
52+
derivative(xval,
53+
0.01F,
54+
[](float x) -> float
5355
{
5456
return std::sin(x);
5557
});
@@ -61,7 +63,7 @@ auto main() -> int
6163
std::stringstream strm { };
6264

6365
// 0.500003
64-
strm << std::setprecision(std::numeric_limits<float>::digits10) << y << '\n';
66+
strm << std::setprecision(std::numeric_limits<float>::digits10) << std::fixed << y << '\n';
6567

6668
using std::fabs;
6769

code_snippets/chapter12/chapter12_07-002_deriv_quadratic.cpp

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
///////////////////////////////////////////////////////////////////////////////
2-
// Copyright Christopher Kormanyos 2017 - 2025.
2+
// Copyright Christopher Kormanyos 2017 - 2026.
33
// Distributed under the Boost Software License,
44
// Version 1.0. (See accompanying file LICENSE_1_0.txt
55
// or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -15,9 +15,10 @@
1515

1616
template<typename ValueType,
1717
typename FunctionType>
18-
auto derivative(const ValueType x,
19-
const ValueType dx,
20-
FunctionType function) -> ValueType
18+
constexpr auto derivative(const ValueType x,
19+
const ValueType dx,
20+
FunctionType function)
21+
-> ValueType
2122
{
2223
using value_type = ValueType;
2324

@@ -49,34 +50,31 @@ class quadratic
4950
const T b;
5051
const T c;
5152

52-
quadratic(const T& a_,
53-
const T& b_,
54-
const T& c_) : a(a_),
55-
b(b_),
56-
c(c_) { }
53+
explicit constexpr quadratic(T a_, T b_, T c_)
54+
: a(a_), b(b_), c(c_) { }
5755

58-
auto operator()(const T& x) const -> T
56+
constexpr auto operator()(T x) const -> T
5957
{
6058
return ((a * x + b) * x) + c;
6159
}
6260
};
6361

64-
const float x { 0.5F };
62+
constexpr float xval { 0.5F };
6563

6664
// Should be very near 4.6.
67-
const float y =
68-
derivative(x,
65+
constexpr float y =
66+
derivative(xval,
6967
0.01F,
70-
quadratic<float>(1.2F, 3.4F, 5.6F));
68+
quadratic(1.2F, 3.4F, 5.6F));
7169

7270
auto main() -> int;
7371

7472
auto main() -> int
7573
{
7674
std::stringstream strm { };
7775

78-
// 4.6
79-
strm << std::setprecision(std::numeric_limits<float>::digits10) << y << '\n';
76+
// 4.600001
77+
strm << std::setprecision(std::numeric_limits<float>::digits10) << std::fixed << y << '\n';
8078

8179
using std::fabs;
8280

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
rem ///////////////////////////////////////////////////////////////////////////////
2+
rem // Copyright Christopher Kormanyos 2007 - 2025.
3+
rem // Distributed under the Boost Software License,
4+
rem // Version 1.0. (See accompanying file LICENSE_1_0.txt
5+
rem // or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
rem //
7+
8+
echo off
9+
10+
set AVRDUDE=.\avrdude.exe
11+
12+
set HEX=../../../../../examples/chapter11_07a/bin/chapter11_07a.hex
13+
14+
15+
rem Erase the chip.
16+
echo "Erase the chip."
17+
%AVRDUDE% -c avrisp2 -p m328p -P usb -e
18+
echo.
19+
20+
rem Flash the HEX-file.
21+
echo "Flash the HEX-file."
22+
%AVRDUDE% -c avrisp2 -p m328p -P usb -U flash:w:%HEX%:i
23+
echo.
24+
25+
rem Verify the flash.
26+
echo "Verify the flash."
27+
%AVRDUDE% -c avrisp2 -p m328p -P usb -U flash:v:%HEX%:i
28+
echo.

0 commit comments

Comments
 (0)