Open ensisoft opened 10 months ago
Neither a bug, nor a feature: It is the expected behavior.
This is a fundamental Lua
behavior, Lua
has no concept of integer type, every integer pushed to Lua
(by the C API
) will be stored as a double-precision floating-point.
Check Number
The number type represents real (double-precision floating-point) numbers. Lua has no integer type, as it does not need it.
This is similar to Javascript
too.
@deadlocklogic in the C API level Lua has both integers (lua_Integer) as well as floats (lua_Number) and since Lua 5.3 even the Lua programmer can choose between the internal sub type representation.
A quick test As you may see:
print(math.type(123)) -- integer
print(type(math.tointeger(123))) -- number
print(type(123)) -- number
And Lua 5.3:
There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.
So nothing fundamental changed, integers are still a number
type
but it added 64 bit representation for better precision, you have to use the C API
yourself to check if a number
is an integer
or not.
Your example can be better written like this: Example
Hello all
seems like using std::variant there's some loss of precise typing.
consider
integer type is lost and the variant is stored as float.
I did a bunch of other tests and if you write an int the the variant in native code and read it in Lua you get an int.
Is this a bug or a feature?