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