lua-users home
lua-l archive

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


Ah, very good. 

FYI, I did try using the l_mathop() macro, but I quickly ran into compilation issues with some math functions not appearing to have __f variants. However, I didn't test in a vanilla solution, just in my full project. I will do further tests later to see if that's a me thing. 

However, I will also update my version of lua to use 5.3's updated math_modf function. 

Thanks for the quick replies!


On Tue, Jan 7, 2014 at 9:20 AM, Roberto Ierusalimschy <roberto@inf.puc-rio.br> wrote:
> > the standard library implementation of math_modf which is used by lua
> > ends up passing a pointer to a float to the standard library modf function.
>
> If your C library supports float variants of the standard math functions,
> you should be able to use modff by changing l_mathop in luaconf.h to
>       #define l_mathop(op)    op##f

At least until some time ago, the implementation of "fmodf" in Visual
Studio was buggy. (The bug was similar to this problem in Lua; it simply
did type casts over the parameters to the original modf, casting a
pointer to double into a pointer to float.) To avoid all these problems,
Lua 5.3 implements its modf without using the modf from C:

static int math_modf (lua_State *L) {
  lua_Number n = luaL_checknumber(L, 1);
  /* integer part (rounds toward zero) */
  lua_Number ip = (n < 0) ? -l_mathop(floor)(-n) : l_mathop(floor)(n);
  lua_pushnumber(L, ip);
  /* fractionary part (test handles inf/-inf) */
  lua_pushnumber(L, (n == ip) ? 0.0 : (n - ip));
  return 2;
}


-- Roberto