T-057: One maths library, so native and web can never drift apart
Goal
A world must give the same bits on the Mac, in the browser and on any future machine. That
is what lets a run made headless be opened on the web and continued. The danger is quiet:
functions such as exp, sin, cos, tanh, ln and powf come from the platform and
may differ in the last bit between targets. One differing bit grows into a different world.
T-011 found this and wrote its own exp and sine in crates/sim/src/fields/noise.rs.
Every other module will meet the same problem. Solve it once, for everyone, in a way that
cannot be forgotten.
Scope
- One home for these functions in
crates/sim/src/math.rs, asdocs/ARCHITECTURE.mdalready intends. Move the fields' versions there. Add the others the sim uses or will plainly need (physics, control and development are the likely users). State each one's accuracy and its cost next to the platform version. - Make the mistake impossible, not just documented. A build or lint step must fail when
code in
crates/simcalls a platform function whose result may differ by target. Find the general way to do this: the list of functions above is examples, and the check should catch ones nobody listed. - A cross-target test: the same world, using every module that exists, stepped natively and
as wasm, ends with the same checksum. T-011 has an example to start from
(
crates/sim/examples/wasm_prob*). It must run from one command and be part of the normal checks. - Speed: these functions sit in hot paths. Measure before and after with
runner bench, and say what the exactness costs.
Done when
- ☐ No module has its own copy of such a function.
- ☐ Adding
x.exp()orx.sin()anywhere incrates/simfails the checks, with a message that says what to use instead. Show it failing. - ☐ The cross-target test passes, and fails when one platform call is put back.
- ☐ Bench entries before and after in
docs/perf/LOG.md. - ☐
docs/ARCHITECTURE.mdsays where the functions live and why.
Notes
- Waits for T-009 and T-010 because both are being written now and both use maths. Starting earlier would mean editing their files under them.
sqrtand plain arithmetic are exact by the floating-point standard. Check that claim for the wasm target rather than trusting this note. Fused multiply-add is another place where targets can differ.