static int math_ceil (lua_State *L) { lua_Number v; float x, y, z, w; switch (lua_type(L,1)) { case LUA_TNUMBER: if (lua_isinteger(L, 1)) { lua_settop(L, 1); /* integer is its own ceil */ } else { v = lua_tonumber(L,1); pushnumint(L,ceil(v)); } return 1; case LUA_TVECTOR2: lua_checkvector2(L,1,&x,&y); lua_pushvector2(L,ceilf(x),ceilf(y)); return 1; case LUA_TVECTOR3: lua_checkvector3(L,1,&x,&y,&z); lua_pushvector3(L,ceilf(x),ceilf(y),ceilf(z)); return 1; case LUA_TVECTOR4: lua_checkvector4(L,1,&x,&y,&z,&w); lua_pushvector4(L,ceilf(x),ceilf(y),ceilf(z),ceilf(w)); return 1; } luaL_error(L, "ceil takes a number, integer, vector2, vector3, or vector4."); return 1; }
static int math_ceil(lua_State *L) { if (lua_isinteger(L, 1)) lua_settop(L, 1); /* integer is its own ceil */ else { lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1)); pushnumint(L, d); } return 1; }
/* ** next function does not use 'modf', avoiding problems with 'double*' ** (which is not compatible with 'float*') when lua_Number is not ** 'double'. */ static int math_modf(lua_State *L) { if (lua_isinteger(L, 1)) { lua_settop(L, 1); /* number is its own integer part */ lua_pushnumber(L, 0); /* no fractional part */ } else { lua_Number n = luaL_checknumber(L, 1); /* integer part (rounds toward zero) */ lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n); pushnumint(L, ip); /* fractional part (test needed for inf/-inf) */ lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip)); } return 2; }