Mass Battle Frameworkv1.20.0

浮点确定性与定点确定性 Float Determinism and Fixed-Point Determinism

NETWORK & REPLICATION

为什么先做浮点,定点留到什么时候 · 2026-11-21Why float comes first, and when fixed-point arrives · 2026-11-21

系列这些文章写给人和他的 agent 一起看。这套文档怎么读 →SeriesThese posts are for you and your agent. How to read these docs →

"确定性"其实有两种 There Are Two Kinds of Determinism

说“联机靠确定性锁步”的时候,有一件事最容易被含糊过去:确定给谁看?同一台机器跑两遍、同一份二进制跑两遍,要一致很容易;让 Windows 和 Android 跑出逐位相同的结果,是另一个问题。浮点确定性和定点确定性的分界就在这里。

"Networking runs on deterministic lockstep" glosses over one question: deterministic for whom? Two runs on one machine, or one binary run twice, agree without much trouble; Windows and Android producing bit-identical results is a separate problem. That is where float determinism and fixed-point determinism part ways.

浮点确定性 Float Determinism

浮点的好处是快,而且跟引擎天然一致。数学库、物理、变换、动画全是浮点,你写下的运算已经是原生那条指令。

What you get is speed, and agreement with the engine built in. The math library, physics, transforms and animation are all float; a line of your arithmetic is already the native instruction.

边界同样清楚:跨平台不成立。同一段浮点代码,换 CPU、换编译器就会有差别:指令集对乘加的处理不同(乘加被收缩成一条指令)、中间结果保留扩展精度、数学库的最后几位不同、编译优化会重排。每一处单看都“应该一样”,合起来足够让两端在第几百帧分叉。所以浮点确定性保证的是同一个平台、同一份构建上的逐位一致。

The limit is just as plain: it does not survive a platform change. The same arithmetic differs across CPUs and compilers: one instruction set fuses a multiply and add, another does not; intermediates keep extended precision; math libraries disagree in the last bits; the optimiser reorders. Every step "should" match, and together they are enough to fork two peers a few hundred frames in. Float determinism guarantees bit-identical results on one platform, with one build.

“同平台、同构建”只是必要条件。同一份二进制要跑出逐位一致,还得同时守住三件事:没有未播种的随机(哪怕一次 FMath::Rand)、不依赖并行归约的顺序(求和、求最小值这类,线程调度换了结果就换)、不依赖容器的遍历顺序(TMap 不承诺遍历顺序,TArray 承诺)。漏掉任何一条,两端照样在几百帧后分叉,而且当时没有任何报错。所以这三条只能当纪律守着,写进配置也没用。

"Same platform, same build" is only a necessary condition. Two runs of one binary still agree bit for bit only if three things hold at once: no unseeded randomness (not even one FMath::Rand), no dependence on parallel reduction order (a sum or a minimum changes when the scheduler moves it), and no dependence on container iteration order (TMap makes no ordering promise; TArray does). Miss one and the peers fork a few hundred frames in with nothing raised at the time. So treat those three as discipline; no setting enforces them.

定点确定性 Fixed-Point Determinism

定点把小数变成整数(我们用 Q31.32:一个 64 位整数,低 32 位是小数部分)。整数运算在每个平台上定义完全一样,没有舍入模式,没有扩展精度,没有乘加收缩。所以它能跨平台逐位一致,这是它唯一的好处,也是决定性的那一个。

Fixed point turns decimals into integers (ours is Q31.32: a 64-bit integer with the low 32 bits as the fraction). Integer arithmetic is defined identically everywhere — no rounding modes, no extended precision, no fused multiply-add. That is why it reproduces bit for bit across platforms, its only advantage and a decisive one.

代价在性能上:每个运算都拿整数指令模拟,乘法完要移位修正,跟引擎那些浮点类型来回转换也要时间。同一段模拟,定点版本会更慢。

The price is performance: every operation is emulated with integer instructions, each multiply needs a shift to renormalise, and converting to and from the engine's float types costs time as well. The same simulation runs slower in fixed point.

