summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lmathlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'apps/plugins/lua/lmathlib.c')
-rw-r--r--apps/plugins/lua/lmathlib.c92
1 files changed, 39 insertions, 53 deletions
diff --git a/apps/plugins/lua/lmathlib.c b/apps/plugins/lua/lmathlib.c
index 0b288e6a12..99a104050c 100644
--- a/apps/plugins/lua/lmathlib.c
+++ b/apps/plugins/lua/lmathlib.c
@@ -1,12 +1,14 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.83.1.1 2013/04/12 18:48:47 roberto Exp $ 2** $Id: lmathlib.c,v 1.67.1.1 2007/12/27 13:02:25 roberto Exp $
3** Standard mathematical library 3** Standard mathematical library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
6 6
7 7
8#if 0
8#include <stdlib.h> 9#include <stdlib.h>
9#include <math.h> 10#include <math.h>
11#endif
10 12
11#define lmathlib_c 13#define lmathlib_c
12#define LUA_LIB 14#define LUA_LIB
@@ -18,9 +20,9 @@
18 20
19 21
20#undef PI 22#undef PI
21#define PI ((lua_Number)(3.1415926535897932384626433832795)) 23#define PI (3.14159265358979323846)
22#define RADIANS_PER_DEGREE ((lua_Number)(PI/180.0)) 24#define RADIANS_PER_DEGREE (PI/180.0)
23#define DEGREES_PER_RADIAN ((lua_Number)180.0/PI) 25#define DEGREES_PER_RADIAN (180.0/PI)
24 26
25 27
26static int math_abs (lua_State *L) { 28static int math_abs (lua_State *L) {
@@ -32,53 +34,52 @@ static int math_abs (lua_State *L) {
32 34
33#if 0 35#if 0
34static int math_sin (lua_State *L) { 36static int math_sin (lua_State *L) {
35 lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1))); 37 lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
36 return 1; 38 return 1;
37} 39}
38 40
39static int math_sinh (lua_State *L) { 41static int math_sinh (lua_State *L) {
40 lua_pushnumber(L, l_mathop(sinh)(luaL_checknumber(L, 1))); 42 lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
41 return 1; 43 return 1;
42} 44}
43 45
44static int math_cos (lua_State *L) { 46static int math_cos (lua_State *L) {
45 lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1))); 47 lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
46 return 1; 48 return 1;
47} 49}
48 50
49static int math_cosh (lua_State *L) { 51static int math_cosh (lua_State *L) {
50 lua_pushnumber(L, l_mathop(cosh)(luaL_checknumber(L, 1))); 52 lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
51 return 1; 53 return 1;
52} 54}
53 55
54static int math_tan (lua_State *L) { 56static int math_tan (lua_State *L) {
55 lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1))); 57 lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
56 return 1; 58 return 1;
57} 59}
58 60
59static int math_tanh (lua_State *L) { 61static int math_tanh (lua_State *L) {
60 lua_pushnumber(L, l_mathop(tanh)(luaL_checknumber(L, 1))); 62 lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
61 return 1; 63 return 1;
62} 64}
63 65
64static int math_asin (lua_State *L) { 66static int math_asin (lua_State *L) {
65 lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1))); 67 lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
66 return 1; 68 return 1;
67} 69}
68 70
69static int math_acos (lua_State *L) { 71static int math_acos (lua_State *L) {
70 lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1))); 72 lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
71 return 1; 73 return 1;
72} 74}
73 75
74static int math_atan (lua_State *L) { 76static int math_atan (lua_State *L) {
75 lua_pushnumber(L, l_mathop(atan)(luaL_checknumber(L, 1))); 77 lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
76 return 1; 78 return 1;
77} 79}
78 80
79static int math_atan2 (lua_State *L) { 81static int math_atan2 (lua_State *L) {
80 lua_pushnumber(L, l_mathop(atan2)(luaL_checknumber(L, 1), 82 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
81 luaL_checknumber(L, 2)));
82 return 1; 83 return 1;
83} 84}
84#endif 85#endif
@@ -103,48 +104,35 @@ static int math_fmod (lua_State *L) {
103 104
104#if 0 105#if 0
105static int math_modf (lua_State *L) { 106static int math_modf (lua_State *L) {
106 lua_Number ip; 107 double ip;
107 lua_Number fp = l_mathop(modf)(luaL_checknumber(L, 1), &ip); 108 double fp = modf(luaL_checknumber(L, 1), &ip);
108 lua_pushnumber(L, ip); 109 lua_pushnumber(L, ip);
109 lua_pushnumber(L, fp); 110 lua_pushnumber(L, fp);
110 return 2; 111 return 2;
111} 112}
112 113
113static int math_sqrt (lua_State *L) { 114static int math_sqrt (lua_State *L) {
114 lua_pushnumber(L, l_mathop(sqrt)(luaL_checknumber(L, 1))); 115 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
115 return 1; 116 return 1;
116} 117}
117 118
118static int math_pow (lua_State *L) { 119static int math_pow (lua_State *L) {
119 lua_Number x = luaL_checknumber(L, 1); 120 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
120 lua_Number y = luaL_checknumber(L, 2);
121 lua_pushnumber(L, l_mathop(pow)(x, y));
122 return 1; 121 return 1;
123} 122}
124 123
125static int math_log (lua_State *L) { 124static int math_log (lua_State *L) {
126 lua_Number x = luaL_checknumber(L, 1); 125 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
127 lua_Number res;
128 if (lua_isnoneornil(L, 2))
129 res = l_mathop(log)(x);
130 else {
131 lua_Number base = luaL_checknumber(L, 2);
132 if (base == (lua_Number)10.0) res = l_mathop(log10)(x);
133 else res = l_mathop(log)(x)/l_mathop(log)(base);
134 }
135 lua_pushnumber(L, res);
136 return 1; 126 return 1;
137} 127}
138 128
139#if defined(LUA_COMPAT_LOG10)
140static int math_log10 (lua_State *L) { 129static int math_log10 (lua_State *L) {
141 lua_pushnumber(L, l_mathop(log10)(luaL_checknumber(L, 1))); 130 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
142 return 1; 131 return 1;
143} 132}
144#endif
145 133
146static int math_exp (lua_State *L) { 134static int math_exp (lua_State *L) {
147 lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1))); 135 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
148 return 1; 136 return 1;
149} 137}
150#endif 138#endif
@@ -152,7 +140,6 @@ static int math_deg (lua_State *L) {
152 lua_pushnumber(L, luaL_checknumber(L, 1)*DEGREES_PER_RADIAN); 140 lua_pushnumber(L, luaL_checknumber(L, 1)*DEGREES_PER_RADIAN);
153 return 1; 141 return 1;
154} 142}
155
156static int math_rad (lua_State *L) { 143static int math_rad (lua_State *L) {
157 lua_pushnumber(L, (luaL_checknumber(L, 1)*100)/(DEGREES_PER_RADIAN*100)); 144 lua_pushnumber(L, (luaL_checknumber(L, 1)*100)/(DEGREES_PER_RADIAN*100));
158 return 1; 145 return 1;
@@ -161,15 +148,13 @@ static int math_rad (lua_State *L) {
161#if 0 148#if 0
162static int math_frexp (lua_State *L) { 149static int math_frexp (lua_State *L) {
163 int e; 150 int e;
164 lua_pushnumber(L, l_mathop(frexp)(luaL_checknumber(L, 1), &e)); 151 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
165 lua_pushinteger(L, e); 152 lua_pushinteger(L, e);
166 return 2; 153 return 2;
167} 154}
168 155
169static int math_ldexp (lua_State *L) { 156static int math_ldexp (lua_State *L) {
170 lua_Number x = luaL_checknumber(L, 1); 157 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
171 int ep = luaL_checkint(L, 2);
172 lua_pushnumber(L, l_mathop(ldexp)(x, ep));
173 return 1; 158 return 1;
174} 159}
175#endif 160#endif
@@ -212,16 +197,16 @@ static int math_random (lua_State *L) {
212 break; 197 break;
213 } 198 }
214 case 1: { /* only upper limit */ 199 case 1: { /* only upper limit */
215 lua_Number u = luaL_checknumber(L, 1); 200 int u = luaL_checkint(L, 1);
216 luaL_argcheck(L, 1 <= u, 1, "interval is empty"); 201 luaL_argcheck(L, 1<=u, 1, "interval is empty");
217 lua_pushnumber(L, r%u+1); /* int between 1 and `u' */ 202 lua_pushnumber(L, r%u+1); /* int between 1 and `u' */
218 break; 203 break;
219 } 204 }
220 case 2: { /* lower and upper limits */ 205 case 2: { /* lower and upper limits */
221 lua_Number l = luaL_checknumber(L, 1); 206 int l = luaL_checkint(L, 1);
222 lua_Number u = luaL_checknumber(L, 2); 207 int u = luaL_checkint(L, 2);
223 luaL_argcheck(L, l <= u, 2, "interval is empty"); 208 luaL_argcheck(L, l<=u, 2, "interval is empty");
224 lua_pushnumber(L, r%(u-l+1)+l); /* [l, u] */ 209 lua_pushnumber(L, r%(u-l+1)+l); /* int between `l' and `u' */
225 break; 210 break;
226 } 211 }
227 default: return luaL_error(L, "wrong number of arguments"); 212 default: return luaL_error(L, "wrong number of arguments");
@@ -231,7 +216,7 @@ static int math_random (lua_State *L) {
231 216
232 217
233static int math_randomseed (lua_State *L) { 218static int math_randomseed (lua_State *L) {
234 rb->srand(luaL_checkunsigned(L, 1)); 219 rb->srand(luaL_checkint(L, 1));
235 return 0; 220 return 0;
236} 221}
237 222
@@ -258,9 +243,7 @@ static const luaL_Reg mathlib[] = {
258#if 0 243#if 0
259 {"frexp", math_frexp}, 244 {"frexp", math_frexp},
260 {"ldexp", math_ldexp}, 245 {"ldexp", math_ldexp},
261#if defined(LUA_COMPAT_LOG10)
262 {"log10", math_log10}, 246 {"log10", math_log10},
263#endif
264 {"log", math_log}, 247 {"log", math_log},
265#endif 248#endif
266 {"max", math_max}, 249 {"max", math_max},
@@ -276,7 +259,7 @@ static const luaL_Reg mathlib[] = {
276 {"sinh", math_sinh}, 259 {"sinh", math_sinh},
277 {"sin", math_sin}, 260 {"sin", math_sin},
278 {"sqrt", math_sqrt}, 261 {"sqrt", math_sqrt},
279 {"tanh", math_tanh}, 262 {"tanh", math_tanh},
280 {"tan", math_tan}, 263 {"tan", math_tan},
281#endif 264#endif
282 {NULL, NULL} 265 {NULL, NULL}
@@ -286,14 +269,17 @@ static const luaL_Reg mathlib[] = {
286/* 269/*
287** Open math library 270** Open math library
288*/ 271*/
289LUAMOD_API int luaopen_math (lua_State *L) { 272LUALIB_API int luaopen_math (lua_State *L) {
290 luaL_newlib(L, mathlib); 273 luaL_register(L, LUA_MATHLIBNAME, mathlib);
291#if 0 /* No use in adding floating point constants when there's no FP */ 274#if 0 /* No use in adding floating point constants when there's no FP */
292 lua_pushnumber(L, PI); 275 lua_pushnumber(L, PI);
293 lua_setfield(L, -2, "pi"); 276 lua_setfield(L, -2, "pi");
294 lua_pushnumber(L, HUGE_VAL); 277 lua_pushnumber(L, HUGE_VAL);
295 lua_setfield(L, -2, "huge"); 278 lua_setfield(L, -2, "huge");
279#if defined(LUA_COMPAT_MOD)
280 lua_getfield(L, -1, "fmod");
281 lua_setfield(L, -2, "mod");
282#endif
296#endif 283#endif
297 return 1; 284 return 1;
298} 285}
299