lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi there

I think i have found a bug.

The LNUM patch changes the test below from lua_isnumber() to
lua_isinteger() in luaL_checkinteger(). However this has the
unfortunate side effect of causing Lua to claim that numbers between
-1.0 < x < 1.0 can not be integers:


> return string.format("%d", 0.999)
stdin:1: bad argument #2 to 'format' (integer expected, got number)
stack traceback:
	[C]: in function 'format'
	stdin:1: in main chunk
	[C]: ?
> return string.format("%d", 1.0001)
Printing 1 values:
1
>
> return string.format("%d", -0.999)
stdin:1: bad argument #2 to 'format' (integer expected, got number)
stack traceback:
	[C]: in function 'format'
	stdin:1: in main chunk
	[C]: ?
> return string.format("%d", -0.0001)
stdin:1: bad argument #2 to 'format' (integer expected, got number)
stack traceback:
	[C]: in function 'format'
	stdin:1: in main chunk
	[C]: ?
> return string.format("%d", -1.0001)
Printing 1 values:
-1
>


The patch below changes this behaviour back again.

Rgds /Flemming

--- lauxlib.c.orig	2010-04-21 17:34:48.000000000 +0200
+++ lauxlib.c	2010-05-11 10:59:23.000000000 +0200
@@ -188,7 +188,7 @@

 LUALIB_API lua_Integer luaL_checkinteger (lua_State *L, int narg) {
   lua_Integer d = lua_tointeger(L, narg);
-  if (d == 0 && !lua_isinteger(L, narg))  /* avoid extra test when d
is not 0 */
+  if (d == 0 && !lua_isnumber(L, narg))  /* avoid extra test when d is not 0 */
     tag_error(L, narg, -1 /*integer*/);
   return d;
 }