template<class T>
Magnum::Math::TypeTraits struct

Traits class for builtin arithmetic types.

Useful for detecting type features at compile time without the need for repeated code such as method overloading, cascaded ifs or template specializations for given types. All builtin arithmetic types have this class implemented.

Public types

using FloatingPointType = U
Corresponding floating-point type for normalization.

Public static functions

static auto name() -> const char* constexpr
Type name.
static auto epsilon() -> T constexpr
Epsilon value for fuzzy compare.
static auto equals(T a, T b) -> bool
Fuzzy compare.
static auto equalsZero(T a, T magnitude) -> bool
Fuzzy compare to zero with magnitude.

Typedef documentation

template<class T>
typedef U Magnum::Math::TypeTraits<T>::FloatingPointType

Corresponding floating-point type for normalization.

If the type is not already floating-point, defines smallest larger floating-point type.

Function documentation

template<class T>
static const char* Magnum::Math::TypeTraits<T>::name() constexpr

Type name.

Returns a string representation of type name, such as "UnsignedInt" for UnsignedInt.

template<class T>
static T Magnum::Math::TypeTraits<T>::epsilon() constexpr

Epsilon value for fuzzy compare.

Returns minimal difference between numbers to be considered inequal. Not implemented for arbitrary types. Returns 1 for integer types and

  • 1.0e-5f for float,
  • 1.0e-15 for double,
  • 1.0e-17l for long double on platforms where it is 80-bit, and 1.0e-14l on platforms where it is 64-bit.

This matches fuzzy comparison precision in Corrade::TestSuite and is always one digit less than how Corrade::Utility::Debug or Corrade::Utility::format() prints given type.

template<class T>
static bool Magnum::Math::TypeTraits<T>::equals(T a, T b)

Fuzzy compare.

Uses fuzzy compare for all floating-point types except Half (using the epsilon() value), pure equality comparison everywhere else. The Half type has representable values sparse enough that no fuzzy comparison needs to be done. Algorithm adapted from http://floating-point-gui.de/errors/comparison/.

template<class T>
static bool Magnum::Math::TypeTraits<T>::equalsZero(T a, T magnitude)

Fuzzy compare to zero with magnitude.

Uses fuzzy compare for floating-point types (using epsilon() value), pure equality comparison everywhere else. Use this function when comparing e.g. a calculated nearly-zero difference with zero, knowing the magnitude of original values so the epsilon can be properly scaled. In other words, the following lines are equivalent:

Math::TypeTraits<Float>::equals(a, b);
Math::TypeTraits<Float>::equalsZero(a - b,
    Math::max(Math::abs(a), Math::abs(b)));