Should a cross-platform deterministic lockstep game simulate in fixed-point arithmetic or in tightly controlled IEEE floats, and what decides it?
answer
- integers agree bit for bit
- range and resolution fixed up front
- overflow in products and squared distances
- who owns sin, sqrt and atan2
- only the simulation must be deterministic
basics
~20 sFixed point makes cross-platform determinism a property of integer arithmetic, at the cost of range limits, overflow care and hand-written math; controlled floats keep speed and familiar tooling but need strict flags, own transcendentals and cross-platform tests. The platform spread usually decides.
solid answer
~50 sFixed point stores each value as a scaled integer, for example Q16.16 with 16 fraction bits in a 32-bit integer, so every operation is integer arithmetic and gives identical bits on every platform and compiler. The price is a fixed range and resolution, products that need a wider intermediate, squared distances that overflow early, and sqrt, trig and atan2 written by hand. Controlled floats keep hardware speed, SIMD and existing float code, and IEEE 754 basic operations are reproducible, but only with strict build settings, no fused multiply-add contraction, own transcendentals and a fixed operation order. I pick fixed point for cross-play across architectures and compilers with little third-party float code in the simulation, and controlled floats for one platform family or float middleware; either way only the simulation is deterministic, and cross-platform replay tests run in continuous integration.
code
pseudocode · 10 lines// Q16.16 overflow trap: squared distance between two units
dx = toFixed(150.0) // 150 * 65536 = 9,830,400
dy = toFixed(130.0) // 130 * 65536 = 8,519,680
distSq = fixedMul(dx, dx) + fixedMul(dy, dy)
// true value is 22,500 + 16,900 = 39,400: above the Q16.16 limit of 32,768
// each product fits, but the 32-bit sum overflows
// safer: compare squared distances in a 64-bit accumulator
distSq64 = toInt64(dx) * dx + toInt64(dy) * dy // scale 2^32, no overflow
inRange = distSq64 <= toInt64(range) * rangego deeper
Recall that fixed point stores a number as a scaled integer, so its arithmetic is exact integer math with a fixed resolution and range.
Explain the Q16.16 multiply and divide with a wider intermediate, the range the format gives, and why squared distances overflow long before positions do.
List what breaks determinism under each choice, from fast-math and library sin to overflow and rounding of negative shifts, and how a cross-platform replay test catches it.
Own the call from the platform matrix, the third-party float code inside the simulation, world size and team skill, and draw the deterministic boundary so rendering stays fast floats.
## What fixed point is A **fixed-point** number is an integer with an agreed scale. In the **Q16.16** format a 32-bit signed integer holds a value multiplied by `2^16 = 65536`: - resolution is `1/65536`, about `0.0000153`; - range is from `-32768` to just under `32768`; - addition and subtraction are plain integer operations; - multiplication needs a 64-bit intermediate, then a right shift by 16; - division shifts the dividend left by 16 in 64 bits, then divides; - square root, trigonometry and atan2 are written with integer algorithms, lookup tables or fixed-point polynomials. ``` // Q16.16: a real value v is stored as the integer round(v * 65536) FRACTION_BITS = 16 function fixedMul(a, b): // a, b: 32-bit signed integers wide = toInt64(a) * toInt64(b) // fits: at most 2^62 in magnitude return toInt32(wide >> FRACTION_BITS) // out of range unless -32768 <= true result < 32768 function fixedDiv(a, b): // b must not be 0 return toInt32((toInt64(a) << FRACTION_BITS) / b) ``` As long as the rounding direction of a right shift on negative values and of integer division is defined once and implemented the same way on every platform, every platform produces the same bits. ## The case for fixed point - **Determinism is structural.** Integer arithmetic has no rounding modes, no fused forms and no library-specific transcendentals, so different CPUs and compilers agree by construction. - **Uniform precision.** Resolution is the same everywhere in the range, so there is no far-from-origin degradation within it. - **Auditable.** Every rounding decision is in your own code, so a desync investigation reads your arithmetic rather than a compiler's or a library's choices. Its costs: - **Range and overflow.** Q16.16 ends just under 32,768 units, and intermediates overflow much sooner: squaring a 200-unit distance gives 40,000, already out of range. Larger formats such as 64-bit with 32 fraction bits need 128-bit intermediates for multiplication. - **Hand-written math** for everything beyond arithmetic, and a team that knows how to scale and order operations to keep precision. - **No float middleware** inside the simulation: third-party physics or pathfinding code written for floats cannot run there. ## The case for controlled floats - **Speed and familiarity.** Hardware float and SIMD units, existing libraries, debuggers that print readable values. - **Reproducible basic operations.** IEEE 754 requires addition, subtraction, multiplication, division and square root to be correctly rounded, so identical operation sequences agree across x86 and ARM. Its costs are a discipline that must hold on every build: - strict floating-point settings, no fast-math and no automatic fused multiply-add contraction in simulation code; - own implementations of `sin`, `cos`, `atan2` and `exp` built only from basic operations; - a fixed evaluation order, including deterministic reductions in threaded and vectorised code; - re-validation whenever a compiler, platform or third-party library changes. ## Comparing the two | concern | fixed point | controlled floats | |---|---|---| | cross-architecture determinism | by construction | by discipline, re-proved per toolchain | | range | fixed by the format | very wide | | performance | integer speed, but products need wide intermediates | full float and SIMD speed | | third-party code in the simulation | must be fixed point too | float libraries usable if they obey the rules | | effort | hand-written math, overflow audits | build-flag control, own transcendentals, order audits | ## What decides it 1. **The platform matrix.** One platform family and one compiler makes controlled floats realistic; cross-play between different architectures and compilers favours fixed point. 2. **Third-party code inside the simulation.** Float middleware in the simulation rules out fixed point for that part. 3. **World size against resolution.** Does the map fit the format's range at the resolution gameplay needs, with headroom for products? 4. **Team skill and schedule.** Fixed point front-loads cost; controlled floats spread it over the project's life as a maintenance tax. 5. **The deterministic boundary.** Only state feeding the next tick must be bit-identical; rendering, audio, particles and camera can convert to ordinary floats and use fast math freely, as long as nothing flows back. Either choice needs the same safety net: recorded matches replayed on every target platform in continuous integration, comparing state hashes, because neither approach stays deterministic on its own.
- Why is Q16.16 a risky default for a large strategy map?Its range ends just under 32,768 units, and products overflow much earlier: a distance of about 200 units already squares to 40,000, beyond it. Large maps need a wider format, such as 64 bits with 32 fraction bits, or wider accumulators for every product and squared distance, and the multiply then needs a 128-bit intermediate or split arithmetic.
- With controlled floats, how do you make sin and cos deterministic?Do not call the platform math library inside the simulation. Ship your own implementation, typically a range reduction followed by a fixed polynomial, using only correctly rounded basic operations, compiled without contraction or fast-math. Every platform then runs the same operation sequence and produces the same bits.
- Does the renderer also have to be deterministic?No. Only state that feeds the next simulation tick must be bit-identical across clients. Rendering, audio, particles and the camera can convert simulation values to ordinary floats, interpolate and use fast math, provided nothing they compute flows back into the simulation.
- Fixed point removes every determinism risk, so no cross-platform testing is needed.
- Doubles are deterministic across platforms where single-precision floats are not.
- Fixed point needs no range planning because integers never lose precision.
- If the simulation uses fixed point, rendering must use fixed point too.
- Floats can never be made deterministic across x86 and ARM.