fix(domain): js_number rounds exact decimal ties to even as ECMAScript (zmij digits); V8 oracle of 44,322 values, Zod oracle bytes checked against the core's printing
This commit is contained in:
1 parent
64d4595e73
commit
aed6b1e300
8 files changed
+225
-10
No files matched your search
Generated
+1
@@ -372,6 +372,7 @@ dependencies = [
|
||||
"sha2",
|
||||
"ts-rs",
|
||||
"url",
|
||||
"zmij",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -42,6 +42,9 @@ libc = "0.2"
|
||||
# Supabase over HTTPS) and the hash of the clean output. No async runtime: the loop is sequential.
|
||||
ureq = { version = "3.4.2", default-features = false, features = ["rustls", "json"] }
|
||||
sha2 = "0.11"
|
||||
# Shortest round-trip float digits with ECMAScript's tie rule (round half to even), the
|
||||
# formatter serde_json already uses (R5.1b, F1): the version serde_json locks.
|
||||
zmij = "1.0.23"
|
||||
|
||||
# The library crates deny unwrap/expect so that no panic path depends on content;
|
||||
# the worker binary relaxes disallowed_methods (it owns I/O) in its own [lints].
|
||||
|
||||
@@ -28,6 +28,7 @@ serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
url.workspace = true
|
||||
roxmltree.workspace = true
|
||||
zmij.workspace = true
|
||||
ts-rs = { workspace = true, optional = true }
|
||||
miniz_oxide = { workspace = true, optional = true }
|
||||
|
||||
|
||||
+15
-8
@@ -34,16 +34,23 @@ pub fn js_number(value: f64) -> String {
|
||||
/// Shortest round-trip digits of a positive finite value and the position `n` of the
|
||||
/// decimal point relative to the first digit (ECMAScript's `n`): value = 0.d₁d₂… × 10ⁿ.
|
||||
///
|
||||
/// Rust's `LowerExp` prints the shortest round-trip mantissa (`1.7154285714285713e3`),
|
||||
/// chosen by the same rule as ECMAScript (shortest, then closest to the value).
|
||||
/// The digits come from `zmij`, which picks among the shortest candidates the one closest to
|
||||
/// the value and, on an exact tie, the even one: ECMAScript's rule. Rust's `{:e}` rounds a
|
||||
/// tie upward (`840.16925048828125` → `…813`, R5.1b F1). zmij prints either a plain decimal
|
||||
/// (`840.1692504882812`, `1523.0`, `0.001`) or a mantissa and an exponent (`1e21`,
|
||||
/// `1.5e-7`); both read back the same way here.
|
||||
fn shortest_digits(value: f64) -> (String, i32) {
|
||||
let scientific = format!("{value:e}");
|
||||
let (mantissa, exponent) = scientific
|
||||
.split_once('e')
|
||||
.unwrap_or((scientific.as_str(), "0"));
|
||||
let mut buffer = zmij::Buffer::new();
|
||||
let printed = buffer.format_finite(value);
|
||||
let (mantissa, exponent) = printed.split_once(['e', 'E']).unwrap_or((printed, "0"));
|
||||
let exponent: i32 = exponent.parse().unwrap_or(0);
|
||||
let digits: String = mantissa.chars().filter(char::is_ascii_digit).collect();
|
||||
(digits, exponent + 1)
|
||||
let (integer, fraction) = mantissa.split_once('.').unwrap_or((mantissa, ""));
|
||||
let mut point = i32::try_from(integer.len()).unwrap_or(i32::MAX) + exponent;
|
||||
let all: String = format!("{integer}{fraction}");
|
||||
let leading = all.len() - all.trim_start_matches('0').len();
|
||||
point -= i32::try_from(leading).unwrap_or(0);
|
||||
let digits = all.trim_matches('0').to_owned();
|
||||
(digits, point)
|
||||
}
|
||||
|
||||
/// ECMAScript Number::toString steps 6 to 10 for `k` digits and point position `n`.
|
||||
|
||||
@@ -39,9 +39,19 @@ pub fn oracle_file(name: &str) -> (String, Value) {
|
||||
|
||||
/// A Zod oracle (`crates/core/tests/oracles/<name>`): its source file and the Zod output in
|
||||
/// the JavaScript pretty form.
|
||||
///
|
||||
/// The expected text is printed here by `to_js_pretty`, the function under test; so the
|
||||
/// oracle's own bytes, written by `JSON.stringify` in Node, are first checked against the
|
||||
/// same value printed compactly: a number this side prints differently from V8 fails here
|
||||
/// (R5.1b, F1), not silently on both sides of the comparison.
|
||||
pub fn zod_oracle(name: &str) -> (String, String) {
|
||||
let oracle: Value =
|
||||
serde_json::from_str(&repo_file(&format!("crates/core/tests/oracles/{name}"))).unwrap();
|
||||
let raw = repo_file(&format!("crates/core/tests/oracles/{name}"));
|
||||
let oracle: Value = serde_json::from_str(&raw).unwrap();
|
||||
let reprinted = faunosteo_core::json::to_js_compact(&oracle).unwrap();
|
||||
assert!(
|
||||
raw == format!("{reprinted}\n"),
|
||||
"{name}: the core does not print the oracle's numbers as V8 wrote them"
|
||||
);
|
||||
let source = oracle["source"].as_str().unwrap().to_owned();
|
||||
let output = faunosteo_core::json::to_js_pretty(&oracle["output"]).unwrap();
|
||||
(repo_file(&source), output)
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
//! `js_number` against V8 (R5.1b, F1). The expected texts come from
|
||||
//! `crates/core/tests/oracles/r5/numbers.json`, written by `String(value)` in Node
|
||||
//! (`scripts/r5-number-oracle.mjs`); each value is given by its IEEE 754 bits, so no
|
||||
//! decimal parsing on this side decides the expectation.
|
||||
#![allow(clippy::disallowed_methods, clippy::unwrap_used, clippy::expect_used)]
|
||||
|
||||
mod common;
|
||||
|
||||
use faunosteo_core::json::js_number;
|
||||
use serde_json::Value;
|
||||
|
||||
fn oracle_rows() -> Vec<(u64, String)> {
|
||||
let oracle: Value = serde_json::from_str(&common::repo_file(
|
||||
"crates/core/tests/oracles/r5/numbers.json",
|
||||
))
|
||||
.unwrap();
|
||||
oracle["rows"]
|
||||
.as_array()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|row| {
|
||||
let bits = u64::from_str_radix(row[0].as_str().unwrap(), 16).unwrap();
|
||||
(bits, row[1].as_str().unwrap().to_owned())
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn every_oracle_value_prints_as_v8_prints_it() {
|
||||
let rows = oracle_rows();
|
||||
assert!(rows.len() > 40_000, "{} rows", rows.len());
|
||||
let mismatches: Vec<String> = rows
|
||||
.iter()
|
||||
.filter_map(|(bits, expected)| {
|
||||
let actual = js_number(f64::from_bits(*bits));
|
||||
(actual != *expected).then(|| format!("{bits:016x}: {actual} vs V8 {expected}"))
|
||||
})
|
||||
.collect();
|
||||
assert!(
|
||||
mismatches.is_empty(),
|
||||
"{} mismatches, first: {:?}",
|
||||
mismatches.len(),
|
||||
&mismatches[..mismatches.len().min(5)]
|
||||
);
|
||||
}
|
||||
|
||||
/// The value the R5.1 study found in the legacy baseline (`Horse.svg`, annotation `cx`):
|
||||
/// exactly 840.16925048828125, halfway between the two 16-digit candidates; ECMAScript
|
||||
/// picks the even last digit.
|
||||
#[test]
|
||||
fn an_exact_decimal_tie_rounds_to_the_even_digit() {
|
||||
let value = f64::from_bits(0x408a_415a_a000_0000);
|
||||
assert_eq!(js_number(value), "840.1692504882812");
|
||||
assert_eq!(js_number(-value), "-840.1692504882812");
|
||||
assert_eq!(js_number(21.699_905_395_507_812), "21.699905395507812");
|
||||
assert_eq!(js_number(960.304_260_253_906_2), "960.3042602539062");
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"frozen_from":"V8 13.6.233.17-node.53, Node v24.21.0: String(value)","rows":[["0000000000000000","0"],["8000000000000000","0"],["0000000000000001","5e-324"],["8000000000000001","-5e-324"],["0000000000000002","1e-323"],["8000000000000002","-1e-323"],["0010000000000000","2.2250738585072014e-308"],["8010000000000000","-2.2250738585072014e-308"],["000fffffffffffff","2.225073858507201e-308"],["800fffffffffffff","-2.225073858507201e-308"],["7fefffffffffffff","1.7976931348623157e+308"],["ffefffffffffffff","-1.7976931348623157e+308"],["433fffffffffffff","9007199254740991"],["c33fffffffffffff","-9007199254740991"],["4340000000000000","9007199254740992"],["c340000000000000","-9007199254740992"],["4340000000000001","9007199254740994"],["c340000000000001","-9007199254740994"],["43f0000000000000","18446744073709552000"],["c3f0000000000000","-18446744073709552000"],["444b1ae4d6e2ef50","1e+21"],["c44b1ae4d6e2ef50","-1e+21"],["444b1ae4d6e2ef4f","999999999999999900000"],["c44b1ae4d6e2ef4f","-999999999999999900000"],["4415af1d78b58c40","100000000000000000000"],["c415af1d78b58c40","-100000000000000000000"],["441ac53a7e04bcda","123456789012345680000"],["c41ac53a7e04bcda","-123456789012345680000"],["3eb0c6f7a0b5ed8d","0.000001"],["beb0c6f7a0b5ed8d","-0.000001"],["3e7ad7f29abcaf48","1e-7"],["be7ad7f29abcaf48","-1e-7"],["3eb4b6231abfd271","0.0000012345"],["beb4b6231abfd271","-0.0000012345"],["3e8421f5f40d8376","1.5e-7"],["be8421f5f40d8376","-1.5e-7"],["3fb999999999999a","0.1"],["bfb999999999999a","-0.1"],["3fc999999999999a","0.2"],["bfc999999999999a","-0.2"],["3fd3333333333333","0.3"],["bfd3333333333333","-0.3"],["3fd3333333333334","0.30000000000000004"],["bfd3333333333334","-0.30000000000000004"],["3fd5555555555555","0.3333333333333333"],["bfd5555555555555","-0.3333333333333333"],["3fe5555555555555","0.6666666666666666"],["bfe5555555555555","-0.6666666666666666"],["3feb333333333333","0.85"],["bfeb333333333333","-0.85"],["3ff8000000000000","1.5"],["bff8000000000000","-1.5"],["4004000000000000","2.5"],["c004000000000000","-2.5"],["4097cc0000000000","1523"],["c097cc0000000000","-1523"],["4097ce0000000000","1523.5"],["c097ce0000000000","-1523.5"],["409acdb6db6db6db","1715.4285714285713"],["c09acdb6db6db6db","-1715.4285714285713"],["4011666666666666","4.35"],["c011666666666666","-4.35"],["3d3c25c268497682","1e-13"],["bd3c25c268497682","-1e-13"],["01a56e1fc2f8f359","1e-300"],["81a56e1fc2f8f359","-1e-300"],["7e37e43c8800759c","1e+300"],["fe37e43c8800759c","-1e+300"],["7fe1ccf385ebc8a0","1e+308"],["ffe1ccf385ebc8a0","-1e+308"],["41b3de4355555555","333333333.3333333"],["c1b3de4355555555","-333333333.3333333"],["4012000000000000","4.5"],["c012000000000000","-4.5"],["3f60624dd2f1a9fc","0.002"],["bf60624dd2f1a9fc","-0.002"],["3a53ce9a36f23c10","1e-27"],["ba53ce9a36f23c10","-1e-27"],["46293e5939a08cea","1e+30"],["c6293e5939a08cea","-1e+30"],["4059000000000000","100"],["c059000000000000","-100"],["4345ee2a2eb5a5c4","12345678901234568"],["c345ee2a2eb5a5c4","-12345678901234568"],["408a415aa0000000","840.1692504882812"],["c08a415aa0000000","-840.1692504882812"],["4035b32d00000000","21.699905395507812"],["c035b32d00000000","-21.699905395507812"],["408e026f20000000","960.3042602539062"],["c08e026f20000000","-960.3042602539062"],["40964110a0000000","1424.2662353515625"],["c0964110a0000000","-1424.2662353515625"],["409177e880000000","1117.97705078125"],["c09177e880000000","-1117.97705078125"],["407d51c8e0000000","469.1115417480469"],["c07d51c8e0000000","-469.1115417480469"],["4087385c60000000","743.0451049804688"],["c087385c60000000","-743.0451049804688"],["4078f13420000000","399.0752258300781"],["c078f13420000000","-399.0752258300781"],["39b4484bfeebc2a0","1e-30"],["b9b4484bfeebc2a0","-1e-30"],["3e10000000000000","9.313225746154785e-10"],["be10000000000000","-9.313225746154785e-10"],["39e95a5efea6b347","1e-29"],["b9e95a5efea6b347","-1e-29"],["3e20000000000000","1.862645149230957e-9"],["be20000000000000","-1.862645149230957e-9"],["3a1fb0f6be506019","1e-28"],["ba1fb0f6be506019","-1e-28"],["3e30000000000000","3.725290298461914e-9"],["be30000000000000","-3.725290298461914e-9"],["3e40000000000000","7.450580596923828e-9"],["be40000000000000","-7.450580596923828e-9"],["3a88c240c4aecb14","1e-26"],["ba88c240c4aecb14","-1e-26"],["3e50000000000000","1.4901161193847656e-8"],["be50000000000000","-1.4901161193847656e-8"],["3abef2d0f5da7dd9","1e-25"],["babef2d0f5da7dd9","-1e-25"],["3e60000000000000","2.9802322387695312e-8"],["be60000000000000","-2.9802322387695312e-8"],["3af357c299a88ea7","1e-24"],["baf357c299a88ea7","-1e-24"],["3e70000000000000","5.960464477539063e-8"],["be70000000000000","-5.960464477539063e-8"],["3b282db34012b251","1e-23"],["bb282db34012b251","-1e-23"],["3e80000000000000","1.1920928955078125e-7"],["be80000000000000","-1.1920928955078125e-7"],["3b5e392010175ee6","1e-22"],["bb5e392010175ee6","-1e-22"],["3e90000000000000","2.384185791015625e-7"],["be90000000000000","-2.384185791015625e-7"],["3b92e3b40a0e9b4f","1e-21"],["bb92e3b40a0e9b4f","-1e-2Line truncated
|
||||
@@ -0,0 +1,135 @@
|
||||
#!/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}`)
|
||||
Reference in new issue
Block a user