File indexing completed on 2025-01-18 09:56:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef EIGEN_NUMTRAITS_H
0011 #define EIGEN_NUMTRAITS_H
0012
0013 namespace Eigen {
0014
0015 namespace internal {
0016
0017
0018
0019 template< typename T,
0020 bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
0021 bool is_integer = NumTraits<T>::IsInteger>
0022 struct default_digits10_impl
0023 {
0024 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0025 static int run() { return std::numeric_limits<T>::digits10; }
0026 };
0027
0028 template<typename T>
0029 struct default_digits10_impl<T,false,false>
0030 {
0031 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0032 static int run() {
0033 using std::log10;
0034 using std::ceil;
0035 typedef typename NumTraits<T>::Real Real;
0036 return int(ceil(-log10(NumTraits<Real>::epsilon())));
0037 }
0038 };
0039
0040 template<typename T>
0041 struct default_digits10_impl<T,false,true>
0042 {
0043 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0044 static int run() { return 0; }
0045 };
0046
0047
0048
0049
0050 template< typename T,
0051 bool use_numeric_limits = std::numeric_limits<T>::is_specialized,
0052 bool is_integer = NumTraits<T>::IsInteger>
0053 struct default_digits_impl
0054 {
0055 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0056 static int run() { return std::numeric_limits<T>::digits; }
0057 };
0058
0059 template<typename T>
0060 struct default_digits_impl<T,false,false>
0061 {
0062 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0063 static int run() {
0064 using std::log;
0065 using std::ceil;
0066 typedef typename NumTraits<T>::Real Real;
0067 return int(ceil(-log(NumTraits<Real>::epsilon())/log(static_cast<Real>(2))));
0068 }
0069 };
0070
0071 template<typename T>
0072 struct default_digits_impl<T,false,true>
0073 {
0074 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0075 static int run() { return 0; }
0076 };
0077
0078 }
0079
0080 namespace numext {
0081
0082
0083
0084 template <typename Tgt, typename Src>
0085 EIGEN_STRONG_INLINE EIGEN_DEVICE_FUNC Tgt bit_cast(const Src& src) {
0086 #if EIGEN_HAS_TYPE_TRAITS
0087
0088 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Src>::value, THIS_TYPE_IS_NOT_SUPPORTED);
0089 EIGEN_STATIC_ASSERT(std::is_trivially_copyable<Tgt>::value && std::is_default_constructible<Tgt>::value,
0090 THIS_TYPE_IS_NOT_SUPPORTED);
0091 #endif
0092
0093 EIGEN_STATIC_ASSERT(sizeof(Src) == sizeof(Tgt), THIS_TYPE_IS_NOT_SUPPORTED);
0094 Tgt tgt;
0095 EIGEN_USING_STD(memcpy)
0096 memcpy(&tgt, &src, sizeof(Tgt));
0097 return tgt;
0098 }
0099 }
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 template<typename T> struct GenericNumTraits
0153 {
0154 enum {
0155 IsInteger = std::numeric_limits<T>::is_integer,
0156 IsSigned = std::numeric_limits<T>::is_signed,
0157 IsComplex = 0,
0158 RequireInitialization = internal::is_arithmetic<T>::value ? 0 : 1,
0159 ReadCost = 1,
0160 AddCost = 1,
0161 MulCost = 1
0162 };
0163
0164 typedef T Real;
0165 typedef typename internal::conditional<
0166 IsInteger,
0167 typename internal::conditional<sizeof(T)<=2, float, double>::type,
0168 T
0169 >::type NonInteger;
0170 typedef T Nested;
0171 typedef T Literal;
0172
0173 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0174 static inline Real epsilon()
0175 {
0176 return numext::numeric_limits<T>::epsilon();
0177 }
0178
0179 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0180 static inline int digits10()
0181 {
0182 return internal::default_digits10_impl<T>::run();
0183 }
0184
0185 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0186 static inline int digits()
0187 {
0188 return internal::default_digits_impl<T>::run();
0189 }
0190
0191 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0192 static inline int min_exponent()
0193 {
0194 return numext::numeric_limits<T>::min_exponent;
0195 }
0196
0197 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0198 static inline int max_exponent()
0199 {
0200 return numext::numeric_limits<T>::max_exponent;
0201 }
0202
0203 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0204 static inline Real dummy_precision()
0205 {
0206
0207 return Real(0);
0208 }
0209
0210 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0211 static inline T highest() {
0212 return (numext::numeric_limits<T>::max)();
0213 }
0214
0215 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0216 static inline T lowest() {
0217 return IsInteger ? (numext::numeric_limits<T>::min)()
0218 : static_cast<T>(-(numext::numeric_limits<T>::max)());
0219 }
0220
0221 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0222 static inline T infinity() {
0223 return numext::numeric_limits<T>::infinity();
0224 }
0225
0226 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0227 static inline T quiet_NaN() {
0228 return numext::numeric_limits<T>::quiet_NaN();
0229 }
0230 };
0231
0232 template<typename T> struct NumTraits : GenericNumTraits<T>
0233 {};
0234
0235 template<> struct NumTraits<float>
0236 : GenericNumTraits<float>
0237 {
0238 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0239 static inline float dummy_precision() { return 1e-5f; }
0240 };
0241
0242 template<> struct NumTraits<double> : GenericNumTraits<double>
0243 {
0244 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0245 static inline double dummy_precision() { return 1e-12; }
0246 };
0247
0248 template<> struct NumTraits<long double>
0249 : GenericNumTraits<long double>
0250 {
0251 EIGEN_CONSTEXPR
0252 static inline long double dummy_precision() { return 1e-15l; }
0253 };
0254
0255 template<typename _Real> struct NumTraits<std::complex<_Real> >
0256 : GenericNumTraits<std::complex<_Real> >
0257 {
0258 typedef _Real Real;
0259 typedef typename NumTraits<_Real>::Literal Literal;
0260 enum {
0261 IsComplex = 1,
0262 RequireInitialization = NumTraits<_Real>::RequireInitialization,
0263 ReadCost = 2 * NumTraits<_Real>::ReadCost,
0264 AddCost = 2 * NumTraits<Real>::AddCost,
0265 MulCost = 4 * NumTraits<Real>::MulCost + 2 * NumTraits<Real>::AddCost
0266 };
0267
0268 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0269 static inline Real epsilon() { return NumTraits<Real>::epsilon(); }
0270 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0271 static inline Real dummy_precision() { return NumTraits<Real>::dummy_precision(); }
0272 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0273 static inline int digits10() { return NumTraits<Real>::digits10(); }
0274 };
0275
0276 template<typename Scalar, int Rows, int Cols, int Options, int MaxRows, int MaxCols>
0277 struct NumTraits<Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> >
0278 {
0279 typedef Array<Scalar, Rows, Cols, Options, MaxRows, MaxCols> ArrayType;
0280 typedef typename NumTraits<Scalar>::Real RealScalar;
0281 typedef Array<RealScalar, Rows, Cols, Options, MaxRows, MaxCols> Real;
0282 typedef typename NumTraits<Scalar>::NonInteger NonIntegerScalar;
0283 typedef Array<NonIntegerScalar, Rows, Cols, Options, MaxRows, MaxCols> NonInteger;
0284 typedef ArrayType & Nested;
0285 typedef typename NumTraits<Scalar>::Literal Literal;
0286
0287 enum {
0288 IsComplex = NumTraits<Scalar>::IsComplex,
0289 IsInteger = NumTraits<Scalar>::IsInteger,
0290 IsSigned = NumTraits<Scalar>::IsSigned,
0291 RequireInitialization = 1,
0292 ReadCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::ReadCost),
0293 AddCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::AddCost),
0294 MulCost = ArrayType::SizeAtCompileTime==Dynamic ? HugeCost : ArrayType::SizeAtCompileTime * int(NumTraits<Scalar>::MulCost)
0295 };
0296
0297 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0298 static inline RealScalar epsilon() { return NumTraits<RealScalar>::epsilon(); }
0299 EIGEN_DEVICE_FUNC EIGEN_CONSTEXPR
0300 static inline RealScalar dummy_precision() { return NumTraits<RealScalar>::dummy_precision(); }
0301
0302 EIGEN_CONSTEXPR
0303 static inline int digits10() { return NumTraits<Scalar>::digits10(); }
0304 };
0305
0306 template<> struct NumTraits<std::string>
0307 : GenericNumTraits<std::string>
0308 {
0309 enum {
0310 RequireInitialization = 1,
0311 ReadCost = HugeCost,
0312 AddCost = HugeCost,
0313 MulCost = HugeCost
0314 };
0315
0316 EIGEN_CONSTEXPR
0317 static inline int digits10() { return 0; }
0318
0319 private:
0320 static inline std::string epsilon();
0321 static inline std::string dummy_precision();
0322 static inline std::string lowest();
0323 static inline std::string highest();
0324 static inline std::string infinity();
0325 static inline std::string quiet_NaN();
0326 };
0327
0328
0329 template<> struct NumTraits<void> {};
0330
0331 template<> struct NumTraits<bool> : GenericNumTraits<bool> {};
0332
0333 }
0334
0335 #endif