pow operation is defined in mathlib.c

This commit is contained in:
Roberto Ierusalimschy 1994-11-17 17:43:34 -02:00
parent 9bee23fd05
commit a84aa11f71
2 changed files with 31 additions and 30 deletions

View File

@ -3,7 +3,7 @@
** Mathematics library to LUA
*/
char *rcs_mathlib="$Id: mathlib.c,v 1.3 1994/08/15 14:13:44 celes Exp celes $";
char *rcs_mathlib="$Id: mathlib.c,v 1.4 1994/10/11 13:06:47 celes Exp roberto $";
#include <stdio.h> /* NULL */
#include <math.h>
@ -160,16 +160,27 @@ static void math_sqrt (void)
lua_pushnumber (sqrt(d));
}
static int old_pow;
static void math_pow (void)
{
double d1, d2;
lua_Object o1 = lua_getparam (1);
lua_Object o2 = lua_getparam (2);
if (!lua_isnumber(o1) || !lua_isnumber(o2))
{ lua_error ("incorrect arguments to function `pow'"); return; }
d1 = lua_getnumber(o1);
d2 = lua_getnumber(o2);
lua_pushnumber (pow(d1,d2));
lua_Object op = lua_getparam(3);
if (!lua_isnumber(o1) || !lua_isnumber(o2) || *(lua_getstring(op)) != 'p')
{
lua_pushobject(o1);
lua_pushobject(o2);
lua_pushobject(op);
if (lua_callfunction(lua_getlocked(old_pow)) != 0)
lua_error(NULL);
}
else
{
double d1 = lua_getnumber(o1);
double d2 = lua_getnumber(o2);
lua_pushnumber (pow(d1,d2));
}
}
static void math_min (void)
@ -292,7 +303,6 @@ void mathlib_open (void)
lua_register ("floor", math_floor);
lua_register ("mod", math_mod);
lua_register ("sqrt", math_sqrt);
lua_register ("pow", math_pow);
lua_register ("min", math_min);
lua_register ("max", math_max);
lua_register ("log", math_log);
@ -300,4 +310,5 @@ void mathlib_open (void)
lua_register ("exp", math_exp);
lua_register ("deg", math_deg);
lua_register ("rad", math_rad);
old_pow = lua_lock(lua_setfallback("arith", math_pow));
}

View File

@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio
*/
char *rcs_opcode="$Id: opcode.c,v 3.14 1994/11/17 13:58:57 roberto Exp roberto $";
char *rcs_opcode="$Id: opcode.c,v 3.15 1994/11/17 16:41:42 roberto Exp roberto $";
#include <setjmp.h>
#include <stdio.h>
@ -69,7 +69,7 @@ static void lua_message (char *s)
*/
void lua_error (char *s)
{
lua_message(s);
if (s) lua_message(s);
if (errorJmp)
longjmp(*errorJmp, 1);
else
@ -877,19 +877,19 @@ static int lua_execute (Byte *pc, int base)
break;
case LTOP:
comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "<");
comparison(LUA_T_NUMBER, LUA_T_NIL, LUA_T_NIL, "lt");
break;
case LEOP:
comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "<=");
comparison(LUA_T_NUMBER, LUA_T_NUMBER, LUA_T_NIL, "le");
break;
case GTOP:
comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, ">");
comparison(LUA_T_NIL, LUA_T_NIL, LUA_T_NUMBER, "gt");
break;
case GEOP:
comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, ">=");
comparison(LUA_T_NIL, LUA_T_NUMBER, LUA_T_NUMBER, "ge");
break;
case ADDOP:
@ -897,7 +897,7 @@ static int lua_execute (Byte *pc, int base)
Object *l = top-2;
Object *r = top-1;
if (tonumber(r) || tonumber(l))
call_arith("+");
call_arith("add");
else
{
nvalue(l) += nvalue(r);
@ -911,7 +911,7 @@ static int lua_execute (Byte *pc, int base)
Object *l = top-2;
Object *r = top-1;
if (tonumber(r) || tonumber(l))
call_arith("-");
call_arith("sub");
else
{
nvalue(l) -= nvalue(r);
@ -925,7 +925,7 @@ static int lua_execute (Byte *pc, int base)
Object *l = top-2;
Object *r = top-1;
if (tonumber(r) || tonumber(l))
call_arith("*");
call_arith("mul");
else
{
nvalue(l) *= nvalue(r);
@ -939,7 +939,7 @@ static int lua_execute (Byte *pc, int base)
Object *l = top-2;
Object *r = top-1;
if (tonumber(r) || tonumber(l))
call_arith("/");
call_arith("div");
else
{
nvalue(l) /= nvalue(r);
@ -949,18 +949,8 @@ static int lua_execute (Byte *pc, int base)
break;
case POWOP:
{
Object *l = top-2;
Object *r = top-1;
if (tonumber(r) || tonumber(l))
call_arith("^");
else
{
nvalue(l) = pow(nvalue(l), nvalue(r));
--top;
}
}
break;
call_arith("pow");
break;
case CONCOP:
{