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
+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`.
|
||||
|
||||
Reference in new issue
Block a user