1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
3 #include "base/dictionary.hpp"
4 #include "base/function.hpp"
5 #include "base/functionwrapper.hpp"
6 #include "base/scriptframe.hpp"
7 #include "base/initialize.hpp"
8 #include "base/namespace.hpp"
9 #include <boost/math/special_functions/round.hpp>
12 using namespace icinga;
14 static double MathAbs(double x)
19 static double MathAcos(double x)
24 static double MathAsin(double x)
29 static double MathAtan(double x)
34 static double MathAtan2(double y, double x)
36 return std::atan2(y, x);
39 static double MathCeil(double x)
44 static double MathCos(double x)
49 static double MathExp(double x)
54 static double MathFloor(double x)
59 static double MathLog(double x)
64 static Value MathMax(const std::vector<Value>& args)
67 Value result = -INFINITY;
69 for (const Value& arg : args) {
70 if (first || arg > result) {
79 static Value MathMin(const std::vector<Value>& args)
82 Value result = INFINITY;
84 for (const Value& arg : args) {
85 if (first || arg < result) {
94 static double MathPow(double x, double y)
96 return std::pow(x, y);
99 static double MathRandom()
101 return (double)std::rand() / RAND_MAX;
104 static double MathRound(double x)
106 return boost::math::round(x);
109 static double MathSin(double x)
114 static double MathSqrt(double x)
119 static double MathTan(double x)
124 static bool MathIsnan(double x)
126 return boost::math::isnan(x);
129 static bool MathIsinf(double x)
131 return boost::math::isinf(x);
134 static double MathSign(double x)
144 INITIALIZE_ONCE([]() {
145 auto mathNSBehavior = new ConstNamespaceBehavior();
146 Namespace::Ptr mathNS = new Namespace(mathNSBehavior);
149 mathNS->Set("E", 2.71828182845904523536);
150 mathNS->Set("LN2", 0.693147180559945309417);
151 mathNS->Set("LN10", 2.30258509299404568402);
152 mathNS->Set("LOG2E", 1.44269504088896340736);
153 mathNS->Set("LOG10E", 0.434294481903251827651);
154 mathNS->Set("PI", 3.14159265358979323846);
155 mathNS->Set("SQRT1_2", 0.707106781186547524401);
156 mathNS->Set("SQRT2", 1.41421356237309504880);
159 mathNS->Set("abs", new Function("Math#abs", MathAbs, { "x" }, true));
160 mathNS->Set("acos", new Function("Math#acos", MathAcos, { "x" }, true));
161 mathNS->Set("asin", new Function("Math#asin", MathAsin, { "x" }, true));
162 mathNS->Set("atan", new Function("Math#atan", MathAtan, { "x" }, true));
163 mathNS->Set("atan2", new Function("Math#atan2", MathAtan2, { "x", "y" }, true));
164 mathNS->Set("ceil", new Function("Math#ceil", MathCeil, { "x" }, true));
165 mathNS->Set("cos", new Function("Math#cos", MathCos, { "x" }, true));
166 mathNS->Set("exp", new Function("Math#exp", MathExp, { "x" }, true));
167 mathNS->Set("floor", new Function("Math#floor", MathFloor, { "x" }, true));
168 mathNS->Set("log", new Function("Math#log", MathLog, { "x" }, true));
169 mathNS->Set("max", new Function("Math#max", MathMax, {}, true));
170 mathNS->Set("min", new Function("Math#min", MathMin, {}, true));
171 mathNS->Set("pow", new Function("Math#pow", MathPow, { "x", "y" }, true));
172 mathNS->Set("random", new Function("Math#random", MathRandom, {}, true));
173 mathNS->Set("round", new Function("Math#round", MathRound, { "x" }, true));
174 mathNS->Set("sin", new Function("Math#sin", MathSin, { "x" }, true));
175 mathNS->Set("sqrt", new Function("Math#sqrt", MathSqrt, { "x" }, true));
176 mathNS->Set("tan", new Function("Math#tan", MathTan, { "x" }, true));
177 mathNS->Set("isnan", new Function("Math#isnan", MathIsnan, { "x" }, true));
178 mathNS->Set("isinf", new Function("Math#isinf", MathIsinf, { "x" }, true));
179 mathNS->Set("sign", new Function("Math#sign", MathSign, { "x" }, true));
181 mathNSBehavior->Freeze();
183 Namespace::Ptr systemNS = ScriptGlobal::Get("System");
184 systemNS->SetAttribute("Math", std::make_shared<ConstEmbeddedNamespaceValue>(mathNS));