lua-users home
lua-l archive

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


At 07:04 PM 6/19/2002 +0200, Björn De Meyer wrote:
>However, I cannot fathom why anyone would want to use 
>floating point keys in a hash table. Integer indexes and
>string indexes make sense, but what is ` x[1.5] = "foo" ' 
>supposed to express? What languages besides Lua allow you to do that
>natively?

I concur.

Floating points model reals, and suffer from modelling artifact (round-off errors, inexact arithmetic, etc.).  

On the other hand C-language ints and strings model points in discrete spaces.  Strings can be thought of as being generalized forms of integers, at least in terms of enumeration.

It would seem that it makes sense to convert floats to ints [via a portably-defined rounding rule -- not just depending on how C happens to do it on a given platform] prior to indexing.  

Code that generates indices using floating point computation is subject to interesting surprises, for example:

[using Lua 4 on Win32]
 > k=99999
 > i=1/k
 > j=1/i
 > t={}
 > t[k]=1
 > print(t[j])
nil

Here's why:
 > print(j-k)
1.164153218269348e-010

Of course, it's easy to generate other examples.  But perhaps folding down to ints would be better.  The semantics are no more confusing, and this would allow for true linear allocation of data if the implementor desired.

--Terry