Browse Source

Math: added a scalar equal() / notEqual() variant.

pull/364/head
Vladimír Vondruš 7 years ago
parent
commit
1887fe0ab9
  1. 6
      doc/changelog.dox
  2. 19
      src/Magnum/Math/Test/TypeTraitsTest.cpp
  3. 23
      src/Magnum/Math/TypeTraits.h
  4. 4
      src/Magnum/Math/Vector.h

6
doc/changelog.dox

@ -170,9 +170,9 @@ See also:
@ref Math::IsFloatingPoint and @ref Math::IsUnitless type traits and a
@ref Math::UnderlyingTypeOf utility
- Added batch versions of @ref Math::isInf() and @ref Math::isNan()
- @ref Math::equal() and @ref Math::notEqual() for component-wise comparison
of @ref Math::Vector, as the equality/non-equality operators return a
single @cpp bool @ce
- Scalar and vector @ref Math::equal() and @ref Math::notEqual() which do a
component-wise comparison in the vector case, as the equality/non-equality
operators return a single @cpp bool @ce
@subsubsection changelog-latest-new-meshtools MeshTools library

19
src/Magnum/Math/Test/TypeTraitsTest.cpp

@ -58,6 +58,8 @@ struct TypeTraitsTest: Corrade::TestSuite::Tester {
template<class T> void equalsZeroFloatingPoint();
template<class T> void equalsZeroFloatingPointSmall();
template<class T> void equalsZeroFloatingPointLarge();
void equal();
};
enum: std::size_t { EqualsZeroDataCount = 3 };
@ -162,6 +164,8 @@ TypeTraitsTest::TypeTraitsTest() {
&TypeTraitsTest::equalsZeroFloatingPoint<long double>
#endif
}, EqualsZeroDataCount);
addTests({&TypeTraitsTest::equal});
}
void TypeTraitsTest::sizeOfLongDouble() {
@ -274,6 +278,9 @@ template<class T> void TypeTraitsTest::equalsFloatingPointLarge() {
CORRADE_VERIFY(TypeTraits<T>::equals(T(25)+TypeTraits<T>::epsilon()*T(2), T(25)));
CORRADE_VERIFY(!TypeTraits<T>::equals(T(25)+TypeTraits<T>::epsilon()*T(75), T(25)));
CORRADE_VERIFY(TypeTraits<T>::equals(T(25)+TypeTraits<T>::epsilon()*T(2), T(25)));
CORRADE_VERIFY(!TypeTraits<T>::equals(T(25)+TypeTraits<T>::epsilon()*T(75), T(25)));
}
template<class T> void TypeTraitsTest::equalsFloatingPointInfinity() {
@ -326,6 +333,18 @@ template<class T> void TypeTraitsTest::equalsZeroFloatingPoint() {
CORRADE_VERIFY(!TypeTraits<T>::equalsZero(a - step*T(2.0) - a, magnitude));
}
void TypeTraitsTest::equal() {
CORRADE_VERIFY(Math::equal(1, 1));
CORRADE_VERIFY(!Math::equal(1, -1));
CORRADE_VERIFY(Math::equal(1.0f + TypeTraits<Float>::epsilon()/2.0f, 1.0f));
CORRADE_VERIFY(!Math::equal(1.0f + TypeTraits<Float>::epsilon()*3.0f, 1.0f));
CORRADE_VERIFY(!Math::notEqual(1, 1));
CORRADE_VERIFY(Math::notEqual(1, -1));
CORRADE_VERIFY(!Math::notEqual(1.0f + TypeTraits<Float>::epsilon()/2.0f, 1.0f));
CORRADE_VERIFY(Math::notEqual(1.0f + TypeTraits<Float>::epsilon()*3.0f, 1.0f));
}
}}}}
CORRADE_TEST_MAIN(Magnum::Math::Test::TypeTraitsTest)

23
src/Magnum/Math/TypeTraits.h

@ -327,6 +327,7 @@ template<class T> struct TypeTraits: Implementation::TypeTraitsDefault<T> {
* Uses fuzzy compare for floating-point types (using @ref epsilon()
* value), pure equality comparison everywhere else. Algorithm adapted from
* http://floating-point-gui.de/errors/comparison/.
* @see @ref Math::equal(T, T), @ref Math::notEqual(T, T)
*/
static bool equals(T a, T b);
@ -345,6 +346,28 @@ template<class T> struct TypeTraits: Implementation::TypeTraitsDefault<T> {
#endif
};
/**
@brief Equality comparison of scalar types
Calls @ref TypeTraits<T>::equals() --- using fuzzy compare for floating-point
types and doing equality comparison on integral types. Scalar complement to
@ref equal(const Vector<size, T>& a, const Vector<size, T>&).
*/
template<class T> inline typename std::enable_if<IsScalar<T>::value, bool>::type equal(T a, T b) {
return TypeTraits<T>::equals(a, b);
}
/**
@brief Non-equality comparison of scalar types
Calls @ref TypeTraits<T>::equals() --- using fuzzy compare for floating-point
types and doing equality comparison on integral types. Scalar complement to
@ref notEqual(const Vector<size, T>& a, const Vector<size, T>&).
*/
template<class T> inline typename std::enable_if<IsScalar<T>::value, bool>::type notEqual(T a, T b) {
return !TypeTraits<T>::equals(a, b);
}
/* Integral scalar types */
namespace Implementation {
template<class> struct TypeTraitsName;

4
src/Magnum/Math/Vector.h

@ -672,7 +672,7 @@ template<std::size_t size, class T> class Vector {
@brief Component-wise equality comparison
Unlike @ref Vector::operator==() returns a @ref BoolVector instead of a single
value.
value. Vector complement to @ref equal(T, T).
*/
template<std::size_t size, class T> inline BoolVector<size> equal(const Vector<size, T>& a, const Vector<size, T>& b) {
BoolVector<size> out;
@ -687,7 +687,7 @@ template<std::size_t size, class T> inline BoolVector<size> equal(const Vector<s
@brief Component-wise non-equality comparison
Unlike @ref Vector::operator!=() returns a @ref BoolVector instead of a single
value.
value. Vector complement to @ref notEqual(T, T).
*/
template<std::size_t size, class T> inline BoolVector<size> notEqual(const Vector<size, T>& a, const Vector<size, T>& b) {
BoolVector<size> out;

Loading…
Cancel
Save