// Deterministic math for the simulations. Math.sin, Math.exp, Math.pow … may differ in the last bit // between browsers (V8, JavaScriptCore and SpiderMonkey use different implementations), and a // chaotic double pendulum amplifies a last-bit difference into a different performance within // seconds. These are ports of the standard fdlibm/musl algorithms built only from + − × ÷ and // Math.sqrt/round/floor/abs, which IEEE 754 makes identical everywhere. So the same seed and // settings play the same physics, and the same notes, on every device. // // Rule: simulation code (everything in a score) uses these, never Math.sin/cos/exp/log/pow/hypot. const buf = new DataView(new ArrayBuffer(8)); const hiWord = (x: number) => (buf.setFloat64(0, x), buf.getInt32(0)); const fromWords = (hi: number, lo: number) => (buf.setInt32(0, hi), buf.setInt32(4, lo), buf.getFloat64(0)); const loWord = (x: number) => (buf.setFloat64(0, x), buf.getInt32(4)); // ── sin / cos ── const S1 = -1.66666666666666324348e-1, S2 = 8.33333333332248946124e-3, S3 = -1.98412698298579493134e-4; const S4 = 2.75573137070700676789e-6, S5 = -2.50507602534068634195e-8, S6 = 1.58969099521155010221e-10; const C1 = 4.16666666666666019037e-2, C2 = -1.38888888888741095749e-3, C3 = 2.48015872894767294178e-5; const C4 = -2.75573143513906633035e-7, C5 = 2.08757232129817482790e-9, C6 = -1.13596475577881948265e-11; function ksin(x: number, y: number): number { const z = x * x, v = z * x; const r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6))); return x - ((z * (0.5 * y - v * r) - y) - v * S1); } function kcos(x: number, y: number): number { const z = x * x; const r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6))))); const hz = 0.5 * z, w = 1 - hz; return w + (((1 - w) - hz) + (z * r - x * y)); } const INV_PIO2 = 6.36619772367581382433e-1; const PIO2_1 = 1.57079632673412561417; const PIO2_2 = 6.07710050630396597660e-11, PIO2_2T = 2.02226624879595063154e-21; /** x = n·π/2 + (y0 + y1), |y0 + y1| ≤ π/4 (Cody–Waite, two stages: accurate for |x| ≲ 1e6). */ let red0 = 0, red1 = 0; function reduce(x: number): number { const fn = Math.round(x * INV_PIO2); const t = x - fn * PIO2_1; // exact (fn·PIO2_1 has trailing zeros) let w = fn * PIO2_2; let r: number; r = t - w; w = fn * PIO2_2T - ((t - r) - w); red0 = r - w; red1 = (r - red0) - w; return fn; } export function sin(x: number): number { if (!Number.isFinite(x)) return NaN; if (Math.abs(x) < 7.85398163397448278999e-1) return ksin(x, 0); const n = reduce(x) & 3; if (n === 0) return ksin(red0, red1); if (n === 1) return kcos(red0, red1); if (n === 2) return -ksin(red0, red1); return -kcos(red0, red1); } export function cos(x: number): number { if (!Number.isFinite(x)) return NaN; if (Math.abs(x) < 7.85398163397448278999e-1) return kcos(x, 0); const n = reduce(x) & 3; if (n === 0) return kcos(red0, red1); if (n === 1) return -ksin(red0, red1); if (n === 2) return -kcos(red0, red1); return ksin(red0, red1); } // ── exp / log / pow ── const LN2_HI = 6.93147180369123816490e-1, LN2_LO = 1.90821492927058770002e-10, INV_LN2 = 1.44269504088896338700; const P1 = 1.66666666666666019037e-1, P2 = -2.77777777770155933842e-3, P3 = 6.61375632143793436117e-5; const P4 = -1.65339022054652515390e-6, P5 = 4.13813679705723846039e-8; /** y·2^k without libm (exact scaling, splitting k so each factor is a normal power of two). */ function scale2(y: number, k: number): number { while (k > 1023) { y *= fromWords(0x7fe00000, 0); // 2^1023 k -= 1023; } while (k < -1022) { y *= fromWords(0x00100000, 0); // 2^-1022 k += 1022; } return y * fromWords((k + 1023) << 20, 0); } export function exp(x: number): number { if (Number.isNaN(x)) return NaN; if (x > 709.782712893383973096) return Infinity; if (x < -745.13321910194110842) return 0; let hi: number, lo: number, k: number; const ax = Math.abs(x); if (ax > 0.5 * 6.93147180559945286227e-1) { k = Math.trunc(INV_LN2 * x + (x < 0 ? -0.5 : 0.5)); hi = x - k * LN2_HI; lo = k * LN2_LO; x = hi - lo; } else if (ax > 3.7252902984e-9) { k = 0; hi = x; lo = 0; } else return 1 + x; const xx = x * x; const c = x - xx * (P1 + xx * (P2 + xx * (P3 + xx * (P4 + xx * P5)))); const y = 1 + ((x * c) / (2 - c) - lo + hi); return k === 0 ? y : scale2(y, k); } const LG1 = 6.666666666666735130e-1, LG2 = 3.999999999940941908e-1, LG3 = 2.857142874366239149e-1; const LG4 = 2.222219843214978396e-1, LG5 = 1.818357216161805012e-1, LG6 = 1.531383769920937332e-1; const LG7 = 1.479819860511658591e-1; export function log(x: number): number { if (Number.isNaN(x) || x < 0) return NaN; if (x === 0) return -Infinity; if (x === Infinity) return Infinity; let k = 0; if (x < 2.2250738585072014e-308) { x *= 18014398509481984; // 2^54: subnormal → normal k -= 54; } let hx = hiWord(x); const lx = loWord(x); // reduce x into [√2/2, √2) hx += 0x3ff00000 - 0x3fe6a09e; k += (hx >> 20) - 0x3ff; hx = (hx & 0x000fffff) + 0x3fe6a09e; x = fromWords(hx, lx); const f = x - 1; const hfsq = 0.5 * f * f; const s = f / (2 + f); const z = s * s, w = z * z; const t1 = w * (LG2 + w * (LG4 + w * LG6)); const t2 = z * (LG1 + w * (LG3 + w * (LG5 + w * LG7))); const R = t2 + t1; return s * (hfsq + R) + k * LN2_LO - hfsq + f + k * LN2_HI; } /** x^y for the cases simulations use (x ≥ 0, or x < 0 with integer y). */ export function pow(x: number, y: number): number { if (y === 0 || x === 1) return 1; if (Number.isNaN(x) || Number.isNaN(y)) return NaN; if (x === 0) return y > 0 ? 0 : Infinity; if (x < 0) { if (!Number.isInteger(y)) return NaN; const r = exp(y * log(-x)); return y % 2 === 0 ? r : -r; } if (y === 1) return x; if (y === 2) return x * x; if (y === 0.5) return Math.sqrt(x); return exp(y * log(x)); } export function hypot(x: number, y: number): number { return Math.sqrt(x * x + y * y); } export function tanh(x: number): number { if (x > 20) return 1; if (x < -20) return -1; const e = exp(2 * x); return (e - 1) / (e + 1); } const TWO_PI = 6.283185307179586; /** Angle wrapped to [−π, π), by arithmetic (no atan2). */ export function wrap(a: number): number { return a - TWO_PI * Math.floor((a + Math.PI) / TWO_PI); }