summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lmathlib.c195
-rw-r--r--mathlib.c221
-rw-r--r--mathlib.h13
3 files changed, 195 insertions, 234 deletions
diff --git a/lmathlib.c b/lmathlib.c
new file mode 100644
index 00000000..ad50ee96
--- /dev/null
+++ b/lmathlib.c
@@ -0,0 +1,195 @@
1/*
2** $Id: $
3** Lua standard mathematical library
4** See Copyright Notice in lua.h
5*/
6
7
8#include <stdlib.h>
9#include <math.h>
10
11#include "lauxlib.h"
12#include "lua.h"
13#include "lualib.h"
14
15#ifndef PI
16#define PI 3.14159265358979323846
17#endif
18
19#define TODEGREE(a) ((a)*180.0/PI)
20#define TORAD(a) ((a)*PI/180.0)
21
22
23
24static void math_abs (void)
25{
26 double d = luaL_check_number(1);
27 if (d < 0) d = -d;
28 lua_pushnumber(d);
29}
30
31static void math_sin (void)
32{
33 lua_pushnumber(sin(TORAD(luaL_check_number(1))));
34}
35
36static void math_cos (void)
37{
38 lua_pushnumber(cos(TORAD(luaL_check_number(1))));
39}
40
41static void math_tan (void)
42{
43 lua_pushnumber(tan(TORAD(luaL_check_number(1))));
44}
45
46static void math_asin (void)
47{
48 lua_pushnumber(asin(TODEGREE(luaL_check_number(1))));
49}
50
51static void math_acos (void)
52{
53 lua_pushnumber(acos(TODEGREE(luaL_check_number(1))));
54}
55
56static void math_atan (void)
57{
58 lua_pushnumber(atan(TODEGREE(luaL_check_number(1))));
59}
60
61static void math_atan2 (void)
62{
63 lua_pushnumber(TODEGREE(atan2(luaL_check_number(1), luaL_check_number(2))));
64}
65
66static void math_ceil (void)
67{
68 lua_pushnumber(ceil(luaL_check_number(1)));
69}
70
71static void math_floor (void)
72{
73 lua_pushnumber(floor(luaL_check_number(1)));
74}
75
76static void math_mod (void)
77{
78 lua_pushnumber(fmod(luaL_check_number(1), luaL_check_number(2)));
79}
80
81static void math_sqrt (void)
82{
83 lua_pushnumber(sqrt(luaL_check_number(1)));
84}
85
86static void math_pow (void)
87{
88 lua_pushnumber(pow(luaL_check_number(1), luaL_check_number(2)));
89}
90
91static void math_log (void)
92{
93 lua_pushnumber(log(luaL_check_number(1)));
94}
95
96static void math_log10 (void)
97{
98 lua_pushnumber(log10(luaL_check_number(1)));
99}
100
101static void math_exp (void)
102{
103 lua_pushnumber(exp(luaL_check_number(1)));
104}
105
106static void math_deg (void)
107{
108 lua_pushnumber(luaL_check_number(1)*180./PI);
109}
110
111static void math_rad (void)
112{
113 lua_pushnumber(luaL_check_number(1)/180.*PI);
114}
115
116
117
118static void math_min (void)
119{
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;
126 }
127 lua_pushnumber(dmin);
128}
129
130
131static void math_max (void)
132{
133 int i = 1;
134 double dmax = luaL_check_number(i);
135 while (lua_getparam(++i) != LUA_NOOBJECT) {
136 double d = luaL_check_number(i);
137 if (d > dmax)
138 dmax = d;
139 }
140 lua_pushnumber(dmax);
141}
142
143
144static void math_random (void)
145{
146 double l = luaL_opt_number(1, 0);
147 double r = (double)rand() / (double)RAND_MAX;
148 if (l == 0)
149 lua_pushnumber(r);
150 else
151 lua_pushnumber((int)(r*l)+1);
152}
153
154
155static void math_randomseed (void)
156{
157 srand(luaL_check_number(1));
158}
159
160
161static struct luaL_reg mathlib[] = {
162{"abs", math_abs},
163{"sin", math_sin},
164{"cos", math_cos},
165{"tan", math_tan},
166{"asin", math_asin},
167{"acos", math_acos},
168{"atan", math_atan},
169{"atan2", math_atan2},
170{"ceil", math_ceil},
171{"floor", math_floor},
172{"mod", math_mod},
173{"sqrt", math_sqrt},
174{"min", math_min},
175{"max", math_max},
176{"log", math_log},
177{"log10", math_log10},
178{"exp", math_exp},
179{"deg", math_deg},
180{"rad", math_rad},
181{"random", math_random},
182{"randomseed", math_randomseed}
183};
184
185/*
186** Open math library
187*/
188void lua_mathlibopen (void)
189{
190 luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
191 lua_pushcfunction(math_pow);
192 lua_pushnumber(0); /* to get its tag */
193 lua_settagmethod(lua_tag(lua_pop()), "pow");
194}
195
diff --git a/mathlib.c b/mathlib.c
deleted file mode 100644
index 3be26b67..00000000
--- a/mathlib.c
+++ /dev/null
@@ -1,221 +0,0 @@
1/*
2** mathlib.c
3** Mathematics library to LUA
4*/
5
6char *rcs_mathlib="$Id: mathlib.c,v 1.25 1997/06/19 18:03:04 roberto Exp roberto $";
7
8#include <stdlib.h>
9#include <math.h>
10
11#include "lualib.h"
12#include "auxlib.h"
13#include "lua.h"
14
15#ifndef PI
16#define PI 3.14159265358979323846
17#endif
18#define TODEGREE(a) ((a)*180.0/PI)
19#define TORAD(a) ((a)*PI/180.0)
20
21static void math_abs (void)
22{
23 double d = luaL_check_number(1);
24 if (d < 0) d = -d;
25 lua_pushnumber (d);
26}
27
28
29static void math_sin (void)
30{
31 double d = luaL_check_number(1);
32 lua_pushnumber (sin(TORAD(d)));
33}
34
35
36
37static void math_cos (void)
38{
39 double d = luaL_check_number(1);
40 lua_pushnumber (cos(TORAD(d)));
41}
42
43
44
45static void math_tan (void)
46{
47 double d = luaL_check_number(1);
48 lua_pushnumber (tan(TORAD(d)));
49}
50
51
52static void math_asin (void)
53{
54 double d = luaL_check_number(1);
55 lua_pushnumber (TODEGREE(asin(d)));
56}
57
58
59static void math_acos (void)
60{
61 double d = luaL_check_number(1);
62 lua_pushnumber (TODEGREE(acos(d)));
63}
64
65
66static void math_atan (void)
67{
68 double d = luaL_check_number(1);
69 lua_pushnumber (TODEGREE(atan(d)));
70}
71
72
73static void math_atan2 (void)
74{
75 double d1 = luaL_check_number(1);
76 double d2 = luaL_check_number(2);
77 lua_pushnumber (TODEGREE(atan2(d1, d2)));
78}
79
80
81static void math_ceil (void)
82{
83 double d = luaL_check_number(1);
84 lua_pushnumber (ceil(d));
85}
86
87
88static void math_floor (void)
89{
90 double d = luaL_check_number(1);
91 lua_pushnumber (floor(d));
92}
93
94static void math_mod (void)
95{
96 float x = luaL_check_number(1);
97 float y = luaL_check_number(2);
98 lua_pushnumber(fmod(x, y));
99}
100
101
102static void math_sqrt (void)
103{
104 double d = luaL_check_number(1);
105 lua_pushnumber (sqrt(d));
106}
107
108
109static void math_pow (void)
110{
111 double d1 = luaL_check_number(1);
112 double d2 = luaL_check_number(2);
113 lua_pushnumber(pow(d1,d2));
114}
115
116static void math_min (void)
117{
118 int i=1;
119 double dmin = luaL_check_number(i);
120 while (lua_getparam(++i) != LUA_NOOBJECT)
121 {
122 double d = luaL_check_number(i);
123 if (d < dmin) dmin = d;
124 }
125 lua_pushnumber (dmin);
126}
127
128static void math_max (void)
129{
130 int i=1;
131 double dmax = luaL_check_number(i);
132 while (lua_getparam(++i) != LUA_NOOBJECT)
133 {
134 double d = luaL_check_number(i);
135 if (d > dmax) dmax = d;
136 }
137 lua_pushnumber (dmax);
138}
139
140static void math_log (void)
141{
142 double d = luaL_check_number(1);
143 lua_pushnumber (log(d));
144}
145
146
147static void math_log10 (void)
148{
149 double d = luaL_check_number(1);
150 lua_pushnumber (log10(d));
151}
152
153
154static void math_exp (void)
155{
156 double d = luaL_check_number(1);
157 lua_pushnumber (exp(d));
158}
159
160static void math_deg (void)
161{
162 float d = luaL_check_number(1);
163 lua_pushnumber (d*180./PI);
164}
165
166static void math_rad (void)
167{
168 float d = luaL_check_number(1);
169 lua_pushnumber (d/180.*PI);
170}
171
172static void math_random (void)
173{
174 double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
175 if (lua_getparam(1) == LUA_NOOBJECT)
176 lua_pushnumber(r);
177 else
178 lua_pushnumber((int)(r*luaL_check_number(1)) + 1);
179}
180
181static void math_randomseed (void)
182{
183 srand(luaL_check_number(1));
184}
185
186
187static struct luaL_reg mathlib[] = {
188{"abs", math_abs},
189{"sin", math_sin},
190{"cos", math_cos},
191{"tan", math_tan},
192{"asin", math_asin},
193{"acos", math_acos},
194{"atan", math_atan},
195{"atan2", math_atan2},
196{"ceil", math_ceil},
197{"floor", math_floor},
198{"mod", math_mod},
199{"sqrt", math_sqrt},
200{"min", math_min},
201{"max", math_max},
202{"log", math_log},
203{"log10", math_log10},
204{"exp", math_exp},
205{"deg", math_deg},
206{"rad", math_rad},
207{"random", math_random},
208{"randomseed", math_randomseed}
209};
210
211/*
212** Open math library
213*/
214void mathlib_open (void)
215{
216 luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0])));
217 lua_pushcfunction(math_pow);
218 lua_pushnumber(0); /* to get its tag */
219 lua_settagmethod(lua_tag(lua_pop()), "pow");
220}
221
diff --git a/mathlib.h b/mathlib.h
deleted file mode 100644
index 2adb2de9..00000000
--- a/mathlib.h
+++ /dev/null
@@ -1,13 +0,0 @@
1/*
2** Math library to LUA
3** TeCGraf - PUC-Rio
4** $Id: $
5*/
6
7
8#ifndef strlib_h
9
10void mathlib_open (void);
11
12#endif
13