diff options
author | The Lua team <lua@tecgraf.puc-rio.br> | 1993-07-28 10:18:00 -0300 |
---|---|---|
committer | The Lua team <lua@tecgraf.puc-rio.br> | 1993-07-28 10:18:00 -0300 |
commit | cd05d9c5cb69020c069f037ba7f243f705d0a48a (patch) | |
tree | cb7f08c0684c10970a528984741047fb3babadd3 /mathlib.c | |
download | lua-cd05d9c5cb69020c069f037ba7f243f705d0a48a.tar.gz lua-cd05d9c5cb69020c069f037ba7f243f705d0a48a.tar.bz2 lua-cd05d9c5cb69020c069f037ba7f243f705d0a48a.zip |
oldest known commit
Diffstat (limited to 'mathlib.c')
-rw-r--r-- | mathlib.c | 234 |
1 files changed, 234 insertions, 0 deletions
diff --git a/mathlib.c b/mathlib.c new file mode 100644 index 00000000..b07c8c47 --- /dev/null +++ b/mathlib.c | |||
@@ -0,0 +1,234 @@ | |||
1 | /* | ||
2 | ** mathlib.c | ||
3 | ** Mathematica library to LUA | ||
4 | ** | ||
5 | ** Waldemar Celes Filho | ||
6 | ** TeCGraf - PUC-Rio | ||
7 | ** 19 May 93 | ||
8 | */ | ||
9 | |||
10 | #include <stdio.h> /* NULL */ | ||
11 | #include <math.h> | ||
12 | |||
13 | #include "lua.h" | ||
14 | |||
15 | static void math_abs (void) | ||
16 | { | ||
17 | double d; | ||
18 | lua_Object o = lua_getparam (1); | ||
19 | if (o == NULL) | ||
20 | { lua_error ("too few arguments to function `abs'"); return; } | ||
21 | if (!lua_isnumber(o)) | ||
22 | { lua_error ("incorrect arguments to function `abs'"); return; } | ||
23 | d = lua_getnumber(o); | ||
24 | if (d < 0) d = -d; | ||
25 | lua_pushnumber (d); | ||
26 | } | ||
27 | |||
28 | |||
29 | static void math_sin (void) | ||
30 | { | ||
31 | double d; | ||
32 | lua_Object o = lua_getparam (1); | ||
33 | if (o == NULL) | ||
34 | { lua_error ("too few arguments to function `sin'"); return; } | ||
35 | if (!lua_isnumber(o)) | ||
36 | { lua_error ("incorrect arguments to function `sin'"); return; } | ||
37 | d = lua_getnumber(o); | ||
38 | lua_pushnumber (sin(d)); | ||
39 | } | ||
40 | |||
41 | |||
42 | |||
43 | static void math_cos (void) | ||
44 | { | ||
45 | double d; | ||
46 | lua_Object o = lua_getparam (1); | ||
47 | if (o == NULL) | ||
48 | { lua_error ("too few arguments to function `cos'"); return; } | ||
49 | if (!lua_isnumber(o)) | ||
50 | { lua_error ("incorrect arguments to function `cos'"); return; } | ||
51 | d = lua_getnumber(o); | ||
52 | lua_pushnumber (cos(d)); | ||
53 | } | ||
54 | |||
55 | |||
56 | |||
57 | static void math_tan (void) | ||
58 | { | ||
59 | double d; | ||
60 | lua_Object o = lua_getparam (1); | ||
61 | if (o == NULL) | ||
62 | { lua_error ("too few arguments to function `tan'"); return; } | ||
63 | if (!lua_isnumber(o)) | ||
64 | { lua_error ("incorrect arguments to function `tan'"); return; } | ||
65 | d = lua_getnumber(o); | ||
66 | lua_pushnumber (tan(d)); | ||
67 | } | ||
68 | |||
69 | |||
70 | static void math_asin (void) | ||
71 | { | ||
72 | double d; | ||
73 | lua_Object o = lua_getparam (1); | ||
74 | if (o == NULL) | ||
75 | { lua_error ("too few arguments to function `asin'"); return; } | ||
76 | if (!lua_isnumber(o)) | ||
77 | { lua_error ("incorrect arguments to function `asin'"); return; } | ||
78 | d = lua_getnumber(o); | ||
79 | lua_pushnumber (asin(d)); | ||
80 | } | ||
81 | |||
82 | |||
83 | static void math_acos (void) | ||
84 | { | ||
85 | double d; | ||
86 | lua_Object o = lua_getparam (1); | ||
87 | if (o == NULL) | ||
88 | { lua_error ("too few arguments to function `acos'"); return; } | ||
89 | if (!lua_isnumber(o)) | ||
90 | { lua_error ("incorrect arguments to function `acos'"); return; } | ||
91 | d = lua_getnumber(o); | ||
92 | lua_pushnumber (acos(d)); | ||
93 | } | ||
94 | |||
95 | |||
96 | |||
97 | static void math_atan (void) | ||
98 | { | ||
99 | double d; | ||
100 | lua_Object o = lua_getparam (1); | ||
101 | if (o == NULL) | ||
102 | { lua_error ("too few arguments to function `atan'"); return; } | ||
103 | if (!lua_isnumber(o)) | ||
104 | { lua_error ("incorrect arguments to function `atan'"); return; } | ||
105 | d = lua_getnumber(o); | ||
106 | lua_pushnumber (atan(d)); | ||
107 | } | ||
108 | |||
109 | |||
110 | static void math_ceil (void) | ||
111 | { | ||
112 | double d; | ||
113 | lua_Object o = lua_getparam (1); | ||
114 | if (o == NULL) | ||
115 | { lua_error ("too few arguments to function `ceil'"); return; } | ||
116 | if (!lua_isnumber(o)) | ||
117 | { lua_error ("incorrect arguments to function `ceil'"); return; } | ||
118 | d = lua_getnumber(o); | ||
119 | lua_pushnumber (ceil(d)); | ||
120 | } | ||
121 | |||
122 | |||
123 | static void math_floor (void) | ||
124 | { | ||
125 | double d; | ||
126 | lua_Object o = lua_getparam (1); | ||
127 | if (o == NULL) | ||
128 | { lua_error ("too few arguments to function `floor'"); return; } | ||
129 | if (!lua_isnumber(o)) | ||
130 | { lua_error ("incorrect arguments to function `floor'"); return; } | ||
131 | d = lua_getnumber(o); | ||
132 | lua_pushnumber (floor(d)); | ||
133 | } | ||
134 | |||
135 | static void math_mod (void) | ||
136 | { | ||
137 | int d1, d2; | ||
138 | lua_Object o1 = lua_getparam (1); | ||
139 | lua_Object o2 = lua_getparam (2); | ||
140 | if (!lua_isnumber(o1) || !lua_isnumber(o2)) | ||
141 | { lua_error ("incorrect arguments to function `mod'"); return; } | ||
142 | d1 = (int) lua_getnumber(o1); | ||
143 | d2 = (int) lua_getnumber(o2); | ||
144 | lua_pushnumber (d1%d2); | ||
145 | } | ||
146 | |||
147 | |||
148 | static void math_sqrt (void) | ||
149 | { | ||
150 | double d; | ||
151 | lua_Object o = lua_getparam (1); | ||
152 | if (o == NULL) | ||
153 | { lua_error ("too few arguments to function `sqrt'"); return; } | ||
154 | if (!lua_isnumber(o)) | ||
155 | { lua_error ("incorrect arguments to function `sqrt'"); return; } | ||
156 | d = lua_getnumber(o); | ||
157 | lua_pushnumber (sqrt(d)); | ||
158 | } | ||
159 | |||
160 | static void math_pow (void) | ||
161 | { | ||
162 | double d1, d2; | ||
163 | lua_Object o1 = lua_getparam (1); | ||
164 | lua_Object o2 = lua_getparam (2); | ||
165 | if (!lua_isnumber(o1) || !lua_isnumber(o2)) | ||
166 | { lua_error ("incorrect arguments to function `pow'"); return; } | ||
167 | d1 = lua_getnumber(o1); | ||
168 | d2 = lua_getnumber(o2); | ||
169 | lua_pushnumber (pow(d1,d2)); | ||
170 | } | ||
171 | |||
172 | static void math_min (void) | ||
173 | { | ||
174 | int i=1; | ||
175 | double d, dmin; | ||
176 | lua_Object o; | ||
177 | if ((o = lua_getparam(i++)) == NULL) | ||
178 | { lua_error ("too few arguments to function `min'"); return; } | ||
179 | if (!lua_isnumber(o)) | ||
180 | { lua_error ("incorrect arguments to function `min'"); return; } | ||
181 | dmin = lua_getnumber (o); | ||
182 | while ((o = lua_getparam(i++)) != NULL) | ||
183 | { | ||
184 | if (!lua_isnumber(o)) | ||
185 | { lua_error ("incorrect arguments to function `min'"); return; } | ||
186 | d = lua_getnumber (o); | ||
187 | if (d < dmin) dmin = d; | ||
188 | } | ||
189 | lua_pushnumber (dmin); | ||
190 | } | ||
191 | |||
192 | |||
193 | static void math_max (void) | ||
194 | { | ||
195 | int i=1; | ||
196 | double d, dmax; | ||
197 | lua_Object o; | ||
198 | if ((o = lua_getparam(i++)) == NULL) | ||
199 | { lua_error ("too few arguments to function `max'"); return; } | ||
200 | if (!lua_isnumber(o)) | ||
201 | { lua_error ("incorrect arguments to function `max'"); return; } | ||
202 | dmax = lua_getnumber (o); | ||
203 | while ((o = lua_getparam(i++)) != NULL) | ||
204 | { | ||
205 | if (!lua_isnumber(o)) | ||
206 | { lua_error ("incorrect arguments to function `max'"); return; } | ||
207 | d = lua_getnumber (o); | ||
208 | if (d > dmax) dmax = d; | ||
209 | } | ||
210 | lua_pushnumber (dmax); | ||
211 | } | ||
212 | |||
213 | |||
214 | |||
215 | /* | ||
216 | ** Open math library | ||
217 | */ | ||
218 | void mathlib_open (void) | ||
219 | { | ||
220 | lua_register ("abs", math_abs); | ||
221 | lua_register ("sin", math_sin); | ||
222 | lua_register ("cos", math_cos); | ||
223 | lua_register ("tan", math_tan); | ||
224 | lua_register ("asin", math_asin); | ||
225 | lua_register ("acos", math_acos); | ||
226 | lua_register ("atan", math_atan); | ||
227 | lua_register ("ceil", math_ceil); | ||
228 | lua_register ("floor", math_floor); | ||
229 | lua_register ("mod", math_mod); | ||
230 | lua_register ("sqrt", math_sqrt); | ||
231 | lua_register ("pow", math_pow); | ||
232 | lua_register ("min", math_min); | ||
233 | lua_register ("max", math_max); | ||
234 | } | ||