summaryrefslogtreecommitdiff
path: root/lib/lua/src/LuaLib/lmathlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/lua/src/LuaLib/lmathlib.c')
-rw-r--r--lib/lua/src/LuaLib/lmathlib.c105
1 files changed, 61 insertions, 44 deletions
diff --git a/lib/lua/src/LuaLib/lmathlib.c b/lib/lua/src/LuaLib/lmathlib.c
index 998c46c..417fdca 100644
--- a/lib/lua/src/LuaLib/lmathlib.c
+++ b/lib/lua/src/LuaLib/lmathlib.c
@@ -1,5 +1,5 @@
/*
-** $Id: lmathlib.c,v 1.4 2004-11-27 21:46:10 pixel Exp $
+** $Id: lmathlib.c,v 1.5 2007-07-27 10:05:55 pixel Exp $
** Standard mathematical library
** See Copyright Notice in lua.h
*/
@@ -9,6 +9,7 @@
#include <math.h>
#define lmathlib_c
+#define LUA_LIB
#include "lua.h"
@@ -22,56 +23,58 @@
-/*
-** If you want Lua to operate in degrees (instead of radians),
-** define USE_DEGREES
-*/
-#ifdef USE_DEGREES
-#define FROMRAD(a) ((a)/RADIANS_PER_DEGREE)
-#define TORAD(a) ((a)*RADIANS_PER_DEGREE)
-#else
-#define FROMRAD(a) (a)
-#define TORAD(a) (a)
-#endif
-
-
static int math_abs (lua_State *L) {
lua_pushnumber(L, fabs(luaL_checknumber(L, 1)));
return 1;
}
static int math_sin (lua_State *L) {
- lua_pushnumber(L, sin(TORAD(luaL_checknumber(L, 1))));
+ lua_pushnumber(L, sin(luaL_checknumber(L, 1)));
+ return 1;
+}
+
+static int math_sinh (lua_State *L) {
+ lua_pushnumber(L, sinh(luaL_checknumber(L, 1)));
return 1;
}
static int math_cos (lua_State *L) {
- lua_pushnumber(L, cos(TORAD(luaL_checknumber(L, 1))));
+ lua_pushnumber(L, cos(luaL_checknumber(L, 1)));
+ return 1;
+}
+
+static int math_cosh (lua_State *L) {
+ lua_pushnumber(L, cosh(luaL_checknumber(L, 1)));
return 1;
}
static int math_tan (lua_State *L) {
- lua_pushnumber(L, tan(TORAD(luaL_checknumber(L, 1))));
+ lua_pushnumber(L, tan(luaL_checknumber(L, 1)));
+ return 1;
+}
+
+static int math_tanh (lua_State *L) {
+ lua_pushnumber(L, tanh(luaL_checknumber(L, 1)));
return 1;
}
static int math_asin (lua_State *L) {
- lua_pushnumber(L, FROMRAD(asin(luaL_checknumber(L, 1))));
+ lua_pushnumber(L, asin(luaL_checknumber(L, 1)));
return 1;
}
static int math_acos (lua_State *L) {
- lua_pushnumber(L, FROMRAD(acos(luaL_checknumber(L, 1))));
+ lua_pushnumber(L, acos(luaL_checknumber(L, 1)));
return 1;
}
static int math_atan (lua_State *L) {
- lua_pushnumber(L, FROMRAD(atan(luaL_checknumber(L, 1))));
+ lua_pushnumber(L, atan(luaL_checknumber(L, 1)));
return 1;
}
static int math_atan2 (lua_State *L) {
- lua_pushnumber(L, FROMRAD(atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2))));
+ lua_pushnumber(L, atan2(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
return 1;
}
@@ -85,11 +88,19 @@ static int math_floor (lua_State *L) {
return 1;
}
-static int math_mod (lua_State *L) {
+static int math_fmod (lua_State *L) {
lua_pushnumber(L, fmod(luaL_checknumber(L, 1), luaL_checknumber(L, 2)));
return 1;
}
+static int math_modf (lua_State *L) {
+ double ip;
+ double fp = modf(luaL_checknumber(L, 1), &ip);
+ lua_pushnumber(L, ip);
+ lua_pushnumber(L, fp);
+ return 2;
+}
+
static int math_sqrt (lua_State *L) {
lua_pushnumber(L, sqrt(luaL_checknumber(L, 1)));
return 1;
@@ -128,7 +139,7 @@ static int math_rad (lua_State *L) {
static int math_frexp (lua_State *L) {
int e;
lua_pushnumber(L, frexp(luaL_checknumber(L, 1), &e));
- lua_pushnumber(L, e);
+ lua_pushinteger(L, e);
return 2;
}
@@ -179,14 +190,14 @@ static int math_random (lua_State *L) {
case 1: { /* only upper limit */
int u = luaL_checkint(L, 1);
luaL_argcheck(L, 1<=u, 1, "interval is empty");
- lua_pushnumber(L, (int)floor(r*u)+1); /* int between 1 and `u' */
+ lua_pushnumber(L, floor(r*u)+1); /* int between 1 and `u' */
break;
}
case 2: { /* lower and upper limits */
int l = luaL_checkint(L, 1);
int u = luaL_checkint(L, 2);
luaL_argcheck(L, l<=u, 2, "interval is empty");
- lua_pushnumber(L, (int)floor(r*(u-l+1))+l); /* int between `l' and `u' */
+ lua_pushnumber(L, floor(r*(u-l+1))+l); /* int between `l' and `u' */
break;
}
default: return luaL_error(L, "wrong number of arguments");
@@ -201,31 +212,35 @@ static int math_randomseed (lua_State *L) {
}
-static const luaL_reg mathlib[] = {
+static const luaL_Reg mathlib[] = {
{"abs", math_abs},
- {"sin", math_sin},
- {"cos", math_cos},
- {"tan", math_tan},
- {"asin", math_asin},
{"acos", math_acos},
- {"atan", math_atan},
+ {"asin", math_asin},
{"atan2", math_atan2},
+ {"atan", math_atan},
{"ceil", math_ceil},
+ {"cosh", math_cosh},
+ {"cos", math_cos},
+ {"deg", math_deg},
+ {"exp", math_exp},
{"floor", math_floor},
- {"mod", math_mod},
+ {"fmod", math_fmod},
{"frexp", math_frexp},
{"ldexp", math_ldexp},
- {"sqrt", math_sqrt},
- {"min", math_min},
- {"max", math_max},
- {"log", math_log},
{"log10", math_log10},
- {"exp", math_exp},
- {"deg", math_deg},
+ {"log", math_log},
+ {"max", math_max},
+ {"min", math_min},
+ {"modf", math_modf},
{"pow", math_pow},
{"rad", math_rad},
{"random", math_random},
{"randomseed", math_randomseed},
+ {"sinh", math_sinh},
+ {"sin", math_sin},
+ {"sqrt", math_sqrt},
+ {"tanh", math_tanh},
+ {"tan", math_tan},
{NULL, NULL}
};
@@ -234,13 +249,15 @@ static const luaL_reg mathlib[] = {
** Open math library
*/
LUALIB_API int luaopen_math (lua_State *L) {
- luaL_openlib(L, LUA_MATHLIBNAME, mathlib, 0);
- lua_pushliteral(L, "pi");
+ luaL_register(L, LUA_MATHLIBNAME, mathlib);
lua_pushnumber(L, PI);
- lua_settable(L, -3);
- lua_pushliteral(L, "__pow");
- lua_pushcfunction(L, math_pow);
- lua_settable(L, LUA_GLOBALSINDEX);
+ lua_setfield(L, -2, "pi");
+ lua_pushnumber(L, HUGE_VAL);
+ lua_setfield(L, -2, "huge");
+#if defined(LUA_COMPAT_MOD)
+ lua_getfield(L, -1, "fmod");
+ lua_setfield(L, -2, "mod");
+#endif
return 1;
}