lua-users home
lua-l archive

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


In message <4.2.1.20020619125233.01531960@205.232.45.141>, Terry Moore writes:
> At 07:04 PM 6/19/2002 +0200, Björn De Meyer wrote:
> 
> It would seem that it makes sense to convert floats to ints [via a portably-d
>>efined rounding rule -- not just depending on how C happens to do it on a giv
>>en platform] prior to indexing.  

When C converts floating point types to integers there is no "platform
dependent" behaviour.  The value of the floating point type has it's
fractional part discarded (ie truncated towards zero) and if that value,
which is not an integer, can be represented by the integer type then
that is the value of the conversion, otherwise the value cannot be
represented by the integer type and the behaviour is undefined.  C has
plenty of implementation defined and undefined behaviour without us
pretending it has more.

Your suggestion (as I understand it) would mean you could have:
x ~= y
and yet when you go:
a={}
a[x]="foobar"
print(a[y])
you would get "foobar" printed out.

I have used tables to count things or make things unique.  For
example suppose a is an array of things indexed by integers from
1 to n (though that won't matter in the code below).  We want to
count the number of unique things:

c={}
for k,v in a do
  c[v]=1
end
n=0
for k,v in c do n=n+1 end
print(n)

We make a mark in c for every different value that we see in a and then
count the number of things in c.  This counts the number of unique value
in a.  Under your suggestion different keys (in c) would use the same
table slot, so I wouldn't be able to count things this way.

Does it also worry you that one index tables by tables and functions?
Like this:
a={}
a[{}]="foo"
a[function (x) return x*x end] = "bar"

Cheers,
 drj