lua-users home
lua-l archive

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


Mauro Vezzosi wrote:
> > ... you may use ldexp, which directly adds its second integer
> > argument to the IEEE FP exponent of the first double argument
> > and returns the result.
> > static void bitshiftleft(lua_State *L)
> > {
> >         lua_pushnumber((lua_Number)
> > (ldexp(luaL_check_number(1),(int)luaL_check_number(2))));
> > }
> ldexp() don't adds its second argument but sets it as the exponent, so
> you must add it manually to the exponent of the first argument.

Sure it does, at least under Linux-Intel and SGI.

> And ldexp() doesn't resolve the problem of the fractional part and the
> overflow of a real integer type (I mean that you should check if the
> result is bigger than 2^16 or 2^32).

Sure it does, at least under Linux-Intel and SGI;)
And the overflow is easily checked with 

f, e = frexp(d); if e>16 then print "short integer overflow" end;

I assume that, depending on the application, one might ignore the
fractional part when doing "bit shifts" with doubles. If not, there is
still modf to get rid of the fractional binary digits before using the
integral part.

> Just a curiosity: why ldexp() and frexp() are in the Lua math library ?
> Are they so much used ? Isn't it more useful modf() which breaks down a
> double value into fractional and integer parts ?

All three (should!) directly access the IEEE FP format, so they can be
assumed to be really fast. And then, there are surely scenarios where
each of them might make sense, like the emulation of integer bit
arithmetics with double values without casting back and forth (which is
much slower).

Dolfi

-- 
Adolf Mathias              EMail: dolfi at zkm dot de  Web: www.zkm.de
|||| / |< ||| ZKM Institute for Visual Media - Institut für Bildmedien
P.B. 6919 D-76049 Karlsruhe, Germany; fon +49 721-8100-1544, fax -1509