core/vnl/vnl_numeric_traits.h
Go to the documentation of this file.
00001 // This is core/vnl/vnl_numeric_traits.h
00002 #ifndef vnl_numeric_traits_h_
00003 #define vnl_numeric_traits_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Templated zero/one/precision
00010 // \author Andrew W. Fitzgibbon, Oxford RRG
00011 // \date   04 Sep 96
00012 //
00013 //  To allow templated numerical algorithms to determine appropriate
00014 //  values for zero, one, maxval, and types for double precision,
00015 //  maximum product etc.
00016 //
00017 // \verbatim
00018 //  Modifications
00019 //   980212           AWF      Initial version.
00020 //   AWF              010498   Moved to math
00021 //   LSB (Manchester) 23/3/01  Documentation tidied
00022 //   Peter Vanroose   14/7/01  vnl_rational added
00023 //   Peter Vanroose   14/10/01 vnl_rational moved to vnl_rational.h
00024 //   AWF              250202   Add const T specializations for the basic types.
00025 // \endverbatim
00026 //
00027 //-----------------------------------------------------------------------------
00028 
00029 #include <vxl_config.h> // for type vxl_uint_64
00030 #include <vcl_complex.h>
00031 
00032 // this is an empty class template.
00033 // only the specializations make sense.
00034 #if !defined(VCL_VC)
00035 template <class T>
00036 class vnl_numeric_traits;
00037 #else
00038 // However, *some* compilers require the template to be defined
00039 // under some circumstances...
00040 // Since the non-specialized template doesn't make any sense, make
00041 // sure that any types "accidently" derived from it will cause
00042 // compiler errors.
00043 class vnl_numeric_traits_not_a_valid_type { };
00044 template <class T>
00045 class vnl_numeric_traits
00046 {
00047  public:
00048   //: Additive identity
00049   static const vnl_numeric_traits_not_a_valid_type zero;
00050 
00051   //: Multiplicative identity
00052   static const vnl_numeric_traits_not_a_valid_type one;
00053 
00054   //: Maximum value which this type can assume
00055   static const vnl_numeric_traits_not_a_valid_type maxval;
00056 
00057   //: Return value of abs()
00058   typedef vnl_numeric_traits_not_a_valid_type abs_t;
00059 
00060   //: Name of a type twice as long as this one for accumulators and products.
00061   typedef vnl_numeric_traits_not_a_valid_type double_t;
00062 
00063   //: Name of type which results from multiplying this type with a double
00064   typedef vnl_numeric_traits_not_a_valid_type real_t;
00065 };
00066 #endif
00067 
00068 #ifndef NO_STD_BOOL
00069 VCL_DEFINE_SPECIALIZATION
00070 class vnl_numeric_traits<bool>
00071 {
00072  public:
00073   //: Additive identity
00074   static const bool zero VCL_STATIC_CONST_INIT_INT_DECL(false);
00075   //: Multiplicative identity
00076   static const bool one VCL_STATIC_CONST_INIT_INT_DECL(true);
00077   //: Maximum value which this type can assume
00078   static const bool maxval VCL_STATIC_CONST_INIT_INT_DECL(true);
00079   //: Return value of abs()
00080   typedef unsigned int abs_t;
00081   //: Name of a type twice as long as this one for accumulators and products.
00082   typedef unsigned int double_t;
00083   //: Name of type which results from multiplying this type with a double
00084   typedef double real_t;
00085 };
00086 
00087 #if !VCL_CANNOT_SPECIALIZE_CV
00088 VCL_DEFINE_SPECIALIZATION
00089 class vnl_numeric_traits<bool const> : public vnl_numeric_traits<bool> {};
00090 #endif
00091 #endif
00092 
00093 VCL_DEFINE_SPECIALIZATION
00094 class vnl_numeric_traits<char>
00095 {
00096  public:
00097   //: Additive identity
00098   static const char zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00099   //: Multiplicative identity
00100   static const char one VCL_STATIC_CONST_INIT_INT_DECL(1);
00101   //: Maximum value which this type can assume.
00102   //  It is 127 (and not 255) since "char" is not guaranteed to be unsigned.
00103 #ifdef _MSC_VER
00104 #ifdef _CHAR_UNSIGNED
00105   static const char maxval VCL_STATIC_CONST_INIT_INT_DECL(255);
00106 #else
00107   static const char maxval VCL_STATIC_CONST_INIT_INT_DECL(127);
00108 #endif
00109 #else
00110   static const char maxval VCL_STATIC_CONST_INIT_INT_DECL(char(255)<0?127:255);
00111 #endif
00112   //: Return value of abs()
00113   typedef unsigned char abs_t;
00114   //: Name of a type twice as long as this one for accumulators and products.
00115   typedef short double_t;
00116   //: Name of type which results from multiplying this type with a double
00117   typedef double real_t;
00118 };
00119 
00120 #if !VCL_CANNOT_SPECIALIZE_CV
00121 VCL_DEFINE_SPECIALIZATION
00122 class vnl_numeric_traits<char const> : public vnl_numeric_traits<char> {};
00123 #endif
00124 
00125 VCL_DEFINE_SPECIALIZATION
00126 class vnl_numeric_traits<unsigned char>
00127 {
00128  public:
00129   //: Additive identity
00130   static const unsigned char zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00131   //: Multiplicative identity
00132   static const unsigned char one VCL_STATIC_CONST_INIT_INT_DECL(1);
00133   //: Maximum value which this type can assume
00134   static const unsigned char maxval VCL_STATIC_CONST_INIT_INT_DECL(255);
00135   //: Return value of abs()
00136   typedef unsigned char abs_t;
00137   //: Name of a type twice as long as this one for accumulators and products.
00138   typedef unsigned short double_t;
00139   //: Name of type which results from multiplying this type with a double
00140   typedef double real_t;
00141 };
00142 
00143 #if !VCL_CANNOT_SPECIALIZE_CV
00144 VCL_DEFINE_SPECIALIZATION
00145 class vnl_numeric_traits<unsigned char const> : public vnl_numeric_traits<unsigned char> {};
00146 #endif
00147 
00148 VCL_DEFINE_SPECIALIZATION
00149 class vnl_numeric_traits<signed char>
00150 {
00151  public:
00152   //: Additive identity
00153   static const signed char zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00154   //: Multiplicative identity
00155   static const signed char one VCL_STATIC_CONST_INIT_INT_DECL(1);
00156   //: Maximum value which this type can assume
00157   static const signed char maxval VCL_STATIC_CONST_INIT_INT_DECL(127);
00158   //: Return value of abs()
00159   typedef unsigned char abs_t;
00160   //: Name of a type twice as long as this one for accumulators and products.
00161   typedef signed short double_t;
00162   //: Name of type which results from multiplying this type with a double
00163   typedef double real_t;
00164 };
00165 
00166 #if !VCL_CANNOT_SPECIALIZE_CV
00167 VCL_DEFINE_SPECIALIZATION
00168 class vnl_numeric_traits<signed char const> : public vnl_numeric_traits<signed char> {};
00169 #endif
00170 
00171 VCL_DEFINE_SPECIALIZATION
00172 class vnl_numeric_traits<short>
00173 {
00174  public:
00175   //: Additive identity
00176   static const short zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00177   //: Multiplicative identity
00178   static const short one VCL_STATIC_CONST_INIT_INT_DECL(1);
00179   //: Maximum value which this type can assume
00180   static const short maxval; // = 0x7fff;
00181   //: Return value of abs()
00182   typedef unsigned short abs_t;
00183   //: Name of a type twice as long as this one for accumulators and products.
00184   typedef int double_t;
00185   //: Name of type which results from multiplying this type with a double
00186   typedef double real_t;
00187 };
00188 
00189 #if !VCL_CANNOT_SPECIALIZE_CV
00190 VCL_DEFINE_SPECIALIZATION
00191 class vnl_numeric_traits<short const> : public vnl_numeric_traits<short> {};
00192 #endif
00193 
00194 VCL_DEFINE_SPECIALIZATION
00195 class vnl_numeric_traits<unsigned short>
00196 {
00197  public:
00198   //: Additive identity
00199   static const unsigned short zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00200   //: Multiplicative identity
00201   static const unsigned short one VCL_STATIC_CONST_INIT_INT_DECL(1);
00202   //: Maximum value which this type can assume
00203   static const unsigned short maxval; // = 0xffff;
00204   //: Return value of abs()
00205   typedef unsigned short abs_t;
00206   //: Name of a type twice as long as this one for accumulators and products.
00207   typedef unsigned int double_t;
00208   //: Name of type which results from multiplying this type with a double
00209   typedef double real_t;
00210 };
00211 
00212 #if !VCL_CANNOT_SPECIALIZE_CV
00213 VCL_DEFINE_SPECIALIZATION
00214 class vnl_numeric_traits<unsigned short const> : public vnl_numeric_traits<unsigned short> {};
00215 #endif
00216 
00217 VCL_DEFINE_SPECIALIZATION
00218 class vnl_numeric_traits<int>
00219 {
00220  public:
00221   //: Additive identity
00222   static const int zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00223   //: Multiplicative identity
00224   static const int one VCL_STATIC_CONST_INIT_INT_DECL(1);
00225   //: Maximum value which this type can assume
00226   static const int maxval; // = 0x7fffffff;
00227   //: Return value of abs()
00228   typedef unsigned int abs_t;
00229   //: Name of a type twice as long as this one for accumulators and products.
00230   typedef long double_t;
00231   //: Name of type which results from multiplying this type with a double
00232   typedef double real_t;
00233 };
00234 
00235 #if !VCL_CANNOT_SPECIALIZE_CV
00236 VCL_DEFINE_SPECIALIZATION
00237 class vnl_numeric_traits<int const> : public vnl_numeric_traits<int> {};
00238 #endif
00239 
00240 VCL_DEFINE_SPECIALIZATION
00241 class vnl_numeric_traits<unsigned int>
00242 {
00243  public:
00244   //: Additive identity
00245   static const unsigned int zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00246   //: Multiplicative identity
00247   static const unsigned int one VCL_STATIC_CONST_INIT_INT_DECL(1);
00248   //: Maximum value which this type can assume
00249   static const unsigned int maxval; // = 0xffffffff;
00250   //: Return value of abs()
00251   typedef unsigned int abs_t;
00252   //: Name of a type twice as long as this one for accumulators and products.
00253   typedef unsigned long double_t;
00254   //: Name of type which results from multiplying this type with a double
00255   typedef double real_t;
00256 };
00257 
00258 #if !VCL_CANNOT_SPECIALIZE_CV
00259 VCL_DEFINE_SPECIALIZATION
00260 class vnl_numeric_traits<unsigned int const> : public vnl_numeric_traits<unsigned int> {};
00261 #endif
00262 
00263 VCL_DEFINE_SPECIALIZATION
00264 class vnl_numeric_traits<long>
00265 {
00266  public:
00267   //: Additive identity
00268   static const long zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00269   //: Multiplicative identity
00270   static const long one VCL_STATIC_CONST_INIT_INT_DECL(1);
00271   //: Maximum value which this type can assume
00272   static const long maxval; // = 0x7fffffffL or 0x7fffffffffffffffL;
00273   //: Return value of abs()
00274   typedef unsigned long abs_t;
00275   //: Name of a type twice as long as this one for accumulators and products.
00276   typedef vxl_sint_64 double_t;
00277   //: Name of type which results from multiplying this type with a double
00278   typedef double real_t;
00279 };
00280 
00281 #if !VCL_CANNOT_SPECIALIZE_CV
00282 VCL_DEFINE_SPECIALIZATION
00283 class vnl_numeric_traits<long const> : public vnl_numeric_traits<long > {};
00284 #endif
00285 
00286 VCL_DEFINE_SPECIALIZATION
00287 class vnl_numeric_traits<unsigned long>
00288 {
00289  public:
00290   //: Additive identity
00291   static const unsigned long zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00292   //: Multiplicative identity
00293   static const unsigned long one VCL_STATIC_CONST_INIT_INT_DECL(1);
00294   //: Maximum value which this type can assume
00295   static const unsigned long maxval; // = 0xffffffffL or 0xffffffffffffffffL;
00296   //: Return value of abs()
00297   typedef unsigned long abs_t;
00298   //: Name of a type twice as long as this one for accumulators and products.
00299   typedef vxl_uint_64 double_t;
00300   //: Name of type which results from multiplying this type with a double
00301   typedef double real_t;
00302 };
00303 
00304 #if !VCL_CANNOT_SPECIALIZE_CV
00305 VCL_DEFINE_SPECIALIZATION
00306 class vnl_numeric_traits<unsigned long const> : public vnl_numeric_traits<unsigned long> {};
00307 #endif
00308 
00309 #if defined(_WIN64) && !VCL_HAS_LONG_LONG
00310 VCL_DEFINE_SPECIALIZATION
00311 class vnl_numeric_traits<size_t>
00312 {
00313  public:
00314   //: Additive identity
00315   static const size_t zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00316   //: Multiplicative identity
00317   static const size_t one VCL_STATIC_CONST_INIT_INT_DECL(1);
00318   //: Maximum value which this type can assume
00319   static const size_t maxval; // = 0x7fffffff;
00320   //: Return value of abs()
00321   typedef size_t abs_t;
00322   //: Name of a type twice as long as this one for accumulators and products.
00323   typedef size_t double_t;
00324   //: Name of type which results from multiplying this type with a double
00325   typedef double real_t;
00326 };
00327 
00328 #if !VCL_CANNOT_SPECIALIZE_CV
00329 VCL_DEFINE_SPECIALIZATION
00330 class vnl_numeric_traits<size_t const> : public vnl_numeric_traits<size_t> {};
00331 #endif
00332 #endif
00333 
00334 #if VCL_HAS_LONG_LONG
00335 VCL_DEFINE_SPECIALIZATION
00336 class vnl_numeric_traits<long long>
00337 {
00338  public:
00339   //: Additive identity
00340   static const long long zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00341   //: Multiplicative identity
00342   static const long long one VCL_STATIC_CONST_INIT_INT_DECL(1);
00343   //: Maximum value which this type can assume
00344   static const long long maxval;
00345   //: Return value of abs()
00346   typedef unsigned long long abs_t;
00347   //: Name of a type twice as long as this one for accumulators and products.
00348   typedef long long double_t;
00349   //: Name of type which results from multiplying this type with a double
00350   typedef double real_t;
00351 };
00352 
00353 #if !VCL_CANNOT_SPECIALIZE_CV
00354 VCL_DEFINE_SPECIALIZATION
00355 class vnl_numeric_traits<long long const> : public vnl_numeric_traits<long long> {};
00356 #endif
00357 
00358 VCL_DEFINE_SPECIALIZATION
00359 class vnl_numeric_traits<unsigned long long>
00360 {
00361  public:
00362   //: Additive identity
00363   static const unsigned long long zero VCL_STATIC_CONST_INIT_INT_DECL(0);
00364   //: Multiplicative identity
00365   static const unsigned long long one VCL_STATIC_CONST_INIT_INT_DECL(1);
00366   //: Maximum value which this type can assume
00367   static const unsigned long long maxval;
00368   //: Return value of abs()
00369   typedef unsigned long long abs_t;
00370   //: Name of a type twice as long as this one for accumulators and products.
00371   typedef unsigned long long double_t;
00372   //: Name of type which results from multiplying this type with a double
00373   typedef double real_t;
00374 };
00375 
00376 #if !VCL_CANNOT_SPECIALIZE_CV
00377 VCL_DEFINE_SPECIALIZATION
00378 class vnl_numeric_traits<unsigned long long const> : public vnl_numeric_traits<unsigned long long> {};
00379 #endif
00380 #endif
00381 
00382 VCL_DEFINE_SPECIALIZATION
00383 class vnl_numeric_traits<float>
00384 {
00385  public:
00386   //: Additive identity
00387   static const float zero VCL_STATIC_CONST_INIT_FLOAT_DECL(0.0F);
00388   //: Multiplicative identity
00389   static const float one VCL_STATIC_CONST_INIT_FLOAT_DECL(1.0F);
00390   //: Maximum value which this type can assume
00391   static const float maxval VCL_STATIC_CONST_INIT_FLOAT_DECL(3.40282346638528860e+38F);
00392   //: Return value of abs()
00393   typedef float abs_t;
00394   //: Name of a type twice as long as this one for accumulators and products.
00395   typedef double double_t;
00396   //: Name of type which results from multiplying this type with a double
00397   typedef double real_t;
00398 };
00399 
00400 #if !VCL_CANNOT_SPECIALIZE_CV
00401 VCL_DEFINE_SPECIALIZATION
00402 class vnl_numeric_traits<float const> : public vnl_numeric_traits<float> {};
00403 #endif
00404 
00405 VCL_DEFINE_SPECIALIZATION
00406 class vnl_numeric_traits<double>
00407 {
00408  public:
00409   //: Additive identity
00410   static const double zero VCL_STATIC_CONST_INIT_FLOAT_DECL(0.0);
00411   //: Multiplicative identity
00412   static const double one VCL_STATIC_CONST_INIT_FLOAT_DECL(1.0);
00413   //: Maximum value which this type can assume
00414   static const double maxval VCL_STATIC_CONST_INIT_FLOAT_DECL(1.7976931348623157E+308);
00415   //: Return value of abs()
00416   typedef double abs_t;
00417   //: Name of a type twice as long as this one for accumulators and products.
00418   typedef long double double_t;
00419   //: Name of type which results from multiplying this type with a double
00420   typedef double real_t;
00421 };
00422 
00423 #if !VCL_CANNOT_SPECIALIZE_CV
00424 VCL_DEFINE_SPECIALIZATION
00425 class vnl_numeric_traits<double const> : public vnl_numeric_traits<double> {};
00426 #endif
00427 
00428 VCL_DEFINE_SPECIALIZATION
00429 class vnl_numeric_traits<long double>
00430 {
00431  public:
00432   //: Additive identity
00433   static const long double zero VCL_STATIC_CONST_INIT_FLOAT_DECL(0.0);
00434   //: Multiplicative identity
00435   static const long double one VCL_STATIC_CONST_INIT_FLOAT_DECL(1.0);
00436   //: Maximum value which this type can assume
00437   static const long double maxval VCL_STATIC_CONST_INIT_FLOAT_DECL(1.7976931348623157E+308);
00438   //: Return value of abs()
00439   typedef long double abs_t;
00440   //: Name of a type twice as long as this one for accumulators and products.
00441   typedef long double double_t; // ahem
00442   //: Name of type which results from multiplying this type with a double
00443   typedef long double real_t;
00444 };
00445 
00446 #if !VCL_CANNOT_SPECIALIZE_CV
00447 VCL_DEFINE_SPECIALIZATION
00448 class vnl_numeric_traits<long double const> : public vnl_numeric_traits<long double> {};
00449 #endif
00450 
00451 VCL_DEFINE_SPECIALIZATION
00452 class vnl_numeric_traits< vcl_complex<float> >
00453 {
00454  public:
00455   //: Additive identity
00456   static const vcl_complex<float> zero;
00457   //: Multiplicative identity
00458   static const vcl_complex<float> one;
00459   // Maximum value which this type can assume; makes no sense for this type
00460   //static const vcl_complex<float> maxval;
00461 
00462   //: Return value of abs()
00463   typedef float abs_t;
00464   //: Name of a type twice as long as this one for accumulators and products.
00465   typedef vcl_complex<vnl_numeric_traits<float>::double_t> double_t;
00466   //: Name of type which results from multiplying this type with a double
00467   typedef vcl_complex<float> real_t;
00468 };
00469 
00470 #if !VCL_CANNOT_SPECIALIZE_CV
00471 VCL_DEFINE_SPECIALIZATION
00472 class vnl_numeric_traits<vcl_complex<float> const> : public vnl_numeric_traits<vcl_complex<float> > {};
00473 #endif
00474 
00475 VCL_DEFINE_SPECIALIZATION
00476 class vnl_numeric_traits< vcl_complex<double> >
00477 {
00478  public:
00479   //: Additive identity
00480   static const vcl_complex<double> zero;
00481   //: Multiplicative identity
00482   static const vcl_complex<double> one;
00483   // Maximum value which this type can assume; makes no sense for this type
00484   //static const vcl_complex<double> maxval;
00485 
00486   //: Return value of abs()
00487   typedef double abs_t;
00488   //: Name of a type twice as long as this one for accumulators and products.
00489   typedef vcl_complex<vnl_numeric_traits<double>::double_t> double_t;
00490   //: Name of type which results from multiplying this type with a double
00491   typedef vcl_complex<double> real_t;
00492 };
00493 
00494 #if !VCL_CANNOT_SPECIALIZE_CV
00495 VCL_DEFINE_SPECIALIZATION
00496 class vnl_numeric_traits<vcl_complex<double> const> : public vnl_numeric_traits<vcl_complex<double> > {};
00497 #endif
00498 
00499 VCL_DEFINE_SPECIALIZATION
00500 class vnl_numeric_traits< vcl_complex<long double> >
00501 {
00502  public:
00503   //: Additive identity
00504   static const vcl_complex<long double> zero;
00505   //: Multiplicative identity
00506   static const vcl_complex<long double> one;
00507   // Maximum value which this type can assume; makes no sense for this type
00508   //static const vcl_complex<long double> maxval;
00509 
00510   //: Return value of abs()
00511   typedef long double abs_t;
00512   //: Name of a type twice as long as this one for accumulators and products.
00513   typedef vcl_complex<vnl_numeric_traits<long double>::double_t> double_t;
00514   //: Name of type which results from multiplying this type with a double
00515   typedef vcl_complex<long double> real_t;
00516 };
00517 
00518 #if !VCL_CANNOT_SPECIALIZE_CV
00519 VCL_DEFINE_SPECIALIZATION
00520 class vnl_numeric_traits<vcl_complex<long double> const> : public vnl_numeric_traits<vcl_complex<long double> > {};
00521 #endif
00522 
00523 #endif // vnl_numeric_traits_h_