library of assembled shared sources

http://lass.cocamware.com

num_traits.h

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 
00044 #ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_TRAITS_H
00045 #define LASS_GUARDIAN_OF_INCLUSION_NUM_TRAITS_H
00046 
00047 #include "num_common.h"
00048 
00049 #ifdef LASS_NUM_NUM_TRAITS_HAVE_MSVC_FLOAT_H
00050 #   include <float.h>
00051 #endif
00052 
00053 namespace lass
00054 {
00055 namespace num
00056 {
00057 
00058 template<class C>
00059 struct LASS_DLL NumTraits
00060 {
00061     typedef C   selfType;               /**< our own type */
00062     typedef C   baseType;               /**< is the base type where selfType is based on */
00063     typedef C   intervalType;           /**< type of the support, useful for distributions */
00064 
00065     enum
00066     {
00067         isDistribution = 0, /**< true for distribution like types */
00068         isIntegral = 0,     /**< true for integral types, ie supporting ++ and -- fi */
00069         isNative = 0,       /**< true for the native types, ie char, int , ... */
00070         isSigned = 0,       /**< true for signed types */
00071         hasInfinity = 0,    /**< true for types having infinity */
00072         hasNaN = 0,         /**< true for types have the concept of Not a Number */
00073         isFloatingPoint = 0 /**< true for floating point types */
00074     };
00075 
00076     static const size_t   memorySize;      /**< memory footprint */
00077     static const size_t   mantisseSize; /**< number of bits used for mantisse, useful for error analysis */
00078     static const std::string name() { return "unknown"; }   /**< name of the selfType */
00079 
00080     static const C  one;                /**< definition of one */
00081     static const C  zero;               /**< definition of zero */
00082 
00083     /* some constants */
00084     static const C  sNaN;               /**< signaling Nan, see http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html */
00085     static const C  qNaN;               /**< quiet NaN, see http://research.microsoft.com/~hollasch/cgindex/coding/ieeefloat.html */
00086     static const C  epsilon;            /**< the smallest positive value such that one + epsilon != one*/
00087     static const C infinity;            /**< infinity */
00088     static const C min;                 /**< the smallest value representable */
00089     static const C  max;                /**< the largest positive value */
00090     static const C minStrictPositive;   /**< the smallest strictly positive value */
00091     static const C pi;
00092     static const C e;
00093     static const C sqrt2;
00094     static const C sqrtPi;
00095 };
00096 
00097 template<class C> inline
00098 bool isNaN( const C& iV )
00099 {
00100 #ifdef LASS_NUM_NUM_TRAITS_HAVE_MSVC_FLOAT_H
00101     return NumTraits<C>::hasNaN && _isnan(static_cast<double>(iV)) != 0;
00102 #else
00103     return iV != iV;
00104 #endif
00105 }
00106 
00107 /** return true if iV equals minus or plus Infinity
00108  */
00109 template<class C>
00110 bool isInf( const C& iV )
00111 {
00112     return NumTraits<C>::hasInfinity &&
00113         (iV == NumTraits<C>::infinity || iV == -NumTraits<C>::infinity);
00114 }
00115 
00116 #define LASS_NUM_DECLARE_FLOATING_TRAITS( ttype, tname ) \
00117 template<> \
00118 struct LASS_DLL NumTraits<ttype>\
00119 {\
00120     typedef ttype   selfType;\
00121     typedef ttype   baseType;\
00122     typedef ttype   intervalType;\
00123     enum\
00124     {\
00125         isDistribution = false,\
00126         isIntegral = false,\
00127         isNative = true,\
00128         isSigned = true,\
00129         hasInfinity = true,\
00130         hasNaN = true,\
00131         isFloatingPoint = true\
00132     };\
00133     static const size_t   memorySize;\
00134     static const size_t   mantisseSize;\
00135     static const std::string name() { return tname ; }\
00136     static const ttype one;\
00137     static const ttype zero;\
00138     static const ttype infinity;\
00139     static const ttype qNaN;\
00140     static const ttype sNaN;\
00141     static const ttype epsilon;\
00142     static const ttype min;\
00143     static const ttype max;\
00144     static const ttype minStrictPositive;\
00145     static const ttype pi;\
00146     static const ttype e;\
00147     static const ttype sqrt2;\
00148     static const ttype sqrtPi;\
00149 };
00150 
00151 #define LASS_NUM_DECLARE_COMPLEX_FLOATING_TRAITS( ttype, tname ) \
00152 template<> \
00153 struct LASS_DLL NumTraits< ttype >\
00154 {\
00155     typedef ttype   selfType;\
00156     typedef ttype::value_type   baseType;\
00157     typedef ttype   intervalType;\
00158     enum\
00159     {\
00160         isDistribution = false,\
00161         isIntegral = false,\
00162         isNative = false,\
00163         isSigned = true,\
00164         hasInfinity = false,\
00165         hasNaN = false,\
00166         isFloatingPoint = NumTraits< baseType >::isFloatingPoint\
00167     };\
00168     static const size_t   memorySize;\
00169     static const size_t   mantisseSize;\
00170     static const std::string name() { return tname ; }\
00171     static const ttype one;\
00172     static const ttype zero;\
00173     static const ttype pi;\
00174     static const ttype e;\
00175     static const ttype sqrt2;\
00176     static const ttype sqrtPi;\
00177 };
00178 
00179 LASS_NUM_DECLARE_FLOATING_TRAITS( float, "float" )
00180 LASS_NUM_DECLARE_COMPLEX_FLOATING_TRAITS( std::complex< float > , "complex<float>" )
00181 
00182 LASS_NUM_DECLARE_FLOATING_TRAITS( double , "double" )
00183 LASS_NUM_DECLARE_COMPLEX_FLOATING_TRAITS( std::complex< double > , "complex<double>" )
00184 
00185 LASS_NUM_DECLARE_FLOATING_TRAITS( long double, "long double" )
00186 LASS_NUM_DECLARE_COMPLEX_FLOATING_TRAITS( std::complex< long double > , "complex<long double>" )
00187 
00188 template<>
00189 struct LASS_DLL NumTraits<char>
00190 {
00191     typedef char selfType;
00192     typedef char baseType;
00193     typedef float   intervalType;
00194     typedef signed char signedType;
00195     typedef unsigned char unsignedType;
00196     enum
00197     {
00198         isDistribution = 0,
00199         isIntegral = 1,
00200         isNative = 1,
00201 #ifdef LASS_CHAR_IS_SIGNED
00202         isSigned = 1,
00203 #else
00204         isSigned = 0,
00205 #endif
00206         hasInfinity = 0,
00207         hasNaN = 0,
00208         isFloatingPoint = 0
00209     };
00210     static const size_t   memorySize;
00211     static const size_t   mantisseSize;
00212     static const std::string name() { return LASS_STRINGIFY(char); }
00213     static const selfType one;
00214     static const selfType zero;
00215     static const selfType epsilon;
00216     static const selfType min;
00217     static const selfType max;
00218     static const selfType minStrictPositive;
00219 };
00220 
00221 #define LASS_NUM_DECLARE_INTEGRAL_TRAITS( sign, type, is_signed ) \
00222 template<> \
00223 struct LASS_DLL NumTraits<sign type> \
00224 {\
00225     typedef sign type selfType;\
00226     typedef sign type baseType;\
00227     typedef float   intervalType;\
00228     typedef signed type signedType;\
00229     typedef unsigned type unsignedType;\
00230     enum\
00231     {\
00232         isDistribution = 0,\
00233         isIntegral = 1,\
00234         isNative = 1,\
00235         isSigned = is_signed,\
00236         hasInfinity = 0,\
00237         hasNaN = 0,\
00238         isFloatingPoint = 0\
00239     };\
00240     static const size_t   memorySize;\
00241     static const size_t   mantisseSize;\
00242     static const std::string name() { return LASS_STRINGIFY(sign type); }\
00243     static const selfType one;\
00244     static const selfType zero;\
00245     static const selfType epsilon;\
00246     static const selfType min;\
00247     static const selfType max;\
00248     static const selfType minStrictPositive;\
00249 };
00250 
00251 LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, char, 1 )
00252 LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, char, 0 )
00253 LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, short, 1 )
00254 LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, short, 0 )
00255 LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, int, 1 )
00256 LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, int, 0 )
00257 LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, long, 1 )
00258 LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, long, 0 )
00259 #if LASS_PLATFORM_TYPE == LASS_PLATFORM_TYPE_WIN32
00260     LASS_NUM_DECLARE_INTEGRAL_TRAITS( signed, __int64, 1 )
00261     LASS_NUM_DECLARE_INTEGRAL_TRAITS( unsigned, __int64, 0 )
00262 #endif
00263 
00264 }
00265 
00266 }
00267 
00268 // we didn't include basic_ops.h in num_common.h when including num_traits.h,
00269 // because basic_ops.h needs NumTraits to be defined first.  So now it's time
00270 // to include basic_ops.h anyway [Bramz]
00271 //
00272 #include "basic_ops.h"
00273 
00274 #endif

Generated on Mon Nov 10 14:20:31 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo