aboutsummaryrefslogtreecommitdiff
path: root/mathlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-21 15:37:28 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1997-03-21 15:37:28 -0300
commit6fef372fb8b14be806070d6c2e96fd2b9cb2e41a (patch)
treeb47572e1d79829b1871e89a86559e35510a6635a /mathlib.c
parent052a1cc46c3b1a2e589bd061be0e1bd153c9e47a (diff)
downloadlua-6fef372fb8b14be806070d6c2e96fd2b9cb2e41a.tar.gz
lua-6fef372fb8b14be806070d6c2e96fd2b9cb2e41a.tar.bz2
lua-6fef372fb8b14be806070d6c2e96fd2b9cb2e41a.zip
mathlib now uses i.m. for "pow" operator.
Diffstat (limited to 'mathlib.c')
-rw-r--r--mathlib.c26
1 files changed, 5 insertions, 21 deletions
diff --git a/mathlib.c b/mathlib.c
index 798ba322..16d2a815 100644
--- a/mathlib.c
+++ b/mathlib.c
@@ -3,7 +3,7 @@
3** Mathematics library to LUA 3** Mathematics library to LUA
4*/ 4*/
5 5
6char *rcs_mathlib="$Id: mathlib.c,v 1.19 1997/03/17 17:01:10 roberto Exp roberto $"; 6char *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
108static int old_pow;
109 108
110static void math_pow (void) 109static 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
132static void math_min (void) 116static void math_min (void)
@@ -226,6 +210,6 @@ static struct luaL_reg mathlib[] = {
226void mathlib_open (void) 210void 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