我们为什么先做浮点 Why We Built Float First

理由只有一条:引擎的地基是浮点的。在这样的地基上先把浮点确定性跑通,顺序、随机、哈希、失步检测全都验过一遍,比一上来就换掉整个地基快得多,也更容易把“确定性”这件事本身做对。先把锁步跑通,再讨论数字用什么表示;顺序反了,两件事都做不成。

For one reason: the engine's foundation is float. Getting float determinism working on top of that — ordering, randomness, hashing, desync detection all verified — comes far faster than replacing the foundation on day one, and it is the safer way to get determinism itself right. Prove the lockstep, then argue about number representation. The other order finishes neither.

定点部分已经验证到哪一步 How Far the Fixed-Point Side Is Proven

两件事已经做完,定点不再是纸上的计划:

Two things are already done, so the fixed-point side is not a plan on paper:

做了什么What was done结果Result
Q31.32 定点数学库,外加两个专用测试库(数学正确性、溢出行为)A Q31.32 fixed-point math library, with two dedicated test libraries (arithmetic correctness, overflow behaviour) 测试全部通过The tests pass
整条移动处理器改成定点版,跑同样的场景和浮点版对比The whole movement processor rebuilt in fixed point and run through the same scenes against the float version 结果和浮点版基本一致,性能明显更差,符合预期Results match the float version closely; performance is visibly worse, as expected

所以“定点能不能替我们跑”已经有答案:能。剩下的是工作量,可行性已经验过。

So "can fixed point carry this simulation" has an answer: yes. What is left is effort; feasibility is settled.

为什么不是现在全改 Why Not Convert Everything Now

因为移动处理器只是整个插件里的一小块。周围的代码库是它的十五倍以上,把那么大一片全换成定点,还要测试全绿、行为不变、性能能接受,工作量是另一个量级。

Because the movement processor is one small piece. The code around it is more than fifteen times its size, and converting all of that while keeping tests green, behaviour unchanged and performance acceptable is a different order of work.

所以顺序是:先把功能做完、定型,再做定点重构。功能还在动的时候去改算术表示,等于每次功能调整都要在两条数学路径上各改一遍、各测一遍,两边一起慢。

So the order is: finish the features and let them settle, then do the fixed-point refactor. Change the number representation while features are still moving and every feature change has to be made twice and tested twice, on two math paths, and both get slower.

这件事也不该单独做:定点重构和移动端 / 多平台支持是同一趟车。跨平台同步的需求本来就来自“让不同设备进同一局”,把它跟平台适配放在一起,要验的场景、要测的设备、要解的问题才是同一批。

It should not travel alone either: the fixed-point refactor rides with mobile and multi-platform support. Cross-platform sync exists to put different devices in one match, so pairing the two means the same scenes, the same devices and the same problems get exercised in one go.

现在该怎么用 What This Means Today

给今天就要做联机的人一句话:同一平台、同一份构建 → 用浮点确定性,这是默认值,也验过了;要跨平台同局 → 等定点重构落地。别指望在引擎里开个开关就跨平台:开关确实有(子系统上的运算精度选择),但它今天只管移动那一块,其余模拟还是浮点。

If you ship networked play today: one platform and one build → use float determinism, that is the default and it has been verified; cross-platform in one match → wait for the fixed-point refactor. Do not expect a switch to make the game cross-platform. The switch exists (the subsystem's arithmetic-precision option), but today it covers movement and nothing else; the rest of the simulation is still float.

还有一条不能破的规矩:同一局里所有端必须选同一条路。一端浮点、一端定点,就是两套算术各算一遍,必然分叉,跟精度高低无关。

One rule no matter what: every peer in a match picks the same path. A float peer and a fixed-point peer run two arithmetic systems over one simulation; the fork is guaranteed, whatever the accuracy.

Leroy Works · Mass Battle Framework 作者
联系:Discord · QQ 群 916358710 · Fab · 全部联系方式

Leroy Works — author of Mass Battle Framework
Contact: Discord · QQ group 916358710 · Fab · all channels