From 92071a342c96ed105e8375aa260bdcfac43d1787 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Fri, 30 Mar 2018 16:20:07 +0200 Subject: [PATCH] Math: added isInf() and isNan() --- doc/changelog.dox | 4 ++++ src/Magnum/Math/Constants.h | 15 +++++++++++++-- src/Magnum/Math/Functions.h | 15 +++++++++++++++ src/Magnum/Math/Test/FunctionsTest.cpp | 18 ++++++++++++++++++ 4 files changed, 50 insertions(+), 2 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index 1e0a114db3..bf68af5e67 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -60,6 +60,10 @@ See also: @webgl_extension{EXT,color_buffer_float} - Ported @ref OpenGLTester to WebGL +@subsubsection changelog-latest-new-math Math library + +- Added @ref Math::isInf(), @ref Math::isNan() + @subsubsection changelog-latest-new-platform Platform libraries - Added @ref Platform::AndroidApplication::windowSize() diff --git a/src/Magnum/Math/Constants.h b/src/Magnum/Math/Constants.h index e8b72ae0d6..df27393630 100644 --- a/src/Magnum/Math/Constants.h +++ b/src/Magnum/Math/Constants.h @@ -71,8 +71,19 @@ template struct Constants { static constexpr T sqrt2(); /**< @brief Square root of 2 */ static constexpr T sqrt3(); /**< @brief Square root of 3 */ - static constexpr T nan(); /**< @brief Quiet NaN */ - static constexpr T inf(); /**< @brief Positive infinity */ + /** + * @brief Quiet NaN + * + * @see @ref isNan() + */ + static constexpr T nan(); + + /** + * @brief Positive infinity + * + * @see @ref isInf() + */ + static constexpr T inf(); #endif }; diff --git a/src/Magnum/Math/Functions.h b/src/Magnum/Math/Functions.h index 6dbae49032..163c9318b3 100644 --- a/src/Magnum/Math/Functions.h +++ b/src/Magnum/Math/Functions.h @@ -112,6 +112,21 @@ template std::pair div(Integral x, Integral return {result.quot, result.rem}; } +/** +@brief If given number is a positive or negative infinity + +@see @ref isNan(), @ref Constants::inf() +*/ +template bool isInf(T value) { return std::isinf(value); } + +/** +@brief If given number is NaN + +Equivalent to @cpp value != value @ce. +@see @ref isInf(), @ref Constants::nan() +*/ +template bool isNan(T value) { return std::isnan(value); } + /** @todo Can't trigonometric functions be done with only one overload? */ /* The functions accept Unit instead of Rad to make them working with operator diff --git a/src/Magnum/Math/Test/FunctionsTest.cpp b/src/Magnum/Math/Test/FunctionsTest.cpp index 7244a1c3d4..d1e4a758be 100644 --- a/src/Magnum/Math/Test/FunctionsTest.cpp +++ b/src/Magnum/Math/Test/FunctionsTest.cpp @@ -65,6 +65,8 @@ struct FunctionsTest: Corrade::TestSuite::Tester { void log(); void exp(); void div(); + void isInf(); + void isNan(); void trigonometric(); void trigonometricWithBase(); }; @@ -110,6 +112,8 @@ FunctionsTest::FunctionsTest() { &FunctionsTest::log, &FunctionsTest::exp, &FunctionsTest::div, + &FunctionsTest::isInf, + &FunctionsTest::isNan, &FunctionsTest::trigonometric, &FunctionsTest::trigonometricWithBase}); } @@ -332,6 +336,20 @@ void FunctionsTest::div() { CORRADE_COMPARE(remainder, 3); } +void FunctionsTest::isInf() { + CORRADE_VERIFY(Math::isInf(Constants::inf())); + CORRADE_VERIFY(Math::isInf(-Constants::inf())); + CORRADE_VERIFY(!Math::isInf(Constants::nan())); + CORRADE_VERIFY(!Math::isInf(5.3f)); +} + +void FunctionsTest::isNan() { + CORRADE_VERIFY(!Math::isNan(Constants::inf())); + CORRADE_VERIFY(!Math::isNan(-Constants::inf())); + CORRADE_VERIFY(Math::isNan(Constants::nan())); + CORRADE_VERIFY(!Math::isNan(5.3f)); +} + void FunctionsTest::trigonometric() { CORRADE_COMPARE(Math::sin(Deg(30.0f)), 0.5f); CORRADE_COMPARE(Math::sin(Rad(Constants::pi()/6)), 0.5f);