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
13namespace Eigen {
14
50
51template<typename T> struct GenericNumTraits
52{
53 enum {
54 IsInteger = std::numeric_limits<T>::is_integer,
55 IsSigned = std::numeric_limits<T>::is_signed,
56 IsComplex = 0,
57 RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
58 ReadCost = 1,
59 AddCost = 1,
60 MulCost = 1
61 };
62
63 typedef T Real;
64 typedef typename internal::conditional<
65 IsInteger,
66 typename internal::conditional<sizeof(T)<=2, float, double>::type,
67 T
68 >::type NonInteger;
69 typedef T Nested;
70
71 static inline Real epsilon() { return std::numeric_limits<T>::epsilon(); }
72 static inline Real dummy_precision()
73 {
74 // make sure to override this for floating-point types
75 return Real(0);
76 }
77 static inline T highest() { return (std::numeric_limits<T>::max)(); }
78 static inline T lowest() { return IsInteger ? (std::numeric_limits<T>::min)() : (-(std::numeric_limits<T>::max)()); }
79
80#ifdef EIGEN2_SUPPORT
81 enum {
82 HasFloatingPoint = !IsInteger
83 };
84 typedef NonInteger FloatingPoint;
85#endif
86};
87
88template<typename T> struct NumTraits : GenericNumTraits<T>
89{};
90
91template<> struct NumTraits<float>
92 : GenericNumTraits<float>
93{
94 static inline float dummy_precision() { return 1e-5f; }
95};
96
97template<> struct NumTraits<double> : GenericNumTraits<double>
98{
99 static inline double dummy_precision() { return 1e-12; }
100};
101
102template<> struct NumTraits<long double>
103 : GenericNumTraits<long double>
104{
105 static inline long double dummy_precision() { return 1e-15l; }
106};
107
108template<typename _Real> struct NumTraits<std::complex<_Real> >
109 : GenericNumTraits<std::complex<_Real> >
110{
111 typedef _Real Real;
112 enum {
113 IsComplex = 1,
114 RequireInitialization = NumTraits<_Real>::RequireInitialization,
115 ReadCost = 2 * NumTraits<_Real>::ReadCost,
116 AddCost = 2 * NumTraits<Real>::AddCost,
117 MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
118 };
119
120 static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
121 static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
122};
123
124template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
125struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
126{
127 typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
128 typedef typename NumTraits<Scalar>::Real RealScalar;
129 typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
130 typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
131 typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
132 typedef ArrayType & Nested;
133
134 enum {
135 IsComplex = NumTraits<Scalar>::IsComplex,
136 IsInteger = NumTraits<Scalar>::IsInteger,
137 IsSigned = NumTraits<Scalar>::IsSigned,
138 RequireInitialization = 1,
139 ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::ReadCost,
140 AddCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::AddCost,
141 MulCost = ArrayType::SizeAtCompileTime==Dynamic ? Dynamic : ArrayType::SizeAtCompileTime * NumTraits<Scalar>::MulCost
142 };
143};
144
145} // end namespace Eigen
146
147#endif // EIGEN_NUMTRAITS_H
General-purpose arrays with easy API for coefficient-wise operations.
Definition Array.h:44
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:89