aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-11-03 18:07:47 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2014-11-03 18:07:47 -0200
commitbde14c3adc055d2a16cfe718f1b698801dcd4568 (patch)
treea2168704d5a4c3398eb55b7b1f90732ca83b17c8
parent4b839d7c72f2b3ae6f0a2c91316be0ab84d0f708 (diff)
downloadlua-bde14c3adc055d2a16cfe718f1b698801dcd4568.tar.gz
lua-bde14c3adc055d2a16cfe718f1b698801dcd4568.tar.bz2
lua-bde14c3adc055d2a16cfe718f1b698801dcd4568.zip
macro to change method of conversion from float to integer (make it
use floor intead of requiring an exact integral value)
-rw-r--r--lvm.c21
1 files 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 @@
1/* 1/*
2** $Id: lvm.c,v 2.226 2014/10/25 11:50:46 roberto Exp roberto $ 2** $Id: lvm.c,v 2.227 2014/11/02 19:19:04 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*/
@@ -30,6 +30,15 @@
30#include "lvm.h" 30#include "lvm.h"
31 31
32 32
33/*
34** You can define LUA_FLOORN2I if you want to convert floats to integers
35** by flooring them (instead of raising an error if they are not
36** integral values)
37*/
38#if !defined(LUA_FLOORN2I)
39#define LUA_FLOORN2I 0
40#endif
41
33 42
34/* limit for table tag-method chains (to avoid loops) */ 43/* limit for table tag-method chains (to avoid loops) */
35#define MAXTAGLOOP 2000 44#define MAXTAGLOOP 2000
@@ -80,8 +89,8 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
80/* 89/*
81** try to convert a value to an integer, rounding according to 'mode': 90** try to convert a value to an integer, rounding according to 'mode':
82** mode == 0: accepts only integral values 91** mode == 0: accepts only integral values
83** mode < 0: takes the floor of the number 92** mode == 1: takes the floor of the number
84** mode > 0: takes the ceil of the number 93** mode == 2: takes the ceil of the number
85*/ 94*/
86static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { 95static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
87 TValue v; 96 TValue v;
@@ -91,7 +100,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
91 lua_Number f = l_floor(n); 100 lua_Number f = l_floor(n);
92 if (n != f) { /* not an integral value? */ 101 if (n != f) { /* not an integral value? */
93 if (mode == 0) return 0; /* fails if mode demands integral value */ 102 if (mode == 0) return 0; /* fails if mode demands integral value */
94 else if (mode > 0) /* needs ceil? */ 103 else if (mode > 1) /* needs ceil? */
95 f += 1; /* convert floor to ceil (remember: n != f) */ 104 f += 1; /* convert floor to ceil (remember: n != f) */
96 } 105 }
97 return lua_numbertointeger(f, p); 106 return lua_numbertointeger(f, p);
@@ -113,7 +122,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
113** try to convert a value to an integer 122** try to convert a value to an integer
114*/ 123*/
115int luaV_tointeger_ (const TValue *obj, lua_Integer *p) { 124int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
116 return tointeger_aux(obj, p, 0); 125 return tointeger_aux(obj, p, LUA_FLOORN2I);
117} 126}
118 127
119 128
@@ -135,7 +144,7 @@ int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
135static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step, 144static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
136 int *stopnow) { 145 int *stopnow) {
137 *stopnow = 0; /* usually, let loops run */ 146 *stopnow = 0; /* usually, let loops run */
138 if (!tointeger_aux(obj, p, (step < 0 ? 1 : -1))) { /* not fit in integer? */ 147 if (!tointeger_aux(obj, p, (step < 0 ? 2 : 1))) { /* not fit in integer? */
139 lua_Number n; /* try to convert to float */ 148 lua_Number n; /* try to convert to float */
140 if (!tonumber(obj, &n)) /* cannot convert to float? */ 149 if (!tonumber(obj, &n)) /* cannot convert to float? */
141 return 0; /* not a number */ 150 return 0; /* not a number */