Functions: fix corner case x_low == x_high == value resulting in NAN

and added unit test to cover it
This commit is contained in:
Matthias Grob
2021-10-13 15:24:59 +02:00
committed by Daniel Agar
parent f5e1da5b0f
commit 8b37db7825
2 changed files with 7 additions and 1 deletions

View File

@@ -140,7 +140,7 @@ const T expo_deadzone(const T &value, const T &e, const T &dz)
template<typename T>
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) {

View File

@@ -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)