2015-01-15 12:31:36 +01:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2018-01-02 12:06:00 +01:00
|
|
|
* Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
|
2015-01-15 12:31:36 +01:00
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
#include "base/dictionary.hpp"
|
2015-01-21 08:47:45 +01:00
|
|
|
#include "base/function.hpp"
|
|
|
|
#include "base/functionwrapper.hpp"
|
2015-01-15 12:31:36 +01:00
|
|
|
#include "base/scriptframe.hpp"
|
|
|
|
#include "base/initialize.hpp"
|
|
|
|
#include <boost/math/special_functions/round.hpp>
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
static double MathAbs(double x)
|
|
|
|
{
|
|
|
|
return std::fabs(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathAcos(double x)
|
|
|
|
{
|
|
|
|
return std::acos(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathAsin(double x)
|
|
|
|
{
|
|
|
|
return std::asin(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathAtan(double x)
|
|
|
|
{
|
|
|
|
return std::atan(x);
|
|
|
|
}
|
|
|
|
|
2015-01-21 08:01:49 +01:00
|
|
|
static double MathAtan2(double y, double x)
|
2015-01-15 12:31:36 +01:00
|
|
|
{
|
2015-01-21 08:01:49 +01:00
|
|
|
return std::atan2(y, x);
|
2015-01-15 12:31:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static double MathCeil(double x)
|
|
|
|
{
|
|
|
|
return std::ceil(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathCos(double x)
|
|
|
|
{
|
|
|
|
return std::cos(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathExp(double x)
|
|
|
|
{
|
|
|
|
return std::exp(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathFloor(double x)
|
|
|
|
{
|
|
|
|
return std::floor(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathLog(double x)
|
|
|
|
{
|
|
|
|
return std::log(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Value MathMax(const std::vector<Value>& args)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
Value result = -INFINITY;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& arg : args) {
|
2015-01-15 12:31:36 +01:00
|
|
|
if (first || arg > result) {
|
|
|
|
first = false;
|
|
|
|
result = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Value MathMin(const std::vector<Value>& args)
|
|
|
|
{
|
|
|
|
bool first = true;
|
|
|
|
Value result = INFINITY;
|
|
|
|
|
2016-08-25 06:19:44 +02:00
|
|
|
for (const Value& arg : args) {
|
2015-01-15 12:31:36 +01:00
|
|
|
if (first || arg < result) {
|
|
|
|
first = false;
|
|
|
|
result = arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathPow(double x, double y)
|
|
|
|
{
|
|
|
|
return std::pow(x, y);
|
|
|
|
}
|
|
|
|
|
2018-01-04 04:25:35 +01:00
|
|
|
static double MathRandom()
|
2015-01-15 12:31:36 +01:00
|
|
|
{
|
|
|
|
return (double)std::rand() / RAND_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathRound(double x)
|
|
|
|
{
|
|
|
|
return boost::math::round(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathSin(double x)
|
|
|
|
{
|
|
|
|
return std::sin(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathSqrt(double x)
|
|
|
|
{
|
|
|
|
return std::sqrt(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
static double MathTan(double x)
|
|
|
|
{
|
|
|
|
return std::tan(x);
|
|
|
|
}
|
|
|
|
|
2015-01-16 10:35:20 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-27 09:35:08 +02:00
|
|
|
INITIALIZE_ONCE([]() {
|
2018-01-11 11:17:38 +01:00
|
|
|
Dictionary::Ptr mathObj = new Dictionary({
|
|
|
|
/* Constants */
|
|
|
|
{ "E", 2.71828182845904523536 },
|
|
|
|
{ "LN2", 0.693147180559945309417 },
|
|
|
|
{ "LN10", 2.30258509299404568402 },
|
|
|
|
{ "LOG2E", 1.44269504088896340736 },
|
|
|
|
{ "LOG10E", 0.434294481903251827651 },
|
|
|
|
{ "PI", 3.14159265358979323846 },
|
|
|
|
{ "SQRT1_2", 0.707106781186547524401 },
|
|
|
|
{ "SQRT2", 1.41421356237309504880 },
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
{ "abs", new Function("Math#abs", MathAbs, { "x" }, true) },
|
|
|
|
{ "acos", new Function("Math#acos", MathAcos, { "x" }, true) },
|
|
|
|
{ "asin", new Function("Math#asin", MathAsin, { "x" }, true) },
|
|
|
|
{ "atan", new Function("Math#atan", MathAtan, { "x" }, true) },
|
|
|
|
{ "atan2", new Function("Math#atan2", MathAtan2, { "x", "y" }, true) },
|
|
|
|
{ "ceil", new Function("Math#ceil", MathCeil, { "x" }, true) },
|
|
|
|
{ "cos", new Function("Math#cos", MathCos, { "x" }, true) },
|
|
|
|
{ "exp", new Function("Math#exp", MathExp, { "x" }, true) },
|
|
|
|
{ "floor", new Function("Math#floor", MathFloor, { "x" }, true) },
|
|
|
|
{ "log", new Function("Math#log", MathLog, { "x" }, true) },
|
|
|
|
{ "max", new Function("Math#max", MathMax, {}, true) },
|
|
|
|
{ "min", new Function("Math#min", MathMin, {}, true) },
|
|
|
|
{ "pow", new Function("Math#pow", MathPow, { "x", "y" }, true) },
|
|
|
|
{ "random", new Function("Math#random", MathRandom, {}, true) },
|
|
|
|
{ "round", new Function("Math#round", MathRound, { "x" }, true) },
|
|
|
|
{ "sin", new Function("Math#sin", MathSin, { "x" }, true) },
|
|
|
|
{ "sqrt", new Function("Math#sqrt", MathSqrt, { "x" }, true) },
|
|
|
|
{ "tan", new Function("Math#tan", MathTan, { "x" }, true) },
|
|
|
|
{ "isnan", new Function("Math#isnan", MathIsnan, { "x" }, true) },
|
|
|
|
{ "isinf", new Function("Math#isinf", MathIsinf, { "x" }, true) },
|
|
|
|
{ "sign", new Function("Math#sign", MathSign, { "x" }, true) }
|
|
|
|
});
|
2015-01-15 12:31:36 +01:00
|
|
|
|
|
|
|
ScriptGlobal::Set("Math", mathObj);
|
2016-08-27 09:35:08 +02:00
|
|
|
});
|