aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c122
1 files changed, 77 insertions, 45 deletions
diff --git a/lmathlib.c b/lmathlib.c
index 86850cc9..d6957e76 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.25 2000/06/12 13:52:05 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.26 2000/08/09 19:16:57 roberto Exp roberto $
3** Standard mathematical library 3** Standard mathematical library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -33,138 +33,169 @@
33#endif 33#endif
34 34
35 35
36static void math_abs (lua_State *L) { 36static int math_abs (lua_State *L) {
37 lua_pushnumber(L, fabs(luaL_check_number(L, 1))); 37 lua_pushnumber(L, fabs(luaL_check_number(L, 1)));
38 return 1;
38} 39}
39 40
40static void math_sin (lua_State *L) { 41static int math_sin (lua_State *L) {
41 lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1)))); 42 lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1))));
43 return 1;
42} 44}
43 45
44static void math_cos (lua_State *L) { 46static int math_cos (lua_State *L) {
45 lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1)))); 47 lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1))));
48 return 1;
46} 49}
47 50
48static void math_tan (lua_State *L) { 51static int math_tan (lua_State *L) {
49 lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1)))); 52 lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1))));
53 return 1;
50} 54}
51 55
52static void math_asin (lua_State *L) { 56static int math_asin (lua_State *L) {
53 lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1)))); 57 lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1))));
58 return 1;
54} 59}
55 60
56static void math_acos (lua_State *L) { 61static int math_acos (lua_State *L) {
57 lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1)))); 62 lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1))));
63 return 1;
58} 64}
59 65
60static void math_atan (lua_State *L) { 66static int math_atan (lua_State *L) {
61 lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1)))); 67 lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1))));
68 return 1;
62} 69}
63 70
64static void math_atan2 (lua_State *L) { 71static int math_atan2 (lua_State *L) {
65 lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2)))); 72 lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2))));
73 return 1;
66} 74}
67 75
68static void math_ceil (lua_State *L) { 76static int math_ceil (lua_State *L) {
69 lua_pushnumber(L, ceil(luaL_check_number(L, 1))); 77 lua_pushnumber(L, ceil(luaL_check_number(L, 1)));
78 return 1;
70} 79}
71 80
72static void math_floor (lua_State *L) { 81static int math_floor (lua_State *L) {
73 lua_pushnumber(L, floor(luaL_check_number(L, 1))); 82 lua_pushnumber(L, floor(luaL_check_number(L, 1)));
83 return 1;
74} 84}
75 85
76static void math_mod (lua_State *L) { 86static int math_mod (lua_State *L) {
77 lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2))); 87 lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2)));
88 return 1;
78} 89}
79 90
80static void math_sqrt (lua_State *L) { 91static int math_sqrt (lua_State *L) {
81 lua_pushnumber(L, sqrt(luaL_check_number(L, 1))); 92 lua_pushnumber(L, sqrt(luaL_check_number(L, 1)));
93 return 1;
82} 94}
83 95
84static void math_pow (lua_State *L) { 96static int math_pow (lua_State *L) {
85 lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2))); 97 lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2)));
98 return 1;
86} 99}
87 100
88static void math_log (lua_State *L) { 101static int math_log (lua_State *L) {
89 lua_pushnumber(L, log(luaL_check_number(L, 1))); 102 lua_pushnumber(L, log(luaL_check_number(L, 1)));
103 return 1;
90} 104}
91 105
92static void math_log10 (lua_State *L) { 106static int math_log10 (lua_State *L) {
93 lua_pushnumber(L, log10(luaL_check_number(L, 1))); 107 lua_pushnumber(L, log10(luaL_check_number(L, 1)));
108 return 1;
94} 109}
95 110
96static void math_exp (lua_State *L) { 111static int math_exp (lua_State *L) {
97 lua_pushnumber(L, exp(luaL_check_number(L, 1))); 112 lua_pushnumber(L, exp(luaL_check_number(L, 1)));
113 return 1;
98} 114}
99 115
100static void math_deg (lua_State *L) { 116static int math_deg (lua_State *L) {
101 lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE); 117 lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE);
118 return 1;
102} 119}
103 120
104static void math_rad (lua_State *L) { 121static int math_rad (lua_State *L) {
105 lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE); 122 lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE);
123 return 1;
106} 124}
107 125
108static void math_frexp (lua_State *L) { 126static int math_frexp (lua_State *L) {
109 int e; 127 int e;
110 lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e)); 128 lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e));
111 lua_pushnumber(L, e); 129 lua_pushnumber(L, e);
130 return 2;
112} 131}
113 132
114static void math_ldexp (lua_State *L) { 133static int math_ldexp (lua_State *L) {
115 lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2))); 134 lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2)));
135 return 1;
116} 136}
117 137
118 138
119 139
120static void math_min (lua_State *L) { 140static int math_min (lua_State *L) {
121 int i = 1; 141 int n = lua_gettop(L); /* number of arguments */
122 double dmin = luaL_check_number(L, i); 142 double dmin = luaL_check_number(L, 1);
123 while (lua_getparam(L, ++i) != LUA_NOOBJECT) { 143 int i;
144 for (i=2; i<=n; i++) {
124 double d = luaL_check_number(L, i); 145 double d = luaL_check_number(L, i);
125 if (d < dmin) 146 if (d < dmin)
126 dmin = d; 147 dmin = d;
127 } 148 }
128 lua_pushnumber(L, dmin); 149 lua_pushnumber(L, dmin);
150 return 1;
129} 151}
130 152
131 153
132static void math_max (lua_State *L) { 154static int math_max (lua_State *L) {
133 int i = 1; 155 int n = lua_gettop(L); /* number of arguments */
134 double dmax = luaL_check_number(L, i); 156 double dmax = luaL_check_number(L, 1);
135 while (lua_getparam(L, ++i) != LUA_NOOBJECT) { 157 int i;
158 for (i=2; i<=n; i++) {
136 double d = luaL_check_number(L, i); 159 double d = luaL_check_number(L, i);
137 if (d > dmax) 160 if (d > dmax)
138 dmax = d; 161 dmax = d;
139 } 162 }
140 lua_pushnumber(L, dmax); 163 lua_pushnumber(L, dmax);
164 return 1;
141} 165}
142 166
143 167
144static void math_random (lua_State *L) { 168static int math_random (lua_State *L) {
145 /* the '%' avoids the (rare) case of r==1, and is needed also because on 169 /* the '%' avoids the (rare) case of r==1, and is needed also because on
146 some systems (SunOS!) "rand()" may return a value larger than RAND_MAX */ 170 some systems (SunOS!) "rand()" may return a value larger than RAND_MAX */
147 double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX; 171 double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
148 if (lua_getparam(L, 1) == LUA_NOOBJECT) /* no arguments? */ 172 switch (lua_gettop(L)) { /* check number of arguments */
149 lua_pushnumber(L, r); /* Number between 0 and 1 */ 173 case 0: { /* no arguments */
150 else { 174 lua_pushnumber(L, r); /* Number between 0 and 1 */
151 int l, u; /* lower & upper limits */ 175 break;
152 if (lua_getparam(L, 2) == LUA_NOOBJECT) { /* only one argument? */
153 l = 1;
154 u = luaL_check_int(L, 1);
155 } 176 }
156 else { /* two arguments */ 177 case 1: { /* only upper limit */
157 l = luaL_check_int(L, 1); 178 int u = luaL_check_int(L, 1);
158 u = luaL_check_int(L, 2); 179 luaL_arg_check(L, 1<=u, 1, "interval is empty");
180 lua_pushnumber(L, (int)(r*u)+1); /* integer between 1 and `u' */
181 break;
159 } 182 }
160 luaL_arg_check(L, l<=u, 1, "interval is empty"); 183 case 2: { /* lower and upper limits */
161 lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */ 184 int l = luaL_check_int(L, 1);
185 int u = luaL_check_int(L, 2);
186 luaL_arg_check(L, l<=u, 2, "interval is empty");
187 lua_pushnumber(L, (int)(r*(u-l+1))+l); /* integer between `l' and `u' */
188 break;
189 }
190 default: lua_error(L, "wrong number of arguments");
162 } 191 }
192 return 1;
163} 193}
164 194
165 195
166static void math_randomseed (lua_State *L) { 196static int math_randomseed (lua_State *L) {
167 srand(luaL_check_int(L, 1)); 197 srand(luaL_check_int(L, 1));
198 return 0;
168} 199}
169 200
170 201
@@ -199,9 +230,10 @@ static const struct luaL_reg mathlib[] = {
199*/ 230*/
200void lua_mathlibopen (lua_State *L) { 231void lua_mathlibopen (lua_State *L) {
201 luaL_openl(L, mathlib); 232 luaL_openl(L, mathlib);
202 lua_pushcfunction(L, math_pow);
203 lua_pushnumber(L, 0); /* to get its tag */ 233 lua_pushnumber(L, 0); /* to get its tag */
204 lua_settagmethod(L, lua_tag(L, lua_pop(L)), "pow"); 234 lua_pushcfunction(L, math_pow);
235 lua_settagmethod(L, lua_tag(L, -2), "pow");
236 lua_settop(L, -1); /* remove number */
205 lua_pushnumber(L, PI); lua_setglobal(L, "PI"); 237 lua_pushnumber(L, PI); lua_setglobal(L, "PI");
206} 238}
207 239