2
votes

Lua behaves as follows :

print( 1 ) -- prints 1
print( 1.0 ) -- prints 1.0

Since Lua is using lua_Number, which is a floating point value (either float or double), for all number types (i.e both integers and reals), I guess it somehow keeps track of the actual nature of a number value, or else print( 1 ) would print 1.0 since the information that that 1 is an integer would be lost when casting 1 to lua_Number at some point in the Lua's code.

Is there a way, using the C API of Lua 5.3.2, to detect whether a lua_Number was originally an integer or a real ?

Say I want to code a native function expecting either an integer or a real. How can I know, when poping my parameters, if the given value was 1 or 1.0 ?

Thanks.

PS : I already saw this answer : Test if Lua number is integer or float but it doesn't solve my problem since checking if number == (int) number would indicate that any real like N.0 would be an integer.

1

1 Answers

2
votes

Lua 5.3 uses lua_Number (default: double) to represent floating point numbers and lua_Integer (default: long long) for integers. Previous versions only had a floating-point type. The API has the function lua_isinteger to determine if a value is represented as an integer.