Browse Source

Math: document that float types use fuzzy compare for equality.

An omission from the early days when I thought this is an obvious
feature and doesn't need mentioning.
pull/674/head
Vladimír Vondruš 1 year ago
parent
commit
0e0713eb47
  1. 14
      src/Magnum/Math/Bezier.h
  2. 12
      src/Magnum/Math/Complex.h
  3. 12
      src/Magnum/Math/CubicHermite.h
  4. 14
      src/Magnum/Math/Dual.h
  5. 14
      src/Magnum/Math/Frustum.h
  6. 4
      src/Magnum/Math/Half.h
  7. 12
      src/Magnum/Math/Quaternion.h
  8. 16
      src/Magnum/Math/Range.h
  9. 11
      src/Magnum/Math/RectangularMatrix.h
  10. 14
      src/Magnum/Math/Unit.h
  11. 4
      src/Magnum/Math/Vector.h

14
src/Magnum/Math/Bezier.h

@ -183,14 +183,24 @@ template<UnsignedInt order, UnsignedInt dimensions, class T> class Bezier {
constexpr auto data() const -> const VectorType(&)[order + 1] { return _data; } constexpr auto data() const -> const VectorType(&)[order + 1] { return _data; }
#endif #endif
/** @brief Equality comparison */ /**
* @brief Equality comparison
*
* Done by comparing the underlying vectors, which internally uses
* @ref TypeTraits::equals(), i.e. a fuzzy compare.
*/
bool operator==(const Bezier<order, dimensions, T>& other) const { bool operator==(const Bezier<order, dimensions, T>& other) const {
for(std::size_t i = 0; i != order + 1; ++i) for(std::size_t i = 0; i != order + 1; ++i)
if(_data[i] != other._data[i]) return false; if(_data[i] != other._data[i]) return false;
return true; return true;
} }
/** @brief Non-equality comparison */ /**
* @brief Non-equality comparison
*
* Done by comparing the underlying vectors, which internally uses
* @ref TypeTraits::equals(), i.e. a fuzzy compare.
*/
bool operator!=(const Bezier<order, dimensions, T>& other) const { bool operator!=(const Bezier<order, dimensions, T>& other) const {
return !operator==(other); return !operator==(other);
} }

12
src/Magnum/Math/Complex.h

@ -212,13 +212,21 @@ template<class T> class Complex {
} }
#endif #endif
/** @brief Equality comparison */ /**
* @brief Equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare.
*/
bool operator==(const Complex<T>& other) const { bool operator==(const Complex<T>& other) const {
return TypeTraits<T>::equals(_real, other._real) && return TypeTraits<T>::equals(_real, other._real) &&
TypeTraits<T>::equals(_imaginary, other._imaginary); TypeTraits<T>::equals(_imaginary, other._imaginary);
} }
/** @brief Non-equality comparison */ /**
* @brief Non-equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare.
*/
bool operator!=(const Complex<T>& other) const { bool operator!=(const Complex<T>& other) const {
return !operator==(other); return !operator==(other);
} }

12
src/Magnum/Math/CubicHermite.h

@ -172,10 +172,18 @@ template<class T> class CubicHermite {
} }
#endif #endif
/** @brief Equality comparison */ /**
* @brief Equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare.
*/
bool operator==(const CubicHermite<T>& other) const; bool operator==(const CubicHermite<T>& other) const;
/** @brief Non-equality comparison */ /**
* @brief Non-equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare.
*/
bool operator!=(const CubicHermite<T>& other) const { bool operator!=(const CubicHermite<T>& other) const {
return !operator==(other); return !operator==(other);
} }

14
src/Magnum/Math/Dual.h

@ -148,13 +148,23 @@ template<class T> class Dual {
} }
#endif #endif
/** @brief Equality comparison */ /**
* @brief Equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare for
* floating-point types.
*/
bool operator==(const Dual<T>& other) const { bool operator==(const Dual<T>& other) const {
return TypeTraits<T>::equals(_real, other._real) && return TypeTraits<T>::equals(_real, other._real) &&
TypeTraits<T>::equals(_dual, other._dual); TypeTraits<T>::equals(_dual, other._dual);
} }
/** @brief Non-equality comparison */ /**
* @brief Non-equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare for
* floating-point types.
*/
bool operator!=(const Dual<T>& other) const { bool operator!=(const Dual<T>& other) const {
return !operator==(other); return !operator==(other);
} }

14
src/Magnum/Math/Frustum.h

