vnl_numeric_traits.h
Go to the documentation of this file.
1 // This is core/vnl/vnl_numeric_traits.h
2 #ifndef vnl_numeric_traits_h_
3 #define vnl_numeric_traits_h_
4 //:
5 // \file
6 // \brief Templated zero/one/precision
7 // \author Andrew W. Fitzgibbon, Oxford RRG
8 // \date 04 Sep 96
9 //
10 // To allow templated numerical algorithms to determine appropriate
11 // values for zero, one, maxval, and types for double precision,
12 // maximum product etc.
13 //
14 // \verbatim
15 // Modifications
16 // 980212 AWF Initial version.
17 // AWF 010498 Moved to math
18 // LSB (Manchester) 23/3/01 Documentation tidied
19 // Peter Vanroose 14/7/01 vnl_rational added
20 // Peter Vanroose 14/10/01 vnl_rational moved to vnl_rational.h
21 // AWF 250202 Add const T specializations for the basic types.
22 // \endverbatim
23 //
24 //-----------------------------------------------------------------------------
25 
26 #include <complex>
27 #include <vxl_config.h> // for type vxl_uint_64
28 #ifdef _MSC_VER
29 # include <vcl_msvc_warnings.h>
30 #endif
31 #include "vnl/vnl_export.h"
32 
33 // this is an empty class template.
34 // only the specializations make sense.
35 #if !defined(_MSC_VER) || (_MSC_VER >= 1800 )
36 template <class T>
37 class VNL_EXPORT vnl_numeric_traits;
38 #else
39 // However, *some* compilers require the template to be defined
40 // under some circumstances...
41 // Since the non-specialized template doesn't make any sense, make
42 // sure that any types "accidently" derived from it will cause
43 // compiler errors.
44 class VNL_EXPORT vnl_numeric_traits_not_a_valid_type { };
45 template <class T>
46 class VNL_EXPORT vnl_numeric_traits
47 {
48  public:
49  //: Additive identity
50  static constexpr vnl_numeric_traits_not_a_valid_type zero;
51 
52  //: Multiplicative identity
53  static constexpr vnl_numeric_traits_not_a_valid_type one;
54 
55  //: Maximum value which this type can assume
56  static constexpr vnl_numeric_traits_not_a_valid_type maxval;
57 
58  //: Return value of abs()
59  typedef vnl_numeric_traits_not_a_valid_type abs_t;
60 
61  //: Name of a type twice as long as this one for accumulators and products.
62  typedef vnl_numeric_traits_not_a_valid_type double_t;
63 
64  //: Name of type which results from multiplying this type with a double
65  typedef vnl_numeric_traits_not_a_valid_type real_t;
66 };
67 #endif
68 
69 #ifndef NO_STD_BOOL
70 template <>
71 class VNL_EXPORT vnl_numeric_traits<bool>
72 {
73  public:
74  //: Additive identity
75  static constexpr bool zero = false;
76  //: Multiplicative identity
77  static constexpr bool one = true;
78  //: Maximum value which this type can assume
79  static constexpr bool maxval = true;
80  //: Return value of abs()
81  typedef unsigned int abs_t;
82  //: Name of a type twice as long as this one for accumulators and products.
83  typedef unsigned int double_t;
84  //: Name of type which results from multiplying this type with a double
85  typedef double real_t;
86 };
87 
88 template <>
89 class VNL_EXPORT vnl_numeric_traits<bool const> : public vnl_numeric_traits<bool> {};
90 #endif
91 
92 template<>
93 class VNL_EXPORT vnl_numeric_traits<char>
94 {
95  public:
96  //: Additive identity
97  static constexpr char zero = 0;
98  //: Multiplicative identity
99  static constexpr char one = 1;
100  //: Maximum value which this type can assume.
101  // It is 127 (and not 255) since "char" is not guaranteed to be unsigned.
102 #ifdef _MSC_VER
103 #ifdef _CHAR_UNSIGNED
104  static constexpr char maxval = 255;
105 #else
106  static constexpr char maxval = 127;
107 #endif
108 #else
109  static constexpr char maxval = (char(255)<char(0)?char(127):char(255));
110 #endif
111  //: Return value of abs()
112  typedef unsigned char abs_t;
113  //: Name of a type twice as long as this one for accumulators and products.
114  typedef short double_t;
115  //: Name of type which results from multiplying this type with a double
116  typedef double real_t;
117 };
118 
119 template<>
120 class VNL_EXPORT vnl_numeric_traits<char const> : public vnl_numeric_traits<char> {};
121 
122 template<>
123 class VNL_EXPORT vnl_numeric_traits<unsigned char>
124 {
125  public:
126  //: Additive identity
127  static constexpr unsigned char zero = 0;
128  //: Multiplicative identity
129  static constexpr unsigned char one = 1;
130  //: Maximum value which this type can assume
131  static constexpr unsigned char maxval = 255;
132  //: Return value of abs()
133  typedef unsigned char abs_t;
134  //: Name of a type twice as long as this one for accumulators and products.
135  typedef unsigned short double_t;
136  //: Name of type which results from multiplying this type with a double
137  typedef double real_t;
138 };
139 
140 template<>
141 class VNL_EXPORT vnl_numeric_traits<unsigned char const> : public vnl_numeric_traits<unsigned char> {};
142 
143 template<>
144 class VNL_EXPORT vnl_numeric_traits<signed char>
145 {
146  public:
147  //: Additive identity
148  static constexpr signed char zero = 0;
149  //: Multiplicative identity
150  static constexpr signed char one = 1;
151  //: Maximum value which this type can assume
152  static constexpr signed char maxval = 127;
153  //: Return value of abs()
154  typedef unsigned char abs_t;
155  //: Name of a type twice as long as this one for accumulators and products.
156  typedef signed short double_t;
157  //: Name of type which results from multiplying this type with a double
158  typedef double real_t;
159 };
160 
161 template<>
162 class VNL_EXPORT vnl_numeric_traits<signed char const> : public vnl_numeric_traits<signed char> {};
163 
164 template<>
165 class VNL_EXPORT vnl_numeric_traits<short>
166 {
167  public:
168  //: Additive identity
169  static constexpr short zero = 0;
170  //: Multiplicative identity
171  static constexpr short one = 1;
172  //: Maximum value which this type can assume
173  static constexpr short maxval = 0x7fff; // = 0x7fff;
174  //: Return value of abs()
175  typedef unsigned short abs_t;
176  //: Name of a type twice as long as this one for accumulators and products.
177  typedef int double_t;
178  //: Name of type which results from multiplying this type with a double
179  typedef double real_t;
180 };
181 
182 template<>
183 class VNL_EXPORT vnl_numeric_traits<short const> : public vnl_numeric_traits<short> {};
184 
185 template<>
186 class VNL_EXPORT vnl_numeric_traits<unsigned short>
187 {
188  public:
189  //: Additive identity
190  static constexpr unsigned short zero = 0;
191  //: Multiplicative identity
192  static constexpr unsigned short one = 1;
193  //: Maximum value which this type can assume
194  static constexpr unsigned short maxval = 0xffff; // = 0xffff;
195  //: Return value of abs()
196  typedef unsigned short abs_t;
197  //: Name of a type twice as long as this one for accumulators and products.
198  typedef unsigned int double_t;
199  //: Name of type which results from multiplying this type with a double
200  typedef double real_t;
201 };
202 
203 template<>
204 class VNL_EXPORT vnl_numeric_traits<unsigned short const> : public vnl_numeric_traits<unsigned short> {};
205 
206 template<>
207 class VNL_EXPORT vnl_numeric_traits<int>
208 {
209  public:
210  //: Additive identity
211  static constexpr int zero = 0;
212  //: Multiplicative identity
213  static constexpr int one = 1;
214  //: Maximum value which this type can assume
215  static constexpr int maxval = 0x7fffffff; // = 0x7fffffff;
216  //: Return value of abs()
217  typedef unsigned int abs_t;
218  //: Name of a type twice as long as this one for accumulators and products.
219  typedef long double_t;
220  //: Name of type which results from multiplying this type with a double
221  typedef double real_t;
222 };
223 
224 template<>
225 class VNL_EXPORT vnl_numeric_traits<int const> : public vnl_numeric_traits<int> {};
226 
227 template<>
228 class VNL_EXPORT vnl_numeric_traits<unsigned int>
229 {
230  public:
231  //: Additive identity
232  static constexpr unsigned int zero = 0;
233  //: Multiplicative identity
234  static constexpr unsigned int one = 1;
235  //: Maximum value which this type can assume
236  static constexpr unsigned int maxval = 0xffffffff; // = 0xffffffff;
237  //: Return value of abs()
238  typedef unsigned int abs_t;
239  //: Name of a type twice as long as this one for accumulators and products.
240  typedef unsigned long double_t;
241  //: Name of type which results from multiplying this type with a double
242  typedef double real_t;
243 };
244 
245 template<>
246 class VNL_EXPORT vnl_numeric_traits<unsigned int const> : public vnl_numeric_traits<unsigned int> {};
247 
248 template<>
249 class VNL_EXPORT vnl_numeric_traits<long>
250 {
251  public:
252  //: Additive identity
253  static constexpr long zero = 0;
254  //: Multiplicative identity
255  static constexpr long one = 1;
256  //: Maximum value which this type can assume
257  static constexpr long maxval = sizeof(long)==8?(vxl_uint_64)(-1)/2:0x7fffffffL; // = 0x7fffffffL or 0x7fffffffffffffffL;
258  //: Return value of abs()
259  typedef unsigned long abs_t;
260  //: Name of a type twice as long as this one for accumulators and products.
261  typedef vxl_sint_64 double_t;
262  //: Name of type which results from multiplying this type with a double
263  typedef double real_t;
264 };
265 
266 template<>
267 class VNL_EXPORT vnl_numeric_traits<long const> : public vnl_numeric_traits<long > {};
268 
269 template<>
270 class VNL_EXPORT vnl_numeric_traits<unsigned long>
271 {
272  public:
273  //: Additive identity
274  static constexpr unsigned long zero = 0;
275  //: Multiplicative identity
276  static constexpr unsigned long one = 1;
277  //: Maximum value which this type can assume
278  static constexpr unsigned long maxval = sizeof(unsigned long)==8?((vxl_uint_64)(-1)):0xffffffffL ;
279  // = 0xffffffffL or 0xffffffffffffffffL;
280  //: Return value of abs()
281  typedef unsigned long abs_t;
282  //: Name of a type twice as long as this one for accumulators and products.
283  typedef vxl_uint_64 double_t;
284  //: Name of type which results from multiplying this type with a double
285  typedef double real_t;
286 };
287 
288 template<>
289 class VNL_EXPORT vnl_numeric_traits<unsigned long const> : public vnl_numeric_traits<unsigned long> {};
290 
291 
292 //long long - target type will have width of at least 64 bits. (since C++11)
293 template<>
294 class VNL_EXPORT vnl_numeric_traits<long long>
295 {
296  public:
297  //: Additive identity
298  static constexpr long long zero = 0;
299  //: Multiplicative identity
300  static constexpr long long one = 1;
301  //: Maximum value which this type can assume
302  static constexpr long long maxval = sizeof(long long)==8?((vxl_uint_64)(-1))/2:0x7fffffffL ;
303  //: Return value of abs()
304  typedef unsigned long long abs_t;
305  //: Name of a type twice as long as this one for accumulators and products.
306  typedef long long double_t;
307  //: Name of type which results from multiplying this type with a double
308  typedef double real_t;
309 };
310 
311 template<>
312 class VNL_EXPORT vnl_numeric_traits<long long const> : public vnl_numeric_traits<long long> {};
313 
314 template<>
315 class VNL_EXPORT vnl_numeric_traits<unsigned long long>
316 {
317  public:
318  //: Additive identity
319  static constexpr unsigned long long zero = 0;
320  //: Multiplicative identity
321  static constexpr unsigned long long one = 1;
322  //: Maximum value which this type can assume
323  static constexpr unsigned long long maxval = sizeof(unsigned long long)==8?(vxl_uint_64)(-1):0xffffffffL;
324  //: Return value of abs()
325  typedef unsigned long long abs_t;
326  //: Name of a type twice as long as this one for accumulators and products.
327  typedef unsigned long long double_t;
328  //: Name of type which results from multiplying this type with a double
329  typedef double real_t;
330 };
331 
332 template<>
333 class VNL_EXPORT vnl_numeric_traits<unsigned long long const> : public vnl_numeric_traits<unsigned long long> {};
334 
335 
336 template<>
337 class VNL_EXPORT vnl_numeric_traits<float>
338 {
339  public:
340  //: Additive identity
341  static constexpr float zero = 0.0F;
342  //: Multiplicative identity
343  static constexpr float one = 1.0F;
344  //: Maximum value which this type can assume
345  static constexpr float maxval = 3.40282346638528860e+38F;
346  //: Return value of abs()
347  typedef float abs_t;
348  //: Name of a type twice as long as this one for accumulators and products.
349  typedef double double_t;
350  //: Name of type which results from multiplying this type with a double
351  typedef double real_t;
352 };
353 
354 template<>
355 class VNL_EXPORT vnl_numeric_traits<float const> : public vnl_numeric_traits<float> {};
356 
357 template<>
358 class VNL_EXPORT vnl_numeric_traits<double>
359 {
360  public:
361  //: Additive identity
362  static constexpr double zero = 0.0;
363  //: Multiplicative identity
364  static constexpr double one = 1.0;
365  //: Maximum value which this type can assume
366  static constexpr double maxval = 1.7976931348623157E+308;
367  //: Return value of abs()
368  typedef double abs_t;
369  //: Name of a type twice as long as this one for accumulators and products.
370  typedef long double double_t;
371  //: Name of type which results from multiplying this type with a double
372  typedef double real_t;
373 };
374 
375 template<>
376 class VNL_EXPORT vnl_numeric_traits<double const> : public vnl_numeric_traits<double> {};
377 
378 template<>
379 class VNL_EXPORT vnl_numeric_traits<long double>
380 {
381  public:
382  //: Additive identity
383  static constexpr long double zero = 0.0;
384  //: Multiplicative identity
385  static constexpr long double one = 1.0;
386  //: Maximum value which this type can assume
387  static constexpr long double maxval = 1.7976931348623157E+308;
388  //: Return value of abs()
389  typedef long double abs_t;
390  //: Name of a type twice as long as this one for accumulators and products.
391  typedef long double double_t; // ahem
392  //: Name of type which results from multiplying this type with a double
393  typedef long double real_t;
394 };
395 
396 template<>
397 class VNL_EXPORT vnl_numeric_traits<long double const> : public vnl_numeric_traits<long double> {};
398 
399 template<>
400 class VNL_EXPORT vnl_numeric_traits< std::complex<float> >
401 {
402  public:
403  //: Additive identity
404  static const std::complex<float> zero;
405  //: Multiplicative identity
406  static const std::complex<float> one;
407  // Maximum value which this type can assume; makes no sense for this type
408  //static const std::complex<float> maxval;
409 
410  //: Return value of abs()
411  typedef float abs_t;
412  //: Name of a type twice as long as this one for accumulators and products.
413  typedef std::complex<vnl_numeric_traits<float>::double_t> double_t;
414  //: Name of type which results from multiplying this type with a double
415  typedef std::complex<float> real_t;
416 };
417 
418 template<>
419 class VNL_EXPORT vnl_numeric_traits<std::complex<float> const> : public vnl_numeric_traits<std::complex<float> > {};
420 
421 template<>
422 class VNL_EXPORT vnl_numeric_traits< std::complex<double> >
423 {
424  public:
425  //: Additive identity
426  static const std::complex<double> zero;
427  //: Multiplicative identity
428  static const std::complex<double> one;
429  // Maximum value which this type can assume; makes no sense for this type
430  //static const std::complex<double> maxval;
431 
432  //: Return value of abs()
433  typedef double abs_t;
434  //: Name of a type twice as long as this one for accumulators and products.
435  typedef std::complex<vnl_numeric_traits<double>::double_t> double_t;
436  //: Name of type which results from multiplying this type with a double
437  typedef std::complex<double> real_t;
438 };
439 
440 template<>
441 class VNL_EXPORT vnl_numeric_traits<std::complex<double> const> : public vnl_numeric_traits<std::complex<double> > {};
442 
443 template<>
444 class VNL_EXPORT vnl_numeric_traits< std::complex<long double> >
445 {
446  public:
447  //: Additive identity
448  static const std::complex<long double> zero;
449  //: Multiplicative identity
450  static const std::complex<long double> one;
451  // Maximum value which this type can assume; makes no sense for this type
452  //static const std::complex<long double> maxval;
453 
454  //: Return value of abs()
455  typedef long double abs_t;
456  //: Name of a type twice as long as this one for accumulators and products.
457  typedef std::complex<vnl_numeric_traits<long double>::double_t> double_t;
458  //: Name of type which results from multiplying this type with a double
459  typedef std::complex<long double> real_t;
460 };
461 
462 template<>
463 class VNL_EXPORT vnl_numeric_traits<std::complex<long double> const> : public vnl_numeric_traits<std::complex<long double> > {};
464 
465 #endif // vnl_numeric_traits_h_
double real_t
Name of type which results from multiplying this type with a double.
long double double_t
Name of a type twice as long as this one for accumulators and products.
unsigned long abs_t
Return value of abs().
unsigned short abs_t
Return value of abs().
unsigned long long abs_t
Return value of abs().
unsigned char abs_t
Return value of abs().
unsigned int abs_t
Return value of abs().
double real_t
Name of type which results from multiplying this type with a double.
double real_t
Name of type which results from multiplying this type with a double.
unsigned int double_t
Name of a type twice as long as this one for accumulators and products.
float abs_t
Return value of abs().
double real_t
Name of type which results from multiplying this type with a double.
double real_t
Name of type which results from multiplying this type with a double.
unsigned short double_t
Name of a type twice as long as this one for accumulators and products.
std::complex< vnl_numeric_traits< double >::double_t > double_t
Name of a type twice as long as this one for accumulators and products.
vxl_uint_64 double_t
Name of a type twice as long as this one for accumulators and products.
double real_t
Name of type which results from multiplying this type with a double.
long double_t
Name of a type twice as long as this one for accumulators and products.
double real_t
Name of type which results from multiplying this type with a double.
unsigned short abs_t
Return value of abs().
unsigned int abs_t
Return value of abs().
signed short double_t
Name of a type twice as long as this one for accumulators and products.
long double abs_t
Return value of abs().
std::complex< long double > real_t
Name of type which results from multiplying this type with a double.
int double_t
Name of a type twice as long as this one for accumulators and products.
unsigned long long double_t
Name of a type twice as long as this one for accumulators and products.
unsigned long abs_t
Return value of abs().
long long double_t
Name of a type twice as long as this one for accumulators and products.
double real_t
Name of type which results from multiplying this type with a double.
double real_t
Name of type which results from multiplying this type with a double.
unsigned int abs_t
Return value of abs().
double abs_t
Return value of abs().
unsigned int double_t
Name of a type twice as long as this one for accumulators and products.
double real_t
Name of type which results from multiplying this type with a double.
double real_t
Name of type which results from multiplying this type with a double.
unsigned long double_t
Name of a type twice as long as this one for accumulators and products.
short double_t
Name of a type twice as long as this one for accumulators and products.
vxl_sint_64 double_t
Name of a type twice as long as this one for accumulators and products.
double real_t
Name of type which results from multiplying this type with a double.
unsigned char abs_t
Return value of abs().
long double double_t
Name of a type twice as long as this one for accumulators and products.
std::complex< float > real_t
Name of type which results from multiplying this type with a double.
unsigned long long abs_t
Return value of abs().
double double_t
Name of a type twice as long as this one for accumulators and products.
std::complex< vnl_numeric_traits< long double >::double_t > double_t
Name of a type twice as long as this one for accumulators and products.
double real_t
Name of type which results from multiplying this type with a double.
long double real_t
Name of type which results from multiplying this type with a double.
std::complex< vnl_numeric_traits< float >::double_t > double_t
Name of a type twice as long as this one for accumulators and products.
unsigned char abs_t
Return value of abs().
std::complex< double > real_t
Name of type which results from multiplying this type with a double.
double real_t
Name of type which results from multiplying this type with a double.