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