@ -117,7 +117,12 @@ template<class T> class Frustum {
return Implementation::FrustumConverter<T, U>::to(*this); return Implementation::FrustumConverter<T, U>::to(*this);
} }
/** @brief Equality comparison */ /**
* @brief Equality comparison
*
* Done by comparing the underlying vectors, which internally uses
* @ref TypeTraits::equals(), i.e. a fuzzy compare.
*/
bool operator==(const Frustum<T>& other) const { bool operator==(const Frustum<T>& other) const {
for(std::size_t i = 0; i != 6; ++i) for(std::size_t i = 0; i != 6; ++i)
if(_data[i] != other._data[i]) return false; if(_data[i] != other._data[i]) return false;
@ -125,7 +130,12 @@ template<class T> class Frustum {
return true; return true;
} }
/** @brief Non-equality comparison */ /**
* @brief Non-equality comparison
*
* Done by comparing the underlying vectors, which internally uses
* @ref TypeTraits::equals(), i.e. a fuzzy compare.
*/
bool operator!=(const Frustum<T>& other) const { bool operator!=(const Frustum<T>& other) const {
return !operator==(other); return !operator==(other);
} }

4
src/Magnum/Math/Half.h

@ -112,7 +112,9 @@ class Half {
* @brief Equality comparison * @brief Equality comparison
* *
* Returns `false` if one of the values is half-float representation of * Returns `false` if one of the values is half-float representation of
* NaN, otherwise does bitwise comparison. * NaN, otherwise does bitwise comparison. Note that, unlike with other
* floating-point Magnum math types, due to the limited precision of
* half floats it's *not* a fuzzy compare.
*/ */
constexpr bool operator==(Half other) const { constexpr bool operator==(Half other) const {
return ((( _data & 0x7c00) == 0x7c00 && ( _data & 0x03ff)) || return ((( _data & 0x7c00) == 0x7c00 && ( _data & 0x03ff)) ||

12
src/Magnum/Math/Quaternion.h

@ -447,12 +447,20 @@ template<class T> class Quaternion {
} }
#endif #endif
/** @brief Equality comparison */ /**
* @brief Equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare.
*/
bool operator==(const Quaternion<T>& other) const { bool operator==(const Quaternion<T>& other) const {
return _vector == other._vector && TypeTraits<T>::equals(_scalar, other._scalar); return _vector == other._vector && TypeTraits<T>::equals(_scalar, other._scalar);
} }
/** @brief Non-equality comparison */ /**
* @brief Non-equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare.
*/
bool operator!=(const Quaternion<T>& other) const { bool operator!=(const Quaternion<T>& other) const {
return !operator==(other); return !operator==(other);
} }

16
src/Magnum/Math/Range.h

@ -183,10 +183,22 @@ template<UnsignedInt dimensions, class T> class Range {
return Implementation::RangeConverter<dimensions, T, U>::to(*this); return Implementation::RangeConverter<dimensions, T, U>::to(*this);
} }
/** @brief Equality comparison */ /**
* @brief Equality comparison
*
* Done by comparing the underlying vectors, which internally uses
* @ref TypeTraits::equals(), i.e. a fuzzy compare for floating-point
* types.
*/
bool operator==(const Range<dimensions, T>& other) const; bool operator==(const Range<dimensions, T>& other) const;
/** @brief Non-equality comparison */ /**
* @brief Non-equality comparison
*
* Done by comparing the underlying vectors, which internally uses
* @ref TypeTraits::equals(), i.e. a fuzzy compare for floating-point
* types.
*/
bool operator!=(const Range<dimensions, T>& other) const { bool operator!=(const Range<dimensions, T>& other) const {
return !operator==(other); return !operator==(other);
} }

11
src/Magnum/Math/RectangularMatrix.h

@ -307,7 +307,13 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
*/ */
void setRow(std::size_t row, const Vector<cols, T>& data); void setRow(std::size_t row, const Vector<cols, T>& data);
/** @brief Equality comparison */ /**
* @brief Equality comparison
*
* Done by comparing the underlying vectors, which internally uses
* @ref TypeTraits::equals(), i.e. a fuzzy compare for floating-point
* types.
*/
bool operator==(const RectangularMatrix<cols, rows, T>& other) const { bool operator==(const RectangularMatrix<cols, rows, T>& other) const {
for(std::size_t i = 0; i != cols; ++i) for(std::size_t i = 0; i != cols; ++i)
if(_data[i] != other._data[i]) return false; if(_data[i] != other._data[i]) return false;
@ -318,6 +324,9 @@ template<std::size_t cols, std::size_t rows, class T> class RectangularMatrix {
/** /**
* @brief Non-equality comparison * @brief Non-equality comparison
* *
* Done by comparing the underlying vectors, which internally uses
* @ref TypeTraits::equals(), i.e. a fuzzy compare for floating-point
* types.
* @see @ref Vector::operator<(), @ref Vector::operator<=(), * @see @ref Vector::operator<(), @ref Vector::operator<=(),
* @ref Vector::operator>=(), @ref Vector::operator>() * @ref Vector::operator>=(), @ref Vector::operator>()
*/ */

14
src/Magnum/Math/Unit.h

@ -69,12 +69,22 @@ template<template<class> class Derived, class T> class Unit {
/** @brief Explicit conversion to underlying type */ /** @brief Explicit conversion to underlying type */
constexpr explicit operator T() const { return _value; } constexpr explicit operator T() const { return _value; }
/** @brief Equality comparison */ /**
* @brief Equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare for
* floating-point types.
*/
constexpr bool operator==(Unit<Derived, T> other) const { constexpr bool operator==(Unit<Derived, T> other) const {
return TypeTraits<T>::equals(_value, other._value); return TypeTraits<T>::equals(_value, other._value);
} }
/** @brief Non-equality comparison */ /**
* @brief Non-equality comparison
*
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare for
* floating-point types.
*/
constexpr bool operator!=(Unit<Derived, T> other) const { constexpr bool operator!=(Unit<Derived, T> other) const {
return !operator==(other); return !operator==(other);
} }

4
src/Magnum/Math/Vector.h

@ -309,6 +309,8 @@ template<std::size_t size, class T> class Vector {
/** /**
* @brief Equality comparison * @brief Equality comparison
* *
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare for
* floating-point types.
* @see @ref Math::equal() * @see @ref Math::equal()
*/ */
bool operator==(const Vector<size, T>& other) const { bool operator==(const Vector<size, T>& other) const {
@ -321,6 +323,8 @@ template<std::size_t size, class T> class Vector {
/** /**
* @brief Non-equality comparison * @brief Non-equality comparison
* *
* Done using @ref TypeTraits::equals(), i.e. with fuzzy compare for
* floating-point types.
* @see @ref Math::notEqual() * @see @ref Math::notEqual()
*/ */
bool operator!=(const Vector<size, T>& other) const { bool operator!=(const Vector<size, T>& other) const {

Loading…
Cancel
Save