diff --git a/doc/changelog.dox b/doc/changelog.dox index 914cd1d0b..17b448e85 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -337,6 +337,8 @@ See also: @ref Math::scatter(), which is now marked with @ref CORRADE_NODISCARD to better convey that it returns the modified value instead of mutating it in-place +- Added @ref Math::TypeTraits::min() and @relativeref{Math::TypeTraits,max()} + returning minimal and maximal representable values of integer types. @subsubsection changelog-latest-new-materialtools MaterialTools library diff --git a/src/Magnum/Math/Test/TypeTraitsTest.cpp b/src/Magnum/Math/Test/TypeTraitsTest.cpp index 1a935768c..9bb39d11e 100644 --- a/src/Magnum/Math/Test/TypeTraitsTest.cpp +++ b/src/Magnum/Math/Test/TypeTraitsTest.cpp @@ -24,6 +24,7 @@ DEALINGS IN THE SOFTWARE. */ +#include /* for TypeTraits::min() / max() verification */ #include #include #include @@ -50,6 +51,8 @@ struct TypeTraitsTest: TestSuite::Tester { template void epsilonConsistentWithCorrade(); + template void minMax(); + template void equalsIntegral(); void equalsHalf(); template void equalsFloatingPoint0(); @@ -117,6 +120,15 @@ TypeTraitsTest::TypeTraitsTest() { &TypeTraitsTest::epsilonConsistentWithCorrade, &TypeTraitsTest::epsilonConsistentWithCorrade, + &TypeTraitsTest::minMax, + &TypeTraitsTest::minMax, + &TypeTraitsTest::minMax, + &TypeTraitsTest::minMax, + &TypeTraitsTest::minMax, + &TypeTraitsTest::minMax, + &TypeTraitsTest::minMax, + &TypeTraitsTest::minMax, + &TypeTraitsTest::equalsIntegral, &TypeTraitsTest::equalsIntegral, &TypeTraitsTest::equalsIntegral, @@ -260,6 +272,15 @@ template void TypeTraitsTest::epsilonConsistentWithCorrade() { CORRADE_VERIFY(TypeTraits::epsilon() == Utility::Implementation::FloatPrecision::epsilon()); } +template void TypeTraitsTest::minMax() { + setTestCaseTemplateName(TypeTraits::name()); + + /* Using std::numeric_limits to verify the implementation uses the right + values for the right types */ + CORRADE_COMPARE(TypeTraits::min(), std::numeric_limits::min()); + CORRADE_COMPARE(TypeTraits::max(), std::numeric_limits::max()); +} + template void TypeTraitsTest::equalsIntegral() { setTestCaseTemplateName(TypeTraits::name()); diff --git a/src/Magnum/Math/TypeTraits.h b/src/Magnum/Math/TypeTraits.h index 0783a81c3..7f9910331 100644 --- a/src/Magnum/Math/TypeTraits.h +++ b/src/Magnum/Math/TypeTraits.h @@ -370,6 +370,25 @@ template struct TypeTraits: Implementation::TypeTraitsDefault { */ constexpr static T epsilon(); + /** + * @brief Minimal representable integer value + * @m_since_latest + * + * Returns @cpp 0 @ce for unsigned integer types and largest representable + * negative value for signed integer types. Not implemented for + * floating-point types. + */ + constexpr static T min(); + + /** + * @brief Maximal representable integer value + * @m_since_latest + * + * Returns largest representable positive value for integer types. Not + * implemented for floating-point types. + */ + constexpr static T max(); + /** * @brief Fuzzy compare * @@ -459,27 +478,43 @@ namespace Implementation { #ifndef DOXYGEN_GENERATING_OUTPUT template<> struct TypeTraits: Implementation::TypeTraitsIntegral { typedef Float FloatingPointType; + constexpr static UnsignedByte min() { return 0; } + constexpr static UnsignedByte max() { return UINT8_MAX; } }; template<> struct TypeTraits: Implementation::TypeTraitsIntegral { typedef Float FloatingPointType; + constexpr static Byte min() { return INT8_MIN; } + constexpr static Byte max() { return INT8_MAX; } }; template<> struct TypeTraits: Implementation::TypeTraitsIntegral { typedef Float FloatingPointType; + constexpr static UnsignedShort min() { return 0; } + constexpr static UnsignedShort max() { return UINT16_MAX; } }; template<> struct TypeTraits: Implementation::TypeTraitsIntegral { typedef Float FloatingPointType; + constexpr static Short min() { return INT16_MIN; } + constexpr static Short max() { return INT16_MAX; } }; template<> struct TypeTraits: Implementation::TypeTraitsIntegral { typedef Double FloatingPointType; + constexpr static UnsignedInt min() { return 0; } + constexpr static UnsignedInt max() { return UINT32_MAX; } }; template<> struct TypeTraits: Implementation::TypeTraitsIntegral { typedef Double FloatingPointType; + constexpr static Int min() { return INT32_MIN; } + constexpr static Int max() { return INT32_MAX; } }; template<> struct TypeTraits: Implementation::TypeTraitsIntegral { typedef long double FloatingPointType; + constexpr static UnsignedLong min() { return 0; } + constexpr static UnsignedLong max() { return UINT64_MAX; } }; template<> struct TypeTraits: Implementation::TypeTraitsIntegral { typedef long double FloatingPointType; + constexpr static Long min() { return INT64_MIN; } + constexpr static Long max() { return INT64_MAX; } }; /* Floating-point scalar types */