mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-29 11:42:17 +00:00
Math.sumPrecise is a port of JavaScriptCore's xsum accumulator as the reference's Bun ships it, including its rounding of negative sums (Bun's WebKit predates the fix in 8ee560d748ea, so Math.sumPrecise([-1]) is -1.0000000000000002 there, and here). ** and Math.pow follow JavaScriptCore's operationMathPow: square roots for ±0.5 and square-and-multiply for integer exponents up to 1000, so 10 ** 23 is 1e+23 as in the reference. Touches vm.rs (js_pow).
5 lines
973 B
JavaScript
5 lines
973 B
JavaScript
let seed = 11; const rnd = () => { seed = (seed * 1103515245 + 12345) % 2147483648; return seed / 2147483648 }
|
|
const out = [...[[-1], [-0.1], [1, -1, -1], [-8.8], [-Number.MAX_VALUE]].map((a) => Math.sumPrecise(a)), Math.sumPrecise([-1e-300, 1e-300 * 0.5]), Math.sumPrecise([1e-300, -5e-301]), Math.sumPrecise([]), Object.is(Math.sumPrecise([-0]), -0), Math.sumPrecise([1e308, 1e308, -1e308]), Math.sumPrecise([1e308, 1e308]), Math.sumPrecise([5e-324, -5e-324]), Math.sumPrecise([-5e-324]), Math.sumPrecise([0.1, 0.2, 0.3])]
|
|
for (let i = 0; i < 400; i++) { const n = 1 + Math.floor(rnd() * 6); const xs = []; for (let j = 0; j < n; j++) xs.push((rnd() - 0.6) * 10 ** Math.floor(rnd() * 600 - 320)); out.push(String(Math.sumPrecise(xs))) }
|
|
for (let i = 0; i < 400; i++) { const n = 1 + Math.floor(rnd() * 4); const xs = []; for (let j = 0; j < n; j++) xs.push((rnd() - 0.5) * 2 ** Math.floor(rnd() * 20 - 10)); out.push(String(Math.sumPrecise(xs))) }
|
|
return out.map(String)
|