3 ** Mathematica library to LUA
5 ** Waldemar Celes Filho
10 #include <stdio.h> /* NULL */
15 static void math_abs (void)
18 lua_Object o
= lua_getparam (1);
20 { lua_error ("too few arguments to function `abs'"); return; }
22 { lua_error ("incorrect arguments to function `abs'"); return; }
29 static void math_sin (void)
32 lua_Object o
= lua_getparam (1);
34 { lua_error ("too few arguments to function `sin'"); return; }
36 { lua_error ("incorrect arguments to function `sin'"); return; }
38 lua_pushnumber (sin(d
));
43 static void math_cos (void)
46 lua_Object o
= lua_getparam (1);
48 { lua_error ("too few arguments to function `cos'"); return; }
50 { lua_error ("incorrect arguments to function `cos'"); return; }
52 lua_pushnumber (cos(d
));
57 static void math_tan (void)
60 lua_Object o
= lua_getparam (1);
62 { lua_error ("too few arguments to function `tan'"); return; }
64 { lua_error ("incorrect arguments to function `tan'"); return; }
66 lua_pushnumber (tan(d
));
70 static void math_asin (void)
73 lua_Object o
= lua_getparam (1);
75 { lua_error ("too few arguments to function `asin'"); return; }
77 { lua_error ("incorrect arguments to function `asin'"); return; }
79 lua_pushnumber (asin(d
));
83 static void math_acos (void)
86 lua_Object o
= lua_getparam (1);
88 { lua_error ("too few arguments to function `acos'"); return; }
90 { lua_error ("incorrect arguments to function `acos'"); return; }
92 lua_pushnumber (acos(d
));
97 static void math_atan (void)
100 lua_Object o
= lua_getparam (1);
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)
113 lua_Object o
= lua_getparam (1);
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)
126 lua_Object o
= lua_getparam (1);
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)
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)
151 lua_Object o
= lua_getparam (1);
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)
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)
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)
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
);
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
);