lua-users home
lua-l archive

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


> But the question was how do you shift floats?
I don't shift doubles directly. As I already said, I convert doubles to 
unsigned long, shift, and then convert them back to doubles:
/* x << y */
static void bitshiftleft(lua_State *L)
{
        lua_pushnumber((lua_Number) ((unsigned long) luaL_check_number
(1) << (int) luaL_check_number(2)));
}

/* x >> y */
static void bitshiftright(lua_State *L)
{
        lua_pushnumber((lua_Number) ((unsigned long) luaL_check_number
(1) >> (int) luaL_check_number(2)));
}

> And you'd have to do: floor(x)*2.f as floor(x*2.f) would include
> the fractional bit assuming you were simulating integers.
> (imagine shifting 1.5)
Yes, you're right. floor(x)*2.f is better of floor(x*2.f).

> (x *= .5f // >>1, would be quicker still)
It's the same to do x /= 2. You still have the fractional part.

The things become more complex if you need to do x << y (or x >> y).
You need to use pow() or to do a loop.

Bye
Mauro