]> git.mxchange.org Git - simgear.git/blob - simgear/nasal/mathlib.c
Cygwin fixes.
[simgear.git] / simgear / nasal / mathlib.c
1 #ifdef _MSC_VER
2 #define _USE_MATH_DEFINES
3 #endif
4
5 #include <math.h>
6 #include <string.h>
7
8 #include "nasal.h"
9
10 static naRef f_sin(naContext c, naRef me, int argc, naRef* args)
11 {
12     naRef a = naNumValue(argc > 0 ? args[0] : naNil());
13     if(naIsNil(a))
14         naRuntimeError(c, "non numeric argument to sin()");
15     a.num = sin(a.num);
16     return a;
17 }
18
19 static naRef f_cos(naContext c, naRef me, int argc, naRef* args)
20 {
21     naRef a = naNumValue(argc > 0 ? args[0] : naNil());
22     if(naIsNil(a))
23         naRuntimeError(c, "non numeric argument to cos()");
24     a.num = cos(a.num);
25     return a;
26 }
27
28 static naRef f_exp(naContext c, naRef me, int argc, naRef* args)
29 {
30     naRef a = naNumValue(argc > 0 ? args[0] : naNil());
31     if(naIsNil(a))
32         naRuntimeError(c, "non numeric argument to exp()");
33     a.num = exp(a.num);
34     return a;
35 }
36
37 static naRef f_ln(naContext c, naRef me, int argc, naRef* args)
38 {
39     naRef a = naNumValue(argc > 0 ? args[0] : naNil());
40     if(naIsNil(a))
41         naRuntimeError(c, "non numeric argument to ln()");
42     a.num = log(a.num);
43     return a;
44 }
45
46 static naRef f_sqrt(naContext c, naRef me, int argc, naRef* args)
47 {
48     naRef a = naNumValue(argc > 0 ? args[0] : naNil());
49     if(naIsNil(a))
50         naRuntimeError(c, "non numeric argument to sqrt()");
51     a.num = sqrt(a.num);
52     return a;
53 }
54
55 static naRef f_atan2(naContext c, naRef me, int argc, naRef* args)
56 {
57     naRef a = naNumValue(argc > 0 ? args[0] : naNil());
58     naRef b = naNumValue(argc > 1 ? args[1] : naNil());
59     if(naIsNil(a) || naIsNil(b))
60         naRuntimeError(c, "non numeric argument to atan2()");
61     a.num = atan2(a.num, b.num);
62     return a;
63 }
64
65 static struct func { char* name; naCFunction func; } funcs[] = {
66     { "sin", f_sin },
67     { "cos", f_cos },
68     { "exp", f_exp },
69     { "ln", f_ln },
70     { "sqrt", f_sqrt },
71     { "atan2", f_atan2 },
72 };
73
74 naRef naMathLib(naContext c)
75 {
76     naRef name, namespace = naNewHash(c);
77     int i, n = sizeof(funcs)/sizeof(struct func);
78     for(i=0; i<n; i++) {
79         naRef code = naNewCCode(c, funcs[i].func);
80         naRef name = naStr_fromdata(naNewString(c),
81                                     funcs[i].name, strlen(funcs[i].name));
82         naHash_set(namespace, name, naNewFunc(c, code));
83     }
84
85     // Set up constants for math.pi and math.e.  Can't use M_PI or
86     // M_E, becuase those aren't technically part of the C standard.  Sigh.
87     name = naStr_fromdata(naNewString(c), "pi", 2);
88     naHash_set(namespace, name, naNum(3.14159265358979323846));
89
90     name = naStr_fromdata(naNewString(c), "e", 1);
91     name = naInternSymbol(name);
92     naHash_set(namespace, name, naNum(2.7182818284590452354));
93
94     return namespace;
95 }