summaryrefslogtreecommitdiff
path: root/lvm.c
diff options
context:
space:
mode:
Diffstat (limited to 'lvm.c')
-rw-r--r--lvm.c22
1 files changed, 15 insertions, 7 deletions
diff --git a/lvm.c b/lvm.c
index 8f13f448..694d5546 100644
--- a/lvm.c
+++ b/lvm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lvm.c,v 2.216 2014/06/19 18:27:20 roberto Exp roberto $ 2** $Id: lvm.c,v 2.217 2014/06/30 19:48:08 roberto Exp roberto $
3** Lua virtual machine 3** Lua virtual machine
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -80,15 +80,23 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
80 80
81 81
82/* 82/*
83** try to convert a value to an integer, rounding up if 'up' is true 83** try to convert a value to an integer, rounding according to 'mode':
84** mode == 0: accepts only integral values
85** mode < 0: takes the floor of the number
86** mode > 0: takes the ceil of the number
84*/ 87*/
85static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) { 88static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
86 TValue v; 89 TValue v;
87 again: 90 again:
88 if (ttisfloat(obj)) { 91 if (ttisfloat(obj)) {
89 lua_Number n = fltvalue(obj); 92 lua_Number n = fltvalue(obj);
90 n = (up ? -l_floor(-n) : l_floor(n)); 93 lua_Number f = l_floor(n);
91 return lua_numtointeger(n, p); 94 if (n != f) { /* not an integral value? */
95 if (mode == 0) return 0; /* fails if mode demands integral value */
96 else if (mode > 0) /* needs ceil? */
97 f += 1; /* convert floor to ceil (remember: n != f) */
98 }
99 return lua_numtointeger(f, p);
92 } 100 }
93 else if (ttisinteger(obj)) { 101 else if (ttisinteger(obj)) {
94 *p = ivalue(obj); 102 *p = ivalue(obj);
@@ -104,7 +112,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
104 112
105 113
106/* 114/*
107** try to convert a non-integer value to an integer, rounding down 115** try to convert a value to an integer
108*/ 116*/
109int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { 117int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
110 return tointeger_aux(obj, p, 0); 118 return tointeger_aux(obj, p, 0);
@@ -155,7 +163,7 @@ int luaV_tostring (lua_State *L, StkId obj) {
155static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, 163static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
156 int *stopnow) { 164 int *stopnow) {
157 *stopnow = 0; /* usually, let loops run */ 165 *stopnow = 0; /* usually, let loops run */
158 if (!tointeger_aux(obj, p, (step < 0))) { /* does not fit in integer? */ 166 if (!tointeger_aux(obj, p, (step < 0 ? 1 : -1))) { /* not fit in integer? */
159 lua_Number n; /* try to convert to float */ 167 lua_Number n; /* try to convert to float */
160 if (!tonumber(obj, &n)) /* cannot convert to float? */ 168 if (!tonumber(obj, &n)) /* cannot convert to float? */
161 return 0; /* not a number */ 169 return 0; /* not a number */