math_floor

math_floor是lua库函数math.floor的具体实现,其声明为:

static int math_floor (lua_State *L);

math_floor接受一个参数,这个参数必须是number类型(整数或者浮点数)或者字符串。math_floor正确接受参数后,获取不大于参数的最大整数值;注意获取整数值不一定是lua内部的LUA_INTEGER类型,可能是LUA_NUMBER类型。


math_floor的实现:

static int math_floor (lua_State *L) {
    if (lua_isinteger(L, 1))
        lua_settop(L, 1);  /* integer is its own floor */
    else {
        lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
        pushnumint(L, d);
    }
    return 1;
}

math_floor先判断栈上的参数类型:

  • 如果是LUA_INTEGER类型,直接将参数设置到栈顶;
  • 否则,调用luaL_checknumber尝试将参数转换成一个数;转换成功就将这个数传递给floor,由floor给出不大于参数的最大整数值,并调用pushnumint将结果压栈。