TAPs 0.7.7.3
TAPsMath.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002 TAPsMath.cpp
00003 
00004 Under TAPs namespace
00005 Defines a Math class for basic math.
00006 All member functions and data members are static.
00007 
00008 SUKITTI PUNAK   (09/05/2004)
00009 UPDATE          (09/07/2004)
00010 ******************************************************************************/
00011 #include "TAPsMath.hpp"
00012 // Using Inclusion Model (i.e. definitions are included in declarations)
00013 //                       (this name.cpp is included in name.hpp)
00014 // Each friend is defined directly inside its declaration.
00015 
00016 BEGIN_NAMESPACE_TAPs
00017 //=============================================================================
00018 // Constant Values
00019 //=============================================================================
00020 template <typename T> const T TAPs::Math<T>::ZERO = 0;
00021 template <typename T> const T TAPs::Math<T>::ONE  = 1;
00022 template <typename T> const T TAPs::Math<T>::EPSILON  = std::numeric_limits<T>::epsilon();
00023 template <typename T> const T TAPs::Math<T>::INFINITY = std::numeric_limits<T>::infinity();
00024 template <typename T> T TAPs::Math<T>::ThresholdZero = std::numeric_limits<T>::epsilon()*10;
00025 //-----------------------------------------------------------------------------
00026 // max() and min() are defined as macros in numeric_limits from <limits>.
00027 // However, windows.h also has max(a,b) and min(a,b) as macro.
00028 // Therefore, if windows.h is included, then these two fns will not work properly.
00029 #ifndef _WINDOWS_
00030 template <typename T> const T TAPs::Math<T>::MAX = std::numeric_limits<T>::max();
00031 template <typename T> const T TAPs::Math<T>::MIN = std::numeric_limits<T>::min();
00032 #endif
00033 //-----------------------------------------------------------------------------
00034 template <typename T> const T TAPs::Math<T>::ROUND_ERROR = std::numeric_limits<T>::round_error();
00035 //PI = 3.1415926535897932384626433832795
00036 template <typename T> const T TAPs::Math<T>::PI = static_cast<T>(4.0*atan(ONE));    // atan(1.0) gives radians of 45 degs
00037 template <typename T> const T TAPs::Math<T>::TWO_PI     = static_cast<T>(2.0*PI);   // radians of 360 degrees
00038 template <typename T> const T TAPs::Math<T>::HALF_PI    = static_cast<T>(0.5*PI);   // radians of  90 degrees
00039 template <typename T> const T TAPs::Math<T>::QUARTER_PI = static_cast<T>(0.25*PI);  // radians of  45 degrees
00040 template <typename T> const T TAPs::Math<T>::DEG_TO_RAD = static_cast<T>(TAPs::Math<T>::PI/180.0);  // degrees to radians
00041 template <typename T> const T TAPs::Math<T>::RAD_TO_DEG = static_cast<T>(180.0/TAPs::Math<T>::PI);  // radians to degrees
00042 
00043 //=============================================================================
00044 // Absolute, Ceil, Floor, and Sign
00045 //=============================================================================
00046 //-----------------------------------------------------------------------------
00047 template <typename T> inline T TAPs::Math<T>::Abs  ( T tValue )
00048 {   return tValue >= ZERO ? tValue : -tValue;   }
00049 //-----------------------------------------------------------------------------
00050 template <typename T> inline T TAPs::Math<T>::Ceil ( T tValue )
00051 {   return ceil( tValue );  }
00052 //-----------------------------------------------------------------------------
00053 template <typename T> inline T TAPs::Math<T>::Floor ( T tValue )
00054 {   return floor( tValue ); }
00055 //-----------------------------------------------------------------------------
00056 template <typename T> inline T TAPs::Math<T>::Sign ( T tValue )
00057 {   return tValue > ZERO ? ONE : ( tValue < ZERO ? -ONE : ZERO );   }
00058 //-----------------------------------------------------------------------------
00059 
00060 //=============================================================================
00061 // Trigonometric Functions
00062 //=============================================================================
00063 //-----------------------------------------------------------------------------
00064 template <typename T> inline T TAPs::Math<T>::Sin ( T tValue )  // sine
00065 {   return sin( tValue );   }
00066 //-----------------------------------------------------------------------------
00067 template <typename T> inline T TAPs::Math<T>::Cos ( T tValue )  // cosine
00068 {   return cos( tValue );   }
00069 //-----------------------------------------------------------------------------
00070 template <typename T> inline T TAPs::Math<T>::Tan ( T tValue )  // tangent
00071 {   return tan( tValue );   }
00072 //-----------------------------------------------------------------------------
00073 template <typename T> inline T TAPs::Math<T>::Csc ( T tValue )  // cosecant
00074 {   return static_cast<T>(ONE) / sin( tValue ); }
00075 //-----------------------------------------------------------------------------
00076 template <typename T> inline T TAPs::Math<T>::Sec ( T tValue )  // secant
00077 {   return static_cast<T>(ONE) / cos( tValue ); }
00078 //-----------------------------------------------------------------------------
00079 template <typename T> inline T TAPs::Math<T>::Cot ( T tValue )  // cotangent
00080 {   return static_cast<T>(ONE) / tan( tValue ); }
00081 //-----------------------------------------------------------------------------
00082 
00083 //=============================================================================
00084 // Inverse Trigonometric Functions
00085 //=============================================================================
00086 //-----------------------------------------------------------------------------
00087 template <typename T> inline T TAPs::Math<T>::ASin ( T tValue ) // arc sine
00088 {
00089     // asin function
00090     // I/P: tValue must be in the range [-1,1]
00091     // O/P: return value is in the range [-PI/2,PI/2];
00092 #ifndef _WINDOWS_
00093     if ( tValue < -ONE )
00094     {
00095         //return TAPs::Math<T>::MIN;
00096         return std::numeric_limits<T>::min();
00097     }
00098     else if ( tValue > ONE )
00099     {
00100         //return TAPs::Math<T>::MAX;
00101         return std::numeric_limits<T>::max();
00102     }
00103     else
00104     {
00105         return asin( tValue );
00106     }
00107 #else
00108     return asin( tValue );
00109 #endif
00110 }
00111 //-----------------------------------------------------------------------------
00112 template <typename T> inline T TAPs::Math<T>::ACos ( T tValue ) // arc cosine
00113 {
00114     // acos function
00115     // I/P: tValue must be in the range [-1,1]
00116     // O/P: return value is in the range [0,PI];
00117 #ifndef _WINDOWS_
00118     if ( tValue < -ONE )
00119     {
00120         //return TAPs::Math<T>::MIN;
00121         return std::numeric_limits<T>::min();
00122     }
00123     else if ( tValue > ONE )
00124     {
00125         //return TAPs::Math<T>::MAX;
00126         return std::numeric_limits<T>::max();
00127     }
00128     else
00129     {
00130         return acos( tValue );
00131     }
00132 #else
00133     return acos( tValue );
00134 #endif
00135 }
00136 //-----------------------------------------------------------------------------
00137 template <typename T> inline T TAPs::Math<T>::ATan ( T tValue ) // arc tangent
00138 {
00139     // atan function
00140     // O/P: return value is in the range [-PI/2,PI/2];
00141     return atan( tValue );
00142 }
00143 //-----------------------------------------------------------------------------
00144 template <typename T> inline T TAPs::Math<T>::ATan2 ( T fY, T fX )
00145 {
00146     // atan2 function
00147     // returns the inverse tangent of fY/fX using the sign of both numbers to 
00148     // determine the quadrant for the return value.  It correctly handles the 
00149     // case in which fX is 0.  (That is, it returns PI/2 times the sign of fY 
00150     // for nonzero fY; if fY is 0, the result is implementation-defined and 
00151     // might be a range error).
00152     // O/P: return value is in the range [-PI/2,PI/2];
00153     return atan2( fY, fX );
00154 }
00155 //-----------------------------------------------------------------------------
00156 //template <typename T> inline static T TAPs::Math<T>::ACsc ( T tValue )    // arc cosecant
00157 //-----------------------------------------------------------------------------
00158 //template <typename T> inline static T TAPs::Math<T>::ASec ( T tValue )    // arc secant
00159 //-----------------------------------------------------------------------------
00160 //template <typename T> inline static T TAPs::Math<T>::ACot ( T tValue )    // arc cotangent
00161 //-----------------------------------------------------------------------------
00162 
00163 //=============================================================================
00164 // Exponential and Logarithmic Functions
00165 //=============================================================================
00166 //-----------------------------------------------------------------------------
00167 template <typename T> inline T TAPs::Math<T>::Exp  ( T tValue )
00168 {   return exp( tValue );   }
00169 //-----------------------------------------------------------------------------
00170 template <typename T> inline T TAPs::Math<T>::Log  ( T tValue )
00171 {   return log( tValue );   }
00172 //-----------------------------------------------------------------------------
00173 template <typename T> inline T TAPs::Math<T>::Log10  ( T tValue )
00174 {   return log10( tValue ); }
00175 //-----------------------------------------------------------------------------
00176 template <typename T> inline T TAPs::Math<T>::Pow  ( T tBase, T tExponent )
00177 {   
00178     // pow function
00179     // raises fBase to the fExponent power.  If fBase is negative and fExponent 
00180     // is not an integer, then a domain error occurs.  If fBase is 0 and 
00181     // fExponent is less than or equal to 0, then the result cannot be 
00182     // represented as a real number, a domain error occurs.
00183     return pow( tBase, tExponent );
00184 }
00185 //-----------------------------------------------------------------------------
00186 template <typename T> inline T TAPs::Math<T>::Square ( T tValue )
00187 {   return tValue * tValue; }
00188 //-----------------------------------------------------------------------------
00189 template <typename T> inline T TAPs::Math<T>::Sqrt ( T tValue )
00190 {
00191     // sqrt function
00192     // returns the square root or its argument.  If tValue is negative, a 
00193     // domain error occurs.  The return value is always positive or zero.
00194     return sqrt( tValue );
00195 }
00196 //-----------------------------------------------------------------------------
00197 
00198 //=============================================================================
00199 // Random Numbers
00200 //=============================================================================
00201 //-----------------------------------------------------------------------------
00202 template <typename T> inline T TAPs::Math<T>::UnitRandom ()     // between [0,1]
00203 {
00204     // void srand ( unsigned int seed )     (defined in <cstdlib>)
00205     // saves seed as the seed for a new sequence of pseudo-random numbers to be 
00206     // returned by successive calls to rand.  The default seed is 1.
00207 
00208     // int rand ()      (defined in <cstdlib>)
00209     // returns a pseudo-random integer in the range of 0 to RAND_MAX inclusively
00210     return static_cast<T>( rand() ) / static_cast<T>( RAND_MAX );
00211 }
00212 //-----------------------------------------------------------------------------
00213 template <typename T> inline T TAPs::Math<T>::SymmetricRandom ()    // between [-1,1]
00214 {   return  static_cast<T>(2.0)*UnitRandom() - static_cast<T>(ONE); }
00215 //-----------------------------------------------------------------------------
00216 //=============================================================================
00218 // Explicit Instantiation
00219 //template class TAPs::Math<int>;   // can't create <int> template, ambiguous 
00220                                     // call with double or long double
00221 template class TAPs::Math<float>;
00222 template class TAPs::Math<double>;
00223 template class TAPs::Math<long double>;
00224 //*/
00225 //=============================================================================
00226 END_NAMESPACE_TAPs
00227 //345678901234567890123456789012345678901234567890123456789012345678901234567890
00228 //--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines