I usually compare two floats, seeing if they are in the range of a very small number. It does not work for table keys, because you do not know if they exist without knowing their key (if that makes sense).
Example with lua 5.1 on a 64-bit computer:
a, b, c = 1.7, -0.8, -0.4 d = a + b + c -- prevents constant folding print(string.format('%1.20f', d)) -- 0.49999999999999989000 (around 0.5) assert(d ~= 0.5) t = {[0] = 'foo', [0.5] = 'bar'} print(t[d]) -- nil (I want it to print 'bar')
Basically, how can I find a key in a table when accounting for floating point errors? I would like it to work without comparison with every key in the table, so it can be fast with large tables. There is probably a built-in function for this, but I could not find it.
Any help would be appreciated!
source share