lua-users home
lua-l archive

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


2017-06-24 6:17 GMT+02:00 Martin <eden_martin_fuhrspam@gmx.de>:
> On 06/23/2017 10:04 AM, John Hind wrote:
>> [...] What about positive and
>> negative zero? Would these be distinguished table keys?
>
> With positive and negative float zeros is funny thing: you can't (I
> don't know how) distinguish negative float zero (-0.0) from positive
> float zero (0.0). Usually we detect negative number by comparing it with
> 0, which is not applicable here.
>
> But in tables both three zeros as key maps to integer zero:
>
>   Lua 5.3.3  Copyright (C) 1994-2016 Lua.org, PUC-Rio
>   > t = {[0] = '0', [0.0] = '0.0', [-0.0] = '-0.0'}
>   > for k, v in pairs(t) do print(math.type(k), k, v) end
>   integer       0       -0.0

"The expressions a[i] and a[j] denote the same table element if and
only if i and j are raw equal (that is, equal without metamethods).
In particular, floats with integral values are equal to their respective i
ntegers (e.g., 1.0 == 1). To avoid ambiguities, any float with integral
value used as a key is converted to its respective integer."
--- Lua 5.3.4 Reference Manual §2.1