Math.h

Go to the documentation of this file.
00001 
00005 #ifndef Math_h
00006 #define Math_h
00007 
00008 #ifndef M_PI
00009 #define M_PI 3.141592653589793
00010 #endif
00011 
00012 template<typename T>
00013 static inline T Abs(T x) {
00014   return x<0?-x:x;
00015 }
00016 
00017 template<typename T>
00018 static inline T Min(T m1, T m2) {
00019   return m1<m2?m1:m2;
00020 }
00021 
00022 template<typename T>
00023 static inline T Max(T m1, T m2) {
00024   return m1>=m2?m1:m2;
00025 }
00026 
00027 template<typename T>
00028 static inline T Min(T m1, T m2, T m3) {
00029   return m1<m2?m1<m3?m1:m3:m2<m3?m2:m3;
00030 }
00031 
00032 template<typename T>
00033 static inline T Max(T m1, T m2, T m3) {
00034   return m1>=m2?m1>=m3?m1:m3:m2>=m3?m2:m3;
00035 }
00036 
00037 #if 0
00038 // These break the IBM xlC compiler
00039 template<typename T>
00040 static inline T Min(T m1, T m2, T m3, T m4) {
00041   return Min(Min(m1, m2), Min(m3, m4));
00042 }
00043 
00044 template<typename T>
00045 static inline T Max(T m1, T m2, T m3, T m4) {
00046   return Max(Max(m1, m2), Max(m3, m4));
00047 }
00048 #else
00049 template<typename T>
00050 static inline T Min(T m1, T m2, T m3, T m4) {
00051   T ma = m1<m2?m1:m2;
00052   T mb = m3<m4?m3:m4;
00053   return ma<mb?ma:mb;
00054 }
00055 
00056 template<typename T>
00057 static inline T Max(T m1, T m2, T m3, T m4) {
00058   T ma = m1>=m2?m1:m2;
00059   T mb = m3>=m4?m3:m4;
00060   return ma>=mb?ma:mb;
00061 }
00062 #endif
00063 
00064 template<typename T>
00065 static inline T Clamp(T value, T min, T max) {
00066   return value < min? min: value>max? max: value;
00067 }
00068 
00069 inline int Floor(double d)
00070 {
00071   if(d<0){
00072     int i = -static_cast<int>(-d);
00073     if(i == d)
00074       return i;
00075     else
00076       return i-1;
00077   } else {
00078     return static_cast<int>(d);
00079   }
00080 }
00081 
00082 inline int Ceil(double d)
00083 {
00084   if(d<0){
00085     int i = -static_cast<int>(-d);
00086     return i;
00087   } else {
00088     int i = static_cast<int>(d);
00089     if(i == d)
00090       return i;
00091     else
00092       return i+1;
00093   }
00094 }
00095 
00096 inline double ipow(double x, int p)
00097 {
00098   double result=1;
00099   while(p){
00100     if(p&1)
00101       result*=x;
00102     x*=x;
00103     p>>=1;
00104   }
00105   return result;
00106 }
00107 
00108 inline float ipow(float x, int p)
00109 {
00110   float result=1;
00111   while(p){
00112     if(p&1)
00113       result*=x;
00114     x*=x;
00115     p>>=1;
00116   }
00117   return result;
00118 }
00119 
00120 #endif

Generated on Tue Jan 29 21:34:53 2008 for specter by  doxygen 1.4.6