In my C++ program, I need to know if a Lua variable is an integer number or a floating-point number. The C API provides lua_isnumber() but this function does not distinguish between int/float/double.
So far I have worked around this by using modf():
if (lua_isnumber(luaCtx, -1)) // int / unsigned int / float:
{
luaVarName = lua_tostring(luaCtx, -2);
double n = static_cast<double>(lua_tonumber(luaCtx, -1));
// Figure out if int or float:
double fractPart, intPart;
fractPart = modf(n, &intPart);
if (fractPart != 0.0)
{
luaVarType = ScriptVar::TypeTag::Float;
luaVarData.asFloat = static_cast<float>(n);
}
else
{
luaVarType = ScriptVar::TypeTag::Integer;
luaVarData.asInteger = static_cast<int>(n);
}
}
Does the Lua API provide a way to infer the variable type more precisely?
lua_tounsignedorlua_checkunsigned. Otherwise you have to cast it like you have.