Browse Source

Math: vector overloads for isInf() and isNan().

pull/286/merge
Vladimír Vondruš 8 years ago
parent
commit
ebedbce2f2
  1. 1
      doc/changelog.dox
  2. 26
      src/Magnum/Math/Functions.h
  3. 14
      src/Magnum/Math/Test/FunctionsTest.cpp

1
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

26
src/Magnum/Math/Functions.h

@ -129,15 +129,35 @@ template<class Integral> inline std::pair<Integral, Integral> div(Integral x, In
@see @ref isNan(), @ref Constants::inf()
*/
template<class T> bool isInf(T value) { return std::isinf(value); }
template<class T> inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type isInf(T value) {
return std::isinf(value);
}
/** @overload */
template<std::size_t size, class T> inline BoolVector<size> isInf(const Vector<size, T>& value) {
BoolVector<size> 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<class T> bool isNan(T value) { return std::isnan(value); }
template<class T> inline typename std::enable_if<std::is_arithmetic<T>::value, bool>::type isNan(T value) {
return std::isnan(value);
}
/** @overload */
template<std::size_t size, class T> inline BoolVector<size> isNan(const Vector<size, T>& value) {
BoolVector<size> 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? */

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

Loading…
Cancel
Save