3 ** Mathematics library to LUA
6 char *rcs_mathlib
="$Id: mathlib.c,v 1.18 1996/08/01 14:55:33 roberto Exp $";
15 #define PI 3.14159265358979323846
17 #define TODEGREE(a) ((a)*180.0/PI)
18 #define TORAD(a) ((a)*PI/180.0)
20 static void math_abs (void)
22 double d
= lua_check_number(1, "abs");
28 static void math_sin (void)
30 double d
= lua_check_number(1, "sin");
31 lua_pushnumber (sin(TORAD(d
)));
36 static void math_cos (void)
38 double d
= lua_check_number(1, "cos");
39 lua_pushnumber (cos(TORAD(d
)));
44 static void math_tan (void)
46 double d
= lua_check_number(1, "tan");
47 lua_pushnumber (tan(TORAD(d
)));
51 static void math_asin (void)
53 double d
= lua_check_number(1, "asin");
54 lua_pushnumber (TODEGREE(asin(d
)));
58 static void math_acos (void)
60 double d
= lua_check_number(1, "acos");
61 lua_pushnumber (TODEGREE(acos(d
)));
65 static void math_atan (void)
67 double d
= lua_check_number(1, "atan");
68 lua_pushnumber (TODEGREE(atan(d
)));
72 static void math_atan2 (void)
74 double d1
= lua_check_number(1, "atan2");
75 double d2
= lua_check_number(2, "atan2");
76 lua_pushnumber (TODEGREE(atan2(d1
, d2
)));
80 static void math_ceil (void)
82 double d
= lua_check_number(1, "ceil");
83 lua_pushnumber (ceil(d
));
87 static void math_floor (void)
89 double d
= lua_check_number(1, "floor");
90 lua_pushnumber (floor(d
));
93 static void math_mod (void)
95 float x
= lua_check_number(1, "mod");
96 float y
= lua_check_number(2, "mod");
97 lua_pushnumber(fmod(x
, y
));
101 static void math_sqrt (void)
103 double d
= lua_check_number(1, "sqrt");
104 lua_pushnumber (sqrt(d
));
109 static void math_pow (void)
111 lua_Object o1
= lua_getparam (1);
112 lua_Object o2
= lua_getparam (2);
113 lua_Object op
= lua_getparam(3);
114 if (!lua_isnumber(o1
) || !lua_isnumber(o2
) || *(lua_getstring(op
)) != 'p')
116 lua_Object old
= lua_getref(old_pow
);
120 if (lua_callfunction(old
) != 0)
125 double d1
= lua_getnumber(o1
);
126 double d2
= lua_getnumber(o2
);
127 lua_pushnumber (pow(d1
,d2
));
131 static void math_min (void)
134 double dmin
= lua_check_number(i
, "min");
135 while (lua_getparam(++i
) != LUA_NOOBJECT
)
137 double d
= lua_check_number(i
, "min");
138 if (d
< dmin
) dmin
= d
;
140 lua_pushnumber (dmin
);
143 static void math_max (void)
146 double dmax
= lua_check_number(i
, "max");
147 while (lua_getparam(++i
) != LUA_NOOBJECT
)
149 double d
= lua_check_number(i
, "max");
150 if (d
> dmax
) dmax
= d
;
152 lua_pushnumber (dmax
);
155 static void math_log (void)
157 double d
= lua_check_number(1, "log");
158 lua_pushnumber (log(d
));
162 static void math_log10 (void)
164 double d
= lua_check_number(1, "log10");
165 lua_pushnumber (log10(d
));
169 static void math_exp (void)
171 double d
= lua_check_number(1, "exp");
172 lua_pushnumber (exp(d
));
175 static void math_deg (void)
177 float d
= lua_check_number(1, "deg");
178 lua_pushnumber (d
*180./PI
);
181 static void math_rad (void)
183 float d
= lua_check_number(1, "rad");
184 lua_pushnumber (d
/180.*PI
);
187 static void math_random (void)
189 lua_pushnumber((double)(rand()%RAND_MAX
) / (double)RAND_MAX
);
192 static void math_randomseed (void)
194 srand(lua_check_number(1, "randomseed"));
198 static struct lua_reg mathlib
[] = {
206 {"atan2", math_atan2
},
208 {"floor", math_floor
},
214 {"log10", math_log10
},
218 {"random", math_random
},
219 {"randomseed", math_randomseed
}
225 void mathlib_open (void)
227 luaI_openlib(mathlib
, (sizeof(mathlib
)/sizeof(mathlib
[0])));
228 old_pow
= lua_refobject(lua_setfallback("arith", math_pow
), 1);