Main MRPT website > C++ reference
MRPT logo

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 /** \class NumTraits
00029   * \ingroup Core_Module
00030   *
00031   * \brief Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
00032   *
00033   * \param T the numeric type at hand
00034   *
00035   * This class stores enums, typedefs and static methods giving information about a numeric type.
00036   *
00037   * The provided data consists of:
00038   * \li A typedef \a Real, giving the "real part" type of \a T. If \a T is already real,
00039   *     then \a Real is just a typedef to \a T. If \a T is \c std::complex<U> then \a Real
00040   *     is a typedef to \a U.
00041   * \li A typedef \a NonInteger, giving the type that should be used for operations producing non-integral values,
00042   *     such as quotients, square roots, etc. If \a T is a floating-point type, then this typedef just gives
00043   *     \a T again. Note however that many Eigen functions such as internal::sqrt simply refuse to
00044   *     take integers. Outside of a few cases, Eigen doesn't do automatic type promotion. Thus, this typedef is
00045   *     only intended as a helper for code that needs to explicitly promote types.
00046   * \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
00047   *     this means, just use \a T here.
00048   * \li An enum value \a IsComplex. It is equal to 1 if \a T is a \c std::complex
00049   *     type, and to 0 otherwise.
00050   * \li An enum value \a IsInteger. It is equal to \c 1 if \a T is an integer type such as \c int,
00051   *     and to \c 0 otherwise.
00052   * \li Enum values ReadCost, AddCost and MulCost representing a rough estimate of the number of CPU cycles needed
00053   *     to by move / add / mul instructions respectively, assuming the data is already stored in CPU registers.
00054   *     Stay vague here. No need to do architecture-specific stuff.
00055   * \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.
00056   * \li An epsilon() function which, unlike std::numeric_limits::epsilon(), returns a \a Real instead of a \a T.
00057   * \li A dummy_precision() function returning a weak epsilon value. It is mainly used as a default
00058   *     value by the fuzzy comparison operators.
00059   * \li highest() and lowest() functions returning the highest and lowest possible values respectively.
00060   */
00061 
00062 template<typename T> struct GenericNumTraits
00063 {
00064   enum {
00065     IsInteger = std::numeric_limits<T>::is_integer,
00066     IsSigned = std::numeric_limits<T>::is_signed,
00067     IsComplex = 0,
00068     ReadCost = 1,
00069     AddCost = 1,
00070     MulCost = 1
00071   };
00072 
00073   typedef T Real;
00074   typedef typename internal::conditional<
00075                      IsInteger,
00076                      typename internal::conditional<sizeof(T)<=2, float, double>::type,
00077                      T
00078                    >::type NonInteger;
00079   typedef T Nested;
00080 
00081   inline static Real epsilon() { return std::numeric_limits<T>::epsilon(); }
00082   inline static Real dummy_precision()
00083   {
00084     // make sure to override this for floating-point types
00085     return Real(0);
00086   }
00087   inline static T highest() { return std::numeric_limits<T>::max(); }
00088   inline static T lowest()  { return IsInteger ? std::numeric_limits<T>::min() : (-std::numeric_limits<T>::max()); }
00089 };
00090 
00091 template<typename T> struct NumTraits : GenericNumTraits<T>
00092 {};
00093 
00094 template<> struct NumTraits<float>
00095   : GenericNumTraits<float>
00096 {
00097   inline static float dummy_precision() { return 1e-5f; }
00098 };
00099 
00100 template<> struct NumTraits<double> : GenericNumTraits<double>
00101 {
00102   inline static double dummy_precision() { return 1e-12; }
00103 };
00104 
00105 template<> struct NumTraits<long double>
00106   : GenericNumTraits<long double>
00107 {
00108   static inline long double dummy_precision() { return 1e-15l; }
00109 };
00110 
00111 template<typename _Real> struct NumTraits<std::complex<_Real> >
00112   : GenericNumTraits<std::complex<_Real> >
00113 {
00114   typedef _Real Real;
00115   enum {
00116     IsComplex = 1,
00117     ReadCost = 2 * NumTraits<_Real>::ReadCost,
00118     AddCost = 2 * NumTraits<Real>::AddCost,
00119     MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
00120   };
00121 
00122   inline static Real epsilon() { return NumTraits<Real>::epsilon(); }
00123   inline static Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
00124 };
00125 
00126 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
00127 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
00128 {
00129   typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
00130   typedef typename NumTraits<Scalar>::Real RealScalar;
00131   typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
00132   typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
00133   typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
00134   typedef ArrayType & Nested;
00135   
00136   enum {
00137     IsComplex = NumTraits<Scalar>::IsComplex,
00138     IsInteger = NumTraits<Scalar>::IsInteger,
00139     IsSigned  = NumTraits<Scalar>::IsSigned,
00140     ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
00141     AddCost  = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
00142     MulCost  = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
00143   };
00144 };
00145 
00146 
00147 
00148 #endif // EIGEN_NUMTRAITS_H



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN:exported at Tue Jan 25 21:56:31 UTC 2011