diff options
Diffstat (limited to 'mathlib.c')
-rw-r--r-- | mathlib.c | 221 |
1 files changed, 0 insertions, 221 deletions
diff --git a/mathlib.c b/mathlib.c deleted file mode 100644 index 3be26b67..00000000 --- a/mathlib.c +++ /dev/null | |||
@@ -1,221 +0,0 @@ | |||
1 | /* | ||
2 | ** mathlib.c | ||
3 | ** Mathematics library to LUA | ||
4 | */ | ||
5 | |||
6 | char *rcs_mathlib="$Id: mathlib.c,v 1.25 1997/06/19 18:03:04 roberto Exp roberto $"; | ||
7 | |||
8 | #include <stdlib.h> | ||
9 | #include <math.h> | ||
10 | |||
11 | #include "lualib.h" | ||
12 | #include "auxlib.h" | ||
13 | #include "lua.h" | ||
14 | |||
15 | #ifndef PI | ||
16 | #define PI 3.14159265358979323846 | ||
17 | #endif | ||
18 | #define TODEGREE(a) ((a)*180.0/PI) | ||
19 | #define TORAD(a) ((a)*PI/180.0) | ||
20 | |||
21 | static void math_abs (void) | ||
22 | { | ||
23 | double d = luaL_check_number(1); | ||
24 | if (d < 0) d = -d; | ||
25 | lua_pushnumber (d); | ||
26 | } | ||
27 | |||
28 | |||
29 | static void math_sin (void) | ||
30 | { | ||
31 | double d = luaL_check_number(1); | ||
32 | lua_pushnumber (sin(TORAD(d))); | ||
33 | } | ||
34 | |||
35 | |||
36 | |||
37 | static void math_cos (void) | ||
38 | { | ||
39 | double d = luaL_check_number(1); | ||
40 | lua_pushnumber (cos(TORAD(d))); | ||
41 | } | ||
42 | |||
43 | |||
44 | |||
45 | static void math_tan (void) | ||
46 | { | ||
47 | double d = luaL_check_number(1); | ||
48 | lua_pushnumber (tan(TORAD(d))); | ||
49 | } | ||
50 | |||
51 | |||
52 | static void math_asin (void) | ||
53 | { | ||
54 | double d = luaL_check_number(1); | ||
55 | lua_pushnumber (TODEGREE(asin(d))); | ||
56 | } | ||
57 | |||
58 | |||
59 | static void math_acos (void) | ||
60 | { | ||
61 | double d = luaL_check_number(1); | ||
62 | lua_pushnumber (TODEGREE(acos(d))); | ||
63 | } | ||
64 | |||
65 | |||
66 | static void math_atan (void) | ||
67 | { | ||
68 | double d = luaL_check_number(1); | ||
69 | lua_pushnumber (TODEGREE(atan(d))); | ||
70 | } | ||
71 | |||
72 | |||
73 | static void math_atan2 (void) | ||
74 | { | ||
75 | double d1 = luaL_check_number(1); | ||
76 | double d2 = luaL_check_number(2); | ||
77 | lua_pushnumber (TODEGREE(atan2(d1, d2))); | ||
78 | } | ||
79 | |||
80 | |||
81 | static void math_ceil (void) | ||
82 | { | ||
83 | double d = luaL_check_number(1); | ||
84 | lua_pushnumber (ceil(d)); | ||
85 | } | ||
86 | |||
87 | |||
88 | static void math_floor (void) | ||
89 | { | ||
90 | double d = luaL_check_number(1); | ||
91 | lua_pushnumber (floor(d)); | ||
92 | } | ||
93 | |||
94 | static void math_mod (void) | ||
95 | { | ||
96 | float x = luaL_check_number(1); | ||
97 | float y = luaL_check_number(2); | ||
98 | lua_pushnumber(fmod(x, y)); | ||
99 | } | ||
100 | |||
101 | |||
102 | static void math_sqrt (void) | ||
103 | { | ||
104 | double d = luaL_check_number(1); | ||
105 | lua_pushnumber (sqrt(d)); | ||
106 | } | ||
107 | |||
108 | |||
109 | static void math_pow (void) | ||
110 | { | ||
111 | double d1 = luaL_check_number(1); | ||
112 | double d2 = luaL_check_number(2); | ||
113 | lua_pushnumber(pow(d1,d2)); | ||
114 | } | ||
115 | |||
116 | static void math_min (void) | ||
117 | { | ||
118 | int i=1; | ||
119 | double dmin = luaL_check_number(i); | ||
120 | while (lua_getparam(++i) != LUA_NOOBJECT) | ||
121 | { | ||
122 | double d = luaL_check_number(i); | ||
123 | if (d < dmin) dmin = d; | ||
124 | } | ||
125 | lua_pushnumber (dmin); | ||
126 | } | ||
127 | |||
128 | static void math_max (void) | ||
129 | { | ||
130 | int i=1; | ||
131 | double dmax = luaL_check_number(i); | ||
132 | while (lua_getparam(++i) != LUA_NOOBJECT) | ||
133 | { | ||
134 | double d = luaL_check_number(i); | ||
135 | if (d > dmax) dmax = d; | ||
136 | } | ||
137 | lua_pushnumber (dmax); | ||
138 | } | ||
139 | |||
140 | static void math_log (void) | ||
141 | { | ||
142 | double d = luaL_check_number(1); | ||
143 | lua_pushnumber (log(d)); | ||
144 | } | ||
145 | |||
146 | |||
147 | static void math_log10 (void) | ||
148 | { | ||
149 | double d = luaL_check_number(1); | ||
150 | lua_pushnumber (log10(d)); | ||
151 | } | ||
152 | |||
153 | |||
154 | static void math_exp (void) | ||
155 | { | ||
156 | double d = luaL_check_number(1); | ||
157 | lua_pushnumber (exp(d)); | ||
158 | } | ||
159 | |||
160 | static void math_deg (void) | ||
161 | { | ||
162 | float d = luaL_check_number(1); | ||
163 | lua_pushnumber (d*180./PI); | ||
164 | } | ||
165 | |||
166 | static void math_rad (void) | ||
167 | { | ||
168 | float d = luaL_check_number(1); | ||
169 | lua_pushnumber (d/180.*PI); | ||
170 | } | ||
171 | |||
172 | static void math_random (void) | ||
173 | { | ||
174 | double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; | ||
175 | if (lua_getparam(1) == LUA_NOOBJECT) | ||
176 | lua_pushnumber(r); | ||
177 | else | ||
178 | lua_pushnumber((int)(r*luaL_check_number(1)) + 1); | ||
179 | } | ||
180 | |||
181 | static void math_randomseed (void) | ||
182 | { | ||
183 | srand(luaL_check_number(1)); | ||
184 | } | ||
185 | |||
186 | |||
187 | static struct luaL_reg mathlib[] = { | ||
188 | {"abs", math_abs}, | ||
189 | {"sin", math_sin}, | ||
190 | {"cos", math_cos}, | ||
191 | {"tan", math_tan}, | ||
192 | {"asin", math_asin}, | ||
193 | {"acos", math_acos}, | ||
194 | {"atan", math_atan}, | ||
195 | {"atan2", math_atan2}, | ||
196 | {"ceil", math_ceil}, | ||
197 | {"floor", math_floor}, | ||
198 | {"mod", math_mod}, | ||
199 | {"sqrt", math_sqrt}, | ||
200 | {"min", math_min}, | ||
201 | {"max", math_max}, | ||
202 | {"log", math_log}, | ||
203 | {"log10", math_log10}, | ||
204 | {"exp", math_exp}, | ||
205 | {"deg", math_deg}, | ||
206 | {"rad", math_rad}, | ||
207 | {"random", math_random}, | ||
208 | {"randomseed", math_randomseed} | ||
209 | }; | ||
210 | |||
211 | /* | ||
212 | ** Open math library | ||
213 | */ | ||
214 | void mathlib_open (void) | ||
215 | { | ||
216 | luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); | ||
217 | lua_pushcfunction(math_pow); | ||
218 | lua_pushnumber(0); /* to get its tag */ | ||
219 | lua_settagmethod(lua_tag(lua_pop()), "pow"); | ||
220 | } | ||
221 | |||