From 8b37db7825b237a69746cd3f41a321981888c26c Mon Sep 17 00:00:00 2001 From: Matthias Grob Date: Wed, 13 Oct 2021 15:24:59 +0200 Subject: [PATCH] Functions: fix corner case x_low == x_high == value resulting in NAN and added unit test to cover it --- src/lib/mathlib/math/Functions.hpp | 2 +- src/lib/mathlib/math/FunctionsTest.cpp | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib/mathlib/math/Functions.hpp b/src/lib/mathlib/math/Functions.hpp index d3ac322a23..486ea52b6e 100644 --- a/src/lib/mathlib/math/Functions.hpp +++ b/src/lib/mathlib/math/Functions.hpp @@ -140,7 +140,7 @@ const T expo_deadzone(const T &value, const T &e, const T &dz) template const T gradual(const T &value, const T &x_low, const T &x_high, const T &y_low, const T &y_high) { - if (value < x_low) { + if (value <= x_low) { return y_low; } else if (value > x_high) { diff --git a/src/lib/mathlib/math/FunctionsTest.cpp b/src/lib/mathlib/math/FunctionsTest.cpp index dba2ef81d2..324f7f12fe 100644 --- a/src/lib/mathlib/math/FunctionsTest.cpp +++ b/src/lib/mathlib/math/FunctionsTest.cpp @@ -150,6 +150,12 @@ TEST(FunctionsTest, gradual) EXPECT_FLOAT_EQ(gradual(1.75f, 1.f, 2.f, 4.f, 6.f), 5.5f); EXPECT_FLOAT_EQ(gradual(2.f, 1.f, 2.f, 4.f, 6.f), 6.f); EXPECT_FLOAT_EQ(gradual(12.f, 1.f, 2.f, 4.f, 6.f), 6.f); + + // corner case when x_low == x_high == value + EXPECT_FLOAT_EQ(gradual(1.f, 1.f, 1.f, 4.f, 6.f), 4.f); + + // corner case when x_low > x_high + EXPECT_FLOAT_EQ(gradual(1.05f, 1.1f, 1.f, 4.f, 6.f), 4.f); } TEST(FunctionsTest, gradual3)