aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c67
1 files changed, 32 insertions, 35 deletions
diff --git a/lmathlib.c b/lmathlib.c
index 50a7a89f..2a882b49 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.50 2002/08/14 20:07:43 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.51 2002/08/14 20:10:33 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*/
@@ -34,104 +34,104 @@
34 34
35 35
36static int 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_checknumber(L, 1)));
38 return 1; 38 return 1;
39} 39}
40 40
41static int math_sin (lua_State *L) { 41static int math_sin (lua_State *L) {
42 lua_pushnumber(L, sin(TORAD(luaL_check_number(L, 1)))); 42 lua_pushnumber(L, sin(TORAD(luaL_checknumber(L, 1))));
43 return 1; 43 return 1;
44} 44}
45 45
46static int math_cos (lua_State *L) { 46static int math_cos (lua_State *L) {
47 lua_pushnumber(L, cos(TORAD(luaL_check_number(L, 1)))); 47 lua_pushnumber(L, cos(TORAD(luaL_checknumber(L, 1))));
48 return 1; 48 return 1;
49} 49}
50 50
51static int math_tan (lua_State *L) { 51static int math_tan (lua_State *L) {
52 lua_pushnumber(L, tan(TORAD(luaL_check_number(L, 1)))); 52 lua_pushnumber(L, tan(TORAD(luaL_checknumber(L, 1))));
53 return 1; 53 return 1;
54} 54}
55 55
56static int math_asin (lua_State *L) { 56static int math_asin (lua_State *L) {
57 lua_pushnumber(L, FROMRAD(asin(luaL_check_number(L, 1)))); 57 lua_pushnumber(L, FROMRAD(asin(luaL_checknumber(L, 1))));
58 return 1; 58 return 1;
59} 59}
60 60
61static int math_acos (lua_State *L) { 61static int math_acos (lua_State *L) {
62 lua_pushnumber(L, FROMRAD(acos(luaL_check_number(L, 1)))); 62 lua_pushnumber(L, FROMRAD(acos(luaL_checknumber(L, 1))));
63 return 1; 63 return 1;
64} 64}
65 65
66static int math_atan (lua_State *L) { 66static int math_atan (lua_State *L) {
67 lua_pushnumber(L, FROMRAD(atan(luaL_check_number(L, 1)))); 67 lua_pushnumber(L, FROMRAD(atan(luaL_checknumber(L, 1))));
68 return 1; 68 return 1;
69} 69}
70 70
71static int math_atan2 (lua_State *L) { 71static int math_atan2 (lua_State *L) {
72 lua_pushnumber(L, FROMRAD(atan2(luaL_check_number(L, 1), luaL_check_number(L, 2)))); 72 lua_pushnumber(L, FROMRAD(atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))));
73 return 1; 73 return 1;
74} 74}
75 75
76static int math_ceil (lua_State *L) { 76static int math_ceil (lua_State *L) {
77 lua_pushnumber(L, ceil(luaL_check_number(L, 1))); 77 lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
78 return 1; 78 return 1;
79} 79}
80 80
81static int math_floor (lua_State *L) { 81static int math_floor (lua_State *L) {
82 lua_pushnumber(L, floor(luaL_check_number(L, 1))); 82 lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
83 return 1; 83 return 1;
84} 84}
85 85
86static int math_mod (lua_State *L) { 86static int math_mod (lua_State *L) {
87 lua_pushnumber(L, fmod(luaL_check_number(L, 1), luaL_check_number(L, 2))); 87 lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
88 return 1; 88 return 1;
89} 89}
90 90
91static int math_sqrt (lua_State *L) { 91static int math_sqrt (lua_State *L) {
92 lua_pushnumber(L, sqrt(luaL_check_number(L, 1))); 92 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
93 return 1; 93 return 1;
94} 94}
95 95
96static int math_pow (lua_State *L) { 96static int math_pow (lua_State *L) {
97 lua_pushnumber(L, pow(luaL_check_number(L, 1), luaL_check_number(L, 2))); 97 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
98 return 1; 98 return 1;
99} 99}
100 100
101static int math_log (lua_State *L) { 101static int math_log (lua_State *L) {
102 lua_pushnumber(L, log(luaL_check_number(L, 1))); 102 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
103 return 1; 103 return 1;
104} 104}
105 105
106static int math_log10 (lua_State *L) { 106static int math_log10 (lua_State *L) {
107 lua_pushnumber(L, log10(luaL_check_number(L, 1))); 107 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
108 return 1; 108 return 1;
109} 109}
110 110
111static int math_exp (lua_State *L) { 111static int math_exp (lua_State *L) {
112 lua_pushnumber(L, exp(luaL_check_number(L, 1))); 112 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
113 return 1; 113 return 1;
114} 114}
115 115
116static int math_deg (lua_State *L) { 116static int math_deg (lua_State *L) {
117 lua_pushnumber(L, luaL_check_number(L, 1)/RADIANS_PER_DEGREE); 117 lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
118 return 1; 118 return 1;
119} 119}
120 120
121static int math_rad (lua_State *L) { 121static int math_rad (lua_State *L) {
122 lua_pushnumber(L, luaL_check_number(L, 1)*RADIANS_PER_DEGREE); 122 lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
123 return 1; 123 return 1;
124} 124}
125 125
126static int math_frexp (lua_State *L) { 126static int math_frexp (lua_State *L) {
127 int e; 127 int e;
128 lua_pushnumber(L, frexp(luaL_check_number(L, 1), &e)); 128 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
129 lua_pushnumber(L, e); 129 lua_pushnumber(L, e);
130 return 2; 130 return 2;
131} 131}
132 132
133static int math_ldexp (lua_State *L) { 133static int math_ldexp (lua_State *L) {
134 lua_pushnumber(L, ldexp(luaL_check_number(L, 1), luaL_check_int(L, 2))); 134 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
135 return 1; 135 return 1;
136} 136}
137 137
@@ -139,10 +139,10 @@ static int math_ldexp (lua_State *L) {
139 139
140static int math_min (lua_State *L) { 140static int math_min (lua_State *L) {
141 int n = lua_gettop(L); /* number of arguments */ 141 int n = lua_gettop(L); /* number of arguments */
142 lua_Number dmin = luaL_check_number(L, 1); 142 lua_Number dmin = luaL_checknumber(L, 1);
143 int i; 143 int i;
144 for (i=2; i<=n; i++) { 144 for (i=2; i<=n; i++) {
145 lua_Number d = luaL_check_number(L, i); 145 lua_Number d = luaL_checknumber(L, i);
146 if (d < dmin) 146 if (d < dmin)
147 dmin = d; 147 dmin = d;
148 } 148 }
@@ -153,10 +153,10 @@ static int math_min (lua_State *L) {
153 153
154static int math_max (lua_State *L) { 154static int math_max (lua_State *L) {
155 int n = lua_gettop(L); /* number of arguments */ 155 int n = lua_gettop(L); /* number of arguments */
156 lua_Number dmax = luaL_check_number(L, 1); 156 lua_Number dmax = luaL_checknumber(L, 1);
157 int i; 157 int i;
158 for (i=2; i<=n; i++) { 158 for (i=2; i<=n; i++) {
159 lua_Number d = luaL_check_number(L, i); 159 lua_Number d = luaL_checknumber(L, i);
160 if (d > dmax) 160 if (d > dmax)
161 dmax = d; 161 dmax = d;
162 } 162 }
@@ -175,15 +175,15 @@ static int math_random (lua_State *L) {
175 break; 175 break;
176 } 176 }
177 case 1: { /* only upper limit */ 177 case 1: { /* only upper limit */
178 int u = luaL_check_int(L, 1); 178 int u = luaL_checkint(L, 1);
179 luaL_arg_check(L, 1<=u, 1, "interval is empty"); 179 luaL_argcheck(L, 1<=u, 1, "interval is empty");
180 lua_pushnumber(L, (int)floor(r*u)+1); /* int between 1 and `u' */ 180 lua_pushnumber(L, (int)floor(r*u)+1); /* int between 1 and `u' */
181 break; 181 break;
182 } 182 }
183 case 2: { /* lower and upper limits */ 183 case 2: { /* lower and upper limits */
184 int l = luaL_check_int(L, 1); 184 int l = luaL_checkint(L, 1);
185 int u = luaL_check_int(L, 2); 185 int u = luaL_checkint(L, 2);
186 luaL_arg_check(L, l<=u, 2, "interval is empty"); 186 luaL_argcheck(L, l<=u, 2, "interval is empty");
187 lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */ 187 lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */
188 break; 188 break;
189 } 189 }
@@ -194,7 +194,7 @@ static int math_random (lua_State *L) {
194 194
195 195
196static int math_randomseed (lua_State *L) { 196static int math_randomseed (lua_State *L) {
197 srand(luaL_check_int(L, 1)); 197 srand(luaL_checkint(L, 1));
198 return 0; 198 return 0;
199} 199}
200 200
@@ -232,13 +232,10 @@ static const luaL_reg mathlib[] = {
232** Open math library 232** Open math library
233*/ 233*/
234LUALIB_API int lua_mathlibopen (lua_State *L) { 234LUALIB_API int lua_mathlibopen (lua_State *L) {
235 lua_pushliteral(L, LUA_MATHLIBNAME); 235 luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
236 lua_newtable(L);
237 luaL_openlib(L, mathlib, 0);
238 lua_pushliteral(L, "pi"); 236 lua_pushliteral(L, "pi");
239 lua_pushnumber(L, PI); 237 lua_pushnumber(L, PI);
240 lua_settable(L, -3); 238 lua_settable(L, -3);
241 lua_settable(L, LUA_GLOBALSINDEX);
242 lua_pushliteral(L, "__pow"); 239 lua_pushliteral(L, "__pow");
243 lua_pushcfunction(L, math_pow); 240 lua_pushcfunction(L, math_pow);
244 lua_settable(L, LUA_REGISTRYINDEX); 241 lua_settable(L, LUA_REGISTRYINDEX);