-
Notifications
You must be signed in to change notification settings - Fork 0
/
maths.cpp
73 lines (53 loc) · 1.29 KB
/
maths.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "maths.h"
#include <sstream>
#ifdef _OPENMP
#include <omp.h>
#endif
Normal NormalGen;
Uniform UniformGen;
double nRandSeed;
double NormalExt(double nMean, double nStdDev, double nLowBound, double nHighBound) {
double nRet = nStdDev * NormalGen.Next() + nMean;
return ( nRet > nHighBound ) ? nHighBound : ( nRet < nLowBound? nLowBound: nRet );
}
double fnSum( double * pNums, int nSize ) {
double nRet = 0;
for (int i=0; i< nSize; i++) {
nRet += pNums[i];
}
return nRet;
}
int fnCompare (const void * a, const void * b)
{
double nComp = *(double*)a - *(double*)b;
return ( nComp < 0 ) ? -1: ( (nComp == 0)? 0:1 );
}
int fnGetRandIndex(int nSizeOfArray) {
int nRet;
do {
nRet = (int)floor(UniformGen.Next() * (double)nSizeOfArray - 0.01);
nRet = nRet>=0? nRet:0;
}
while(nRet >= nSizeOfArray);
/*
const unsigned long n = nSizeOfArray;
const unsigned long divisor = (RAND_MAX + 1) / n;
unsigned long nRet;
do { nRet = rand() / divisor; } while (nRet >= n);
*/
return nRet;
};
double round(double nNum) {
double nIntegral = (double)((int)nNum);
if (nNum - nIntegral < 0.5) {
return nIntegral;
}
else {
return nIntegral + 1;
}
};
string fnIntToString(int nNum ) {
std::stringstream sstm;
sstm << nNum;
return (sstm.str());
};