NDDEM
NumTraits.h
Go to the documentation of this file.
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 
10 #ifndef EIGEN_NUMTRAITS_H
11 #define EIGEN_NUMTRAITS_H
12 
13 namespace Eigen {
14 
15 namespace internal {
16 
17 // default implementation of digits10(), based on numeric_limits if specialized,
18 // 0 for integer types, and log10(epsilon()) otherwise.
19 template< typename T,
20  bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
21  bool is_integer = NumTraits<T>::IsInteger>
23 {
25  static int run() { return std::numeric_limits<T>::digits10; }
26 };
27 
28 template<typename T>
29 struct default_digits10_impl<T,false,false> // Floating point
30 {
32  static int run() {
33  using std::log10;
34  using std::ceil;
35  typedef typename NumTraits<T>::Real Real;
36  return int(ceil(-log10(NumTraits<Real>::epsilon())));
37  }
38 };
39 
40 template<typename T>
41 struct default_digits10_impl<T,false,true> // Integer
42 {
44  static int run() { return 0; }
45 };
46 
47 
48 // default implementation of digits(), based on numeric_limits if specialized,
49 // 0 for integer types, and log2(epsilon()) otherwise.
50 template< typename T,
51  bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
52  bool is_integer = NumTraits<T>::IsInteger>
54 {
56  static int run() { return std::numeric_limits<T>::digits; }
57 };
58 
59 template<typename T>
60 struct default_digits_impl<T,false,false> // Floating point
61 {
63  static int run() {
64  using std::log;
65  using std::ceil;
66  typedef typename NumTraits<T>::Real Real;
67  return int(ceil(-log(NumTraits<Real>::epsilon())/log(static_cast<Real>(2))));
68  }
69 };
70 
71 template<typename T>
72 struct default_digits_impl<T,false,true> // Integer
73 {
75  static int run() { return 0; }
76 };
77 
78 } // end namespace internal
79 
80 namespace numext {
83 // TODO: Replace by std::bit_cast (available in C++20)
84 template <typename Tgt, typename Src>
86 #if EIGEN_HAS_TYPE_TRAITS
87  // The behaviour of memcpy is not specified for non-trivially copyable types
90  THIS_TYPE_IS_NOT_SUPPORTED);
91 #endif
92 
93  EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED);
94  Tgt tgt;
95  EIGEN_USING_STD(memcpy)
96  memcpy(&tgt, &src, sizeof(Tgt));
97  return tgt;
98 }
99 } // namespace numext
100 
152 template<typename T> struct GenericNumTraits
153 {
154  enum {
155  IsInteger = std::numeric_limits<T>::is_integer,
156  IsSigned = std::numeric_limits<T>::is_signed,
159  ReadCost = 1,
160  AddCost = 1,
161  MulCost = 1
162  };
163 
164  typedef T Real;
165  typedef typename internal::conditional<
166  IsInteger,
167  typename internal::conditional<sizeof(T)<=2, float, double>::type,
168  T
169  >::type NonInteger;
170  typedef T Nested;
171  typedef T Literal;
172 
174  static inline Real epsilon()
175  {
176  return numext::numeric_limits<T>::epsilon();
177  }
178 
180  static inline int digits10()
181  {
183  }
184 
186  static inline int digits()
187  {
189  }
190 
192  static inline int min_exponent()
193  {
194  return numext::numeric_limits<T>::min_exponent;
195  }
196 
198  static inline int max_exponent()
199  {
200  return numext::numeric_limits<T>::max_exponent;
201  }
202 
204  static inline Real dummy_precision()
205  {
206  // make sure to override this for floating-point types
207  return Real(0);
208  }
209 
211  static inline T highest() {
213  }
214 
216  static inline T lowest() {
218  : static_cast<T>(-(numext::numeric_limits<T>::max)());
219  }
220 
222  static inline T infinity() {
223  return numext::numeric_limits<T>::infinity();
224  }
225 
227  static inline T quiet_NaN() {
228  return numext::numeric_limits<T>::quiet_NaN();
229  }
230 };
231 
232 template<typename T> struct NumTraits : GenericNumTraits<T>
233 {};
234 
235 template<> struct NumTraits<float>
236  : GenericNumTraits<float>
237 {
239  static inline float dummy_precision() { return 1e-5f; }
240 };
241 
242 template<> struct NumTraits<double> : GenericNumTraits<double>
243 {
245  static inline double dummy_precision() { return 1e-12; }
246 };
247 
248 template<> struct NumTraits<long double>
249  : GenericNumTraits<long double>
250 {
252  static inline long double dummy_precision() { return 1e-15l; }
253 };
254 
255 template<typename _Real> struct NumTraits<std::complex<_Real> >
256  : GenericNumTraits<std::complex<_Real> >
257 {
258  typedef _Real Real;
260  enum {
266  };
267 
269  static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
273  static inline int digits10() { return NumTraits<Real>::digits10(); }
274 };
275 
276 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
277 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
278 {
284  typedef ArrayType & Nested;
286 
287  enum {
292  ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::ReadCost),
293  AddCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::AddCost),
294  MulCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::MulCost)
295  };
296 
298  static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
301 
303  static inline int digits10() { return NumTraits<Scalar>::digits10(); }
304 };
305 
306 template<> struct NumTraits<std::string>
307  : GenericNumTraits<std::string>
308 {
309  enum {
313  MulCost = HugeCost
314  };
315 
317  static inline int digits10() { return 0; }
318 
319 private:
320  static inline std::string epsilon();
321  static inline std::string dummy_precision();
322  static inline std::string lowest();
323  static inline std::string highest();
324  static inline std::string infinity();
325  static inline std::string quiet_NaN();
326 };
327 
328 // Empty specialization for void to allow template specialization based on NumTraits<T>::Real with T==void and SFINAE.
329 template<> struct NumTraits<void> {};
330 
331 template<> struct NumTraits<bool> : GenericNumTraits<bool> {};
332 
333 } // end namespace Eigen
334 
335 #endif // EIGEN_NUMTRAITS_H
EIGEN_DEVICE_FUNC const Log10ReturnType log10() const
Definition: ArrayCwiseUnaryOps.h:158
EIGEN_DEVICE_FUNC const LogReturnType log() const
Definition: ArrayCwiseUnaryOps.h:128
EIGEN_DEVICE_FUNC const CeilReturnType ceil() const
Definition: ArrayCwiseUnaryOps.h:495
#define EIGEN_USING_STD(FUNC)
Definition: Macros.h:1185
#define EIGEN_CONSTEXPR
Definition: Macros.h:787
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
#define EIGEN_STATIC_ASSERT(CONDITION, MSG)
Definition: StaticAssert.h:127
General-purpose arrays with easy API for coefficient-wise operations.
Definition: Array.h:47
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16() max(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:576
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC bfloat16() min(const bfloat16 &a, const bfloat16 &b)
Definition: BFloat16.h:571
EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src &src)
Definition: NumTraits.h:85
Namespace containing all symbols from the Eigen library.
Definition: LDLT.h:16
const int HugeCost
Definition: Constants.h:44
const int Dynamic
Definition: Constants.h:22
type
The type the bitset is encoded with.
Definition: bitset.hpp:44
Definition: document.h:416
Definition: json.hpp:5678
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1282
Definition: NumTraits.h:153
@ RequireInitialization
Definition: NumTraits.h:158
@ IsSigned
Definition: NumTraits.h:156
@ ReadCost
Definition: NumTraits.h:159
@ AddCost
Definition: NumTraits.h:160
@ IsInteger
Definition: NumTraits.h:155
@ IsComplex
Definition: NumTraits.h:157
@ MulCost
Definition: NumTraits.h:161
T Real
Definition: NumTraits.h:164
NumTraits< Scalar >::Real RealScalar
Definition: NumTraits.h:280
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR RealScalar epsilon()
Definition: NumTraits.h:298
NumTraits< Scalar >::Literal Literal
Definition: NumTraits.h:285
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR RealScalar dummy_precision()
Definition: NumTraits.h:300
NumTraits< Scalar >::NonInteger NonIntegerScalar
Definition: NumTraits.h:282
Array< RealScalar, Rows, Cols, Options, MaxRows, MaxCols > Real
Definition: NumTraits.h:281
Array< Scalar, Rows, Cols, Options, MaxRows, MaxCols > ArrayType
Definition: NumTraits.h:279
static EIGEN_CONSTEXPR int digits10()
Definition: NumTraits.h:303
Array< NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols > NonInteger
Definition: NumTraits.h:283
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR double dummy_precision()
Definition: NumTraits.h:245
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR float dummy_precision()
Definition: NumTraits.h:239
static EIGEN_CONSTEXPR long double dummy_precision()
Definition: NumTraits.h:252
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR Real dummy_precision()
Definition: NumTraits.h:271
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int digits10()
Definition: NumTraits.h:273
NumTraits< _Real >::Literal Literal
Definition: NumTraits.h:259
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR Real epsilon()
Definition: NumTraits.h:269
_Real Real
Definition: NumTraits.h:258
static std::string infinity()
static std::string quiet_NaN()
static EIGEN_CONSTEXPR int digits10()
Definition: NumTraits.h:317
static std::string dummy_precision()
static std::string epsilon()
static std::string highest()
static std::string lowest()
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:233
Definition: Meta.h:109
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:32
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:44
Definition: NumTraits.h:23
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:25
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:63
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:75
Definition: NumTraits.h:54
EIGEN_DEVICE_FUNC static EIGEN_CONSTEXPR int run()
Definition: NumTraits.h:56
Definition: Meta.h:133