Skip to content

Commit 7d7cb79

Browse files
authored
feat(math): Remove accidental double-promotion from float and turn on warnings in example (#528)
* feat(math): Remove accidental double-promotion from float and turn on warnings in example * use powf explicitly in led
1 parent 8e87da0 commit 7d7cb79

File tree

6 files changed

+23
-21
lines changed

6 files changed

+23
-21
lines changed

components/joystick/example/main/joystick_example.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extern "C" void app_main(void) {
1616
float min = 0;
1717
float max = 255;
1818
float center = 127;
19-
float deadband_percent = 0.1;
19+
float deadband_percent = 0.1f;
2020
float deadband = deadband_percent * (max - min);
2121

2222
// circular joystick
@@ -58,8 +58,8 @@ extern "C" void app_main(void) {
5858
float min = 0;
5959
float max = 255;
6060
float center = 127;
61-
float center_deadzone_radius = 0.5;
62-
float range_deadzone_radius = 0.5;
61+
float center_deadzone_radius = 0.5f;
62+
float range_deadzone_radius = 0.5f;
6363

6464
// circular joystick
6565
espp::Joystick js1({
@@ -79,8 +79,8 @@ extern "C" void app_main(void) {
7979
}
8080
// update the deadzones to be very large (overlapping)
8181
logger.info("Setting deadzones to overlap");
82-
js1.set_center_deadzone_radius(0.75);
83-
js1.set_range_deadzone(0.75);
82+
js1.set_center_deadzone_radius(0.75f);
83+
js1.set_range_deadzone(0.75f);
8484
// and run the loop again
8585
fmt::print("raw x, raw y, js x, js y\n");
8686
for (float x = min - 10.0f; x <= max + 10.0f; x += 10.0f) {

components/led/src/led.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ bool IRAM_ATTR Led::cb_ledc_fade_end_event(const ledc_cb_param_t *param, void *u
1919
Led::Led(const Config &config) noexcept
2020
: BaseComponent("Led", config.log_level)
2121
, duty_resolution_(config.duty_resolution)
22-
, max_raw_duty_((uint32_t)(std::pow(2, (int)duty_resolution_) - 1))
22+
, max_raw_duty_((uint32_t)(std::powf(2, (int)duty_resolution_) - 1))
2323
, channels_(config.channels) {
2424

2525
logger_.info("Initializing timer");

components/math/example/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ cmake_minimum_required(VERSION 3.20)
55
set(ENV{IDF_COMPONENT_MANAGER} "0")
66
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
77

8+
add_compile_options(-Wdouble-promotion)
9+
810
# add the component directories that we want to use
911
set(EXTRA_COMPONENT_DIRS
1012
"../../../components/"

components/math/example/main/math_example.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extern "C" void app_main(void) {
4040
{
4141
//! [fast_ln example]
4242
float x = 3.0f;
43-
auto slow = log(x);
43+
auto slow = logf(x);
4444
auto fast = espp::fast_ln(x);
4545
auto diff = std::abs(slow - fast);
4646
fmt::print("ln({}) = {} (slow), {} (fast), diff = {}\n", x, slow, fast, diff);
@@ -49,7 +49,7 @@ extern "C" void app_main(void) {
4949
{
5050
//! [fast_inv_sqrt example]
5151
float x = 3.0f;
52-
auto slow = sqrt(x);
52+
auto slow = sqrtf(x);
5353
auto fast = 1.0f / espp::fast_inv_sqrt(x);
5454
auto diff = std::abs(slow - fast);
5555
fmt::print("sqrt({}) = {} (slow), {} (fast), diff = {}\n", x, slow, fast, diff);
@@ -58,7 +58,7 @@ extern "C" void app_main(void) {
5858
{
5959
//! [fast_sin example]
6060
float x = 3.0f;
61-
auto slow = sin(x);
61+
auto slow = sinf(x);
6262
auto fast = espp::fast_sin(x);
6363
auto diff = std::abs(slow - fast);
6464
fmt::print("sin({}) = {} (slow), {} (fast), diff = {}\n", x, slow, fast, diff);
@@ -67,7 +67,7 @@ extern "C" void app_main(void) {
6767
{
6868
//! [fast_cos example]
6969
float x = 3.0f;
70-
auto slow = cos(x);
70+
auto slow = cosf(x);
7171
auto fast = espp::fast_cos(x);
7272
auto diff = std::abs(slow - fast);
7373
fmt::print("cos({}) = {} (slow), {} (fast), diff = {}\n", x, slow, fast, diff);
@@ -368,7 +368,7 @@ extern "C" void app_main(void) {
368368
};
369369
fmt::print("points: {}\n", points);
370370
fmt::print("% x, instant, aggressive, delayed\n");
371-
for (float x = 0; x <= 1.0; x += 0.01) {
371+
for (float x = 0; x <= 1.0f; x += 0.01f) {
372372
float y1 = espp::piecewise_linear(instant, x);
373373
float y2 = espp::piecewise_linear(aggressive, x);
374374
float y3 = espp::piecewise_linear(delayed, x);

components/math/include/fast_math.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ namespace espp {
1414
* @param degrees Angle in degrees
1515
* @return Angle in radians
1616
*/
17-
[[maybe_unused]] static float deg_to_rad(float degrees) { return degrees * M_PI / 180.0f; }
17+
[[maybe_unused]] static float deg_to_rad(float degrees) { return degrees * (float)(M_PI) / 180.0f; }
1818

1919
/**
2020
* @brief Convert radians to degrees
2121
* @param radians Angle in radians
2222
* @return Angle in degrees
2323
*/
24-
[[maybe_unused]] static float rad_to_deg(float radians) { return radians * 180.0f / M_PI; }
24+
[[maybe_unused]] static float rad_to_deg(float radians) { return radians * 180.0f / (float)(M_PI); }
2525

2626
/**
2727
* @brief Simple square of the input.
@@ -125,7 +125,7 @@ template <typename T> int sgn(T x) { return (T(0) < x) - (x < T(0)); }
125125
* @param x Floating point value to be rounded.
126126
* @return Nearest integer to x.
127127
*/
128-
[[maybe_unused]] static int round(float x) { return (x > 0) ? (int)(x + 0.5) : (int)(x - 0.5); }
128+
[[maybe_unused]] static int round(float x) { return (x > 0) ? (int)(x + 0.5f) : (int)(x - 0.5f); }
129129

130130
/**
131131
* @brief fast natural log function, ln(x).
@@ -144,7 +144,7 @@ template <typename T> int sgn(T x) { return (T(0) < x) - (x < T(0)); }
144144
bx = 1065353216 | (bx & 8388607);
145145
// x = * reinterpret_cast<float *>(&bx);
146146
memcpy(&x, &bx, sizeof(x));
147-
return -1.49278 + (2.11263 + (-0.729104 + 0.10969 * x) * x) * x + 0.6931471806 * t;
147+
return -1.49278f + (2.11263f + (-0.729104f + 0.10969f * x) * x) * x + 0.6931471806f * t;
148148
}
149149

150150
/**
@@ -173,11 +173,11 @@ static constexpr int sine_array[200] = {
173173
* @return Approximation of sin(value)
174174
*/
175175
[[maybe_unused]] static float fast_sin(float angle) {
176-
if (angle < M_PI_2) {
176+
if (angle < (float)(M_PI_2)) {
177177
return 0.0001f * sine_array[round(126.6873f * angle)];
178-
} else if (angle < M_PI) {
178+
} else if (angle < (float)(M_PI)) {
179179
return 0.0001f * sine_array[398 - round(126.6873f * angle)];
180-
} else if (angle < (3.0f * M_PI_2)) {
180+
} else if (angle < (3.0f * (float)(M_PI_2))) {
181181
return -0.0001f * sine_array[round(126.6873f * angle) - 398];
182182
} else {
183183
return -0.0001f * sine_array[796 - round(126.6873f * angle)];
@@ -191,8 +191,8 @@ static constexpr int sine_array[200] = {
191191
* @return Approximation of cos(value)
192192
*/
193193
[[maybe_unused]] static float fast_cos(float angle) {
194-
float a_sin = angle + M_PI_2;
195-
a_sin = (a_sin > (2.0f * M_PI)) ? (a_sin - (2.0f * M_PI)) : a_sin;
194+
float a_sin = angle + (float)(M_PI_2);
195+
a_sin = (a_sin > (2.0f * (float)(M_PI))) ? (a_sin - (2.0f * (float)(M_PI))) : a_sin;
196196
return fast_sin(a_sin);
197197
}
198198
} // namespace espp

components/math/include/gaussian.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Gaussian {
4646
float at(float t) const {
4747
float tmb_y = (t - beta_) / gamma_; // (t - B) / y
4848
float power = -0.5f * tmb_y * tmb_y; // -(t - B)^2 / 2y^2
49-
return alpha_ * exp(power);
49+
return alpha_ * expf(power);
5050
}
5151

5252
/**

0 commit comments

Comments
 (0)