Re: Issue with numerical keys with associative array.
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Issue with numerical keys with associative array.
- From: Ross Berteig <ross@...>
- Date: 2016年10月19日 13:36:08 -0700
On 10/19/2016 12:44 PM, Laurent FAILLIE wrote:
[
What you are trying to do is exactly the same as comparing floating
point values for equality. It will sometimes work. But rounding and
precision loss will always be a problem.
The simplest approach is to use a string to index the table. That is
cheap and fast, and highly optimized in the Lua core (that is how
global variables are stored, as string keyed values in a table). You
would need to use a cannonical format for those strings, such as
string.format("%.2f", x) as Luiz suggests.
You could also round to two places, and use the result as the key.
One simple way to round that avoids numerous subtle edge cases and
other pitfalls is simply tonumber(string.format("%.2f",x)). This
isn't necessarily the fastest way, but it is robust.
In the long run, you are likely to want a better representation of
time. Mis-using the decimal fraction to represent minutes will work
on paper, but is going to cause trouble in the long run.
--
Ross Berteig Ross@CheshireEng.com
Cheshire Engineering Corp. http://www.CheshireEng.com/
+1 626 303 1602