static void math_max (void) { int i=1; double dmax = lua_check_number(i, "max"); while (lua_getparam(++i) != LUA_NOOBJECT) { double d = lua_check_number(i, "max"); if (d > dmax) dmax = d; } lua_pushnumber (dmax); }
static void math_min (void) { int i=1; double dmin = lua_check_number(i, "min"); while (lua_getparam(++i) != LUA_NOOBJECT) { double d = lua_check_number(i, "min"); if (d < dmin) dmin = d; } lua_pushnumber (dmin); }
/* ** Return the substring of a string, from start to end ** LUA interface: ** substring = strsub (string, start, end) */ static void str_sub (void) { char *s = lua_check_string(1, "strsub"); int start = (int)lua_check_number(2, "strsub"); int end = lua_opt_number(3, strlen(s), "strsub"); if (end < start || start < 1 || end > strlen(s)) lua_pushliteral(""); else { luaI_addchar(0); while (start <= end) luaI_addchar(s[start++ - 1]); lua_pushstring (luaI_addchar(0)); } }
static void math_tan (void) { double d = lua_check_number(1, "tan"); lua_pushnumber (tan(TORAD(d))); }
static void math_cos (void) { double d = lua_check_number(1, "cos"); lua_pushnumber (cos(TORAD(d))); }
static void math_sin (void) { double d = lua_check_number(1, "sin"); lua_pushnumber (sin(TORAD(d))); }
static void math_randomseed (void) { srand(lua_check_number(1, "randomseed")); }
static void str_format (void) { int arg = 1; char *strfrmt = lua_check_string(arg++, "format"); luaI_addchar(0); /* initialize */ while (*strfrmt) { if (*strfrmt != '%') luaI_addchar(*strfrmt++); else if (*++strfrmt == '%') luaI_addchar(*strfrmt++); /* %% */ else { /* format item */ char form[MAX_FORMAT]; /* store the format ('%...') */ char buff[MAX_CONVERTION]; /* store the formated value */ int size = 0; int i = 0; form[i++] = '%'; form[i] = *strfrmt++; while (!isalpha(form[i])) { if (isdigit(form[i])) { size = size*10 + form[i]-'0'; if (size >= MAX_CONVERTION) lua_error("format size/precision too long in function `format'"); } else if (form[i] == '.') size = 0; /* re-start */ if (++i >= MAX_FORMAT) lua_error("bad format in function `format'"); form[i] = *strfrmt++; } form[i+1] = 0; /* ends string */ switch (form[i]) { case 'q': luaI_addquoted(lua_check_string(arg++, "format")); buff[0] = '\0'; /* addchar already done */ break; case 's': { char *s = lua_check_string(arg++, "format"); if (strlen(s) >= MAX_CONVERTION) lua_error("string argument too long in function `format'"); sprintf(buff, form, s); break; } case 'c': case 'd': case 'i': case 'o': case 'u': case 'x': case 'X': sprintf(buff, form, (int)lua_check_number(arg++, "format")); break; case 'e': case 'E': case 'f': case 'g': sprintf(buff, form, lua_check_number(arg++, "format")); break; default: /* also treat cases 'pnLlh' */ lua_error("invalid format option in function `format'"); } for (i=0; buff[i]; i++) /* move formated value to result */ luaI_addchar(buff[i]); } } lua_pushstring(luaI_addchar(0)); /* push the result */ }
static void math_floor (void) { double d = lua_check_number(1, "floor"); lua_pushnumber (floor(d)); }
static void math_atan2 (void) { double d1 = lua_check_number(1, "atan2"); double d2 = lua_check_number(2, "atan2"); lua_pushnumber (TODEGREE(atan2(d1, d2))); }
static void math_exp (void) { double d = lua_check_number(1, "exp"); lua_pushnumber (exp(d)); }
static void math_log10 (void) { double d = lua_check_number(1, "log10"); lua_pushnumber (log10(d)); }
static void math_sqrt (void) { double d = lua_check_number(1, "sqrt"); lua_pushnumber (sqrt(d)); }
static int lua_opt_number (int numArg, int def, char *funcname) { return (lua_getparam(numArg) == LUA_NOOBJECT) ? def : (int)lua_check_number(numArg, funcname); }
static void math_acos (void) { double d = lua_check_number(1, "acos"); lua_pushnumber (TODEGREE(acos(d))); }
static void math_atan (void) { double d = lua_check_number(1, "atan"); lua_pushnumber (TODEGREE(atan(d))); }
static void math_deg (void) { float d = lua_check_number(1, "deg"); lua_pushnumber (d*180./PI); }
static void math_ceil (void) { double d = lua_check_number(1, "ceil"); lua_pushnumber (ceil(d)); }
static void math_rad (void) { float d = lua_check_number(1, "rad"); lua_pushnumber (d/180.*PI); }
static void math_mod (void) { int d1 = (int)lua_check_number(1, "mod"); int d2 = (int)lua_check_number(2, "mod"); lua_pushnumber (d1%d2); }
static void math_abs (void) { double d = lua_check_number(1, "abs"); if (d < 0) d = -d; lua_pushnumber (d); }