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 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_NUMTRAITS_H
26 #define EIGEN_NUMTRAITS_H
27 
28 namespace Eigen {
29 
66 template<typename T> struct GenericNumTraits
67 {
68  enum {
69  IsInteger = std::numeric_limits<T>::is_integer,
70  IsSigned = std::numeric_limits<T>::is_signed,
71  IsComplex = 0,
72  RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
73  ReadCost = 1,
74  AddCost = 1,
75  MulCost = 1
76  };
77 
78  typedef T Real;
79  typedef typename internal::conditional<
80  IsInteger,
81  typename internal::conditional<sizeof(T)<=2, float, double>::type,
82  T
83  >::type NonInteger;
84  typedef T Nested;
85 
86  static inline Real epsilon() { return std::numeric_limits<T>::epsilon(); }
87  static inline Real dummy_precision()
88  {
89  // make sure to override this for floating-point types
90  return Real(0);
91  }
92  static inline T highest() { return (std::numeric_limits<T>::max)(); }
93  static inline T lowest() { return IsInteger ? (std::numeric_limits<T>::min)() : (-(std::numeric_limits<T>::max)()); }
94 
95 #ifdef EIGEN2_SUPPORT
96  enum {
97  HasFloatingPoint = !IsInteger
98  };
99  typedef NonInteger FloatingPoint;
100 #endif
101 };
102 
103 template<typename T> struct NumTraits : GenericNumTraits<T>
104 {};
105 
106 template<> struct NumTraits<float>
107  : GenericNumTraits<float>
108 {
109  static inline float dummy_precision() { return 1e-5f; }
110 };
111 
112 template<> struct NumTraits<double> : GenericNumTraits<double>
113 {
114  static inline double dummy_precision() { return 1e-12; }
115 };
116 
117 template<> struct NumTraits<long double>
118  : GenericNumTraits<long double>
119 {
120  static inline long double dummy_precision() { return 1e-15l; }
121 };
122 
123 template<typename _Real> struct NumTraits<std::complex<_Real> >
124  : GenericNumTraits<std::complex<_Real> >
125 {
126  typedef _Real Real;
127  enum {
133  };
134 
135  static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
137 };
138 
139 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
140 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
141 {
147  typedef ArrayType & Nested;
148 
149  enum {
154  ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
155  AddCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
156  MulCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
157  };
158 };
159 
160 } // end namespace Eigen
161 
162 #endif // EIGEN_NUMTRAITS_H