diff options
Diffstat (limited to '')
| -rw-r--r-- | mathlib.c | 26 |
1 files changed, 5 insertions, 21 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** Mathematics library to LUA | 3 | ** Mathematics library to LUA |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.19 1997/03/17 17:01:10 roberto Exp roberto $"; | 6 | char *rcs_mathlib="$Id: mathlib.c,v 1.20 1997/03/18 15:30:50 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <stdlib.h> | 8 | #include <stdlib.h> |
| 9 | #include <math.h> | 9 | #include <math.h> |
| @@ -105,28 +105,12 @@ static void math_sqrt (void) | |||
| 105 | lua_pushnumber (sqrt(d)); | 105 | lua_pushnumber (sqrt(d)); |
| 106 | } | 106 | } |
| 107 | 107 | ||
| 108 | static int old_pow; | ||
| 109 | 108 | ||
| 110 | static void math_pow (void) | 109 | static void math_pow (void) |
| 111 | { | 110 | { |
| 112 | lua_Object o1 = lua_getparam (1); | 111 | double d1 = luaL_check_number(1, "exp"); |
| 113 | lua_Object o2 = lua_getparam (2); | 112 | double d2 = luaL_check_number(2, "exp"); |
| 114 | lua_Object op = lua_getparam(3); | 113 | lua_pushnumber(pow(d1,d2)); |
| 115 | if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p') | ||
| 116 | { | ||
| 117 | lua_Object old = lua_getref(old_pow); | ||
| 118 | lua_pushobject(o1); | ||
| 119 | lua_pushobject(o2); | ||
| 120 | lua_pushobject(op); | ||
| 121 | if (lua_callfunction(old) != 0) | ||
| 122 | lua_error(NULL); | ||
| 123 | } | ||
| 124 | else | ||
| 125 | { | ||
| 126 | double d1 = lua_getnumber(o1); | ||
| 127 | double d2 = lua_getnumber(o2); | ||
| 128 | lua_pushnumber (pow(d1,d2)); | ||
| 129 | } | ||
| 130 | } | 114 | } |
| 131 | 115 | ||
| 132 | static void math_min (void) | 116 | static void math_min (void) |
| @@ -226,6 +210,6 @@ static struct luaL_reg mathlib[] = { | |||
| 226 | void mathlib_open (void) | 210 | void mathlib_open (void) |
| 227 | { | 211 | { |
| 228 | luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); | 212 | luaL_openlib(mathlib, (sizeof(mathlib)/sizeof(mathlib[0]))); |
| 229 | old_pow = lua_refobject(lua_setfallback("arith", math_pow), 1); | 213 | lua_setintmethod(0, "pow", math_pow); |
| 230 | } | 214 | } |
| 231 | 215 | ||
