00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _plMath_h_
00019 #define _plMath_h_
00020
00021
00022
00023
00024
00025 #define PL_USE_NAMESPACE
00026
00027
00028 #include <algorithm>
00029 #include <bitset>
00030
00031 #include <list>
00032 #include <map>
00033 #include <set>
00034 #include <string>
00035 #include <vector>
00036 #include <fstream>
00037
00038 #ifdef PL_USE_NAMESPACE
00039 using namespace std;
00040
00041 #else
00042
00043 #define bitset std::bitset
00044 #define list std::list
00045 #define map std::map
00046 #define string std::string
00047 #define vector std::vector
00048 #define pair std::pair
00049
00050 #define ostream std::ostream
00051 #define istream std::istream
00052 #define ofstream std::ofstream
00053 #define ifstream std::ifstream
00054
00055 #define make_pair (std::make_pair)
00056 #define remove_if (std::remove_if)
00057 #define back_inserter (std::back_inserter)
00058
00059 #define find_if (std::find_if)
00060 #define set_union (std::set_union)
00061 #define set_intersection (std::set_intersection)
00062 #define set_difference (std::set_difference)
00063 #define includes (std::includes)
00064
00065 #define cout (std::cout)
00066 #define cin (std::cin)
00067 #define cerr (std::cerr)
00068 #define endl (std::endl)
00069
00070 #endif
00071
00072
00073
00074
00075
00076
00077 #include <math.h>
00078 #include <stdlib.h>
00079 #include <limits.h>
00080 #include <stdio.h>
00081 #include <float.h>
00082
00083
00084
00085
00086
00087
00088
00089 #define PL_LONG_DOUBLE_PRECISION
00090
00091
00092
00093 #ifdef macintosh
00094 #undef PL_LONG_DOUBLE_PRECISION
00095 #endif
00096
00097
00098
00099 #define PL_DOUBLE_PRECISION
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115 #if defined(WIN32) || defined(_WIN32)
00116 #define PL_USE_TABLE_FOR_ERF
00117 #endif
00118
00119
00120
00121
00122
00123 #ifdef PL_LONG_DOUBLE_PRECISION
00124 typedef long double plFloat;
00125
00126 #define PL_LOG_OF_2 0.69314718055994530942l
00127 #define PL_PI_2 1.57079632679489661923l
00128 #define PL_PI 3.14159265358979323846l
00129 #define PL_2PI 6.28318530717958647692l
00130 #define PL_INV_SQRT_2PI 0.398942280401433l
00131 #define PL_SQRT_PI 1.77245385090551602729816748334l
00132 #define PL_LOG_ROOT_2_PI 0.9189385332046727418l
00133 #define PL_ZERO 0.0l
00134 #define PL_HALF 0.5l
00135 #define PL_ONE 1.0l
00136 #define PL_TWO 2.0l
00137 #define PL_SQRT_2 1.414213562373095145475l
00138 #define PL_E 2.7182818284590452354l
00139 #define PL_EPSILON LDBL_EPSILON // smallest number such that 1+Eps != 1
00140 #define PL_FLOAT_MAX LDBL_MAX
00141 #define PL_MIN_EXP -745.0l
00142 #define PL_LOG_OF_10_BASE2 3.321928094887362660l
00143
00144 #define PL_MINUS_ONE -1.0l
00145 #else
00146
00147 #ifdef PL_DOUBLE_PRECISION
00148
00149 typedef double plFloat;
00150
00151
00152 #define PL_LOG_OF_2 0.69314718055994530942
00153 #define PL_PI_2 1.57079632679489661923
00154 #define PL_PI 3.14159265358979323846
00155 #define PL_2PI 6.28318530717958647692
00156 #define PL_INV_SQRT_2PI 0.398942280401433
00157 #define PL_SQRT_PI 1.77245385090551602729816748334
00158 #define PL_LOG_ROOT_2_PI 0.9189385332046727418
00159 #define PL_ZERO 0.0
00160 #define PL_HALF 0.5
00161 #define PL_ONE 1.0
00162 #define PL_TWO 2.0
00163 #define PL_SQRT_2 1.414213562373095145475
00164 #define PL_E 2.7182818284590452354
00165 #define PL_EPSILON DBL_EPSILON // smallest number such that 1+Eps != 1
00166 #define PL_FLOAT_MAX DBL_MAX
00167 #define PL_MIN_EXP -745.0
00168 #define PL_LOG_OF_10_BASE2 3.321928094887362660
00169
00170 #define PL_MINUS_ONE -1.0
00171 #else
00172
00173 typedef float plFloat;
00174
00175
00176 #define PL_LOG_OF_2 0.69314718055994530942f
00177 #define PL_PI_2 1.57079632679489661923f
00178 #define PL_PI 3.14159265358979323846f
00179 #define PL_2PI 6.28318530717958647692f
00180 #define PL_INV_SQRT_2PI 0.398942280401433f
00181 #define PL_SQRT_PI 1.77245385090551602729816748334f
00182 #define PL_LOG_ROOT_2_PI 0.9189385332046727418f
00183 #define PL_ZERO 0.0f
00184 #define PL_HALF 0.5f
00185 #define PL_ONE 1.0f
00186 #define PL_TWO 2.0f
00187 #define PL_SQRT_2 1.414213562373095145475f
00188 #define PL_E 2.7182818284590452354f
00189 #define PL_EPSILON FLT_EPSILON // smallest number such that 1+Eps != 1
00190 #define PL_FLOAT_MAX FLT_MAX
00191 #define PL_MIN_EXP -745.0f
00192 #define PL_LOG_OF_10_BASE2 3.321928094887362660f
00193
00194 #define PL_MINUS_ONE -1.0f
00195 #endif
00196
00197 #endif
00198
00199
00200
00201
00202
00203
00204
00205 inline plFloat plLogBase2(plFloat v)
00206 {
00207 return log(v)/PL_LOG_OF_2;
00208 }
00209
00210 static const unsigned int PL_NUMBER_OF_BITS_BY_CHAR = sizeof(unsigned char)*8;
00211
00212
00213 inline int plNbBits(int _int)
00214 {
00215 int i=0;
00216 _int = _int -1;
00217 while( _int != 0){
00218 _int = _int >> 1;
00219 i++;
00220 }
00221 return i;
00222 }
00223
00224 #define PL_MAX_ULONG ULONG_MAX
00225
00226
00227 inline int plRound( plFloat x )
00228 {
00229 plFloat tmp1 = floor( x );
00230 plFloat tmp2 = ceil( x );
00231 return (int) ( ( x - tmp1 < tmp2 - x ) ? tmp1 : tmp2);
00232 }
00233
00234
00235
00236 inline plFloat plFabs( plFloat x )
00237 {
00238 return (x < PL_ZERO) ? -x : x;
00239 }
00240
00241
00242
00243
00244
00245
00246 #include <plTimer.h>
00247
00248
00249
00250 #define PL_TIME_SEC(prg, time_in_seconds)\
00251 plTimer::timer.start();\
00252 prg;\
00253 plTimer::timer.stop();\
00254 time_in_seconds = plTimer::timer.get_time_sec();
00255
00256
00257
00258 #define PL_TIME_MSEC(prg, time_in_milliseconds)\
00259 plTimer::timer.start();\
00260 prg;\
00261 plTimer::timer.stop();\
00262 time_in_milliseconds = plTimer::timer.get_time_msec();
00263
00264 #define PL_TIME PL_TIME_SEC
00265
00266
00267
00268
00269
00270 #ifndef PL_USE_PLPROBA
00271
00272
00279 typedef plFloat plProbValue;
00280 #define PL_ZERO_PROB PL_ZERO
00281 #define PL_HALF_PROB PL_HALF
00282 #define PL_ONE_PROB PL_ONE
00283 #define PL_TWO_PROB PL_TWO
00284
00285 #define PL_INFINITY_PROB HUGE_VAL
00286 #define PL_INVALID_PROB -1.0
00287 #else
00288
00289
00290 #include <plProbability32.h>
00291
00298 typedef plProbability plProbValue;
00299 #define PL_ZERO_PROB plProbability::plZeroProbaConstant
00300 #define PL_HALF_PROB plProbability::plHalfProbaConstant
00301 #define PL_ONE_PROB plProbability::plOneProbaConstant
00302 #define PL_TWO_PROB plProbability::plTwoProbaConstant
00303
00304 #define PL_INFINITY_PROB plProbability::plInfinityProbaConstant
00305 #define PL_INVALID_PROB plProbability::plNaNProbaConstant
00306 #endif
00307
00308
00309
00310
00311 #include <plRandom.h>
00312
00313
00314
00315
00316 #endif