release.sh changes & fixes
[minix3.git] / external / mit / lua / dist / src / lmathlib.c
blob1ef3fc862811ad532dcd1373726a40ed122f824c
1 /* $NetBSD: lmathlib.c,v 1.1.1.2 2012/03/15 00:08:05 alnsn Exp $ */
3 /*
4 ** $Id: lmathlib.c,v 1.1.1.2 2012/03/15 00:08:05 alnsn Exp $
5 ** Standard mathematical library
6 ** See Copyright Notice in lua.h
7 */
10 #include <stdlib.h>
11 #include <math.h>
13 #define lmathlib_c
14 #define LUA_LIB
16 #include "lua.h"
18 #include "lauxlib.h"
19 #include "lualib.h"
22 #undef PI
23 #define PI (3.14159265358979323846)
24 #define RADIANS_PER_DEGREE (PI/180.0)
28 static int math_abs (lua_State *L) {
29 lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
30 return 1;
33 static int math_sin (lua_State *L) {
34 lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
35 return 1;
38 static int math_sinh (lua_State *L) {
39 lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
40 return 1;
43 static int math_cos (lua_State *L) {
44 lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
45 return 1;
48 static int math_cosh (lua_State *L) {
49 lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
50 return 1;
53 static int math_tan (lua_State *L) {
54 lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
55 return 1;
58 static int math_tanh (lua_State *L) {
59 lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
60 return 1;
63 static int math_asin (lua_State *L) {
64 lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
65 return 1;
68 static int math_acos (lua_State *L) {
69 lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
70 return 1;
73 static int math_atan (lua_State *L) {
74 lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
75 return 1;
78 static int math_atan2 (lua_State *L) {
79 lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
80 return 1;
83 static int math_ceil (lua_State *L) {
84 lua_pushnumber(L, ceil(luaL_checknumber(L, 1)));
85 return 1;
88 static int math_floor (lua_State *L) {
89 lua_pushnumber(L, floor(luaL_checknumber(L, 1)));
90 return 1;
93 static int math_fmod (lua_State *L) {
94 lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
95 return 1;
98 static int math_modf (lua_State *L) {
99 double ip;
100 double fp = modf(luaL_checknumber(L, 1), &ip);
101 lua_pushnumber(L, ip);
102 lua_pushnumber(L, fp);
103 return 2;
106 static int math_sqrt (lua_State *L) {
107 lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
108 return 1;
111 static int math_pow (lua_State *L) {
112 lua_pushnumber(L, pow(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
113 return 1;
116 static int math_log (lua_State *L) {
117 lua_pushnumber(L, log(luaL_checknumber(L, 1)));
118 return 1;
121 static int math_log10 (lua_State *L) {
122 lua_pushnumber(L, log10(luaL_checknumber(L, 1)));
123 return 1;
126 static int math_exp (lua_State *L) {
127 lua_pushnumber(L, exp(luaL_checknumber(L, 1)));
128 return 1;
131 static int math_deg (lua_State *L) {
132 lua_pushnumber(L, luaL_checknumber(L, 1)/RADIANS_PER_DEGREE);
133 return 1;
136 static int math_rad (lua_State *L) {
137 lua_pushnumber(L, luaL_checknumber(L, 1)*RADIANS_PER_DEGREE);
138 return 1;
141 static int math_frexp (lua_State *L) {
142 int e;
143 lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
144 lua_pushinteger(L, e);
145 return 2;
148 static int math_ldexp (lua_State *L) {
149 lua_pushnumber(L, ldexp(luaL_checknumber(L, 1), luaL_checkint(L, 2)));
150 return 1;
155 static int math_min (lua_State *L) {
156 int n = lua_gettop(L); /* number of arguments */
157 lua_Number dmin = luaL_checknumber(L, 1);
158 int i;
159 for (i=2; i<=n; i++) {
160 lua_Number d = luaL_checknumber(L, i);
161 if (d < dmin)
162 dmin = d;
164 lua_pushnumber(L, dmin);
165 return 1;
169 static int math_max (lua_State *L) {
170 int n = lua_gettop(L); /* number of arguments */
171 lua_Number dmax = luaL_checknumber(L, 1);
172 int i;
173 for (i=2; i<=n; i++) {
174 lua_Number d = luaL_checknumber(L, i);
175 if (d > dmax)
176 dmax = d;
178 lua_pushnumber(L, dmax);
179 return 1;
183 static int math_random (lua_State *L) {
184 /* the `%' avoids the (rare) case of r==1, and is needed also because on
185 some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
186 lua_Number r = (lua_Number)(rand()%RAND_MAX) / (lua_Number)RAND_MAX;
187 switch (lua_gettop(L)) { /* check number of arguments */
188 case 0: { /* no arguments */
189 lua_pushnumber(L, r); /* Number between 0 and 1 */
190 break;
192 case 1: { /* only upper limit */
193 int u = luaL_checkint(L, 1);
194 luaL_argcheck(L, 1<=u, 1, "interval is empty");
195 lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
196 break;
198 case 2: { /* lower and upper limits */
199 int l = luaL_checkint(L, 1);
200 int u = luaL_checkint(L, 2);
201 luaL_argcheck(L, l<=u, 2, "interval is empty");
202 lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
203 break;
205 default: return luaL_error(L, "wrong number of arguments");
207 return 1;
211 static int math_randomseed (lua_State *L) {
212 srand(luaL_checkint(L, 1));
213 return 0;
217 static const luaL_Reg mathlib[] = {
218 {"abs", math_abs},
219 {"acos", math_acos},
220 {"asin", math_asin},
221 {"atan2", math_atan2},
222 {"atan", math_atan},
223 {"ceil", math_ceil},
224 {"cosh", math_cosh},
225 {"cos", math_cos},
226 {"deg", math_deg},
227 {"exp", math_exp},
228 {"floor", math_floor},
229 {"fmod", math_fmod},
230 {"frexp", math_frexp},
231 {"ldexp", math_ldexp},
232 {"log10", math_log10},
233 {"log", math_log},
234 {"max", math_max},
235 {"min", math_min},
236 {"modf", math_modf},
237 {"pow", math_pow},
238 {"rad", math_rad},
239 {"random", math_random},
240 {"randomseed", math_randomseed},
241 {"sinh", math_sinh},
242 {"sin", math_sin},
243 {"sqrt", math_sqrt},
244 {"tanh", math_tanh},
245 {"tan", math_tan},
246 {NULL, NULL}
251 ** Open math library
253 LUALIB_API int luaopen_math (lua_State *L) {
254 luaL_register(L, LUA_MATHLIBNAME, mathlib);
255 lua_pushnumber(L, PI);
256 lua_setfield(L, -2, "pi");
257 lua_pushnumber(L, HUGE_VAL);
258 lua_setfield(L, -2, "huge");
259 #if defined(LUA_COMPAT_MOD)
260 lua_getfield(L, -1, "fmod");
261 lua_setfield(L, -2, "mod");
262 #endif
263 return 1;