00001
00002
00003
00004
00005
00006
00007
00008 #include <stdlib.h>
00009 #include <math.h>
00010
00011 #define lmathlib_c
00012 #define LUA_LIB
00013
00014 #include "lua.h"
00015
00016 #include "lauxlib.h"
00017 #include "lualib.h"
00018
00019
00020 static int math_abs (lua_State *L)
00021 {
00022 fixed param(true, luaL_checknumber(L, 1));
00023 fixed result = param.abs();
00024 lua_pushnumber(L, result.getInternal());
00025 return 1;
00026 }
00027
00028 static int math_sin (lua_State *L)
00029 {
00030 fixed param(true, luaL_checknumber(L, 1));
00031 fixed result = param.sin();
00032 lua_pushnumber(L, result.getInternal());
00033 return 1;
00034 }
00035
00036 static int math_cos (lua_State *L)
00037 {
00038 fixed param(true, luaL_checknumber(L, 1));
00039 fixed result = param.cos();
00040 lua_pushnumber(L, result.getInternal());
00041 return 1;
00042 }
00043
00044 static int math_tan (lua_State *L)
00045 {
00046 fixed param(true, luaL_checknumber(L, 1));
00047 fixed result = param.tan();
00048 lua_pushnumber(L, result.getInternal());
00049 return 1;
00050 }
00051
00052 static int math_acos (lua_State *L)
00053 {
00054 fixed param(true, luaL_checknumber(L, 1));
00055 fixed result = param.acos();
00056 lua_pushnumber(L, result.getInternal());
00057 return 1;
00058 }
00059
00060 static int math_ceil (lua_State *L)
00061 {
00062 fixed param(true, luaL_checknumber(L, 1));
00063 fixed result = param.ceil();
00064 lua_pushnumber(L, result.getInternal());
00065 return 1;
00066 }
00067
00068 static int math_floor (lua_State *L)
00069 {
00070 fixed param(true, luaL_checknumber(L, 1));
00071 fixed result = param.floor();
00072 lua_pushnumber(L, result.getInternal());
00073 return 1;
00074 }
00075
00076 static int math_sqrt (lua_State *L)
00077 {
00078 fixed param(true, luaL_checknumber(L, 1));
00079 fixed result = param.sqrt();
00080 lua_pushnumber(L, result.getInternal());
00081 return 1;
00082 }
00083
00084 static int math_pow (lua_State *L)
00085 {
00086 fixed param1(true, luaL_checknumber(L, 1));
00087 fixed param2(true, luaL_checknumber(L, 2));
00088 fixed result = param1.pow(param2);
00089 lua_pushnumber(L, result.getInternal());
00090 return 1;
00091 }
00092
00093 static int math_log (lua_State *L)
00094 {
00095 fixed param(true, luaL_checknumber(L, 1));
00096 fixed result = param.log();
00097 lua_pushnumber(L, result.getInternal());
00098 return 1;
00099 }
00100
00101 static int math_log10 (lua_State *L)
00102 {
00103 fixed param(true, luaL_checknumber(L, 1));
00104 fixed result = param.log10();
00105 lua_pushnumber(L, result.getInternal());
00106 return 1;
00107 }
00108
00109 static int math_exp (lua_State *L)
00110 {
00111 fixed param(true, luaL_checknumber(L, 1));
00112 fixed result = param.exp();
00113 lua_pushnumber(L, result.getInternal());
00114 return 1;
00115 }
00116
00117 static int math_min (lua_State *L)
00118 {
00119 int n = lua_gettop(L);
00120 lua_Number dmin = luaL_checknumber(L, 1);
00121 int i;
00122 for (i=2; i<=n; i++) {
00123 lua_Number d = luaL_checknumber(L, i);
00124 if (d < dmin)
00125 dmin = d;
00126 }
00127 lua_pushnumber(L, dmin);
00128 return 1;
00129 }
00130
00131 static int math_max (lua_State *L)
00132 {
00133 int n = lua_gettop(L);
00134 lua_Number dmax = luaL_checknumber(L, 1);
00135 int i;
00136 for (i=2; i<=n; i++) {
00137 lua_Number d = luaL_checknumber(L, i);
00138 if (d > dmax)
00139 dmax = d;
00140 }
00141 lua_pushnumber(L, dmax);
00142 return 1;
00143 }
00144
00145
00146 static const luaL_Reg mathlib[] = {
00147 {"abs", math_abs},
00148 {"acos", math_acos},
00149
00150
00151
00152 {"ceil", math_ceil},
00153
00154 {"cos", math_cos},
00155
00156 {"exp", math_exp},
00157 {"floor", math_floor},
00158
00159
00160
00161 {"log10", math_log10},
00162 {"log", math_log},
00163 {"max", math_max},
00164 {"min", math_min},
00165
00166 {"pow", math_pow},
00167
00168
00169 {"sin", math_sin},
00170 {"sqrt", math_sqrt},
00171
00172 {"tan", math_tan},
00173 {NULL, NULL}
00174 };
00175
00176
00177
00178
00179
00180 LUALIB_API int luaopen_math (lua_State *L) {
00181 luaL_register(L, LUA_MATHLIBNAME, mathlib);
00182 return 1;
00183 }
00184