找回密码
 register

QQ登录

只需一步,快速开始

搜索
查看: 10|回复: 0

[每天自学] 天龙八部源端 x64:Lua4 增加 Lua5 的 math.xxx 用法

[复制链接]
  • 打卡等级:本地老炮
  • 打卡总天数:384
  • 打卡月天数:1
  • 打卡总奖励:382
  • 最近打卡:2026-01-01 00:21:32
Waylee 发表于 2025-12-31 22:20 | 显示全部楼层 |阅读模式 | Google Chrome | Windows 10

马上注册,查看网站隐藏内容!!

您需要 登录 才可以下载或查看,没有账号?register

×

天龙八部服务端是 Lua4,很多数学函数历史包袱比较重:老脚本直接用全局 sin() / cos(),而且三角函数还是角度制。但 Lua5 开始,官方数学库已经统一走 math.xxx,并且三角函数改成了弧度制

我的做法是 B 方案

  • 原来的全局函数完全不动(老脚本不炸)
  • 新增一套 math.xxx(Lua5 风格):三角函数按 弧度工作,补齐 math.pi / math.huge / math.fmod / math.modf 等常用字段
    下面只写 Lua 端怎么用

新增函数 作用
table.concat(list[, sep[, i[, j]]]) list[i..j] 拼成一个字符串,中间用 sep 连接(默认空串)。
table.insert(list, [pos,] value) 往数组表里插入元素:不写 pos 就追加到末尾,写了就插入并整体后移。
table.remove(list[, pos]) 删除并返回 pos 位置元素:不写 pos 默认删末尾,同时把后面的元素前移。
table.sort(list[, comp]) 原地排序 list[1..table.getn(list)];可选比较函数 comp(a,b)
table.pack(...) 把所有参数打包成表:t[1]=...,并写入 t.n=参数个数(支持中间有 nil)。
table.unpack(list[, i[, j]]) 展开返回 list[i..j]i 默认 1j 默认 table.getn(list)
table.getn(t) 取长度(以 Lua4 规则为准:通常依赖 t.n 或内部长度策略)。
table.setn(t, n) 设置表长度( Lua4 风格:写 t["n"]=n)。
table.foreach(t, f) 遍历 table:对每个 k,v 调用 f(k,v);若返回非 nil 就立刻停止并返回该值。
table.foreachi(t, f) 遍历数组部分:按 1..getn(t) 调用 f(i, t[i]);若返回非 nil 就立刻停止并返回该值。

0) 重点:角度 vs 弧度(别踩坑)

  • 老脚本:sin(90) —— 角度制(保持不变)
  • 新写法:math.sin(math.pi/2) —— 弧度制(Lua5) (lua.org)

如果你手里是角度(比如 90 度),Lua5 风格写法就是:

print(math.sin(math.rad(90)))   -- 1

1) math.pi

作用:圆周率 π

print(math.pi)   -- 3.1415926535...

2) math.huge

作用:一个非常大的数(HUGE_VAL),基本当作 “∞” 用 (lua.org)

print(math.huge)

3) 三角函数(Lua5 风格:弧度)

math.sin(x) / math.cos(x) / math.tan(x)

print(math.sin(math.pi/2))     -- 1
print(math.cos(0))             -- 1
print(math.tan(math.pi/4))     -- 1

math.asin(x) / math.acos(x) / math.atan(x) / math.atan2(y, x)

返回值也是 弧度

print(math.asin(1))            -- 1.570796...
print(math.atan2(1, 1))        -- 0.785398...

4) 角度/弧度转换

math.rad(deg):角度 → 弧度

math.deg(rad):弧度 → 角度

(Lua5 常用)(lua.org)

print(math.rad(180))           -- 3.141592...
print(math.deg(math.pi))       -- 180

5) 取余

math.fmod(x, y)(Lua5 常用)(lua.org)

math.mod(x, y)(和fmod同一个逻辑,方便兼容老写法)

print(math.fmod(10, 3))        -- 1
print(math.mod(10, 3))         -- 1

6) math.modf(x)

作用:把数字拆成 整数部分小数部分(两个返回值)

local ip, fp = math.modf(12.34)
print(ip, fp)                  -- 12    0.34(左右,浮点会有误差)

7) math.pow(x, y)

作用:乘方(Lua5 风格表函数)

print(math.pow(2, 10))         -- 1024

8) 常用基础函数(基本都跟 Lua5 一致)

print(math.abs(-3.5))          -- 3.5
print(math.ceil(3.1))          -- 4
print(math.floor(3.9))         -- 3
print(math.sqrt(9))            -- 3
print(math.min(3,9,2,7))       -- 2
print(math.max(3,9,2,7))       -- 9
print(math.log(10))            -- ln(10)
print(math.log10(1000))        -- 3
print(math.exp(1))             -- e

9) 随机数

math.random() / math.random(u) / math.random(l, u)

math.randomseed(12345)

print(math.random())           -- 0~1
print(math.random(10))         -- 1~10
print(math.random(5, 8))       -- 5~8

综合测试脚本(验证 math 是否生效)

print("=== math basic ===")
print(math.abs(-3.5))
print(math.ceil(3.1))
print(math.floor(3.9))
print(math.sqrt(9))
print(math.min(3,9,2,7))
print(math.max(3,9,2,7))
print(math.pow(2,10))

print("\n=== math pi/huge ===")
print(math.pi)
print(math.huge)

print("\n=== trig (radians) ===")
print(math.sin(math.pi/2))
print(math.cos(0))
print(math.tan(math.pi/4))
print(math.deg(math.pi))
print(math.rad(180))

print("\n=== fmod/mod ===")
print(math.mod(10,3))
print(math.fmod(10,3))

print("\n=== modf ===")
local ip, fp = math.modf(12.34)
print(ip, fp)

print("\n=== random ===")
math.randomseed(12345)
print(math.random())
print(math.random(10))
print(math.random(5,8))

输出结果:

=== math basic ===
3.5
4
3
3
2
9
1024

=== math pi/huge ===
3.141592653589793
inf

=== trig (radians) ===
1
1
0.9999999999999999
180
3.141592653589793

=== fmod/mod ===
1
1

=== modf ===
12      0.3399999999999999

=== random ===
0.1783953044462927
4
5

老脚本照跑,新脚本按 Lua5 写也舒服。(lua.org)

您需要登录后才可以回帖 登录 | register

本版积分规则

雪舞知识库 ( 浙ICP备15015590号-1 | 萌ICP备20232229号|浙公网安备33048102000118号 ) |网站地图|天天打卡

GMT+8, 2026-1-1 03:49 , Processed in 0.086964 second(s), 6 queries , Redis On.

Powered by Discuz! X3.5 Licensed

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表