Lua floating point errors in table index

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!

+4
source share
1 answer

If you really need to index floating point numbers coming from different sources, I suggest you convert them to fixed format strings with as many decimal places as you want to save and index the table with these strings.

You can even do this automatically by setting the appropriate __newindex __newindex for the proxy table.

+4
source

Source: https://habr.com/ru/post/1439048/


All Articles