- attempt to fix additional oos issues

This commit is contained in:
Mark Vejvoda
2013-09-28 05:06:04 +00:00
parent 91b7803270
commit 9967df316c
13 changed files with 98 additions and 112 deletions

View File

@@ -73,23 +73,15 @@ std::string RandomGen::getLastCaller() const {
return result;
}
int RandomGen::randRange(int min, int max,string lastCaller) {
//assert(min<=max);
if(min > max) {
char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d] min > max, min = %d, max = %d",__FILE__,__FUNCTION__,__LINE__,min,max);
throw megaglest_runtime_error(szBuf);
}
//#ifdef USE_STREFLOP
// int res = streflop::Random<true, false, float>(min, max); // streflop
//#else
int diff= max-min;
//int res= min + static_cast<int>(truncateDecimal<double>(static_cast<double>(diff+1),2)*RandomGen::rand() / m);
double numerator = static_cast<double>(diff + 1) * static_cast<double>(RandomGen::rand(lastCaller));
int res= min + static_cast<int>(truncateDecimal<double>(numerator / static_cast<double>(m)));
//int res= min + static_cast<int>(truncateDecimal<double>(static_cast<double>(diff+1 * RandomGen::rand()) / static_cast<double>(m)));
//#endif
//assert(res>=min && res<=max);
int res= min + static_cast<int>(truncateDecimal<double>(numerator / static_cast<double>(m),16));
if(res < min || res > max) {
char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d] res < min || res > max, min = %d, max = %d, res = %d",__FILE__,__FUNCTION__,__LINE__,min,max,res);
@@ -102,22 +94,16 @@ int RandomGen::randRange(int min, int max,string lastCaller) {
}
double RandomGen::randRange(double min, double max,string lastCaller) {
//assert(min<=max);
if(min > max) {
char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d] min > max, min = %f, max = %f",__FILE__,__FUNCTION__,__LINE__,min,max);
throw megaglest_runtime_error(szBuf);
}
//#ifdef USE_STREFLOP
// float res = streflop::Random<true, false, float>(min, max, randomState); // streflop
//#else
double rand01= static_cast<double>(RandomGen::rand(lastCaller))/(m-1);
double res= min+(max-min)*rand01;
res = truncateDecimal<double>(res);
//#endif
double rand01 = static_cast<double>(RandomGen::rand(lastCaller)) / (m-1);
double res= min + (max - min) * rand01;
res = truncateDecimal<double>(res,16);
//assert(res>=min && res<=max);
if(res < min || res > max) {
char szBuf[8096]="";
snprintf(szBuf,8096,"In [%s::%s Line: %d] res < min || res > max, min = %f, max = %f, res = %f",__FILE__,__FUNCTION__,__LINE__,min,max,res);

View File

@@ -710,11 +710,11 @@ void copyStringToBuffer(char *buffer, int bufferSize, const string& s){
// ==================== numeric fcs ====================
float saturate(float value){
if (value<0.f){
double saturate(double value) {
if (value < 0.f){
return 0.f;
}
if (value>1.f){
if (value > 1.f){
return 1.f;
}
return value;
@@ -740,17 +740,17 @@ int64 clamp(int64 value, int64 min, int64 max){
return value;
}
float clamp(float value, float min, float max){
if (value<min){
double clamp(double value, double min, double max) {
if (value < min) {
return min;
}
if (value>max){
if (value > max) {
return max;
}
return value;
}
int round(float f){
int round(double f){
return (int) f;
}