diff options
Diffstat (limited to 'host')
-rw-r--r-- | host/include/uhd/utils/math.hpp | 15 | ||||
-rw-r--r-- | host/tests/math_test.cpp | 7 |
2 files changed, 22 insertions, 0 deletions
diff --git a/host/include/uhd/utils/math.hpp b/host/include/uhd/utils/math.hpp index becc256f1..4918ddf2e 100644 --- a/host/include/uhd/utils/math.hpp +++ b/host/include/uhd/utils/math.hpp @@ -263,6 +263,21 @@ inline IntegerType gcd(IntegerType x, IntegerType y) return _bmint::gcd<IntegerType>(x, y); } +//! Returns the sign of x +// +// Note: This is equivalent to the Boost.Math version, but without the +// dependency. +// +// Returns +1 for positive arguments, -1 for negative arguments, and 0 if x is +// zero. +template <typename T> +inline constexpr int sign(T x) +{ + // Note: If T is unsigned, then this will compile with a warning. Should + // we need that, expand the template logic. + return (T(0) < x) - (x < T(0)); +} + } // namespace math } // namespace uhd diff --git a/host/tests/math_test.cpp b/host/tests/math_test.cpp index c1b6793bb..1fb01f5c1 100644 --- a/host/tests/math_test.cpp +++ b/host/tests/math_test.cpp @@ -17,3 +17,10 @@ BOOST_AUTO_TEST_CASE(test_gcd) { BOOST_CHECK_EQUAL(uhd::math::gcd<int>(6, 15), 3); } + +BOOST_AUTO_TEST_CASE(test_sign) +{ + BOOST_CHECK_EQUAL(uhd::math::sign(2.3), +1); + BOOST_CHECK_EQUAL(uhd::math::sign(-2.3), -1); + BOOST_CHECK_EQUAL(uhd::math::sign(0.0), 0); +} |