lua-users home
lua-l archive

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


On Sun, Jul 24, 2011 at 04:25:29AM +0200, David Manura wrote:
> There are a number of occasions (e.g. rotation transformations) where
> both the sine and cosine of a number need to be computed together, and
> it can be more efficient to do this as a single operation [1,2].  
... [LuaJIT implementation demonstrated]
> This all doesn't seem to make a whole lot of difference though.
> ___libm_sse2_sincos is actually a little slower than the fsincos here
> and the speedup is only maybe 30% than with the separate fsin/fcos
> instructions, but it depends on your library implementation and its
> accuracy level.  It may make a bit more difference in standard Lua,
> and the lqd binding has one [3].  Even Lua has the somewhat related
> math.atan2, though not for the same reasons.  Here's an example of it
> added to lmathlib.c:
> 
>   static int math_sincos (lua_State *L) {
>     lua_Number x = luaL_checknumber(L, 1);
>     lua_pushnumber(L, l_tg(sin)(x));
>     lua_pushnumber(L, l_tg(cos)(x));
>     return 2;
>   }

The speedup is never going to be better than 50%, so 30% is not at all
bad.  

I'd prefer calling the function math.tan2, then we have it for the
same reasons as math.atan2.  

math.tan2(math(atan2(x,y))) would equal (x,y)/math.sqrt(x^2+y^2), and 
(math.atan2(math(tan2(theta)))-theta)%(2*math.pi) would equal 0, up to 
numerical error.  With the obvious exceptions when (x,y)==(0,0).

Dirk