Imported from ../lua-3.2.tar.gz.
[lua.git] / src / lib / lmathlib.c
blob19cb11c211e3f16f7a99dbddf5819d0a167c075c
1 /*
2 ** $Id: lmathlib.c,v 1.17 1999/07/07 17:54:08 roberto Exp $
3 ** Lua standard mathematical library
4 ** See Copyright Notice in lua.h
5 */
8 #include <stdlib.h>
9 #include <math.h>
11 #include "lauxlib.h"
12 #include "lua.h"
13 #include "lualib.h"
16 #undef PI
17 #define PI (3.14159265358979323846)
18 #define RADIANS_PER_DEGREE (PI/180.0)
23 ** If you want Lua to operate in radians (instead of degrees),
24 ** define RADIANS
26 #ifdef RADIANS
27 #define FROMRAD(a) (a)
28 #define TORAD(a) (a)
29 #else
30 #define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
31 #define TORAD(a) ((a)*RADIANS_PER_DEGREE)
32 #endif
35 static void math_abs (void) {
36 lua_pushnumber(fabs(luaL_check_number(1)));
39 static void math_sin (void) {
40 lua_pushnumber(sin(TORAD(luaL_check_number(1))));
43 static void math_cos (void) {
44 lua_pushnumber(cos(TORAD(luaL_check_number(1))));
47 static void math_tan (void) {
48 lua_pushnumber(tan(TORAD(luaL_check_number(1))));
51 static void math_asin (void) {
52 lua_pushnumber(FROMRAD(asin(luaL_check_number(1))));
55 static void math_acos (void) {
56 lua_pushnumber(FROMRAD(acos(luaL_check_number(1))));
59 static void math_atan (void) {
60 lua_pushnumber(FROMRAD(atan(luaL_check_number(1))));
63 static void math_atan2 (void) {
64 lua_pushnumber(FROMRAD(atan2(luaL_check_number(1), luaL_check_number(2))));
67 static void math_ceil (void) {
68 lua_pushnumber(ceil(luaL_check_number(1)));
71 static void math_floor (void) {
72 lua_pushnumber(floor(luaL_check_number(1)));
75 static void math_mod (void) {
76 lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
79 static void math_sqrt (void) {
80 lua_pushnumber(sqrt(luaL_check_number(1)));
83 static void math_pow (void) {
84 lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
87 static void math_log (void) {
88 lua_pushnumber(log(luaL_check_number(1)));
91 static void math_log10 (void) {
92 lua_pushnumber(log10(luaL_check_number(1)));
95 static void math_exp (void) {
96 lua_pushnumber(exp(luaL_check_number(1)));
99 static void math_deg (void) {
100 lua_pushnumber(luaL_check_number(1)/RADIANS_PER_DEGREE);
103 static void math_rad (void) {
104 lua_pushnumber(luaL_check_number(1)*RADIANS_PER_DEGREE);
107 static void math_frexp (void) {
108 int e;
109 lua_pushnumber(frexp(luaL_check_number(1), &e));
110 lua_pushnumber(e);
113 static void math_ldexp (void) {
114 lua_pushnumber(ldexp(luaL_check_number(1), luaL_check_int(2)));
119 static void math_min (void) {
120 int i = 1;
121 double dmin = luaL_check_number(i);
122 while (lua_getparam(++i) != LUA_NOOBJECT) {
123 double d = luaL_check_number(i);
124 if (d < dmin)
125 dmin = d;
127 lua_pushnumber(dmin);
131 static void math_max (void) {
132 int i = 1;
133 double dmax = luaL_check_number(i);
134 while (lua_getparam(++i) != LUA_NOOBJECT) {
135 double d = luaL_check_number(i);
136 if (d > dmax)
137 dmax = d;
139 lua_pushnumber(dmax);
143 static void math_random (void) {
144 /* the '%' avoids the (rare) case of r==1, and is needed also because on
145 some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */
146 double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
147 int l = luaL_opt_int(1, 0);
148 if (l == 0)
149 lua_pushnumber(r);
150 else {
151 int u = luaL_opt_int(2, 0);
152 if (u == 0) {
153 u = l;
154 l = 1;
156 luaL_arg_check(l<=u, 1, "interval is empty");
157 lua_pushnumber((int)(r*(u-l+1))+l);
162 static void math_randomseed (void) {
163 srand(luaL_check_int(1));
167 static struct luaL_reg mathlib[] = {
168 {"abs", math_abs},
169 {"sin", math_sin},
170 {"cos", math_cos},
171 {"tan", math_tan},
172 {"asin", math_asin},
173 {"acos", math_acos},
174 {"atan", math_atan},
175 {"atan2", math_atan2},
176 {"ceil", math_ceil},
177 {"floor", math_floor},
178 {"mod", math_mod},
179 {"frexp", math_frexp},
180 {"ldexp", math_ldexp},
181 {"sqrt", math_sqrt},
182 {"min", math_min},
183 {"max", math_max},
184 {"log", math_log},
185 {"log10", math_log10},
186 {"exp", math_exp},
187 {"deg", math_deg},
188 {"rad", math_rad},
189 {"random", math_random},
190 {"randomseed", math_randomseed}
194 ** Open math library
196 void lua_mathlibopen (void) {
197 luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
198 lua_pushcfunction(math_pow);
199 lua_pushnumber(0); /* to get its tag */
200 lua_settagmethod(lua_tag(lua_pop()), "pow");
201 lua_pushnumber(PI); lua_setglobal("PI");