Imported from ../lua-1.0.tar.gz.
[lua.git] / mathlib.c
blobb07c8c47548159e4f960d6e281f8b48c9f90b885
1 /*
2 ** mathlib.c
3 ** Mathematica library to LUA
4 **
5 ** Waldemar Celes Filho
6 ** TeCGraf - PUC-Rio
7 ** 19 May 93
8 */
10 #include <stdio.h> /* NULL */
11 #include <math.h>
13 #include "lua.h"
15 static void math_abs (void)
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);
29 static void math_sin (void)
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));
43 static void math_cos (void)
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));
57 static void math_tan (void)
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));
70 static void math_asin (void)
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));
83 static void math_acos (void)
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));
97 static void math_atan (void)
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));
110 static void math_ceil (void)
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));
123 static void math_floor (void)
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));
135 static void math_mod (void)
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);
148 static void math_sqrt (void)
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));
160 static void math_pow (void)
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));
172 static void math_min (void)
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)
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;
189 lua_pushnumber (dmin);
193 static void math_max (void)
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)
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;
210 lua_pushnumber (dmax);
216 ** Open math library
218 void mathlib_open (void)
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);