diff --git a/doc/changelog.dox b/doc/changelog.dox index bb780412a..72bf29640 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -134,6 +134,7 @@ See also: access to underlying memory like with other math types - New @ref Math::min(), @ref Math::max() and @ref Math::minmax() overloads taking plain C arrays +- Vector overloads for @ref Math::isInf() and @ref Math::isNan() @subsubsection changelog-latest-new-platform Platform libraries diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index d8e3862a7..5a8e1ac49 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -129,15 +129,35 @@ template inline std::pair div(Integral x, In @see @ref isNan(), @ref Constants::inf() */ -template bool isInf(T value) { return std::isinf(value); } +template inline typename std::enable_if::value, bool>::type isInf(T value) { + return std::isinf(value); +} + +/** @overload */ +template inline BoolVector isInf(const Vector& value) { + BoolVector out; + for(std::size_t i = 0; i != size; ++i) + out.set(i, std::isinf(value[i])); + return out; +} /** -@brief If given number is NaN +@brief If given number is a NaN Equivalent to @cpp value != value @ce. @see @ref isInf(), @ref Constants::nan() */ -template bool isNan(T value) { return std::isnan(value); } +template inline typename std::enable_if::value, bool>::type isNan(T value) { + return std::isnan(value); +} + +/** @overload */ +template inline BoolVector isNan(const Vector& value) { + BoolVector out; + for(std::size_t i = 0; i != size; ++i) + out.set(i, std::isnan(value[i])); + return out; +} /** @todo Can't trigonometric functions be done with only one overload? */ diff --git a/src/Magnum/Math/Test/FunctionsTest.cpp b/src/Magnum/Math/Test/FunctionsTest.cpp index 88cdedb50..4eebfc6d0 100644 --- a/src/Magnum/Math/Test/FunctionsTest.cpp +++ b/src/Magnum/Math/Test/FunctionsTest.cpp @@ -68,7 +68,9 @@ struct FunctionsTest: Corrade::TestSuite::Tester { void exp(); void div(); void isInf(); + void isInfVector(); void isNan(); + void isNanfVector(); void trigonometric(); void trigonometricWithBase(); }; @@ -118,7 +120,9 @@ FunctionsTest::FunctionsTest() { &FunctionsTest::exp, &FunctionsTest::div, &FunctionsTest::isInf, + &FunctionsTest::isInfVector, &FunctionsTest::isNan, + &FunctionsTest::isNanfVector, &FunctionsTest::trigonometric, &FunctionsTest::trigonometricWithBase}); } @@ -385,6 +389,11 @@ void FunctionsTest::isInf() { CORRADE_VERIFY(!Math::isInf(5.3f)); } +void FunctionsTest::isInfVector() { + CORRADE_COMPARE(Math::isInf(Vector3{0.3f, -Constants::inf(), 1.0f}), Math::BoolVector<3>{0x02}); + CORRADE_COMPARE(Math::isInf(Vector3{0.3f, 1.0f, -Constants::nan()}), Math::BoolVector<3>{0x00}); +} + void FunctionsTest::isNan() { CORRADE_VERIFY(!Math::isNan(Constants::inf())); CORRADE_VERIFY(!Math::isNan(-Constants::inf())); @@ -392,6 +401,11 @@ void FunctionsTest::isNan() { CORRADE_VERIFY(!Math::isNan(5.3f)); } +void FunctionsTest::isNanfVector() { + CORRADE_COMPARE(Math::isNan(Vector3{0.3f, 1.0f, -Constants::nan()}), Math::BoolVector<3>{0x04}); + CORRADE_COMPARE(Math::isNan(Vector3{0.3f, -Constants::inf(), 1.0f}), Math::BoolVector<3>{0x00}); +} + void FunctionsTest::trigonometric() { CORRADE_COMPARE(Math::sin(Deg(30.0f)), 0.5f); CORRADE_COMPARE(Math::sin(Rad(Constants::pi()/6)), 0.5f);