Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions code_snippets/chapter12/chapter12_07-001_derivative.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2017 - 2025.
// Copyright Christopher Kormanyos 2017 - 2026.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -13,13 +13,15 @@
#include <iomanip>
#include <iostream>
#include <limits>
#include <numbers>
#include <sstream>

template<typename ValueType,
typename FunctionType>
auto derivative(const ValueType x,
const ValueType dx,
FunctionType function) -> ValueType
constexpr auto derivative(ValueType x,
ValueType dx,
FunctionType function)
-> ValueType
{
using value_type = ValueType;

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

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

// Should be very near 0.5.
const auto y =
derivative(x,
static_cast<float>(0.01L),
[](const float& x) -> float
derivative(xval,
0.01F,
[](float x) -> float
{
return std::sin(x);
});
Expand All @@ -61,7 +63,7 @@ auto main() -> int
std::stringstream strm { };

// 0.500003
strm << std::setprecision(std::numeric_limits<float>::digits10) << y << '\n';
strm << std::setprecision(std::numeric_limits<float>::digits10) << std::fixed << y << '\n';

using std::fabs;

Expand Down
30 changes: 14 additions & 16 deletions code_snippets/chapter12/chapter12_07-002_deriv_quadratic.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
///////////////////////////////////////////////////////////////////////////////
// Copyright Christopher Kormanyos 2017 - 2025.
// Copyright Christopher Kormanyos 2017 - 2026.
// Distributed under the Boost Software License,
// Version 1.0. (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)
Expand All @@ -15,9 +15,10 @@

template<typename ValueType,
typename FunctionType>
auto derivative(const ValueType x,
const ValueType dx,
FunctionType function) -> ValueType
constexpr auto derivative(const ValueType x,
const ValueType dx,
FunctionType function)
-> ValueType
{
using value_type = ValueType;

Expand Down Expand Up @@ -49,34 +50,31 @@ class quadratic
const T b;
const T c;

quadratic(const T& a_,
const T& b_,
const T& c_) : a(a_),
b(b_),
c(c_) { }
explicit constexpr quadratic(T a_, T b_, T c_)
: a(a_), b(b_), c(c_) { }

auto operator()(const T& x) const -> T
constexpr auto operator()(T x) const -> T
{
return ((a * x + b) * x) + c;
}
};

const float x { 0.5F };
constexpr float xval { 0.5F };

// Should be very near 4.6.
const float y =
derivative(x,
constexpr float y =
derivative(xval,
0.01F,
quadratic<float>(1.2F, 3.4F, 5.6F));
quadratic(1.2F, 3.4F, 5.6F));

auto main() -> int;

auto main() -> int
{
std::stringstream strm { };

// 4.6
strm << std::setprecision(std::numeric_limits<float>::digits10) << y << '\n';
// 4.600001
strm << std::setprecision(std::numeric_limits<float>::digits10) << std::fixed << y << '\n';

using std::fabs;

Expand Down
28 changes: 28 additions & 0 deletions ref_app/tools/AVR/avrdude/avrdude-v8.0-windows-x64/11_07a.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
rem ///////////////////////////////////////////////////////////////////////////////
rem // Copyright Christopher Kormanyos 2007 - 2025.
rem // Distributed under the Boost Software License,
rem // Version 1.0. (See accompanying file LICENSE_1_0.txt
rem // or copy at http://www.boost.org/LICENSE_1_0.txt)
rem //

echo off

set AVRDUDE=.\avrdude.exe

set HEX=../../../../../examples/chapter11_07a/bin/chapter11_07a.hex


rem Erase the chip.
echo "Erase the chip."
%AVRDUDE% -c avrisp2 -p m328p -P usb -e
echo.

rem Flash the HEX-file.
echo "Flash the HEX-file."
%AVRDUDE% -c avrisp2 -p m328p -P usb -U flash:w:%HEX%:i
echo.

rem Verify the flash.
echo "Verify the flash."
%AVRDUDE% -c avrisp2 -p m328p -P usb -U flash:v:%HEX%:i
echo.
Loading