]> granicus.if.org Git - icinga2/blob - lib/base/math-script.cpp
Merge pull request #7185 from Icinga/bugfix/gelfwriter-wrong-log-facility
[icinga2] / lib / base / math-script.cpp
1 /* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
2
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>
10 #include <cmath>
11
12 using namespace icinga;
13
14 static double MathAbs(double x)
15 {
16         return std::fabs(x);
17 }
18
19 static double MathAcos(double x)
20 {
21         return std::acos(x);
22 }
23
24 static double MathAsin(double x)
25 {
26         return std::asin(x);
27 }
28
29 static double MathAtan(double x)
30 {
31         return std::atan(x);
32 }
33
34 static double MathAtan2(double y, double x)
35 {
36         return std::atan2(y, x);
37 }
38
39 static double MathCeil(double x)
40 {
41         return std::ceil(x);
42 }
43
44 static double MathCos(double x)
45 {
46         return std::cos(x);
47 }
48
49 static double MathExp(double x)
50 {
51         return std::exp(x);
52 }
53
54 static double MathFloor(double x)
55 {
56         return std::floor(x);
57 }
58
59 static double MathLog(double x)
60 {
61         return std::log(x);
62 }
63
64 static Value MathMax(const std::vector<Value>& args)
65 {
66         bool first = true;
67         Value result = -INFINITY;
68
69         for (const Value& arg : args) {
70                 if (first || arg > result) {
71                         first = false;
72                         result = arg;
73                 }
74         }
75
76         return result;
77 }
78
79 static Value MathMin(const std::vector<Value>& args)
80 {
81         bool first = true;
82         Value result = INFINITY;
83
84         for (const Value& arg : args) {
85                 if (first || arg < result) {
86                         first = false;
87                         result = arg;
88                 }
89         }
90
91         return result;
92 }
93
94 static double MathPow(double x, double y)
95 {
96         return std::pow(x, y);
97 }
98
99 static double MathRandom()
100 {
101         return (double)std::rand() / RAND_MAX;
102 }
103
104 static double MathRound(double x)
105 {
106         return boost::math::round(x);
107 }
108
109 static double MathSin(double x)
110 {
111         return std::sin(x);
112 }
113
114 static double MathSqrt(double x)
115 {
116         return std::sqrt(x);
117 }
118
119 static double MathTan(double x)
120 {
121         return std::tan(x);
122 }
123
124 static bool MathIsnan(double x)
125 {
126         return boost::math::isnan(x);
127 }
128
129 static bool MathIsinf(double x)
130 {
131         return boost::math::isinf(x);
132 }
133
134 static double MathSign(double x)
135 {
136         if (x > 0)
137                 return 1;
138         else if (x < 0)
139                 return -1;
140         else
141                 return 0;
142 }
143
144 INITIALIZE_ONCE([]() {
145         auto mathNSBehavior = new ConstNamespaceBehavior();
146         Namespace::Ptr mathNS = new Namespace(mathNSBehavior);
147
148         /* Constants */
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);
157
158         /* Methods */
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));
180
181         mathNSBehavior->Freeze();
182
183         Namespace::Ptr systemNS = ScriptGlobal::Get("System");
184         systemNS->SetAttribute("Math", std::make_shared<ConstEmbeddedNamespaceValue>(mathNS));
185 });