opencode/packages/codemode-rust/conformance/cases/std-number-format.js
kommander ebb15aeb19 feat(codemode): add URI functions, toExponential, Math.sumPrecise, f16round and copyWithin
- encodeURI/encodeURIComponent/decodeURI/decodeURIComponent with the
  reference's URIError messages.
- Number formatting follows JavaScriptCore: toString(radix) ports its exact
  integer digits and shortest-fraction algorithm, toExponential and
  toPrecision round half up on the exact value, toFixed keeps the sign of
  a negative that rounds to zero, and number-to-string picks the even
  digit when two shortest forms tie. Range errors use the host messages.
- Math.sumPrecise sums exactly and rounds once; Math.f16round rounds
  straight from the double; Array.prototype.copyWithin.

Number-to-string and its tie rule live in value.rs, so integers from 2^53
up print their shortest digits instead of every digit.

copyWithin converts its arguments through ToPrimitive like the other Array methods. Number-to-string decides ties in 128-bit integer arithmetic. test262: the reference's skipped.txt drops three copyWithin files the reference passes.
2026-11-18 18:56:09 +01:00

5 lines
1.3 KiB
JavaScript

const out = []
for (const f of [() => (1).toExponential(101), () => (1).toPrecision(0), () => (1).toFixed(101), () => (1).toString(1), () => (1).toString(37), () => Infinity.toExponential(1000), () => NaN.toPrecision(0), () => (1).toFixed(Infinity)]) { try { out.push(f()) } catch (e) { out.push([e.name, e.message]) } }
out.push([0.1, 0.5, 1/3, 2/3, 123.456, -0.1, 1e-7, 3.14159, 1e21, 2**60, 2**60+0.5, 1.7976931348623157e308, 5e-324, 0.000001, 255, -255.5, 0.9999999999999999].map(x => [2, 3, 7, 16, 36].map((r) => x.toString(r)).join(" ")))
out.push((1.25).toExponential(1), (1.35).toExponential(1), (9.995).toPrecision(3), (99.99).toPrecision(2), (0.000001).toPrecision(1), (1e-7).toPrecision(1), (123456).toPrecision(2), (-0).toPrecision(2), (0).toExponential(), (-0).toExponential(1), (25).toPrecision(1), (1.0e-10).toExponential(3), (123.456).toExponential(), (123.456).toExponential(0), (5e-324).toExponential(), (-1.5e-7).toExponential(1), (1e21).toPrecision(3), (0.00001234).toPrecision(2), (1.45).toFixed(1), (1e-7).toFixed(10), (999.9).toPrecision(3), (1).toPrecision(100).length, (1/3).toExponential(100).length, (255).toString({ valueOf() { return 16 } }), (1.5).toFixed("2"), (12345.6789).toLocaleString(), (-1e21).toFixed(2), (0.5).toFixed(0), (2.5).toFixed(0), (-0.5).toFixed(0))
return out