I have a question which seems trivial.
Let's say that at the top of the Lua stack there's a number. I want to find out (in C) if this number is positive, negative, or zero.
A naive solution would be:
lua_Number num = lua_tonumber(L, -1);
if (num > 0)
print("positive")
else if (num < 0)
print("negative")
else
print("zero")
However, this may not work well in Lua 5.3 because if it's a Lua integer (lua_Integer) on the stack it may not fit in our num variable (which is lua_Number).
So how can I write my C code to work in both Lua 5.1/5.2 and Lua 5.3?
(BTW, the reason I'm interested only in the sign, not in the number itself, is because this number is the return value of a comparison function for a sort algorithm. It's the result of comparing two items.)
lua_Numberis a floating-point number, then even if it loses some precision in the conversion, it will still retain the sign. - Colonel Thirty Two