Integer Domain |
|
--**** should be compatible with 5.xx function intlimit() local floor = math.floor -- get highest power of 2 which Lua can still handle as integer local step = 2 while true do local nextstep = step*2 if (floor(nextstep) == nextstep) and (nextstep-1 ~= nextstep) then step = nextstep else break end end -- now get the highest number which Lua can still handle as integer local limit,step = step,step/2 while step > 0 do local nextlimit = limit+step if (floor(nextlimit) == nextlimit) and (nextlimit-1 ~= nextlimit) then limit = nextlimit end step = floor(step/2) end return limit end
Example:
local limit = intlimit() print() print("IntegerDomain - what is the largest supported integer number?") print() --**** do not rely on Lua to print "limit" properly by itself! --local printablelimit = string.format("%d", limit) -- fails under Lua! local printablelimit = string.format("%.16e", limit) print("supported integer range is: -" .. printablelimit .. "...+" .. printablelimit)
As you can see, Lua has no problems to process large integer numbers - as long as you don't try to convert them into strings ;-)
Hmmm, let me think about the requirements:
Additionally, there are two other assumptions I made
Both assumptions may easily be checked explicitly (don't forget to replace "floor" by "ceil" when testing negative numbers!).
If the requirements apply, it is possible to use "successive approximation" in order to get the largest presentable integer.
As I do not mention any concrete results, I do not really assume IEEE apart from my assumptions shown above (sign-magnitude coding guarantees the symmetry) and the test I've chosen:
floor(x) == x
should already be sufficient in order to test if a number is integral
x-1 ~= x
has just been added in order to exclude NaN and Infinity