Browse Source

Functions: add sign from boolean function with unit tests

v1.13.0-BW
Matthias Grob 3 years ago
parent
commit
74c0ae6e55
  1. 11
      src/lib/mathlib/math/Functions.hpp
  2. 9
      src/lib/mathlib/math/FunctionsTest.cpp

11
src/lib/mathlib/math/Functions.hpp

@ -54,6 +54,17 @@ int signNoZero(T val) @@ -54,6 +54,17 @@ int signNoZero(T val)
return (T(0) <= val) - (val < T(0));
}
/**
* Sign function based on a boolean
*
* @param[in] positive Truth value to take the sign from
* @return 1 if positive is true, -1 if positive is false
*/
inline int signFromBool(bool positive)
{
return positive ? 1 : -1;
}
template<typename T>
T sq(T val)
{

9
src/lib/mathlib/math/FunctionsTest.cpp

@ -47,6 +47,15 @@ TEST(FunctionsTest, signNoZero) @@ -47,6 +47,15 @@ TEST(FunctionsTest, signNoZero)
EXPECT_FLOAT_EQ(signNoZero(123.456f), 1.f);
}
TEST(FunctionsTest, signFromBool)
{
EXPECT_EQ(signFromBool(true), 1);
EXPECT_EQ(signFromBool(false), -1);
EXPECT_EQ(signFromBool(100), 1);
EXPECT_EQ(signFromBool(-100), 1);
EXPECT_EQ(signFromBool(0), -1);
}
TEST(FunctionsTest, expo)
{
// input value limits

Loading…
Cancel
Save