lua-users home
lua-l archive

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



There may be several abnormal things regarding the normal Lua integer handling.

What I find most weird (met in making the "integer patches") is that floating point values are silently converted into integers by 'lua_tointeger()'. I would consider this a BUG, since if a C routine specifically requests an integer I think it should be provided one. Anyways, that behaviour remains in the "integer patch" so that outside behaviour remains unaltered.

Could we have this changed, in say 5.2?

Also "lua_tounsigned()" would be handy.

-asko


On Mon, 13 Nov 2006 21:52:56 -0500
 "Brian Weed" <bw@imaginengine.com> wrote:
Today, we converted a project from Lua v5.02 to v5.1.1, and we started having a problem when converting double to int. I have narrowed my problem down to code that has nothing to do with Lua. But I still wanted to get other people's opinions about it.

double d = 0xFFFFFFFF;
int n;
n = (int)d;

// n now contains 0x80000000

n = (int)(unsigned int)d;

// n now contains 0xFFFFFFFF

(this happens on both VS2005, and GCC 4.0.1 (Apple Computer, Inc. build 5363))

Since lua_number2int and lua_number2integer both basically do this: "n = (int)d;"

Wouldn't it be safer to do this: "n = (int)(unsigned int)d;" so as to preserve the values that would otherwise be truncated ?

What sort of problems would this cause? Is my fix unportable?

The initial problem we got stems from doing this in Lua:

d = 0xFFFFFFFF
Foo(d)

// Assuming Foo() is something like this...

int bind_Foo(lua_State *L)
{
Foo(lua_tointeger(L, 1)); // value passed to Foo() is now truncated to 0x80000000

   return(0);
}

Brian