示例工程 03-numbers

03-numbers/04-float-to-int-boundaries/main.lua

lua · 0.6 KB

-- @runtimes lua54
--- 演示:浮点精度边界与 tointeger 的失败。
 
local M = {}
 
--- @return table { exact = boolean, beyond = boolean, converted = integer|nil }
function M.run(log)
	log = log or print
	local exact = (2 ^ 53 + 1) == 2 ^ 53
	local beyond = math.tointeger(2 ^ 63) == nil
	local converted = math.tointeger(3.0)
	log(("2p53_plus_1_collapses=%s"):format(tostring(exact)))
	log(("2p63_tointeger_nil=%s"):format(tostring(beyond)))
	log(("converted=%s"):format(tostring(converted)))
	log(("max=%d"):format(math.maxinteger))
	return { exact = exact, beyond = beyond, converted = converted }
end
 
if not ... then
	M.run()
end
 
return M