diff --git a/doc/changelog.dox b/doc/changelog.dox index 0ba2a5d22..aac749448 100644 --- a/doc/changelog.dox +++ b/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 diff --git a/src/Magnum/Math/Test/TypeTraitsTest.cpp b/src/Magnum/Math/Test/TypeTraitsTest.cpp index 8f3bdbf15..5580d1ddc 100644 --- a/src/Magnum/Math/Test/TypeTraitsTest.cpp +++ b/src/Magnum/Math/Test/TypeTraitsTest.cpp @@ -58,6 +58,8 @@ struct TypeTraitsTest: Corrade::TestSuite::Tester { template void equalsZeroFloatingPoint(); template void equalsZeroFloatingPointSmall(); template void equalsZeroFloatingPointLarge(); + + void equal(); }; enum: std::size_t { EqualsZeroDataCount = 3 }; @@ -162,6 +164,8 @@ TypeTraitsTest::TypeTraitsTest() { &TypeTraitsTest::equalsZeroFloatingPoint #endif }, EqualsZeroDataCount); + + addTests({&TypeTraitsTest::equal}); } void TypeTraitsTest::sizeOfLongDouble() { @@ -274,6 +278,9 @@ template void TypeTraitsTest::equalsFloatingPointLarge() { CORRADE_VERIFY(TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(2), T(25))); CORRADE_VERIFY(!TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(75), T(25))); + + CORRADE_VERIFY(TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(2), T(25))); + CORRADE_VERIFY(!TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(75), T(25))); } template void TypeTraitsTest::equalsFloatingPointInfinity() { @@ -326,6 +333,18 @@ template void TypeTraitsTest::equalsZeroFloatingPoint() { CORRADE_VERIFY(!TypeTraits::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::epsilon()/2.0f, 1.0f)); + CORRADE_VERIFY(!Math::equal(1.0f + TypeTraits::epsilon()*3.0f, 1.0f)); + + CORRADE_VERIFY(!Math::notEqual(1, 1)); + CORRADE_VERIFY(Math::notEqual(1, -1)); + CORRADE_VERIFY(!Math::notEqual(1.0f + TypeTraits::epsilon()/2.0f, 1.0f)); + CORRADE_VERIFY(Math::notEqual(1.0f + TypeTraits::epsilon()*3.0f, 1.0f)); +} + }}}} CORRADE_TEST_MAIN(Magnum::Math::Test::TypeTraitsTest) diff --git a/src/Magnum/Math/TypeTraits.h b/src/Magnum/Math/TypeTraits.h index 6401feea7..7217559d9 100644 --- a/src/Magnum/Math/TypeTraits.h +++ b/src/Magnum/Math/TypeTraits.h @@ -327,6 +327,7 @@ template struct TypeTraits: Implementation::TypeTraitsDefault { * 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 struct TypeTraits: Implementation::TypeTraitsDefault { #endif }; +/** +@brief Equality comparison of scalar types + +Calls @ref TypeTraits::equals() --- using fuzzy compare for floating-point +types and doing equality comparison on integral types. Scalar complement to +@ref equal(const Vector& a, const Vector&). +*/ +template inline typename std::enable_if::value, bool>::type equal(T a, T b) { + return TypeTraits::equals(a, b); +} + +/** +@brief Non-equality comparison of scalar types + +Calls @ref TypeTraits::equals() --- using fuzzy compare for floating-point +types and doing equality comparison on integral types. Scalar complement to +@ref notEqual(const Vector& a, const Vector&). +*/ +template inline typename std::enable_if::value, bool>::type notEqual(T a, T b) { + return !TypeTraits::equals(a, b); +} + /* Integral scalar types */ namespace Implementation { template struct TypeTraitsName; diff --git a/src/Magnum/Math/Vector.h b/src/Magnum/Math/Vector.h index 5ba85f280..18e4bcdd8 100644 --- a/src/Magnum/Math/Vector.h +++ b/src/Magnum/Math/Vector.h @@ -672,7 +672,7 @@ template 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 inline BoolVector equal(const Vector& a, const Vector& b) { BoolVector out; @@ -687,7 +687,7 @@ template inline BoolVector equal(const Vector inline BoolVector notEqual(const Vector& a, const Vector& b) { BoolVector out;