/ music / double-pendulum
Sign in
music / double-pendulum
double-pendulum / src / physics / dmath.ts
175 lines · 6.2 KBCodeBlameHistory
75ca48aOverhaul playback: background-computed, buffered, one clockJeremy Magland 1// Deterministic math for the simulations. Math.sin, Math.exp, Math.pow … may differ in the last bit
2// between browsers (V8, JavaScriptCore and SpiderMonkey use different implementations), and a
3// chaotic double pendulum amplifies a last-bit difference into a different performance within
4// seconds. These are ports of the standard fdlibm/musl algorithms built only from + − × ÷ and
5// Math.sqrt/round/floor/abs, which IEEE 754 makes identical everywhere. So the same seed and
6// settings play the same physics, and the same notes, on every device.
7//
8// Rule: simulation code (everything in a score) uses these, never Math.sin/cos/exp/log/pow/hypot.
10const buf = new DataView(new ArrayBuffer(8));
11const hiWord = (x: number) => (buf.setFloat64(0, x), buf.getInt32(0));
12const fromWords = (hi: number, lo: number) => (buf.setInt32(0, hi), buf.setInt32(4, lo), buf.getFloat64(0));
13const loWord = (x: number) => (buf.setFloat64(0, x), buf.getInt32(4));
15// ── sin / cos ──
17const S1 = -1.66666666666666324348e-1, S2 = 8.33333333332248946124e-3, S3 = -1.98412698298579493134e-4;
18const S4 = 2.75573137070700676789e-6, S5 = -2.50507602534068634195e-8, S6 = 1.58969099521155010221e-10;
19const C1 = 4.16666666666666019037e-2, C2 = -1.38888888888741095749e-3, C3 = 2.48015872894767294178e-5;
20const C4 = -2.75573143513906633035e-7, C5 = 2.08757232129817482790e-9, C6 = -1.13596475577881948265e-11;
22function ksin(x: number, y: number): number {
23 const z = x * x, v = z * x;
24 const r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
25 return x - ((z * (0.5 * y - v * r) - y) - v * S1);
28function kcos(x: number, y: number): number {
29 const z = x * x;
30 const r = z * (C1 + z * (C2 + z * (C3 + z * (C4 + z * (C5 + z * C6)))));
31 const hz = 0.5 * z, w = 1 - hz;
32 return w + (((1 - w) - hz) + (z * r - x * y));
35const INV_PIO2 = 6.36619772367581382433e-1;
36const PIO2_1 = 1.57079632673412561417;
37const PIO2_2 = 6.07710050630396597660e-11, PIO2_2T = 2.02226624879595063154e-21;
39/** x = n·π/2 + (y0 + y1), |y0 + y1| ≤ π/4 (Cody–Waite, two stages: accurate for |x| ≲ 1e6). */
40let red0 = 0, red1 = 0;
41function reduce(x: number): number {
42 const fn = Math.round(x * INV_PIO2);
43 const t = x - fn * PIO2_1; // exact (fn·PIO2_1 has trailing zeros)
44 let w = fn * PIO2_2;
45 let r: number;
46 r = t - w;
47 w = fn * PIO2_2T - ((t - r) - w);
48 red0 = r - w;
49 red1 = (r - red0) - w;
50 return fn;
53export function sin(x: number): number {
54 if (!Number.isFinite(x)) return NaN;
55 if (Math.abs(x) < 7.85398163397448278999e-1) return ksin(x, 0);
56 const n = reduce(x) & 3;
57 if (n === 0) return ksin(red0, red1);
58 if (n === 1) return kcos(red0, red1);
59 if (n === 2) return -ksin(red0, red1);
60 return -kcos(red0, red1);
63export function cos(x: number): number {
64 if (!Number.isFinite(x)) return NaN;
65 if (Math.abs(x) < 7.85398163397448278999e-1) return kcos(x, 0);
66 const n = reduce(x) & 3;
67 if (n === 0) return kcos(red0, red1);
68 if (n === 1) return -ksin(red0, red1);
69 if (n === 2) return -kcos(red0, red1);
70 return ksin(red0, red1);
73// ── exp / log / pow ──
75const LN2_HI = 6.93147180369123816490e-1, LN2_LO = 1.90821492927058770002e-10, INV_LN2 = 1.44269504088896338700;
76const P1 = 1.66666666666666019037e-1, P2 = -2.77777777770155933842e-3, P3 = 6.61375632143793436117e-5;
77const P4 = -1.65339022054652515390e-6, P5 = 4.13813679705723846039e-8;
79/** y·2^k without libm (exact scaling, splitting k so each factor is a normal power of two). */
80function scale2(y: number, k: number): number {
81 while (k > 1023) {
82 y *= fromWords(0x7fe00000, 0); // 2^1023
83 k -= 1023;
84 }
85 while (k < -1022) {
86 y *= fromWords(0x00100000, 0); // 2^-1022
87 k += 1022;
88 }
89 return y * fromWords((k + 1023) << 20, 0);
92export function exp(x: number): number {
93 if (Number.isNaN(x)) return NaN;
94 if (x > 709.782712893383973096) return Infinity;
95 if (x < -745.13321910194110842) return 0;
96 let hi: number, lo: number, k: number;
97 const ax = Math.abs(x);
98 if (ax > 0.5 * 6.93147180559945286227e-1) {
99 k = Math.trunc(INV_LN2 * x + (x < 0 ? -0.5 : 0.5));
100 hi = x - k * LN2_HI;
101 lo = k * LN2_LO;
102 x = hi - lo;
103 } else if (ax > 3.7252902984e-9) {
104 k = 0;
105 hi = x;
106 lo = 0;
107 } else return 1 + x;
108 const xx = x * x;
109 const c = x - xx * (P1 + xx * (P2 + xx * (P3 + xx * (P4 + xx * P5))));
110 const y = 1 + ((x * c) / (2 - c) - lo + hi);
111 return k === 0 ? y : scale2(y, k);
114const LG1 = 6.666666666666735130e-1, LG2 = 3.999999999940941908e-1, LG3 = 2.857142874366239149e-1;
115const LG4 = 2.222219843214978396e-1, LG5 = 1.818357216161805012e-1, LG6 = 1.531383769920937332e-1;
116const LG7 = 1.479819860511658591e-1;
118export function log(x: number): number {
119 if (Number.isNaN(x) || x < 0) return NaN;
120 if (x === 0) return -Infinity;
121 if (x === Infinity) return Infinity;
122 let k = 0;
123 if (x < 2.2250738585072014e-308) {
124 x *= 18014398509481984; // 2^54: subnormal → normal
125 k -= 54;
126 }
127 let hx = hiWord(x);
128 const lx = loWord(x);
129 // reduce x into [√2/2, √2)
130 hx += 0x3ff00000 - 0x3fe6a09e;
131 k += (hx >> 20) - 0x3ff;
132 hx = (hx & 0x000fffff) + 0x3fe6a09e;
133 x = fromWords(hx, lx);
134 const f = x - 1;
135 const hfsq = 0.5 * f * f;
136 const s = f / (2 + f);
137 const z = s * s, w = z * z;
138 const t1 = w * (LG2 + w * (LG4 + w * LG6));
139 const t2 = z * (LG1 + w * (LG3 + w * (LG5 + w * LG7)));
140 const R = t2 + t1;
141 return s * (hfsq + R) + k * LN2_LO - hfsq + f + k * LN2_HI;
144/** x^y for the cases simulations use (x ≥ 0, or x < 0 with integer y). */
145export function pow(x: number, y: number): number {
146 if (y === 0 || x === 1) return 1;
147 if (Number.isNaN(x) || Number.isNaN(y)) return NaN;
148 if (x === 0) return y > 0 ? 0 : Infinity;
149 if (x < 0) {
150 if (!Number.isInteger(y)) return NaN;
151 const r = exp(y * log(-x));
152 return y % 2 === 0 ? r : -r;
153 }
154 if (y === 1) return x;
155 if (y === 2) return x * x;
156 if (y === 0.5) return Math.sqrt(x);
157 return exp(y * log(x));
160export function hypot(x: number, y: number): number {
161 return Math.sqrt(x * x + y * y);
164export function tanh(x: number): number {
165 if (x > 20) return 1;
166 if (x < -20) return -1;
167 const e = exp(2 * x);
168 return (e - 1) / (e + 1);
171const TWO_PI = 6.283185307179586;
172/** Angle wrapped to [−π, π), by arithmetic (no atan2). */
173export function wrap(a: number): number {
174 return a - TWO_PI * Math.floor((a + Math.PI) / TWO_PI);
↑↓ move↵openescclose