math 库

math.pi                    -- 3.14159...
math.huge                  -- inf
math.mininteger / maxinteger    -- 5.3+: 整数范围

math.abs(-5)               -- 5
math.ceil(3.2)             -- 4
math.floor(3.8)            -- 3
math.fmod(10, 3)           -- 1(取模,浮点版)
math.sqrt(16)              -- 4
math.pow(2, 10)            -- 1024(5.3 后用 ^ 替代)
math.exp(1)                -- e
math.log(math.exp(1))      -- 1
math.log(100, 10)          -- 2(5.2+:以 10 为底)

math.sin / cos / tan / asin / acos / atan   -- 弧度
math.deg(math.pi)          -- 180
math.rad(180)              -- π

math.max(1, 5, 3)          -- 5
math.min(1, 5, 3)          -- 1
math.modf(3.75)            -- 3  0.75

math.type(1)               -- "integer"  (5.3+)
math.type(1.0)             -- "float"
math.tointeger(3.0)        -- 3
math.tointeger(3.5)        -- nil

随机数

math.randomseed(os.time())          -- 5.4 起可省略;自动播种
math.random()                       -- [0, 1) 浮点
math.random(10)                     -- 1..10 整数
math.random(5, 10)                  -- 5..10 整数

Lua 5.4 起 math.random() 默认使用 xoshiro256** 算法 + 自动好种子——不调 randomseed 也够用。5.3 及更早需手动播种。

io 库

标准流

io.write("hello ", "world\n")    -- 不加换行;print 自动加
io.read()                         -- 读一行 stdin(默认)
io.read("*n")                     -- 读一个数字
io.read("*a")                     -- 读到 EOF

注意:* 在 5.3 后可省,写 "a" "n" "l" "L"

读文件

-- 方式 1:一次读完
local f = io.open("data.txt", "r")
if not f then error("打不开") end
local content = f:read("*a")
f:close()

-- 方式 2:逐行
for line in io.lines("data.txt") do
    print(line)
end

-- 方式 3:5.4 to-be-closed
do
    local f <close> = assert(io.open("data.txt", "r"))
    for line in f:lines() do print(line) end
    -- 离开 do-end 自动关闭
end

写文件

local f = assert(io.open("out.txt", "w"))   -- "a" 追加 / "w" 覆盖 / "wb" 二进制
f:write("line 1\n")
f:write(string.format("count: %d\n", 42))
f:close()

read 模式

含义
"l" / "*l" 一行(不含换行符),EOF 返 nil
"L" / "*L" 一行(换行符)
"n" / "*n" 一个数字
"a" / "*a" 读到 EOF
数字 读 n 个字节
local line, num = f:read("l", "n")    -- 一次读多个

os 库

时间

os.time()                  -- Unix 时间戳(整数)
os.time({year=2026, month=5, day=11})  -- 转时间戳
os.date()                  -- "Mon May 11 12:34:56 2026"
os.date("%Y-%m-%d")        -- "2026-05-11"
os.date("!%Y-%m-%dT%H:%M:%SZ")    -- UTC(前缀 !)

os.date("*t")              -- 返回表
-- { year=2026, month=5, day=11, hour=12, min=34, sec=56, ... }

os.clock()                 -- CPU 秒(浮点)
os.difftime(t2, t1)        -- 秒差

环境

os.getenv("HOME")          -- "/home/user" 或 nil
os.setenv 没有              -- Lua 不支持改环境变量

文件系统(极弱)

os.remove("path")          -- 删文件
os.rename("a", "b")        -- 重命名
os.tmpname()               -- 临时文件名(注意安全:不会真创建)

Lua 标准库没目录创建、没 stat、没递归遍历——这些功能必须装 LuaFileSystem (lfs):

luarocks install luafilesystem
local lfs = require("lfs")
lfs.mkdir("newdir")
for f in lfs.dir(".") do print(f) end
local attr = lfs.attributes("file.txt")
print(attr.size, attr.modification)

进程

os.execute("ls -l")        -- 返回 true/退出码(实现相关)
os.exit(0)                 -- 退出

os.execute 返回值5.1 vs 5.2+ 行为不同——5.2+ 返回 true/false, exit_kind, code

io.popen 抓输出(5.1+,但需 popen 支持):

local p = io.popen("ls -l")
local output = p:read("*a")
p:close()

→ 下一篇 整数与位运算