Merge pull request #1345 from melex750/dev-cbot-fix-abs

Fix abs() only working on floats
pyro-refactor
tomangelo 2020-08-17 19:28:35 +02:00 committed by GitHub
commit dbc13bad20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 4 deletions

View File

@ -144,13 +144,36 @@ bool rRand(CBotVar* var, CBotVar* result, int& exception, void* user)
bool rAbs(CBotVar* var, CBotVar* result, int& exception, void* user)
{
float value;
switch (result->GetType())
{
case CBotTypDouble:
*result = fabs(var->GetValDouble());
break;
case CBotTypFloat:
*result = fabs(var->GetValFloat());
break;
case CBotTypLong:
*result = abs(var->GetValLong());
break;
default:
*result = abs(var->GetValInt());
break;
}
value = var->GetValFloat();
result->SetValFloat(fabs(value));
return true;
}
CBotTypResult cAbs(CBotVar* &var, void* user)
{
if ( var == nullptr ) return CBotTypResult(CBotErrLowParam);
if ( var->GetType() > CBotTypDouble ) return CBotTypResult(CBotErrBadNum);
CBotTypResult returnType(var->GetType());
var = var->GetNext();
if ( var != nullptr ) return CBotTypResult(CBotErrOverParam);
return returnType;
}
// Instruction "floor()"
bool rFloor(CBotVar* var, CBotVar* result, int& exception, void* user)
@ -209,7 +232,7 @@ void InitMathFunctions()
CBotProgram::AddFunction("sqrt", rSqrt, cOneFloat);
CBotProgram::AddFunction("pow", rPow, cTwoFloat);
CBotProgram::AddFunction("rand", rRand, cNull);
CBotProgram::AddFunction("abs", rAbs, cOneFloat);
CBotProgram::AddFunction("abs", rAbs, cAbs);
CBotProgram::AddFunction("floor", rFloor, cOneFloat);
CBotProgram::AddFunction("ceil", rCeil, cOneFloat);
CBotProgram::AddFunction("round", rRound, cOneFloat);