1 /******************************************************************************
3 * Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "base/dictionary.hpp"
21 #include "base/function.hpp"
22 #include "base/functionwrapper.hpp"
23 #include "base/scriptframe.hpp"
24 #include "base/initialize.hpp"
25 #include <boost/math/special_functions/round.hpp>
26 #include <boost/math/special_functions/fpclassify.hpp>
27 #include <boost/foreach.hpp>
30 using namespace icinga;
32 static double MathAbs(double x)
37 static double MathAcos(double x)
42 static double MathAsin(double x)
47 static double MathAtan(double x)
52 static double MathAtan2(double y, double x)
54 return std::atan2(y, x);
57 static double MathCeil(double x)
62 static double MathCos(double x)
67 static double MathExp(double x)
72 static double MathFloor(double x)
77 static double MathLog(double x)
82 static Value MathMax(const std::vector<Value>& args)
85 Value result = -INFINITY;
87 BOOST_FOREACH(const Value& arg, args) {
88 if (first || arg > result) {
97 static Value MathMin(const std::vector<Value>& args)
100 Value result = INFINITY;
102 BOOST_FOREACH(const Value& arg, args) {
103 if (first || arg < result) {
112 static double MathPow(double x, double y)
114 return std::pow(x, y);
117 static double MathRandom(void)
119 return (double)std::rand() / RAND_MAX;
122 static double MathRound(double x)
124 return boost::math::round(x);
127 static double MathSin(double x)
132 static double MathSqrt(double x)
137 static double MathTan(double x)
142 static bool MathIsnan(double x)
144 return boost::math::isnan(x);
147 static bool MathIsinf(double x)
149 return boost::math::isinf(x);
152 static double MathSign(double x)
162 static void InitializeMathObj(void)
164 Dictionary::Ptr mathObj = new Dictionary();
167 mathObj->Set("E", 2.71828182845904523536);
168 mathObj->Set("LN2", 0.693147180559945309417);
169 mathObj->Set("LN10", 2.30258509299404568402);
170 mathObj->Set("LOG2E", 1.44269504088896340736);
171 mathObj->Set("LOG10E", 0.434294481903251827651);
172 mathObj->Set("PI", 3.14159265358979323846);
173 mathObj->Set("SQRT1_2", 0.707106781186547524401);
174 mathObj->Set("SQRT2", 1.41421356237309504880);
177 mathObj->Set("abs", new Function(WrapFunction(MathAbs), true));
178 mathObj->Set("acos", new Function(WrapFunction(MathAcos), true));
179 mathObj->Set("asin", new Function(WrapFunction(MathAsin), true));
180 mathObj->Set("atan", new Function(WrapFunction(MathAtan), true));
181 mathObj->Set("atan2", new Function(WrapFunction(MathAtan2), true));
182 mathObj->Set("ceil", new Function(WrapFunction(MathCeil), true));
183 mathObj->Set("cos", new Function(WrapFunction(MathCos), true));
184 mathObj->Set("exp", new Function(WrapFunction(MathExp), true));
185 mathObj->Set("floor", new Function(WrapFunction(MathFloor), true));
186 mathObj->Set("log", new Function(WrapFunction(MathLog), true));
187 mathObj->Set("max", new Function(WrapFunction(MathMax), true));
188 mathObj->Set("min", new Function(WrapFunction(MathMin), true));
189 mathObj->Set("pow", new Function(WrapFunction(MathPow), true));
190 mathObj->Set("random", new Function(WrapFunction(MathRandom), true));
191 mathObj->Set("round", new Function(WrapFunction(MathRound), true));
192 mathObj->Set("sin", new Function(WrapFunction(MathSin), true));
193 mathObj->Set("sqrt", new Function(WrapFunction(MathSqrt), true));
194 mathObj->Set("tan", new Function(WrapFunction(MathTan), true));
195 mathObj->Set("isnan", new Function(WrapFunction(MathIsnan), true));
196 mathObj->Set("isinf", new Function(WrapFunction(MathIsinf), true));
197 mathObj->Set("sign", new Function(WrapFunction(MathSign), true));
199 ScriptGlobal::Set("Math", mathObj);
202 INITIALIZE_ONCE(InitializeMathObj);