00001 #if !defined UTILS_H
00002 #define UTILS_H
00003
00004 #include <string>
00005
00006
00007
00008
00009
00010 #define IS_ODD(n) ((n) & 1)
00011 #define IS_EVEN(n) (!(IS_ODD(n)))
00012 #define NEGONE_TO_THE(n) (IS_ODD(n) ? -1 : 1)
00013 #define SIGN(x) ((x) >= 0.0 ? 1 : -1)
00014 #define MAXF(x,y) maxof<double>(x,y)
00015 #define MINF(x,y) minof<double>(x,y)
00016 #define MAXI(x,y) maxof<int>(x,y)
00017 #define MINI(x,y) minof<int>(x,y)
00018
00019
00020
00021
00022
00023 template <class T>
00024 int iround(T x)
00025 {
00026 int i = static_cast<int>(x);
00027
00028 if (x >= static_cast<T>(0.)) {if (x - static_cast<T>(i) >= 0.5) i++;}
00029 else {if (x - static_cast<T>(i) < -0.5) i--;}
00030
00031 return i;
00032 }
00033
00034 template <class T>
00035 inline T sign(T x)
00036 {
00037 return (iround(x) % 2) ? static_cast<T>(-1.) : static_cast<T>(1.);
00038 }
00039
00040 template <class T>
00041 inline T minof(T x1, T x2)
00042 {
00043 return (x1 < x2) ? x1 : x2;
00044 }
00045
00046 template <class T>
00047 inline T maxof(T x1, T x2)
00048 {
00049 return (x1 > x2) ? x1 : x2;
00050 }
00051
00052
00053
00054
00055 bool file_exists( const char* file_name );
00056 bool file_exists( const std::string& file_name );
00057 double mod(double x, double y);
00058
00059 #endif