summaryrefslogtreecommitdiffstats
path: root/lib/base/math-script.cpp
blob: 6cd7b0eb4f7afa0d58dbe7c11257a1120a97e620 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */

#include "base/dictionary.hpp"
#include "base/function.hpp"
#include "base/functionwrapper.hpp"
#include "base/scriptframe.hpp"
#include "base/initialize.hpp"
#include "base/namespace.hpp"
#include <boost/math/special_functions/round.hpp>
#include <cmath>

using namespace icinga;

static double MathAbs(double x)
{
	return std::fabs(x);
}

static double MathAcos(double x)
{
	return std::acos(x);
}

static double MathAsin(double x)
{
	return std::asin(x);
}

static double MathAtan(double x)
{
	return std::atan(x);
}

static double MathAtan2(double y, double x)
{
	return std::atan2(y, x);
}

static double MathCeil(double x)
{
	return std::ceil(x);
}

static double MathCos(double x)
{
	return std::cos(x);
}

static double MathExp(double x)
{
	return std::exp(x);
}

static double MathFloor(double x)
{
	return std::floor(x);
}

static double MathLog(double x)
{
	return std::log(x);
}

static Value MathMax(const std::vector<Value>& args)
{
	bool first = true;
	Value result = -INFINITY;

	for (const Value& arg : args) {
		if (first || arg > result) {
			first = false;
			result = arg;
		}
	}

	return result;
}

static Value MathMin(const std::vector<Value>& args)
{
	bool first = true;
	Value result = INFINITY;

	for (const Value& arg : args) {
		if (first || arg < result) {
			first = false;
			result = arg;
		}
	}

	return result;
}

static double MathPow(double x, double y)
{
	return std::pow(x, y);
}

static double MathRandom()
{
	return (double)std::rand() / RAND_MAX;
}

static double MathRound(double x)
{
	return boost::math::round(x);
}

static double MathSin(double x)
{
	return std::sin(x);
}

static double MathSqrt(double x)
{
	return std::sqrt(x);
}

static double MathTan(double x)
{
	return std::tan(x);
}

static bool MathIsnan(double x)
{
	return boost::math::isnan(x);
}

static bool MathIsinf(double x)
{
	return boost::math::isinf(x);
}

static double MathSign(double x)
{
	if (x > 0)
		return 1;
	else if (x < 0)
		return -1;
	else
		return 0;
}

INITIALIZE_ONCE([]() {
	Namespace::Ptr mathNS = new Namespace(true);

	/* Constants */
	mathNS->Set("E", 2.71828182845904523536);
	mathNS->Set("LN2", 0.693147180559945309417);
	mathNS->Set("LN10", 2.30258509299404568402);
	mathNS->Set("LOG2E", 1.44269504088896340736);
	mathNS->Set("LOG10E", 0.434294481903251827651);
	mathNS->Set("PI", 3.14159265358979323846);
	mathNS->Set("SQRT1_2", 0.707106781186547524401);
	mathNS->Set("SQRT2", 1.41421356237309504880);

	/* Methods */
	mathNS->Set("abs", new Function("Math#abs", MathAbs, { "x" }, true));
	mathNS->Set("acos", new Function("Math#acos", MathAcos, { "x" }, true));
	mathNS->Set("asin", new Function("Math#asin", MathAsin, { "x" }, true));
	mathNS->Set("atan", new Function("Math#atan", MathAtan, { "x" }, true));
	mathNS->Set("atan2", new Function("Math#atan2", MathAtan2, { "x", "y" }, true));
	mathNS->Set("ceil", new Function("Math#ceil", MathCeil, { "x" }, true));
	mathNS->Set("cos", new Function("Math#cos", MathCos, { "x" }, true));
	mathNS->Set("exp", new Function("Math#exp", MathExp, { "x" }, true));
	mathNS->Set("floor", new Function("Math#floor", MathFloor, { "x" }, true));
	mathNS->Set("log", new Function("Math#log", MathLog, { "x" }, true));
	mathNS->Set("max", new Function("Math#max", MathMax, {}, true));
	mathNS->Set("min", new Function("Math#min", MathMin, {}, true));
	mathNS->Set("pow", new Function("Math#pow", MathPow, { "x", "y" }, true));
	mathNS->Set("random", new Function("Math#random", MathRandom, {}, true));
	mathNS->Set("round", new Function("Math#round", MathRound, { "x" }, true));
	mathNS->Set("sin", new Function("Math#sin", MathSin, { "x" }, true));
	mathNS->Set("sqrt", new Function("Math#sqrt", MathSqrt, { "x" }, true));
	mathNS->Set("tan", new Function("Math#tan", MathTan, { "x" }, true));
	mathNS->Set("isnan", new Function("Math#isnan", MathIsnan, { "x" }, true));
	mathNS->Set("isinf", new Function("Math#isinf", MathIsinf, { "x" }, true));
	mathNS->Set("sign", new Function("Math#sign", MathSign, { "x" }, true));

	mathNS->Freeze();

	Namespace::Ptr systemNS = ScriptGlobal::Get("System");
	systemNS->Set("Math", mathNS, true);
});