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:
bchanot committed 2026-10-22 20:50:32 +02:00
1 parent 64d4595e73
commit aed6b1e300
8 files changed
+225 -10

No files matched your search

+12 -2
View File
@@ -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)
+57
View File
@@ -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