NumTraits.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifndef EIGEN_NUMTRAITS_H
  10. #define EIGEN_NUMTRAITS_H
  11. namespace Eigen {
  12. namespace internal {
  13. // default implementation of digits10(), based on numeric_limits if specialized,
  14. // 0 for integer types, and log10(epsilon()) otherwise.
  15. template< typename T,
  16. bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
  17. bool is_integer = NumTraits<T>::IsInteger>
  18. struct default_digits10_impl
  19. {
  20. static int run() { return std::numeric_limits<T>::digits10; }
  21. };
  22. template<typename T>
  23. struct default_digits10_impl<T,false,false> // Floating point
  24. {
  25. static int run() {
  26. using std::log10;
  27. using std::ceil;
  28. typedef typename NumTraits<T>::Real Real;
  29. return int(ceil(-log10(NumTraits<Real>::epsilon())));
  30. }
  31. };
  32. template<typename T>
  33. struct default_digits10_impl<T,false,true> // Integer
  34. {
  35. static int run() { return 0; }
  36. };
  37. } // end namespace internal
  38. /** \class NumTraits
  39. * \ingroup Core_Module
  40. *
  41. * \brief Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
  42. *
  43. * \tparam T the numeric type at hand
  44. *
  45. * This class stores enums, typedefs and static methods giving information about a numeric type.
  46. *
  47. * The provided data consists of:
  48. * \li A typedef \c Real, giving the "real part" type of \a T. If \a T is already real,
  49. * then \c Real is just a typedef to \a T. If \a T is \c std::complex<U> then \c Real
  50. * is a typedef to \a U.
  51. * \li A typedef \c NonInteger, giving the type that should be used for operations producing non-integral values,
  52. * such as quotients, square roots, etc. If \a T is a floating-point type, then this typedef just gives
  53. * \a T again. Note however that many Eigen functions such as internal::sqrt simply refuse to
  54. * take integers. Outside of a few cases, Eigen doesn't do automatic type promotion. Thus, this typedef is
  55. * only intended as a helper for code that needs to explicitly promote types.
  56. * \li A typedef \c Literal giving the type to use for numeric literals such as "2" or "0.5". For instance, for \c std::complex<U>, Literal is defined as \c U.
  57. * Of course, this type must be fully compatible with \a T. In doubt, just use \a T here.
  58. * \li A typedef \a Nested giving the type to use to nest a value inside of the expression tree. If you don't know what
  59. * this means, just use \a T here.
  60. * \li An enum value \a IsComplex. It is equal to 1 if \a T is a \c std::complex
  61. * type, and to 0 otherwise.
  62. * \li An enum value \a IsInteger. It is equal to \c 1 if \a T is an integer type such as \c int,
  63. * and to \c 0 otherwise.
  64. * \li Enum values ReadCost, AddCost and MulCost representing a rough estimate of the number of CPU cycles needed
  65. * to by move / add / mul instructions respectively, assuming the data is already stored in CPU registers.
  66. * Stay vague here. No need to do architecture-specific stuff.
  67. * \li An enum value \a IsSigned. It is equal to \c 1 if \a T is a signed type and to 0 if \a T is unsigned.
  68. * \li An enum value \a RequireInitialization. It is equal to \c 1 if the constructor of the numeric type \a T must
  69. * be called, and to 0 if it is safe not to call it. Default is 0 if \a T is an arithmetic type, and 1 otherwise.
  70. * \li An epsilon() function which, unlike <a href="http://en.cppreference.com/w/cpp/types/numeric_limits/epsilon">std::numeric_limits::epsilon()</a>,
  71. * it returns a \a Real instead of a \a T.
  72. * \li A dummy_precision() function returning a weak epsilon value. It is mainly used as a default
  73. * value by the fuzzy comparison operators.
  74. * \li highest() and lowest() functions returning the highest and lowest possible values respectively.
  75. * \li digits10() function returning the number of decimal digits that can be represented without change. This is
  76. * the analogue of <a href="http://en.cppreference.com/w/cpp/types/numeric_limits/digits10">std::numeric_limits<T>::digits10</a>
  77. * which is used as the default implementation if specialized.
  78. */
  79. template<typename T> struct GenericNumTraits
  80. {
  81. enum {
  82. IsInteger = std::numeric_limits<T>::is_integer,
  83. IsSigned = std::numeric_limits<T>::is_signed,
  84. IsComplex = 0,
  85. RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
  86. ReadCost = 1,
  87. AddCost = 1,
  88. MulCost = 1
  89. };
  90. typedef T Real;
  91. typedef typename internal::conditional<
  92. IsInteger,
  93. typename internal::conditional<sizeof(T)<=2, float, double>::type,
  94. T
  95. >::type NonInteger;
  96. typedef T Nested;
  97. typedef T Literal;
  98. EIGEN_DEVICE_FUNC
  99. static inline Real epsilon()
  100. {
  101. return numext::numeric_limits<T>::epsilon();
  102. }
  103. EIGEN_DEVICE_FUNC
  104. static inline int digits10()
  105. {
  106. return internal::default_digits10_impl<T>::run();
  107. }
  108. EIGEN_DEVICE_FUNC
  109. static inline Real dummy_precision()
  110. {
  111. // make sure to override this for floating-point types
  112. return Real(0);
  113. }
  114. EIGEN_DEVICE_FUNC
  115. static inline T highest() {
  116. return (numext::numeric_limits<T>::max)();
  117. }
  118. EIGEN_DEVICE_FUNC
  119. static inline T lowest() {
  120. return IsInteger ? (numext::numeric_limits<T>::min)() : (-(numext::numeric_limits<T>::max)());
  121. }
  122. EIGEN_DEVICE_FUNC
  123. static inline T infinity() {
  124. return numext::numeric_limits<T>::infinity();
  125. }
  126. EIGEN_DEVICE_FUNC
  127. static inline T quiet_NaN() {
  128. return numext::numeric_limits<T>::quiet_NaN();
  129. }
  130. };
  131. template<typename T> struct NumTraits : GenericNumTraits<T>
  132. {};
  133. template<> struct NumTraits<float>
  134. : GenericNumTraits<float>
  135. {
  136. EIGEN_DEVICE_FUNC
  137. static inline float dummy_precision() { return 1e-5f; }
  138. };
  139. template<> struct NumTraits<double> : GenericNumTraits<double>
  140. {
  141. EIGEN_DEVICE_FUNC
  142. static inline double dummy_precision() { return 1e-12; }
  143. };
  144. template<> struct NumTraits<long double>
  145. : GenericNumTraits<long double>
  146. {
  147. static inline long double dummy_precision() { return 1e-15l; }
  148. };
  149. template<typename _Real> struct NumTraits<std::complex<_Real> >
  150. : GenericNumTraits<std::complex<_Real> >
  151. {
  152. typedef _Real Real;
  153. typedef typename NumTraits<_Real>::Literal Literal;
  154. enum {
  155. IsComplex = 1,
  156. RequireInitialization = NumTraits<_Real>::RequireInitialization,
  157. ReadCost = 2 * NumTraits<_Real>::ReadCost,
  158. AddCost = 2 * NumTraits<Real>::AddCost,
  159. MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
  160. };
  161. EIGEN_DEVICE_FUNC
  162. static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
  163. EIGEN_DEVICE_FUNC
  164. static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
  165. EIGEN_DEVICE_FUNC
  166. static inline int digits10() { return NumTraits<Real>::digits10(); }
  167. };
  168. template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
  169. struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
  170. {
  171. typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
  172. typedef typename NumTraits<Scalar>::Real RealScalar;
  173. typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
  174. typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
  175. typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
  176. typedef ArrayType & Nested;
  177. typedef typename NumTraits<Scalar>::Literal Literal;
  178. enum {
  179. IsComplex = NumTraits<Scalar>::IsComplex,
  180. IsInteger = NumTraits<Scalar>::IsInteger,
  181. IsSigned = NumTraits<Scalar>::IsSigned,
  182. RequireInitialization = 1,
  183. ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
  184. AddCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
  185. MulCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
  186. };
  187. EIGEN_DEVICE_FUNC
  188. static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
  189. EIGEN_DEVICE_FUNC
  190. static inline RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
  191. static inline int digits10() { return NumTraits<Scalar>::digits10(); }
  192. };
  193. template<> struct NumTraits<std::string>
  194. : GenericNumTraits<std::string>
  195. {
  196. enum {
  197. RequireInitialization = 1,
  198. ReadCost = HugeCost,
  199. AddCost = HugeCost,
  200. MulCost = HugeCost
  201. };
  202. static inline int digits10() { return 0; }
  203. private:
  204. static inline std::string epsilon();
  205. static inline std::string dummy_precision();
  206. static inline std::string lowest();
  207. static inline std::string highest();
  208. static inline std::string infinity();
  209. static inline std::string quiet_NaN();
  210. };
  211. // Empty specialization for void to allow template specialization based on NumTraits<T>::Real with T==void and SFINAE.
  212. template<> struct NumTraits<void> {};
  213. } // end namespace Eigen
  214. #endif // EIGEN_NUMTRAITS_H