Lua stores numbers internally as either integers or floats. When doing a comparison operation (e.g. less-than) between numbers of the two different types, it needs to convert from one type to the other to make a comparison.
The integer and float types vary depending on configuration / platform, but certain assumptions are made. (I'm only really interested in "normal" 32 or 64 bit two's complement ints and 32 or 64 bit IEEE754 floats).
When the integer type may not have an exact representation in the floating point format, Lua tries to convert the floating point value to an integer.
This is done in a macro lua_numbertointeger:
/*
@@ lua_numbertointeger converts a float number with an integral value
** to an integer, or returns 0 if float is not within the range of
** a lua_Integer. (The range comparisons are tricky because of
** rounding. The tests here assume a two-complement representation,
** where MININTEGER always has an exact representation as a float;
** MAXINTEGER may not have one, and therefore its conversion to float
** may have an ill-defined value.)
*/
#define lua_numbertointeger(n,p) \
((n) >= (LUA_NUMBER)(LUA_MININTEGER) && \
(n) < -(LUA_NUMBER)(LUA_MININTEGER) && \
(*(p) = (LUA_INTEGER)(n), 1))
My question is thus:
Assuming that LUA_NUMBER is a 32 bit float, and LUA_INTEGER is a 32 bit int why can we assume that LUA_MIN_INTEGER (i.e. INT_MIN) has an exact representation in the floating point type.
(And the same for a 64 bit int and 64 bit floating point... and would it work with 64 bit int and 32 bit float?)