示例工程 03-numbers

03-numbers/02-floor-division-negatives/main.lua

lua · 0.5 KB

-- @runtimes lua54
--- 演示:// 向下取整、% 与 // 配套、math.fmod 与 % 的区别。
 
local M = {}
 
--- @return table { div = integer, mod = integer, fmod = number }
function M.run(log)
	log = log or print
	local div = -7 // 2
	local mod = -7 % 2
	local fmod = math.fmod(-7, 2)
	log(("div=%d"):format(div))
	log(("mod=%d"):format(mod))
	log(("fmod=%s"):format(tostring(fmod)))
	log(("identity=%s"):format(tostring(div * 2 + mod == -7)))
	return { div = div, mod = mod, fmod = fmod }
end
 
if not ... then
	M.run()
end
 
return M