1
0
mirror of https://github.com/lua/lua synced 2025-04-04 03:52:52 +03:00

n^-m gives float result (instead of error)

This commit is contained in:
Roberto Ierusalimschy 2014-04-27 11:41:11 -03:00
parent cbe4998bc2
commit e98ba351ce
4 changed files with 15 additions and 17 deletions

@ -1,5 +1,5 @@
/*
** $Id: lcode.c,v 2.86 2014/04/01 14:39:55 roberto Exp roberto $
** $Id: lcode.c,v 2.87 2014/04/02 16:44:42 roberto Exp roberto $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
@ -768,8 +768,6 @@ static int validop (int op, TValue *v1, TValue *v2) {
return (tointeger(v1, &i) && tointeger(v2, &i));
case LUA_OPMOD: /* integer module by 0 */
return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) == 0);
case LUA_OPPOW: /* negative integer exponentiation */
return !(ttisinteger(v1) && ttisinteger(v2) && ivalue(v2) < 0);
default: return 1; /* everything else is valid */
}
}

@ -1,5 +1,5 @@
/*
** $Id: lobject.c,v 2.79 2014/04/15 14:28:20 roberto Exp roberto $
** $Id: lobject.c,v 2.80 2014/04/15 16:32:49 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
@ -77,7 +77,7 @@ static lua_Integer intarith (lua_State *L, int op, lua_Integer v1,
case LUA_OPSUB:return intop(-, v1, v2);
case LUA_OPMUL:return intop(*, v1, v2);
case LUA_OPMOD: return luaV_mod(L, v1, v2);
case LUA_OPPOW: return luaV_pow(L, v1, v2);
case LUA_OPPOW: return luaV_pow(v1, v2);
case LUA_OPIDIV: return luaV_div(L, v1, v2);
case LUA_OPBAND: return intop(&, v1, v2);
case LUA_OPBOR: return intop(|, v1, v2);
@ -133,7 +133,8 @@ void luaO_arith (lua_State *L, int op, const TValue *p1, const TValue *p2,
}
default: { /* other operations */
lua_Number n1; lua_Number n2;
if (ttisinteger(p1) && ttisinteger(p2)) {
if (ttisinteger(p1) && ttisinteger(p2) &&
(op != LUA_OPPOW || ivalue(p2) >= 0)) {
setivalue(res, intarith(L, op, ivalue(p1), ivalue(p2)));
return;
}

17
lvm.c

@ -1,5 +1,5 @@
/*
** $Id: lvm.c,v 2.197 2014/04/15 14:28:20 roberto Exp roberto $
** $Id: lvm.c,v 2.198 2014/04/15 16:32:49 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -374,12 +374,10 @@ lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y) {
}
lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y) {
if (y <= 0) { /* special cases: 0 or negative exponent */
if (y < 0)
luaG_runerror(L, "integer exponentiation with negative exponent");
lua_Integer luaV_pow (lua_Integer x, lua_Integer y) {
lua_assert(y >= 0);
if (y == 0)
return 1; /* x^0 == 1 */
}
else {
lua_Integer r = 1;
for (; y > 1; y >>= 1) {
@ -765,9 +763,10 @@ void luaV_execute (lua_State *L) {
TValue *rb = RKB(i);
TValue *rc = RKC(i);
lua_Number nb; lua_Number nc;
if (ttisinteger(rb) && ttisinteger(rc)) {
lua_Integer ib = ivalue(rb); lua_Integer ic = ivalue(rc);
setivalue(ra, luaV_pow(L, ib, ic));
lua_Integer ic;
if (ttisinteger(rb) && ttisinteger(rc) && (ic = ivalue(rc)) >= 0) {
lua_Integer ib = ivalue(rb);
setivalue(ra, luaV_pow(ib, ic));
}
else if (tonumber(rb, &nb) && tonumber(rc, &nc)) {
setnvalue(ra, luai_numpow(L, nb, nc));

4
lvm.h

@ -1,5 +1,5 @@
/*
** $Id: lvm.h,v 2.27 2014/04/15 14:28:20 roberto Exp roberto $
** $Id: lvm.h,v 2.28 2014/04/15 16:32:49 roberto Exp roberto $
** Lua virtual machine
** See Copyright Notice in lua.h
*/
@ -40,7 +40,7 @@ LUAI_FUNC void luaV_execute (lua_State *L);
LUAI_FUNC void luaV_concat (lua_State *L, int total);
LUAI_FUNC lua_Integer luaV_div (lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Integer luaV_mod (lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Integer luaV_pow (lua_State *L, lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Integer luaV_pow (lua_Integer x, lua_Integer y);
LUAI_FUNC lua_Integer luaV_shiftl (lua_Integer x, lua_Integer y);
LUAI_FUNC void luaV_objlen (lua_State *L, StkId ra, const TValue *rb);