local min_value = math.min(2, 1, 3)
-- min_value = 1
-- 表要经过 table.unpack:
-- 适合短表,不适合上百万个值
local nums = {2, 1, 3}
local max_value = math.max(table.unpack(nums))
-- max_value = 3
-- 没有 key 参数:用循环找出最长的
local words = {"one", "three", "two"}
local longest = words[1]
for _, word in ipairs(words) do
if #word > #longest then
longest = word
end
end
-- longest = three
print("min_value = " .. min_value)
print("max_value = " .. max_value)
print("longest = " .. longest)
-- 输入为空会报错,没有默认值
local ok, err = pcall(math.max,
table.unpack({}))
-- ok = false
-- err = bad argument #1 to 'math.max'
-- (value expected)
print("ok = " .. tostring(ok))
print("err = " .. err)