Skip to content

Commit 8bb272a

Browse files
committed
new conversion float->integer: conversion is valid only when
float has an exact representation as an integer
1 parent c229ed5 commit 8bb272a

4 files changed

Lines changed: 46 additions & 35 deletions

File tree

lauxlib.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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 $
33
** Auxiliary functions for building Lua libraries
44
** See Copyright Notice in lua.h
55
*/
@@ -396,8 +396,8 @@ LUALIB_API lua_Number luaL_optnumber (lua_State *L, int arg, lua_Number def) {
396396

397397

398398
static void interror (lua_State *L, int arg) {
399-
if (lua_type(L, arg) == LUA_TNUMBER)
400-
luaL_argerror(L, arg, "float value out of integer range");
399+
if (lua_isnumber(L, arg))
400+
luaL_argerror(L, arg, "number has no integer representation");
401401
else
402402
tag_error(L, arg, LUA_TNUMBER);
403403
}

ldebug.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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 $
33
** Debug Interface
44
** See Copyright Notice in lua.h
55
*/
@@ -534,18 +534,20 @@ l_noret luaG_concaterror (lua_State *L, const TValue *p1, const TValue *p2) {
534534

535535
l_noret luaG_aritherror (lua_State *L, const TValue *p1, const TValue *p2) {
536536
lua_Number temp;
537-
if (!tonumber(p1, &temp))
538-
p2 = p1; /* first operand is wrong */
537+
if (!tonumber(p1, &temp)) /* first operand is wrong? */
538+
p2 = p1; /* now second is wrong */
539539
luaG_typeerror(L, p2, "perform arithmetic on");
540540
}
541541

542542

543+
/*
544+
** Error when both values are convertible to numbers, but not to integers
545+
*/
543546
l_noret luaG_tointerror (lua_State *L, const TValue *p1, const TValue *p2) {
544547
lua_Integer temp;
545548
if (!tointeger(p1, &temp))
546549
p2 = p1;
547-
luaG_runerror(L, "attempt to convert an out of range float%s to an integer",
548-
varinfo(L, p2));
550+
luaG_runerror(L, "number%s has no integer representation", varinfo(L, p2));
549551
}
550552

551553

lmathlib.c

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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 $
33
** Standard mathematical library
44
** See Copyright Notice in lua.h
55
*/
@@ -76,39 +76,39 @@ static int math_atan (lua_State *L) {
7676
}
7777

7878

79-
static int math_ifloor (lua_State *L) {
79+
static int math_toint (lua_State *L) {
8080
int valid;
8181
lua_Integer n = lua_tointegerx(L, 1, &valid);
8282
if (valid)
83-
lua_pushinteger(L, n); /* floor computed by Lua */
83+
lua_pushinteger(L, n);
8484
else {
85-
luaL_checktype(L, 1, LUA_TNUMBER); /* argument must be a number */
86-
lua_pushnil(L); /* number is not convertible to integer */
85+
luaL_checkany(L, 1);
86+
lua_pushnil(L); /* value is not convertible to integer */
8787
}
8888
return 1;
8989
}
9090

9191

92-
static 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-
10392
static void pushnumint (lua_State *L, lua_Number d) {
10493
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? */
10695
lua_pushinteger(L, n); /* result is integer */
10796
else
10897
lua_pushnumber(L, d); /* result is float */
10998
}
11099

111100

101+
static 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+
112112
static int math_ceil (lua_State *L) {
113113
if (lua_isinteger(L, 1))
114114
lua_settop(L, 1); /* integer is its own ceil */
@@ -264,15 +264,16 @@ static int math_randomseed (lua_State *L) {
264264

265265

266266
static int math_type (lua_State *L) {
267-
luaL_checkany(L, 1);
268267
if (lua_type(L, 1) == LUA_TNUMBER) {
269268
if (lua_isinteger(L, 1))
270269
lua_pushliteral(L, "integer");
271270
else
272271
lua_pushliteral(L, "float");
273272
}
274-
else
273+
else {
274+
luaL_checkany(L, 1);
275275
lua_pushnil(L);
276+
}
276277
return 1;
277278
}
278279

@@ -339,7 +340,7 @@ static const luaL_Reg mathlib[] = {
339340
{"cos", math_cos},
340341
{"deg", math_deg},
341342
{"exp", math_exp},
342-
{"ifloor", math_ifloor},
343+
{"tointeger", math_toint},
343344
{"floor", math_floor},
344345
{"fmod", math_fmod},
345346
{"log", math_log},

lvm.c

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
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 $
33
** Lua virtual machine
44
** See Copyright Notice in lua.h
55
*/
@@ -80,15 +80,23 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) {
8080

8181

8282
/*
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
8487
*/
85-
static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
88+
static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) {
8689
TValue v;
8790
again:
8891
if (ttisfloat(obj)) {
8992
lua_Number n = fltvalue(obj);
90-
n = (up ? -l_floor(-n) : l_floor(n));
91-
return lua_numtointeger(n, p);
93+
lua_Number f = l_floor(n);
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);
92100
}
93101
else if (ttisinteger(obj)) {
94102
*p = ivalue(obj);
@@ -104,7 +112,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int up) {
104112

105113

106114
/*
107-
** try to convert a non-integer value to an integer, rounding down
115+
** try to convert a value to an integer
108116
*/
109117
int luaV_tointeger_ (const TValue *obj, lua_Integer *p) {
110118
return tointeger_aux(obj, p, 0);
@@ -155,7 +163,7 @@ int luaV_tostring (lua_State *L, StkId obj) {
155163
static int forlimit (const TValue *obj, lua_Integer *p, lua_Integer step,
156164
int *stopnow) {
157165
*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? */
159167
lua_Number n; /* try to convert to float */
160168
if (!tonumber(obj, &n)) /* cannot convert to float? */
161169
return 0; /* not a number */

0 commit comments

Comments
 (0)