From 57d6ded23796a98d2b88001b75d56d09d26d6177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Mon, 6 Aug 2012 22:45:17 +0200 Subject: [PATCH] Added NumericType and FloatingPointType to Math::MathTypeTraits. NumericType is corresponding numeric type with size at least the same as int. It is used in debug operators for Matrix and Vector to prevent printing chars as characters. If the type isn't already floating-point, FloatingPointType is corresponding larger type with sufficient size for normalization of given integral type. Also updated type traits for long types, they are now subclassed either from int or long long based on sizeof(long). --- src/Math/MathTypeTraits.h | 81 +++++++++++++++++++++++++++++++++------ src/Math/Matrix.h | 2 +- src/Math/Vector.h | 2 +- 3 files changed, 71 insertions(+), 14 deletions(-) diff --git a/src/Math/MathTypeTraits.h b/src/Math/MathTypeTraits.h index 4330491e5..5f4bb5cdb 100644 --- a/src/Math/MathTypeTraits.h +++ b/src/Math/MathTypeTraits.h @@ -51,6 +51,22 @@ support given feature, thus forcing the compilation stop with an error. */ template struct MathTypeTraits { #ifdef DOXYGEN_GENERATING_OUTPUT + /** + * @brief Corresponding numeric type large at least as `int` + * + * Usable e.g. to prevent conversion of `char` to characters when printing + * numeric types to output. + */ + typedef U NumericType; + + /** + * @brief Corresponding floating-point type for normalization + * + * If the type is not already floating-point, defines smallest larger + * floating-point type. + */ + typedef U FloatingPointType; + /** * @brief Epsilon value for fuzzy compare * @@ -95,29 +111,70 @@ template struct MathTypeTraitsFloatingPoint { } }; +template struct MathTypeTraitsLong {}; + +template<> struct MathTypeTraitsLong<8> { + typedef unsigned int UnsignedType; + typedef int Type; +}; + +template<> struct MathTypeTraitsLong<16> { + typedef unsigned long long UnsignedType; + typedef long long Type; +}; + } -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral { + typedef unsigned int NumericType; + typedef float FloatingPointType; +}; +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral { + typedef int NumericType; + typedef float FloatingPointType; +}; -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral { + typedef unsigned int NumericType; + typedef float FloatingPointType; +}; +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral { + typedef int NumericType; + typedef float FloatingPointType; +}; -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral { + typedef unsigned int NumericType; + typedef double FloatingPointType; +}; +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral { + typedef int NumericType; + typedef double FloatingPointType; +}; -/* long is 32 bits somewhere and 64 bits elsewhere, so it cannot be mapped to - any of them */ -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral { + typedef unsigned long long NumericType; + typedef long double FloatingPointType; +}; +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral { + typedef long long NumericType; + typedef long double FloatingPointType; +}; -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; -template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral {}; +/* long is 32 bits somewhere and 64 bits elsewhere */ +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral::Type> {}; +template<> struct MathTypeTraits: public Implementation::MathTypeTraitsIntegral::Type> {}; template<> struct MathTypeTraits: public Implementation::MathTypeTraitsFloatingPoint { + typedef float NumericType; + typedef float FloatingPointType; + inline constexpr static float epsilon() { return FLOAT_EQUALITY_PRECISION; } }; template<> struct MathTypeTraits: public Implementation::MathTypeTraitsFloatingPoint { + typedef float NumericType; + typedef double FloatingPointType; + inline constexpr static double epsilon() { return DOUBLE_EQUALITY_PRECISION; } }; #endif diff --git a/src/Math/Matrix.h b/src/Math/Matrix.h index 540f46d7b..a3e2609fe 100644 --- a/src/Math/Matrix.h +++ b/src/Math/Matrix.h @@ -260,7 +260,7 @@ template Corrade::Utility::Debug operator<<(Corrade::Utili if(row != 0) debug << ",\n "; for(size_t col = 0; col != size; ++col) { if(col != 0) debug << ", "; - debug << value[col][row]; + debug << typename MathTypeTraits::NumericType(value[col][row]); } } debug << ')'; diff --git a/src/Math/Vector.h b/src/Math/Vector.h index 96b0fe502..bfdb1681f 100644 --- a/src/Math/Vector.h +++ b/src/Math/Vector.h @@ -324,7 +324,7 @@ template Corrade::Utility::Debug operator<<(Corrade::Utili debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, false); for(size_t i = 0; i != size; ++i) { if(i != 0) debug << ", "; - debug << value[i]; + debug << typename MathTypeTraits::NumericType(value[i]); } debug << ')'; debug.setFlag(Corrade::Utility::Debug::SpaceAfterEachValue, true);