1 /******************************************************************************
3 * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/) *
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>
28 using namespace icinga;
30 static double MathAbs(double x)
35 static double MathAcos(double x)
40 static double MathAsin(double x)
45 static double MathAtan(double x)
50 static double MathAtan2(double y, double x)
52 return std::atan2(y, x);
55 static double MathCeil(double x)
60 static double MathCos(double x)
65 static double MathExp(double x)
70 static double MathFloor(double x)
75 static double MathLog(double x)
80 static Value MathMax(const std::vector<Value>& args)
83 Value result = -INFINITY;
85 for (const Value& arg : args) {
86 if (first || arg > result) {
95 static Value MathMin(const std::vector<Value>& args)
98 Value result = INFINITY;
100 for (const Value& arg : args) {
101 if (first || arg < result) {
110 static double MathPow(double x, double y)
112 return std::pow(x, y);
115 static double MathRandom()
117 return (double)std::rand() / RAND_MAX;
120 static double MathRound(double x)
122 return boost::math::round(x);
125 static double MathSin(double x)
130 static double MathSqrt(double x)
135 static double MathTan(double x)
140 static bool MathIsnan(double x)
142 return boost::math::isnan(x);
145 static bool MathIsinf(double x)
147 return boost::math::isinf(x);
150 static double MathSign(double x)
160 INITIALIZE_ONCE([]() {
161 Dictionary::Ptr mathObj = new Dictionary({
163 { "E", 2.71828182845904523536 },
164 { "LN2", 0.693147180559945309417 },
165 { "LN10", 2.30258509299404568402 },
166 { "LOG2E", 1.44269504088896340736 },
167 { "LOG10E", 0.434294481903251827651 },
168 { "PI", 3.14159265358979323846 },
169 { "SQRT1_2", 0.707106781186547524401 },
170 { "SQRT2", 1.41421356237309504880 },
173 { "abs", new Function("Math#abs", MathAbs, { "x" }, true) },
174 { "acos", new Function("Math#acos", MathAcos, { "x" }, true) },
175 { "asin", new Function("Math#asin", MathAsin, { "x" }, true) },
176 { "atan", new Function("Math#atan", MathAtan, { "x" }, true) },
177 { "atan2", new Function("Math#atan2", MathAtan2, { "x", "y" }, true) },
178 { "ceil", new Function("Math#ceil", MathCeil, { "x" }, true) },
179 { "cos", new Function("Math#cos", MathCos, { "x" }, true) },
180 { "exp", new Function("Math#exp", MathExp, { "x" }, true) },
181 { "floor", new Function("Math#floor", MathFloor, { "x" }, true) },
182 { "log", new Function("Math#log", MathLog, { "x" }, true) },
183 { "max", new Function("Math#max", MathMax, {}, true) },
184 { "min", new Function("Math#min", MathMin, {}, true) },
185 { "pow", new Function("Math#pow", MathPow, { "x", "y" }, true) },
186 { "random", new Function("Math#random", MathRandom, {}, true) },
187 { "round", new Function("Math#round", MathRound, { "x" }, true) },
188 { "sin", new Function("Math#sin", MathSin, { "x" }, true) },
189 { "sqrt", new Function("Math#sqrt", MathSqrt, { "x" }, true) },
190 { "tan", new Function("Math#tan", MathTan, { "x" }, true) },
191 { "isnan", new Function("Math#isnan", MathIsnan, { "x" }, true) },
192 { "isinf", new Function("Math#isinf", MathIsinf, { "x" }, true) },
193 { "sign", new Function("Math#sign", MathSign, { "x" }, true) }
196 ScriptGlobal::Set("Math", mathObj);