136 lines
3.2 KiB
JavaScript
136 lines
3.2 KiB
JavaScript
#!/usr/bin/env node
|
|
/**
|
|
* R5.1b (F1): ECMAScript `Number::toString` as V8 prints it, the independent
|
|
* reference for the core's `js_number` (crates/core/src/json.rs). Each row
|
|
* holds the IEEE 754 bits (hex, so the Rust side never parses a decimal) and
|
|
* the text `String(value)` gives. Deterministic: a fixed LCG, no clock.
|
|
*
|
|
* node scripts/r5-number-oracle.mjs writes crates/core/tests/oracles/r5/numbers.json
|
|
*
|
|
* Classes: edge values (zero, subnormals, extremes, the 1e21 and 1e-7
|
|
* notation thresholds, powers of ten, integers around 2^53), the tie values
|
|
* found by the R5.1 study, float32-exact values (where exact decimal ties
|
|
* between two shortest candidates occur), random doubles in the coordinate
|
|
* range and across the whole exponent range, each also negated.
|
|
*/
|
|
import { writeFileSync } from 'node:fs'
|
|
import { dirname, resolve } from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
const OUT = resolve(
|
|
dirname(fileURLToPath(import.meta.url)),
|
|
'../crates/core/tests/oracles/r5/numbers.json',
|
|
)
|
|
|
|
const view = new DataView(new ArrayBuffer(8))
|
|
function bits(value) {
|
|
view.setFloat64(0, value)
|
|
return view.getBigUint64(0).toString(16).padStart(16, '0')
|
|
}
|
|
function fromBits(hex) {
|
|
view.setBigUint64(0, BigInt(`0x${hex}`))
|
|
return view.getFloat64(0)
|
|
}
|
|
|
|
let seed = 0x5eed
|
|
function random() {
|
|
seed = (Math.imul(seed, 1103515245) + 12345) >>> 0
|
|
return seed / 2 ** 32
|
|
}
|
|
|
|
const EDGES = [
|
|
0,
|
|
-0,
|
|
5e-324,
|
|
1e-323,
|
|
2.2250738585072014e-308,
|
|
2.225073858507201e-308,
|
|
1.7976931348623157e308,
|
|
Number.MAX_SAFE_INTEGER,
|
|
Number.MAX_SAFE_INTEGER + 1,
|
|
2 ** 53 + 2,
|
|
2 ** 64,
|
|
1e21,
|
|
999999999999999900000,
|
|
9.999999999999999e20,
|
|
1e20,
|
|
123456789012345680000,
|
|
1e-6,
|
|
1e-7,
|
|
9.999999999999999e-7,
|
|
0.000001,
|
|
0.0000012345,
|
|
1.5e-7,
|
|
0.1,
|
|
0.2,
|
|
0.3,
|
|
0.1 + 0.2,
|
|
1 / 3,
|
|
2 / 3,
|
|
0.85,
|
|
1.5,
|
|
2.5,
|
|
1523,
|
|
1523.5,
|
|
1715.4285714285713,
|
|
4.35,
|
|
1e-13,
|
|
1e-300,
|
|
1e300,
|
|
1e308,
|
|
333333333.3333333,
|
|
4.5,
|
|
0.002,
|
|
1e-27,
|
|
1e30,
|
|
100,
|
|
1e2,
|
|
12345678901234567,
|
|
840.1692504882812,
|
|
21.699905395507812,
|
|
960.3042602539062,
|
|
1424.2662353515625,
|
|
1117.97705078125,
|
|
469.1115417480469,
|
|
743.0451049804688,
|
|
399.0752258300781,
|
|
]
|
|
for (let e = -30; e <= 30; e++) EDGES.push(10 ** e, 2 ** e)
|
|
|
|
function rows() {
|
|
const values = [...EDGES]
|
|
for (let i = 0; i < 10000; i++) values.push(Math.fround(random() * 2000))
|
|
for (let i = 0; i < 2000; i++) values.push(Math.fround(random() * 65536))
|
|
for (let i = 0; i < 8000; i++) values.push(random() * 2000)
|
|
for (let i = 0; i < 2000; i++) {
|
|
const hi = Math.floor(random() * 0x7fe00000)
|
|
const lo = Math.floor(random() * 2 ** 32)
|
|
values.push(
|
|
fromBits(
|
|
hi.toString(16).padStart(8, '0') + lo.toString(16).padStart(8, '0'),
|
|
),
|
|
)
|
|
}
|
|
const seen = new Set()
|
|
const out = []
|
|
for (const value of values) {
|
|
for (const v of [value, -value]) {
|
|
const key = bits(v)
|
|
if (seen.has(key) || !Number.isFinite(v)) continue
|
|
seen.add(key)
|
|
out.push([key, String(v)])
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
const table = rows()
|
|
writeFileSync(
|
|
OUT,
|
|
`${JSON.stringify({
|
|
frozen_from: `V8 ${process.versions.v8}, Node ${process.version}: String(value)`,
|
|
rows: table,
|
|
})}\n`,
|
|
)
|
|
console.log(`${table.length} rows → ${OUT}`)
|