Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.4.99 (git rev 199c5f2b47eb1f8e5a2d20e60f07e97cd95a6ba6)
NumTraits.h
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>
22 struct default_digits10_impl
23 {
24  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
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 {
31  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
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 {
43  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
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>
53 struct default_digits_impl
54 {
55  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
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 {
62  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
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 {
74  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
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>
85 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) {
86 #if EIGEN_HAS_TYPE_TRAITS
87  // The behaviour of memcpy is not specified for non-trivially copyable types
88  EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Src>::value, THIS_TYPE_IS_NOT_SUPPORTED);
89  EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Tgt>::value && std::is_default_constructible<Tgt>::value,
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 
143 template<typename T> struct GenericNumTraits
144 {
145  enum {
146  IsInteger = std::numeric_limits<T>::is_integer,
147  IsSigned = std::numeric_limits<T>::is_signed,
148  IsComplex = 0,
149  RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
150  ReadCost = 1,
151  AddCost = 1,
152  MulCost = 1
153  };
154 
155  typedef T Real;
156  typedef typename internal::conditional<
157  IsInteger,
158  typename internal::conditional<sizeof(T)<=2, float, double>::type,
159  T
160  >::type NonInteger;
161  typedef T Nested;
162  typedef T Literal;
163 
164  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
165  static inline Real epsilon()
166  {
167  return numext::numeric_limits<T>::epsilon();
168  }
169 
170  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
171  static inline int digits10()
172  {
173  return internal::default_digits10_impl<T>::run();
174  }
175 
176  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
177  static inline int digits()
178  {
179  return internal::default_digits_impl<T>::run();
180  }
181 
182  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
183  static inline Real dummy_precision()
184  {
185  // make sure to override this for floating-point types
186  return Real(0);
187  }
188 
189 
190  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
191  static inline T highest() {
192  return (numext::numeric_limits<T>::max)();
193  }
194 
195  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
196  static inline T lowest() {
197  return IsInteger ? (numext::numeric_limits<T>::min)()
198  : static_cast<T>(-(numext::numeric_limits<T>::max)());
199  }
200 
201  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
202  static inline T infinity() {
203  return numext::numeric_limits<T>::infinity();
204  }
205 
206  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
207  static inline T quiet_NaN() {
208  return numext::numeric_limits<T>::quiet_NaN();
209  }
210 };
211 
212 template<typename T> struct NumTraits : GenericNumTraits<T>
213 {};
214 
215 template<> struct NumTraits<float>
216  : GenericNumTraits<float>
217 {
218  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
219  static inline float dummy_precision() { return 1e-5f; }
220 };
221 
222 template<> struct NumTraits<double> : GenericNumTraits<double>
223 {
224  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
225  static inline double dummy_precision() { return 1e-12; }
226 };
227 
228 template<> struct NumTraits<long double>
229  : GenericNumTraits<long double>
230 {
231  EIGEN_CONSTEXPR
232  static inline long double dummy_precision() { return 1e-15l; }
233 };
234 
235 template<typename _Real> struct NumTraits<std::complex<_Real> >
236  : GenericNumTraits<std::complex<_Real> >
237 {
238  typedef _Real Real;
239  typedef typename NumTraits<_Real>::Literal Literal;
240  enum {
241  IsComplex = 1,
242  RequireInitialization = NumTraits<_Real>::RequireInitialization,
243  ReadCost = 2 * NumTraits<_Real>::ReadCost,
244  AddCost = 2 * NumTraits<Real>::AddCost,
245  MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
246  };
247 
248  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
249  static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
250  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
251  static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
252  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
253  static inline int digits10() { return NumTraits<Real>::digits10(); }
254 };
255 
256 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
257 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
258 {
259  typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
260  typedef typename NumTraits<Scalar>::Real RealScalar;
261  typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
262  typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
263  typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
264  typedef ArrayType & Nested;
265  typedef typename NumTraits<Scalar>::Literal Literal;
266 
267  enum {
268  IsComplex = NumTraits<Scalar>::IsComplex,
269  IsInteger = NumTraits<Scalar>::IsInteger,
270  IsSigned = NumTraits<Scalar>::IsSigned,
271  RequireInitialization = 1,
272  ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
273  AddCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
274  MulCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
275  };
276 
277  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
278  static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
279  EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
280  static inline RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
281 
282  EIGEN_CONSTEXPR
283  static inline int digits10() { return NumTraits<Scalar>::digits10(); }
284 };
285 
286 template<> struct NumTraits<std::string>
287  : GenericNumTraits<std::string>
288 {
289  enum {
290  RequireInitialization = 1,
291  ReadCost = HugeCost,
292  AddCost = HugeCost,
293  MulCost = HugeCost
294  };
295 
296  EIGEN_CONSTEXPR
297  static inline int digits10() { return 0; }
298 
299 private:
300  static inline std::string epsilon();
301  static inline std::string dummy_precision();
302  static inline std::string lowest();
303  static inline std::string highest();
304  static inline std::string infinity();
305  static inline std::string quiet_NaN();
306 };
307 
308 // Empty specialization for void to allow template specialization based on NumTraits<T>::Real with T==void and SFINAE.
309 template<> struct NumTraits<void> {};
310 
311 template<> struct NumTraits<bool> : GenericNumTraits<bool> {};
312 
313 } // end namespace Eigen
314 
315 #endif // EIGEN_NUMTRAITS_H
Namespace containing all symbols from the Eigen library.
Definition: Core:134
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log10_op< typename Derived::Scalar >, const Derived > log10(const Eigen::ArrayBase< Derived > &x)
const int HugeCost
Definition: Constants.h:44
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_ceil_op< typename Derived::Scalar >, const Derived > ceil(const Eigen::ArrayBase< Derived > &x)
const int Dynamic
Definition: Constants.h:22
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_log_op< typename Derived::Scalar >, const Derived > log(const Eigen::ArrayBase< Derived > &x)
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:213