00001
00015 #ifndef _XMATH_H
00016 #define _XMATH_H
00017
00018
00019
00020 namespace xchen
00021 {
00022
00023 inline unsigned long int_pow(int base, int p)
00024 {
00025 unsigned long ret = 1;
00026 for(int i=0; i<p; i++)
00027 ret *= base;
00028 return ret;
00029 }
00030
00031 const double pi = 3.141592;
00032 const double epsilon = 1.0E-5;
00033
00034 extern double epsilon_scaleup;
00035 extern double epsilon_scaledown;
00036
00037
00038 #define irand48(low,high) ( (int) ((low) + ((high)-(low)+1) * drand48()))
00039
00040 inline double lineFun(double a, double b, double c, double x, double y) { return (a*x+b*y+c); }
00041
00042 inline double getYgivenX(double a, double b, double c, double x) { assert(b); return -(a*x+c)/b; }
00043 inline double getXgivenY(double a, double b, double c, double y) { assert(a); return -(b*y+c)/a; }
00044
00045 inline int lineFun(int a, int b, int c, int x, int y) { return (a*x+b*y+c); }
00046 inline int getYgivenX(int a, int b, int c, int x) { assert(b); return -(a*x+c)/b; }
00047 inline int getXgivenY(int a, int b, int c, int y) { assert(a); return -(b*y+c)/a; }
00048
00049 inline int round(double a) { return (a>=0? int(a+.5) : int(a-.5)); }
00050 inline int sign(double num) { if(num>0) return 1; if(num<0) return -1; return 0; }
00051
00052 inline long factorial(int n)
00053 {
00054 if(!n)
00055 return 1;
00056 else
00057 return n * factorial(n-1);
00058 }
00059
00060
00061
00065 template<class T>
00066 bool outside(const T& t, const T& t0, const T& t1) { return (t<t0 || t>t1); }
00067
00069 inline bool approx_eq(double a, double b, double epsilon = 1.0E-6) { return fabs(a-b) < epsilon; }
00070
00072 template<class T>
00073 inline double ratio(T v, T m, T M) { return (v-m)/(M-m); }
00074
00076 template <class T>
00077 inline T interpolate(double t, const T& P1, const T& P2) { return P1*(1-t) + P2*t; }
00078
00080 template <class T>
00081 inline T interpolate (double t, double t1, double t2, const T& p1, const T& p2)
00082 {
00083 assert(t<=t2 && t>=t1 && t1!=t2);
00084 return ( (t2-t) * p1 + (t-t1) * p2 ) / (t2-t1);
00085 }
00086
00087
00088
00089 }
00090
00091
00092 #endif