Library of Assembled Shared Sources
num_traits.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2024 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_TRAITS_H
45#define LASS_GUARDIAN_OF_INCLUSION_NUM_TRAITS_H
46
47#include "num_common.h"
48
49#if LASS_HAVE_STD_ISNAN
50# include <cmath>
51#elif LASS_NUM_NUM_TRAITS_HAVE_MSVC_FLOAT_H
52# include <float.h>
53#else
54# include <math.h>
55#endif
56
57namespace lass
58{
59namespace num
60{
61
62template<class C>
63struct NumTraits
64{
65 //typedef C selfType; /**< our own type */
66 //typedef C baseType; /**< is the base type where selfType is based on */
67 //typedef C intervalType; /**< type of the support, useful for distributions */
68
69 //enum
70 //{
71 // isDistribution = 0, /**< true for distribution like types */
72 // isIntegral = 0, /**< true for integral types, ie supporting ++ and -- fi */
73 // isNative = 0, /**< true for the native types, ie char, int , ... */
74 // isSigned = 0, /**< true for signed types */
75 // hasInfinity = 0, /**< true for types having infinity */
76 // hasNaN = 0, /**< true for types have the concept of Not a Number */
77 // isFloatingPoint = 0 /**< true for floating point types */
78 //};
79
80 //static const size_t memorySize; /**< memory footprint */
81 //static const size_t mantisseSize; /**< number of bits used for mantisse, useful for error analysis */
82 //static const std::string name() { return "unknown"; } /**< name of the selfType */
83
84 //static const C one; /**< definition of one */
85 //static const C zero; /**< definition of zero */
86
87 /* some constants */
88 //static const C sNaN; /**< signaling Nan, see http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html */
89 //static const C qNaN; /**< quiet NaN, see http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html */
90 //static const C epsilon; /**< the smallest positive value such that one + epsilon != one*/
91 //static const C infinity; /**< infinity */
92 //static const C min; /**< the smallest value representable */
93 //static const C max; /**< the largest positive value */
94 //static const C minStrictPositive; /**< the smallest strictly positive value */
95 //static const C pi;
96 //static const C e;
97 //static const C sqrt2;
98 //static const C sqrtPi;
99};
100
101
102template<class C> inline
103bool isNaN( C iV )
104{
105 return iV != iV;
106}
107
108#if LASS_HAVE_STD_ISNAN
109inline bool isNaN( float iV ) { return std::isnan(iV); }
110inline bool isNaN( double iV ) { return std::isnan(iV); }
111inline bool isNaN( long double iV ) { return std::isnan(iV); }
112#elif LASS_NUM_NUM_TRAITS_HAVE_MSVC_FLOAT_H
113inline bool isNaN( float iV ) { return _isnan(static_cast<double>(iV)) != 0; }
114inline bool isNaN( double iV ) { return _isnan(iV) != 0; }
115inline bool isNaN( long double iV ) { return _isnan(static_cast<double>(iV)) != 0; }
116#else
117inline bool isNaN( float iV ) { return isnan(iV) != 0; }
118inline bool isNaN( double iV ) { return isnan(iV) != 0; }
119inline bool isNaN( long double iV ) { return isnan(static_cast<double>(iV)) != 0; }
120#endif
121
122template<class C> inline
123bool isNaN( std::complex<C> iV )
124{
125 return isNaN(iV.real()) || isNaN(iV.imag());
126}
127
128
129/** return true if iV equals minus or plus Infinity
130 */
131template<class C>
132bool isInf( const C& iV )
133{
134 return NumTraits<C>::hasInfinity &&
135 (iV == NumTraits<C>::infinity || iV == -NumTraits<C>::infinity);
136}
137
138inline bool isInf(float iV) { return std::isinf(iV); }
139inline bool isInf(double iV) { return std::isinf(iV); }
140inline bool isInf(long double iV) { return std::isinf(iV); }
141
142#define LASS_NUM_PI 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067
143#define LASS_NUM_E 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427
144#define LASS_NUM_SQRT_2 1.414213562373095048801688724209698078569671875376948073176679737990732478462107038850387534327641572
145#define LASS_NUM_SQRT_PI 1.772453850905516027298167483341145
146
147#define LASS_NUM_DECLARE_FLOATING_TRAITS( ttype, tname ) \
148template<> \
149struct LASS_DLL NumTraits<ttype>\
150{\
151 typedef ttype selfType;\
152 typedef ttype baseType;\
153 typedef ttype intervalType;\
154 enum\
155 {\
156 isDistribution = false,\
157 isIntegral = false,\
158 isNative = true,\
159 isSigned = true,\
160 hasInfinity = true,\
161 hasNaN = true,\
162 isFloatingPoint = true\
163 };\
164 static constexpr size_t memorySize = sizeof(ttype);\
165 static constexpr size_t mantisseSize = std::numeric_limits<ttype>::digits;\
166 static const std::string name() { return tname ; }\
167 static constexpr ttype one = static_cast<ttype>(1);\
168 static constexpr ttype zero = static_cast<ttype>(0);\
169 static constexpr ttype infinity = std::numeric_limits<ttype>::infinity();\
170 static constexpr ttype qNaN = std::numeric_limits<ttype>::quiet_NaN();\
171 static constexpr ttype sNaN = std::numeric_limits<ttype>::signaling_NaN();\
172 static constexpr ttype epsilon= std::numeric_limits<ttype>::epsilon();\
173 static constexpr ttype min = -std::numeric_limits<ttype>::max();\
174 static constexpr ttype max = std::numeric_limits<ttype>::max();\
175 static constexpr ttype minStrictPositive = std::numeric_limits<ttype>::min();\
176 static constexpr ttype pi = static_cast<ttype>(LASS_NUM_PI);\
177 static constexpr ttype e = static_cast<ttype>(LASS_NUM_E);\
178 static constexpr ttype sqrt2 = static_cast<ttype>(LASS_NUM_SQRT_2);\
179 static constexpr ttype sqrtPi = static_cast<ttype>(LASS_NUM_SQRT_PI);\
180\
181 static constexpr ttype gamma(unsigned n) { return (static_cast<ttype>(n) * epsilon) / (2 - static_cast<ttype>(n) * epsilon); }\
182};
183
184#define LASS_NUM_DECLARE_COMPLEX_FLOATING_TRAITS( ttype, tname ) \
185template<> \
186struct LASS_DLL NumTraits< ttype >\
187{\
188 typedef ttype selfType;\
189 typedef ttype::value_type baseType;\
190 typedef ttype intervalType;\
191 enum\
192 {\
193 isDistribution = false,\
194 isIntegral = false,\
195 isNative = false,\
196 isSigned = true,\
197 hasInfinity = false,\
198 hasNaN = false,\
199 isFloatingPoint = NumTraits< baseType >::isFloatingPoint\
200 };\
201 static constexpr size_t memorySize = sizeof(ttype);\
202 static constexpr size_t mantisseSize = NumTraits< baseType >::mantisseSize;\
203 static const std::string name() { return tname ; }\
204 static constexpr ttype one = ttype( NumTraits< baseType >::one );\
205 static constexpr ttype zero = ttype( NumTraits< baseType >::zero );\
206 static constexpr ttype pi = ttype( NumTraits< baseType >::pi );\
207 static constexpr ttype e = ttype( NumTraits< baseType >::e );\
208 static constexpr ttype sqrt2 = ttype( NumTraits< baseType >::sqrt2 );\
209 static constexpr ttype sqrtPi = ttype( NumTraits< baseType >::sqrtPi );\
210};
211
212LASS_NUM_DECLARE_FLOATING_TRAITS( float, "float" )
213LASS_NUM_DECLARE_COMPLEX_FLOATING_TRAITS( std::complex< float > , "complex<float>" )
214
215LASS_NUM_DECLARE_FLOATING_TRAITS( double , "double" )
216LASS_NUM_DECLARE_COMPLEX_FLOATING_TRAITS( std::complex< double > , "complex<double>" )
217
218LASS_NUM_DECLARE_FLOATING_TRAITS( long double, "long double" )
219LASS_NUM_DECLARE_COMPLEX_FLOATING_TRAITS( std::complex< long double > , "complex<long double>" )
220
221template<>
222struct LASS_DLL NumTraits<char>
223{
224 typedef char selfType;
225 typedef char baseType;
226 typedef float intervalType;
227 typedef signed char signedType;
228 typedef unsigned char unsignedType;
229 enum
230 {
231 isDistribution = 0,
232 isIntegral = 1,
233 isNative = 1,
234#ifdef LASS_CHAR_IS_SIGNED
235 isSigned = 1,
236#else
237 isSigned = 0,
238#endif
239 hasInfinity = 0,
240 hasNaN = 0,
241 isFloatingPoint = 0
242 };
243 static constexpr size_t memorySize = sizeof(char);
244 static constexpr size_t mantisseSize = 0;
245 static const std::string name() { return LASS_STRINGIFY(char); }
246 static constexpr selfType one = 1;
247 static constexpr selfType zero = 0;
248 static constexpr selfType epsilon = 1;
249 static constexpr selfType min = std::numeric_limits<char>::min();
250 static constexpr selfType max = std::numeric_limits<char>::max();
251 static constexpr selfType minStrictPositive = 1;
252};
253
254#define LASS_NUM_DECLARE_INTEGRAL_TRAITS( sign, type, is_signed ) \
255template<> \
256struct LASS_DLL NumTraits<sign type> \
257{\
258 typedef sign type selfType;\
259 typedef sign type baseType;\
260 typedef float intervalType;\
261 typedef signed type signedType;\
262 typedef unsigned type unsignedType;\
263 enum\
264 {\
265 isDistribution = 0,\
266 isIntegral = 1,\
267 isNative = 1,\
268 isSigned = is_signed,\
269 hasInfinity = 0,\
270 hasNaN = 0,\
271 isFloatingPoint = 0\
272 };\
273 static constexpr size_t memorySize = sizeof(sign type);\
274 static constexpr size_t mantisseSize = 0;\
275 static const std::string name() { return LASS_STRINGIFY(sign type); }\
276 static constexpr selfType one = 1;\
277 static constexpr selfType zero = 0;\
278 static constexpr selfType epsilon = 1;\
279 static constexpr selfType min = std::numeric_limits<sign type>::min();\
280 static constexpr selfType max = std::numeric_limits<sign type>::max();\
281 static constexpr selfType minStrictPositive = 1;\
282};
283
284LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, char, 1 )
285LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, char, 0 )
286LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, short, 1 )
287LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, short, 0 )
288LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, int, 1 )
289LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, int, 0 )
290LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, long, 1 )
291LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, long, 0 )
292#if LASS_HAVE_LONG_LONG
293 LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, long long, 1 )
294 LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, long long, 0 )
295#elif LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
296 LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, __int64, 1 )
297 LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, __int64, 0 )
298#endif
299
300}
301
302}
303
304// we didn't include basic_ops.h in num_common.h when including num_traits.h,
305// because basic_ops.h needs NumTraits to be defined first. So now it's time
306// to include basic_ops.h anyway [Bramz]
307//
308#include "basic_ops.h"
309
310#endif
#define LASS_DLL
DLL interface: import or export symbols?
numeric types and traits.
Definition basic_ops.h:70
bool isInf(const C &iV)
return true if iV equals minus or plus Infinity
Definition num_traits.h:132
Library for Assembled Shared Sources.
Definition config.h:53