Skip to content

Commit

Permalink
Math: added isInf() and isNan()
Browse files Browse the repository at this point in the history
  • Loading branch information
mosra committed Mar 30, 2018
1 parent 105cef5 commit 92071a3
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions doc/changelog.dox
Expand Up @@ -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()
Expand Down
15 changes: 13 additions & 2 deletions src/Magnum/Math/Constants.h
Expand Up @@ -71,8 +71,19 @@ template<class T> 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
};

Expand Down
15 changes: 15 additions & 0 deletions src/Magnum/Math/Functions.h
Expand Up @@ -112,6 +112,21 @@ template<class Integral> std::pair<Integral, Integral> 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<class T> 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<class T> 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
Expand Down
18 changes: 18 additions & 0 deletions src/Magnum/Math/Test/FunctionsTest.cpp
Expand Up @@ -65,6 +65,8 @@ struct FunctionsTest: Corrade::TestSuite::Tester {
void log();
void exp();
void div();
void isInf();
void isNan();
void trigonometric();
void trigonometricWithBase();
};
Expand Down Expand Up @@ -110,6 +112,8 @@ FunctionsTest::FunctionsTest() {
&FunctionsTest::log,
&FunctionsTest::exp,
&FunctionsTest::div,
&FunctionsTest::isInf,
&FunctionsTest::isNan,
&FunctionsTest::trigonometric,
&FunctionsTest::trigonometricWithBase});
}
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 92071a3

Please sign in to comment.