Module:TableTools: Difference between revisions

From Tardis Wiki, the free Doctor Who reference
741 bytes added ,  16 December 2013
use a hack to handle NaN values
(don't sort union and intersection functions)
(use a hack to handle NaN values)
Line 14: Line 14:
local floor = math.floor
local floor = math.floor
local infinity = math.huge
local infinity = math.huge
-- Define a unique value to represent NaN. This is because NaN cannot be used as a table key.
local nan = {}


--[[
--[[
Line 47: Line 50:
for _, t in ipairs(tables) do
for _, t in ipairs(tables) do
for k, v in pairs(t) do
for k, v in pairs(t) do
if type(v) == 'number' and tostring(v) == '-nan' then
v = nan -- NaN cannot be a table key, so use a proxy variable.
end
vals[v] = true
vals[v] = true
end
end
end
end
for val in pairs(vals) do
for val in pairs(vals) do
if val == nan then
-- This ensures that we output a NaN when we had one as input, although
-- they may have been generated in a completely different way.
val = 0/0
end
ret[#ret + 1] = val
ret[#ret + 1] = val
end
end
Line 71: Line 82:
for _, t in ipairs(tables) do
for _, t in ipairs(tables) do
for k, v in pairs(t) do
for k, v in pairs(t) do
if type(v) == 'number' and tostring(v) == '-nan' then
v = nan -- NaN cannot be a table key, so use a proxy variable.
end
local valCount = vals[v] or 0
local valCount = vals[v] or 0
vals[v] = valCount + 1
vals[v] = valCount + 1
Line 77: Line 91:
for val, count in pairs(vals) do
for val, count in pairs(vals) do
if count == lim then
if count == lim then
if val == nan then
-- This ensures that we output a NaN when we had one as input, although
-- they may have been generated in a completely different way.
val = 0/0
end
ret[#ret + 1] = val
ret[#ret + 1] = val
end
end
Cookies help us deliver our services. By using our services, you agree to our use of cookies.