From bde14c3adc055d2a16cfe718f1b698801dcd4568 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 3 Nov 2014 18:07:47 -0200 Subject: macro to change method of conversion from float to integer (make it use floor intead of requiring an exact integral value) --- lvm.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lvm.c b/lvm.c index b554c8d8..ba4bf398 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 2.226 2014/10/25 11:50:46 roberto Exp roberto $ +** $Id: lvm.c,v 2.227 2014/11/02 19:19:04 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -30,6 +30,15 @@ #include "lvm.h" +/* +** You can define LUA_FLOORN2I if you want to convert floats to integers +** by flooring them (instead of raising an error if they are not +** integral values) +*/ +#if !defined(LUA_FLOORN2I) +#define LUA_FLOORN2I 0 +#endif + /* limit for table tag-method chains (to avoid loops) */ #define MAXTAGLOOP 2000 @@ -80,8 +89,8 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { /* ** try to convert a value to an integer, rounding according to 'mode': ** mode == 0: accepts only integral values -** mode < 0: takes the floor of the number -** mode > 0: takes the ceil of the number +** mode == 1: takes the floor of the number +** mode == 2: takes the ceil of the number */ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { TValue v; @@ -91,7 +100,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { lua_Number f = l_floor(n); if (n != f) { /* not an integral value? */ if (mode == 0) return 0; /* fails if mode demands integral value */ - else if (mode > 0) /* needs ceil? */ + else if (mode > 1) /* needs ceil? */ f += 1; /* convert floor to ceil (remember: n != f) */ } return lua_numbertointeger(f, p); @@ -113,7 +122,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { ** try to convert a value to an integer */ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { - return tointeger_aux(obj, p, 0); + return tointeger_aux(obj, p, LUA_FLOORN2I); } @@ -135,7 +144,7 @@ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, int *stopnow) { *stopnow = 0; /* usually, let loops run */ - if (!tointeger_aux(obj, p, (step < 0 ? 1 : -1))) { /* not fit in integer? */ + if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */ lua_Number n; /* try to convert to float */ if (!tonumber(obj, &n)) /* cannot convert to float? */ return 0; /* not a number */ -- cgit v1.2.3-55-g6feb