aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lauxlib.c6
-rw-r--r--ldebug.c12
-rw-r--r--lmathlib.c41
-rw-r--r--lvm.c22
4 files changed, 46 insertions, 35 deletions
diff --git a/lauxlib.c b/lauxlib.c
index 563362e5..263d9f80 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lauxlib.c,v 1.264 2014/06/26 17:25:11 roberto Exp roberto $ 2** $Id: lauxlib.c,v 1.265 2014/07/16 14:51:36 roberto Exp roberto $
3** Auxiliary functions for building Lua libraries 3** Auxiliary functions for building Lua libraries
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -396,8 +396,8 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) {
396 396
397 397
398static void interror (lua_State *L, int arg) { 398static void interror (lua_State *L, int arg) {
399 if (lua_type(L, arg) == LUA_TNUMBER) 399 if (lua_isnumber(L, arg))
400 luaL_argerror(L, arg, "float value out of integer range"); 400 luaL_argerror(L, arg, "number has no integer representation");
401 else 401 else
402 tag_error(L, arg, LUA_TNUMBER); 402 tag_error(L, arg, LUA_TNUMBER);
403} 403}
diff --git a/ldebug.c b/ldebug.c
index 2c3c70f3..f7b2866e 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 2.97 2013/12/09 14:21:10 roberto Exp roberto $ 2** $Id: ldebug.c,v 2.98 2014/07/15 21:26:50 roberto Exp roberto $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -534,18 +534,20 @@ l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
534 534
535l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) { 535l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
536 lua_Number temp; 536 lua_Number temp;
537 if (!tonumber(p1, &temp)) 537 if (!tonumber(p1, &temp)) /* first operand is wrong? */
538 p2 = p1; /* first operand is wrong */ 538 p2 = p1; /* now second is wrong */
539 luaG_typeerror(L, p2, "perform arithmetic on"); 539 luaG_typeerror(L, p2, "perform arithmetic on");
540} 540}
541 541
542 542
543/*
544** Error when both values are convertible to numbers, but not to integers
545*/
543l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) { 546l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
544 lua_Integer temp; 547 lua_Integer temp;
545 if (!tointeger(p1, &temp)) 548 if (!tointeger(p1, &temp))
546 p2 = p1; 549 p2 = p1;
547 luaG_runerror(L, "attempt to convert an out of range float%s to an integer", 550 luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
548 varinfo(L, p2));
549} 551}
550 552
551 553
diff --git a/lmathlib.c b/lmathlib.c
index 61800799..79232e4d 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lmathlib.c,v 1.105 2014/06/30 19:48:08 roberto Exp roberto $ 2** $Id: lmathlib.c,v 1.106 2014/07/16 13:47:13 roberto Exp roberto $
3** Standard mathematical library 3** Standard mathematical library
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -76,39 +76,39 @@ static int math_atan (lua_State *L) {
76} 76}
77 77
78 78
79static int math_ifloor (lua_State *L) { 79static int math_toint (lua_State *L) {
80 int valid; 80 int valid;
81 lua_Integer n = lua_tointegerx(L, 1, &valid); 81 lua_Integer n = lua_tointegerx(L, 1, &valid);
82 if (valid) 82 if (valid)
83 lua_pushinteger(L, n); /* floor computed by Lua */ 83 lua_pushinteger(L, n);
84 else { 84 else {
85 luaL_checktype(L, 1, LUA_TNUMBER); /* argument must be a number */ 85 luaL_checkany(L, 1);
86 lua_pushnil(L); /* number is not convertible to integer */ 86 lua_pushnil(L); /* value is not convertible to integer */
87 } 87 }
88 return 1; 88 return 1;
89} 89}
90 90
91 91
92static int math_floor (lua_State *L) {
93 int valid;
94 lua_Integer n = lua_tointegerx(L, 1, &valid);
95 if (valid)
96 lua_pushinteger(L, n); /* floor computed by Lua */
97 else
98 lua_pushnumber(L, l_mathop(floor)(luaL_checknumber(L, 1)));
99 return 1;
100}
101
102
103static void pushnumint (lua_State *L, lua_Number d) { 92static void pushnumint (lua_State *L, lua_Number d) {
104 lua_Integer n; 93 lua_Integer n;
105 if (lua_numtointeger(d, &n)) /* fits in an integer? */ 94 if (lua_numtointeger(d, &n)) /* does 'd' fit in an integer? */
106 lua_pushinteger(L, n); /* result is integer */ 95 lua_pushinteger(L, n); /* result is integer */
107 else 96 else
108 lua_pushnumber(L, d); /* result is float */ 97 lua_pushnumber(L, d); /* result is float */
109} 98}
110 99
111 100
101static int math_floor (lua_State *L) {
102 if (lua_isinteger(L, 1))
103 lua_settop(L, 1); /* integer is its own floor */
104 else {
105 lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
106 pushnumint(L, d);
107 }
108 return 1;
109}
110
111
112static int math_ceil (lua_State *L) { 112static int math_ceil (lua_State *L) {
113 if (lua_isinteger(L, 1)) 113 if (lua_isinteger(L, 1))
114 lua_settop(L, 1); /* integer is its own ceil */ 114 lua_settop(L, 1); /* integer is its own ceil */
@@ -264,15 +264,16 @@ static int math_randomseed (lua_State *L) {
264 264
265 265
266static int math_type (lua_State *L) { 266static int math_type (lua_State *L) {
267 luaL_checkany(L, 1);
268 if (lua_type(L, 1) == LUA_TNUMBER) { 267 if (lua_type(L, 1) == LUA_TNUMBER) {
269 if (lua_isinteger(L, 1)) 268 if (lua_isinteger(L, 1))
270 lua_pushliteral(L, "integer"); 269 lua_pushliteral(L, "integer");
271 else 270 else
272 lua_pushliteral(L, "float"); 271 lua_pushliteral(L, "float");
273 } 272 }
274 else 273 else {
274 luaL_checkany(L, 1);
275 lua_pushnil(L); 275 lua_pushnil(L);
276 }
276 return 1; 277 return 1;
277} 278}
278 279
@@ -339,7 +340,7 @@ static const luaL_Reg mathlib[] = {
339 {"cos", math_cos}, 340 {"cos", math_cos},
340 {"deg", math_deg}, 341 {"deg", math_deg},
341 {"exp", math_exp}, 342 {"exp", math_exp},
342 {"ifloor", math_ifloor}, 343 {"tointeger", math_toint},
343 {"floor", math_floor}, 344 {"floor", math_floor},
344 {"fmod", math_fmod}, 345 {"fmod", math_fmod},
345 {"log", math_log}, 346 {"log", math_log},
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 */