summaryrefslogtreecommitdiffstats
path: root/src/modules/math/libkvimath.cpp
blob: 63a29787a81e54a420b44ac7a89bf9becf0c2972 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//=============================================================================
//
//   File : libkvimath.cpp
//   Creation date : Sat Jan 13 14:00:12 2006 GMT by Szymon Stefanek
//
//   This math is part of the KVirc irc client distribution
//   Copyright (C) 2006 Szymon Stefanek (pragma at kvirc dot net)
//
//   This program is FREE software. You can redistribute it and/or
//   modify it under the terms of the GNU General Public License
//   as published by the Free Software Foundation; either version 2
//   of the License, or (at your opinion) any later version.
//
//   This program is distributed in the HOPE that it will be USEFUL,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
//   See the GNU General Public License for more details.
//
//   You should have received a copy of the GNU General Public License
//   along with this program. If not, write to the Free Software Foundation,
//   Inc. ,51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================

#include "kvi_settings.h"
#include "kvi_module.h"
#include "kvi_string.h"

#include <math.h>

// Workaround for lost isnan and isinf definitions after
// inclusion of iostream.h on some MacOS X systems
#ifdef Q_OS_MACX
	#ifndef isnan
		extern "C" int isnan (double);
	#endif
	#ifndef isinf
		extern "C" int isinf (double);
	#endif
#endif

#define MATH_KVS_1PARAM_FUNCTION(__fncname,__paramname,__mathcallname) \
static bool __fncname(KviKvsModuleFunctionCall * c) \
{ \
	kvs_real_t dReal; \
	KVSM_PARAMETERS_BEGIN(c) \
		KVSM_PARAMETER(__paramname,KVS_PT_REAL,0,dReal) \
	KVSM_PARAMETERS_END(c) \
	c->returnValue()->setReal(__mathcallname(dReal)); \
	return true; \
}

#ifdef COMPILE_ON_WINDOWS
// dobbiamo testare meglio, i risultati.
static double cbrt(double x)
{
	if (x > 0.0) return pow(x, 1.0/3.0);
	else return -pow(-x, 1.0/3.0);
}

static int isinf (double d) {
	int expon = 0;
	double val = frexp (d, &expon);
	if (expon == 1025) {
		if (val == 0.5) return 1;
		else if (val == -0.5) return -1;
		else return 0;
	} else {
		return 0;
	}
}
static int isnan (double d) {
	int expon = 0;
	double val = frexp (d, &expon);
	if (expon == 1025) {
		if (val == 0.5) return 0;
		else if (val == -0.5) return 0;
		else return 1;
	} else {
		return 0;
	}
}
#endif
/*
	@doc: math.sin
	@type:
		function
	@title:
		$math.sin
	@short:
		Returns the sinus of the specified angle in radiants
	@syntax:
		<real> $math.sin(<angle:real>)
	@description:
		Returns the sinus of the specified angle in radiants
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_sin,"angle",sin)

/*
	@doc: math.cos
	@type:
		function
	@title:
		$math.cos
	@short:
		Returns the cosinus of the specified angle in radiants
	@syntax:
		<real> $math.cos(<angle:real>)
	@description:
		Returns the cosinus of the specified angle in radiants
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_cos,"angle",cos)

/*
	@doc: math.tan
	@type:
		function
	@title:
		$math.tan
	@short:
		Returns the tangent of the specified angle in radiants
	@syntax:
		<real> $math.tan(<angle:real>)
	@description:
		Returns the tangent of the specified angle in radiants
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_tan,"angle",tan)

/*
	@doc: math.asin
	@type:
		function
	@title:
		$math.asin
	@short:
		Returns the angle in radiants that has the specified sinus value.
	@syntax:
		<real> $math.asin(<sinus:real>)
	@description:
		Returns the angle in radiants that has the specified sinus value.
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_asin,"sinus",asin)

/*
	@doc: math.acos
	@type:
		function
	@title:
		$math.acos
	@short:
		Returns the angle in radiants that has the specified cosinus value.
	@syntax:
		<real> $math.acos(<cosinus:real>)
	@description:
		Returns the angle in radiants that has the specified cosinus value.
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_acos,"cosinus",acos)


/*
	@doc: math.atan
	@type:
		function
	@title:
		$math.atan
	@short:
		Returns the angle in radiants that has the specified tangent value.
	@syntax:
		<real> $math.atan(<tangent:real>)
	@description:
		Returns the angle in radiants that has the specified tangent value.
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_atan,"tangent",atan)

/*
	@doc: math.sqrt
	@type:
		function
	@title:
		$math.sqrt
	@short:
		Returns the square root of a number
	@syntax:
		<real> $math.sqrt(<number:real>)
	@description:
		Returns the square root of the specified number
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_sqrt,"number",sqrt)

/*
	@doc: math.cbrt
	@type:
		function
	@title:
		$math.cbrt
	@short:
		Returns the cube root of a number
	@syntax:
		<real> $math.cbrt(<number:real>)
	@description:
		Returns the cube root of the specified number
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_cbrt,"number",cbrt)

/*
	@doc: math.abs
	@type:
		function
	@title:
		$math.abs
	@short:
		Returns the absolute value of a number
	@syntax:
		<real> $math.abs(<number:real>)
	@description:
		Returns the absolute value of the specified number
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_abs,"number",fabs)

/*
	@doc: math.floor
	@type:
		function
	@title:
		$math.floor
	@short:
		Rounds down to the nearest integer
	@syntax:
		<real> $math.floor(<number:real>)
	@description:
		Rounds down number to the nearest integer
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_floor,"number",floor)

/*
	@doc: math.ceil
	@type:
		function
	@title:
		$math.ceil
	@short:
		Rounds up to the nearest integer
	@syntax:
		<real> $math.ceil(<number:real>)
	@description:
		Rounds up number to the nearest integer
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_ceil,"number",ceil)

/*
	@doc: math.exp
	@type:
		function
	@title:
		$math.exp
	@short:
		Returns the exponential of the specified number
	@syntax:
		<real> $math.exp(<number:real>)
	@description:
		Returns the exponential of the specified number
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_exp,"number",exp)

/*
	@doc: math.log
	@type:
		function
	@title:
		$math.log
	@short:
		Returns the natural logarithm of the specified number
	@syntax:
		<real> $math.log(<number:real>)
	@description:
		Returns the natural (base e) logarithm of the specified number
*/

MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_log,"number",log)


/*
	@doc: math.log10
	@type:
		function
	@title:
		$math.log10
	@short:
		Returns the base 10 logarithm of the specified number
	@syntax:
		<real> $math.log10(<number:real>)
	@description:
		Returns the base 10 logarithm of the specified number
*/
MATH_KVS_1PARAM_FUNCTION(math_kvs_fnc_log10,"number",log10)

/*
	@doc: math.isnan
	@type:
		function
	@title:
		$math.isnan
	@short:
		Checks if the value is not a number
	@syntax:
		<boolean> $math.isnan(<value:real>)
	@description:
		Checks if the specified value is a valid number
		or NaN (Not a Number). NaN is returned by several
		math functions when the result would be undefined.
*/
static bool math_kvs_fnc_isnan(KviKvsModuleFunctionCall * c)
{
	kvs_real_t dReal;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("value",KVS_PT_REAL,0,dReal)
	KVSM_PARAMETERS_END(c)
	c->returnValue()->setBoolean(isnan(dReal));
	return true;
}

/*
	@doc: math.isinf
	@type:
		function
	@title:
		$math.isinf
	@short:
		Checks if the value is the infinity
	@syntax:
		<boolean> $math.isinf(<value:real>)
	@description:
		Checks if the specified value is the infinity.
*/
#ifdef COMPILE_ON_WINDOWS
static bool math_kvs_fnc_isinf(KviKvsModuleFunctionCall * c)
{
	kvs_real_t dReal;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("value",KVS_PT_REAL,0,dReal)
	KVSM_PARAMETERS_END(c)
	c->returnValue()->setBoolean(sinf(dReal));
	return true;
}
#else
static bool math_kvs_fnc_isinf(KviKvsModuleFunctionCall * c)
{
	kvs_real_t dReal;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("value",KVS_PT_REAL,0,dReal)
	KVSM_PARAMETERS_END(c)
	c->returnValue()->setBoolean(isinf(dReal));
	return true;
}
#endif //COMPILE_ON_WINDOWS
/*
	@doc: math.pow
	@type:
		function
	@title:
		$math.pow
	@short:
		Returns a to the power of b
	@syntax:
		<real> $math.pow(<a:real>,<b:real>)
	@description:
		Returns a to the power of b
*/
static bool math_kvs_fnc_pow(KviKvsModuleFunctionCall * c)
{
	kvs_real_t dA,dB;
	KVSM_PARAMETERS_BEGIN(c)
		KVSM_PARAMETER("a",KVS_PT_REAL,0,dA)
		KVSM_PARAMETER("b",KVS_PT_REAL,0,dB)
	KVSM_PARAMETERS_END(c)
	c->returnValue()->setReal(pow(dA,dB));
	return true;
}

