lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Quoting from Programming in lua:
We can write constants larger than 2^63 -1 directly, despite appearances:
  > x = 13835058055282163712 -- 3 << 62
 > x                        --> -4611686018427387904
When I enter this example into an interpreter (you can try in the lua demo) the number is cast to a float, is that supposed to happen?

I wrote the following helper function(s) to "tonumber" strings which may be UInts, but I was wondering I could do it by a more direct method as discussed in the manual:
local function by10(n, i) i = i or 1
    for _ = 1, i do n = (n << 4) - (n << 2) - (n << 1) end
    return n
end

function integer(s) s = s:reverse()
    local n = 0
    for i = 0, #s -1 do
        n = n + by10(s:sub(i+1,i+1)|0, i)
    end
    return n
end