]> granicus.if.org Git - icinga2/blob - lib/base/math-script.cpp
lib->compat->statusdatawriter: fix notifications_enabled
[icinga2] / lib / base / math-script.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)  *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
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 <cmath>
27
28 using namespace icinga;
29
30 static double MathAbs(double x)
31 {
32         return std::fabs(x);
33 }
34
35 static double MathAcos(double x)
36 {
37         return std::acos(x);
38 }
39
40 static double MathAsin(double x)
41 {
42         return std::asin(x);
43 }
44
45 static double MathAtan(double x)
46 {
47         return std::atan(x);
48 }
49
50 static double MathAtan2(double y, double x)
51 {
52         return std::atan2(y, x);
53 }
54
55 static double MathCeil(double x)
56 {
57         return std::ceil(x);
58 }
59
60 static double MathCos(double x)
61 {
62         return std::cos(x);
63 }
64
65 static double MathExp(double x)
66 {
67         return std::exp(x);
68 }
69
70 static double MathFloor(double x)
71 {
72         return std::floor(x);
73 }
74
75 static double MathLog(double x)
76 {
77         return std::log(x);
78 }
79
80 static Value MathMax(const std::vector<Value>& args)
81 {
82         bool first = true;
83         Value result = -INFINITY;
84
85         for (const Value& arg : args) {
86                 if (first || arg > result) {
87                         first = false;
88                         result = arg;
89                 }
90         }
91
92         return result;
93 }
94
95 static Value MathMin(const std::vector<Value>& args)
96 {
97         bool first = true;
98         Value result = INFINITY;
99
100         for (const Value& arg : args) {
101                 if (first || arg < result) {
102                         first = false;
103                         result = arg;
104                 }
105         }
106
107         return result;
108 }
109
110 static double MathPow(double x, double y)
111 {
112         return std::pow(x, y);
113 }
114
115 static double MathRandom()
116 {
117         return (double)std::rand() / RAND_MAX;
118 }
119
120 static double MathRound(double x)
121 {
122         return boost::math::round(x);
123 }
124
125 static double MathSin(double x)
126 {
127         return std::sin(x);
128 }
129
130 static double MathSqrt(double x)
131 {
132         return std::sqrt(x);
133 }
134
135 static double MathTan(double x)
136 {
137         return std::tan(x);
138 }
139
140 static bool MathIsnan(double x)
141 {
142         return boost::math::isnan(x);
143 }
144
145 static bool MathIsinf(double x)
146 {
147         return boost::math::isinf(x);
148 }
149
150 static double MathSign(double x)
151 {
152         if (x > 0)
153                 return 1;
154         else if (x < 0)
155                 return -1;
156         else
157                 return 0;
158 }
159
160 INITIALIZE_ONCE([]() {
161         Dictionary::Ptr mathObj = new Dictionary({
162                 /* Constants */
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 },
171
172                 /* Methods */
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) }
194         });
195
196         ScriptGlobal::Set("Math", mathObj);
197 });