Skip to content

feat(simulation): math, deterministic trig, RNG, clock, hasher (Phase 2, PR 3/5) - #88

Merged
CodeByBryant merged 1 commit into
mainfrom
feature/simulation-foundations
Sep 21, 2026
Merged

CodeByBryant merged 1 commit into
mainfrom
feature/simulation-foundations

Conversation

@CodeByBryant

Copy link
Copy Markdown
Owner

Summary

Phase 2, PR 3 of 5. The deterministic building blocks the world kernel (PR 4) is made of. No World behavior yet.

  • Math: scalar (clamp, lerp, wrapAngle, PI constants), vector (add/sub/scale/dot/length/distance/normalize/clampLength/angle/fromAngle/lerp), geometry (rect containment, clamp to rect, circle containment/intersection, circle-rect). Pure functions, no Math.hypot.
  • Deterministic trig (math/trig.ts): fdlibm ports of sin, cos, atan2, ln using only IEEE-exact operations. Domains are documented and enforced with RangeError.
  • RNG: SeededRandom (xoshiro128**, float/integer/boolean/normal/choose, getState/setState including the cached normal spare), RandomStreams for the six named streams, and the exact derivation from the spec ("evo-rng-v1" bytes -> FNV-1a -> splitmix32 -> state, with the all-zero fallback).
  • Clock: SimulationClock (fixed timestep, time = tick * deltaTime by multiplication, never accumulated).
  • Hasher: StateHasher (cyrb128-style 128-bit, big-endian float64 bits, -0 canonicalized, throws on non-finite, versioned header via HASH_SCHEMA_VERSION), plus an exact utf8Encode (the engine cannot use TextEncoder).
  • docs/simulation/determinism.md gains the normal() vectors and an accuracy statement.

Simulation impact

  • Changes determinism (implements the accepted spec; ADR 0003)

Determinism

  • Uses the seeded random source; no Math.random(), wall clock, platform transcendentals or ** (ESLint enforces it on this code)
  • Determinism tests were added

Testing

Fresh clone: install --frozen-lockfile, format:check, check and build pass. 85 simulation tests (3 contracts, 24 config):

  • Every vector in determinism.md section 10 is reproduced by the real implementation (xoshiro reference, splitmix32, FNV-1a, stream derivations, float(), all-zero fallback, hasher digests including -0 canonicalization). Two of them (xoshiro reference, FNV-1a of a) match published values, giving independent confirmation of the spec.
  • sin/cos within 1e-15 of the platform over |x| <= 1e6 (including neighborhoods of multiples of pi/2), atan2 within 1e-15 (including wildly different magnitudes and signed zeros), ln within 2e-16 relative over 1e-300..1e300 and near 1.
  • Stream isolation (drawing from world leaves environment and genetics untouched), save/restore continuing the exact sequence including a cached spare, chi-square uniformity of integer, normal mean/sigma/tails.

Review notes

  • The fdlibm coefficients were entered from memory; the accuracy tests against the platform functions are what validate them (they passed first time), and the normal() vectors agree exactly with a Math.log reference.
  • Trig accepts |x| <= 1,000,000 and throws beyond it (Payne-Hanek reduction is deliberately not implemented; the engine wraps angles).
  • The placeholder World/ENGINE_VERSION in index.ts are untouched; PR 4 replaces them.

🤖 Generated with Claude Code

… state hasher

Vector/scalar/geometry utilities, fdlibm-based sin/cos/atan2/ln using only
IEEE-exact operations, xoshiro128** streams derived per the determinism
spec (all published test vectors reproduced), fixed-timestep clock and a
versioned 128-bit state hasher. Adds normal() vectors and accuracy notes
to determinism.md.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings September 21, 2026 01:20

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@CodeByBryant
CodeByBryant merged commit 9a30f80 into main Sep 21, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants