Browse Source

Math: added isInf() and isNan()

pull/205/merge
Vladimír Vondruš 8 years ago
parent
commit
92071a342c
  1. 4
      doc/changelog.dox
  2. 15
      src/Magnum/Math/Constants.h
  3. 15
      src/Magnum/Math/Functions.h
  4. 18
      src/Magnum/Math/Test/FunctionsTest.cpp

4
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()

15
src/Magnum/Math/Constants.h

@ -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
};

15
src/Magnum/Math/Functions.h

@ -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

18
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);

Loading…
Cancel
Save