Implement some more utility functions

refs #8169
This commit is contained in:
Gunnar Beutner 2015-01-16 10:35:20 +01:00
parent 4e96d48e87
commit b4c74efde0
2 changed files with 28 additions and 4 deletions

View File

@ -23,6 +23,7 @@
#include "base/scriptframe.hpp"
#include "base/initialize.hpp"
#include <boost/math/special_functions/round.hpp>
#include <boost/math/special_functions/fpclassify.hpp>
#include <boost/foreach.hpp>
#include <cmath>
@ -138,6 +139,26 @@ static double MathTan(double x)
return std::tan(x);
}
static bool MathIsnan(double x)
{
return boost::math::isnan(x);
}
static bool MathIsinf(double x)
{
return boost::math::isinf(x);
}
static double MathSign(double x)
{
if (x > 0)
return 1;
else if (x < 0)
return -1;
else
return 0;
}
static void InitializeMathObj(void)
{
Dictionary::Ptr mathObj = new Dictionary();
@ -171,6 +192,9 @@ static void InitializeMathObj(void)
mathObj->Set("sin", new ScriptFunction(WrapScriptFunction(MathSin)));
mathObj->Set("sqrt", new ScriptFunction(WrapScriptFunction(MathSqrt)));
mathObj->Set("tan", new ScriptFunction(WrapScriptFunction(MathTan)));
mathObj->Set("isnan", new ScriptFunction(WrapScriptFunction(MathIsnan)));
mathObj->Set("isinf", new ScriptFunction(WrapScriptFunction(MathIsinf)));
mathObj->Set("sign", new ScriptFunction(WrapScriptFunction(MathSign)));
ScriptGlobal::Set("Math", mathObj);
}

View File

@ -554,7 +554,7 @@ bool icinga::operator<(const Value& lhs, const Value& rhs)
if (lhs.IsString() && rhs.IsString())
return static_cast<String>(lhs) < static_cast<String>(rhs);
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return static_cast<int>(lhs) < static_cast<int>(rhs);
return static_cast<double>(lhs) < static_cast<double>(rhs);
else if (lhs.GetTypeName() != rhs.GetTypeName())
return lhs.GetTypeName() < rhs.GetTypeName();
else
@ -586,7 +586,7 @@ bool icinga::operator>(const Value& lhs, const Value& rhs)
if (lhs.IsString() && rhs.IsString())
return static_cast<String>(lhs) > static_cast<String>(rhs);
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return static_cast<int>(lhs) > static_cast<int>(rhs);
return static_cast<double>(lhs) > static_cast<double>(rhs);
else if (lhs.GetTypeName() != rhs.GetTypeName())
return lhs.GetTypeName() > rhs.GetTypeName();
else
@ -618,7 +618,7 @@ bool icinga::operator<=(const Value& lhs, const Value& rhs)
if (lhs.IsString() && rhs.IsString())
return static_cast<String>(lhs) <= static_cast<String>(rhs);
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return static_cast<int>(lhs) <= static_cast<int>(rhs);
return static_cast<double>(lhs) <= static_cast<double>(rhs);
else if (lhs.GetTypeName() != rhs.GetTypeName())
return lhs.GetTypeName() <= rhs.GetTypeName();
else
@ -650,7 +650,7 @@ bool icinga::operator>=(const Value& lhs, const Value& rhs)
if (lhs.IsString() && rhs.IsString())
return static_cast<String>(lhs) >= static_cast<String>(rhs);
else if ((lhs.IsNumber() || lhs.IsEmpty()) && (rhs.IsNumber() || rhs.IsEmpty()) && !(lhs.IsEmpty() && rhs.IsEmpty()))
return static_cast<int>(lhs) >= static_cast<int>(rhs);
return static_cast<double>(lhs) >= static_cast<double>(rhs);
else if (lhs.GetTypeName() != rhs.GetTypeName())
return lhs.GetTypeName() >= rhs.GetTypeName();
else