lua-users home
lua-l archive

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


> on my WinXP 32 bit box as well as an Ubuntu Karmic 32-bit box.
> This makes it pretty clear that the conversion from double to int
> is handling the overflow in an unexpected way. Knowing it will
> saturate, having -2147483649 saturate to -2147483648 is
> acceptable. However having 2147483649 saturate to the same value
> is *surprising*.

All values "saturate" to 0x80000000.


> Is this a Lua bug? Is there an easy fix? Is there a fix that
> won't hurt performance and still give an acceptable answer?

It is a Lua bug. The fix seems to be trivial and was sent to this
list today by lhf (not yet on the archives):

> Date: Wed, 17 Mar 2010 12:05:29 -0300
> From lua-bounces@bazar2.conectiva.com.br  Wed Mar 17 12: 7:12 2010
> From: Luiz Henrique de Figueiredo <lhf@tecgraf.puc-rio.br>
> To: Lua list <lua@bazar2.conectiva.com.br>
> Subject: Re: Am I misunderstanding random numbers?
> 
> > This is probably a bug in Lua, since srand should accept unsigned ints.
> 
> This seems to fix it:
> 
> static int math_randomseed (lua_State *L) {
>   srand(luaL_checknumber(L, 1));
>   return 0;
> }
> 
> The original code used luaL_checkint instead of luaL_checknumber.

-- Roberto