/*
	@doc: math.pi
	@type:
		function
	@title:
		$math.pi
	@short:
		Returns the value of pi
	@syntax:
		<real> $math.pi()
	@description:
		Returns the value of pi
*/
#ifdef COMPILE_ON_WINDOWS
static bool math_kvs_fnc_pi(KviKvsModuleFunctionCall * c)
{
	c->returnValue()->setReal(3.141592653589793238462643383279502884197169399375);
	return true;
}
#else
static bool math_kvs_fnc_pi(KviKvsModuleFunctionCall * c)
{
	c->returnValue()->setReal(M_PI);
	return true;
}
#endif
/*
	@doc: math.e
	@type:
		function
	@title:
		$math.e
	@short:
		Returns the value of e
	@syntax:
		<real> $math.e()
	@description:
		Returns the value of the euler constant e
*/


#ifdef COMPILE_ON_WINDOWS
static bool math_kvs_fnc_e(KviKvsModuleFunctionCall * c)
{
	c->returnValue()->setReal(1.56903485);
	return true;
}
#else
static bool math_kvs_fnc_e(KviKvsModuleFunctionCall * c)
{
	c->returnValue()->setReal(M_E);
	return true;
}
#endif
static bool math_module_init(KviModule * m)
{
	KVSM_REGISTER_FUNCTION(m,"sin",math_kvs_fnc_sin);
	KVSM_REGISTER_FUNCTION(m,"cos",math_kvs_fnc_cos);
	KVSM_REGISTER_FUNCTION(m,"tan",math_kvs_fnc_tan);
	KVSM_REGISTER_FUNCTION(m,"asin",math_kvs_fnc_asin);
	KVSM_REGISTER_FUNCTION(m,"acos",math_kvs_fnc_acos);
	KVSM_REGISTER_FUNCTION(m,"atan",math_kvs_fnc_atan);
	KVSM_REGISTER_FUNCTION(m,"sqrt",math_kvs_fnc_sqrt);
	KVSM_REGISTER_FUNCTION(m,"cbrt",math_kvs_fnc_cbrt);
	KVSM_REGISTER_FUNCTION(m,"abs",math_kvs_fnc_abs);
	KVSM_REGISTER_FUNCTION(m,"floor",math_kvs_fnc_floor);
	KVSM_REGISTER_FUNCTION(m,"ceil",math_kvs_fnc_ceil);
	KVSM_REGISTER_FUNCTION(m,"pow",math_kvs_fnc_pow);
	KVSM_REGISTER_FUNCTION(m,"exp",math_kvs_fnc_exp);
	KVSM_REGISTER_FUNCTION(m,"log",math_kvs_fnc_log);
	KVSM_REGISTER_FUNCTION(m,"log10",math_kvs_fnc_log10);
	KVSM_REGISTER_FUNCTION(m,"pi",math_kvs_fnc_pi);
	KVSM_REGISTER_FUNCTION(m,"e",math_kvs_fnc_e);
	KVSM_REGISTER_FUNCTION(m,"isnan",math_kvs_fnc_isnan);
	KVSM_REGISTER_FUNCTION(m,"isinf",math_kvs_fnc_isinf);
	return true;
}


static bool math_module_cleanup(KviModule *m)
{
	return true;
}

KVIRC_MODULE(
	"Math",                                                 // module name
	"1.0.0",                                                // module version
	"Copyright (C) 2006 Szymon Stefanek (pragma at kvirc dot net),"\
	"Tonino Imbesi (grifisx at barmes dot org)," \
	"Alessandro Carbone (noldor at barmes dot org)",
	"Mathematical function module",
	math_module_init,
	0,
	0,
	math_module_cleanup
)