lua-users home
lua-l archive

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


On Mon, Jun 1, 2015 at 7:24 AM, Roberto Ierusalimschy
<roberto@inf.puc-rio.br> wrote:
>> On Mon, Jun 1, 2015 at 6:26 AM, Coda Highland <chighland@gmail.com> wrote:
>> > On Mon, Jun 1, 2015 at 3:06 AM, Tim Hill <drtimhill@gmail.com> wrote:
>> >> This also takes care
>> >> of the issue of FP errors, since all the math is now done on integers.
>>
>> Also, it doesn't resolve FP errors in Lua 5.1/5.2, because numbers are
>> still double-precision floats and not true integers. The act of adding
>> 1 + 1<<53 is what causes the problem, not dealing with fractional
>> values.
>
> I use the following code (or rather I would use it if ever I had to
> check whether a table is a sequence :-):
>
> function issequence (t)
>   local n = #t
>   local c = 0   -- count keys in [1..n]
>   for k in pairs(t) do
>     if type(k) == 'number' and k > 0 then
>       if k ~= math.floor(k) or k > n then return false end
>       c = c + 1
>     end
>   end
>   return c == n
> end
>

That's an interesting solution. Provides an early abort that mine
doesn't, trades 2n additions for a O(log n) #t, and has absolutely no
issues with overflow.

I like it. :D

/s/ Adam