lua-users home
lua-l archive

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


> > 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