NumTraits.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_NUMTRAITS_H
00026 #define EIGEN_NUMTRAITS_H
00027 
00028 namespace Eigen {
00029 
00066 template<typename T> struct GenericNumTraits
00067 {
00068   enum {
00069     IsInteger = std::numeric_limits<T>::is_integer,
00070     IsSigned = std::numeric_limits<T>::is_signed,
00071     IsComplex = 0,
00072     RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
00073     ReadCost = 1,
00074     AddCost = 1,
00075     MulCost = 1
00076   };
00077 
00078   typedef T Real;
00079   typedef typename internal::conditional<
00080                      IsInteger,
00081                      typename internal::conditional<sizeof(T)<=2, float, double>::type,
00082                      T
00083                    >::type NonInteger;
00084   typedef T Nested;
00085 
00086   static inline Real epsilon() { return std::numeric_limits<T>::epsilon(); }
00087   static inline Real dummy_precision()
00088   {
00089     // make sure to override this for floating-point types
00090     return Real(0);
00091   }
00092   static inline T highest() { return (std::numeric_limits<T>::max)(); }
00093   static inline T lowest()  { return IsInteger ? (std::numeric_limits<T>::min)() : (-(std::numeric_limits<T>::max)()); }
00094   
00095 #ifdef EIGEN2_SUPPORT
00096   enum {
00097     HasFloatingPoint = !IsInteger
00098   };
00099   typedef NonInteger FloatingPoint;
00100 #endif
00101 };
00102 
00103 template<typename T> struct NumTraits : GenericNumTraits<T>
00104 {};
00105 
00106 template<> struct NumTraits<float>
00107   : GenericNumTraits<float>
00108 {
00109   static inline float dummy_precision() { return 1e-5f; }
00110 };
00111 
00112 template<> struct NumTraits<double> : GenericNumTraits<double>
00113 {
00114   static inline double dummy_precision() { return 1e-12; }
00115 };
00116 
00117 template<> struct NumTraits<long double>
00118   : GenericNumTraits<long double>
00119 {
00120   static inline long double dummy_precision() { return 1e-15l; }
00121 };
00122 
00123 template<typename _Real> struct NumTraits<std::complex<_Real> >
00124   : GenericNumTraits<std::complex<_Real> >
00125 {
00126   typedef _Real Real;
00127   enum {
00128     IsComplex = 1,
00129     RequireInitialization = NumTraits<_Real>::RequireInitialization,
00130     ReadCost = 2 * NumTraits<_Real>::ReadCost,
00131     AddCost = 2 * NumTraits<Real>::AddCost,
00132     MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
00133   };
00134 
00135   static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
00136   static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
00137 };
00138 
00139 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
00140 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
00141 {
00142   typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
00143   typedef typename NumTraits<Scalar>::Real RealScalar;
00144   typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
00145   typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
00146   typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
00147   typedef ArrayType & Nested;
00148   
00149   enum {
00150     IsComplex = NumTraits<Scalar>::IsComplex,
00151     IsInteger = NumTraits<Scalar>::IsInteger,
00152     IsSigned  = NumTraits<Scalar>::IsSigned,
00153     RequireInitialization = 1,
00154     ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
00155     AddCost  = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
00156     MulCost  = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
00157   };
00158 };
00159 
00160 } // end namespace Eigen
00161 
00162 #endif // EIGEN_NUMTRAITS_H