This source file includes following definitions.
- math_abs
- math_sin
- math_sinh
- math_cos
- math_cosh
- math_tan
- math_tanh
- math_asin
- math_acos
- math_atan
- math_atan2
- math_ceil
- math_floor
- math_fmod
- math_modf
- math_sqrt
- math_pow
- math_log
- math_log10
- math_exp
- math_deg
- math_rad
- math_frexp
- math_ldexp
- math_min
- math_max
- math_random
- math_randomseed
- luaopen_math
1
2
3
4
5
6
7
8 #include <stdlib.h>
9 #include <math.h>
10
11 #define lmathlib_c
12 #define LUA_LIB
13
14 #include "lua.h"
15
16 #include "lauxlib.h"
17 #include "lualib.h"
18
19
20 #undef PI
21 #define PI (3.14159265358979323846)
22 #define RADIANS_PER_DEGREE (PI/180.0)
23
24
25
26 static int math_abs (lua_State *L) {
27 #if 0
28 lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
29 #else
30 lua_pushnumber(L, abs(luaL_checknumber(L, 1)));
31 #endif
32 return 1;
33 }
34
35 #if 0
36 static int math_sin (lua_State *L) {
37 lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
38 return 1;
39 }
40
41 static int math_sinh (lua_State *L) {
42 lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
43 return 1;
44 }
45
46 static int math_cos (lua_State *L) {
47 lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
48 return 1;
49 }
50
51 static int math_cosh (lua_State *L) {
52 lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
53 return 1;
54 }
55
56 static int math_tan (lua_State *L) {
57 lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
58 return 1;
59 }
60
61 static int math_tanh (lua_State *L) {
62 lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
63 return 1;
64 }
65
66 static int math_asin (lua_State *L) {
67 lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
68 return 1;
69 }
70
71 static int math_acos (lua_State *L) {
72 lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
73 return 1;
74 }
75
76 static int math_atan (lua_State *L) {
77 lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
78 return 1;
79 }
80
81 static int math_atan2 (lua_State *L) {
82 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
83 return 1;
84 }
85
86 static int math_ceil (lua_State *L) {
87 lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
88 return 1;
89 }
90
91 static int math_floor (lua_State *L) {
92 lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
93 return 1;
94 }
95
96 static int math_fmod (lua_State *L) {
97 lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
98 return 1;
99 }
100
101 static int math_modf (lua_State *L) {
102 double ip;
103 double fp = modf(luaL_checknumber(L, 1), &ip);
104 lua_pushnumber(L, ip);
105 lua_pushnumber(L, fp);
106 return 2;
107 }
108
109 static int math_sqrt (lua_State *L) {
110 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
111 return 1;
112 }
113 #endif
114 static int math_pow (lua_State *L) {
115 #if 0
116 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
117 #else
118 lua_pushnumber(L, luai_ipow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
119 #endif
120 return 1;
121 }
122 #if 0
123 static int math_log (lua_State *L) {
124 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
125 return 1;
126 }
127
128 static int math_log10 (lua_State *L) {
129 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
130 return 1;
131 }
132
133 static int math_exp (lua_State *L) {
134 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
135 return 1;
136 }
137
138 static int math_deg (lua_State *L) {
139 lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
140 return 1;
141 }
142
143 static int math_rad (lua_State *L) {
144 lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
145 return 1;
146 }
147
148 static int math_frexp (lua_State *L) {
149 int e;
150 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
151 lua_pushinteger(L, e);
152 return 2;
153 }
154
155 static int math_ldexp (lua_State *L) {
156 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
157 return 1;
158 }
159 #endif
160
161
162 static int math_min (lua_State *L) {
163 int n = lua_gettop(L);
164 lua_Number dmin = luaL_checknumber(L, 1);
165 int i;
166 for (i=2; i<=n; i++) {
167 lua_Number d = luaL_checknumber(L, i);
168 if (d < dmin)
169 dmin = d;
170 }
171 lua_pushnumber(L, dmin);
172 return 1;
173 }
174
175
176 static int math_max (lua_State *L) {
177 int n = lua_gettop(L);
178 lua_Number dmax = luaL_checknumber(L, 1);
179 int i;
180 for (i=2; i<=n; i++) {
181 lua_Number d = luaL_checknumber(L, i);
182 if (d > dmax)
183 dmax = d;
184 }
185 lua_pushnumber(L, dmax);
186 return 1;
187 }
188
189
190 static int math_random (lua_State *L) {
191
192
193 #if 0
194 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
195 #endif
196 switch (lua_gettop(L)) {
197 #if 0
198 case 0: {
199 lua_pushnumber(L, r);
200 break;
201 }
202 #endif
203 case 1: {
204 int u = luaL_checkint(L, 1);
205 luaL_argcheck(L, 1<=u, 1, "interval is empty");
206 #if 0
207 lua_pushnumber(L, floor(r*u)+1);
208 #else
209 lua_pushnumber(L, rand()%u+1);
210 #endif
211 break;
212 }
213 case 2: {
214 int l = luaL_checkint(L, 1);
215 int u = luaL_checkint(L, 2);
216 luaL_argcheck(L, l<=u, 2, "interval is empty");
217 #if 0
218 lua_pushnumber(L, floor(r*(u-l+1))+l);
219 #else
220 lua_pushnumber(L, rand()%(u-l+1)+l);
221 #endif
222 break;
223 }
224 default: return luaL_error(L, "wrong number of arguments");
225 }
226 return 1;
227 }
228
229
230 static int math_randomseed (lua_State *L) {
231 srand(luaL_checkint(L, 1));
232 return 0;
233 }
234
235
236 static const luaL_Reg mathlib[] = {
237 {"abs", math_abs},
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253 {"max", math_max},
254 {"min", math_min},
255
256 {"pow", math_pow},
257
258 {"random", math_random},
259 {"randomseed", math_randomseed},
260
261
262
263
264
265 {NULL, NULL}
266 };
267
268
269
270
271
272 LUALIB_API int luaopen_math (lua_State *L) {
273 luaL_register(L, LUA_MATHLIBNAME, mathlib);
274 #if 0
275 lua_pushnumber(L, PI);
276 lua_setfield(L, -2, "pi");
277 lua_pushnumber(L, HUGE_VAL);
278 lua_setfield(L, -2, "huge");
279 #endif
280 #if defined(LUA_COMPAT_MOD)
281 lua_getfield(L, -1, "fmod");
282 lua_setfield(L, -2, "mod");
283 #endif
284 return 1;
285 }
286