Integer Domain

lua-users home
wiki

The following code calculates the actual limits for integer numbers which can be exactly represented by Lua (double precision floating point) numbers - besides, it's a nice example for the "successive approximation" technique:

--**** 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 ;-)

--AndreasRozek


Can the correctness be proven? Does it assume IEEE? --DavidManura

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:

--AndreasRozek


RecentChanges · preferences
edit · history
Last edited April 11, 2007 7:16 am GMT (diff)